我有以下两个 java 类(如下所列) Class BookInfo 声明静态数组块
public class BookInfo {
// Global arrays accessible by all methods
public static String[] isbnInfo;
public static String[] bookTitleInfo;
public static String[] authorInfo;
public static String[] publisherInfo;
public static String[] dateAddedInfo;;
public static int[] qtyOnHandInfo;
public static double[] wholesaleInfo;
public static double[] retailInfo;
static {
isbnInfo = new String[] {
"978-0060014018",
"978-0449221431",
"978-0545132060",
"978-0312474881",
"978-0547745527"
};
bookTitleInfo = new String[] {
"The Greatest Stories",
"The Novel",
"Smile",
"The Bedford Introduction to Drama",
"AWOL on the Appalachian Trail"
};
authorInfo = new String[] {
"Rick Beyer",
"James A. Michener",
"Raina Telgemeier",
"Lee A. Jacobus",
"David Miller"
};
publisherInfo = new String[] {
"HerperResource",
"Fawcett",
"Graphix",
"Bedford St. Martins",
"Mariner Books"
};
dateAddedInfo = new String[] {
"05/18/2003",
"07/07/1992",
"02/01/2010",
"09/05/2008",
"11/01/2011"
};
qtyOnHandInfo = new int[] {7, 5, 10, 2, 8};
wholesaleInfo = new double[] {12.91, 7.99, 6.09, 54.99, 10.17};
retailInfo = new double[] {18.99, 3.84, 4.90, 88.30, 14.95};
}
public static void BookInfo() {
System.out.println(" Serendipity Booksellers");
System.out.println(" Book Information\n");
for(int i = 0; i < isbnInfo.length; i++){
System.out.println("ISBN: " + isbnInfo[i]);
System.out.println("Title: " + bookTitleInfo[i]);
System.out.println("Author: " + authorInfo[i]);
System.out.println("Publisher: " + publisherInfo[i]);
System.out.println("Date Added: " + dateAddedInfo[i]);
System.out.println("Quantity-On-Hand: " + qtyOnHandInfo[i]);
System.out.println("Wholesale Cost: $ " + wholesaleInfo[i]);
System.out.println("Retail Price: $ " + retailInfo[i]);
System.out.println();
}
}
}
如何从此类访问数组列表?到目前为止只有以下内容有效,但是我如何从这个类中修改(添加、删除、编辑等)(这个类中没有主要的主要内容) BookInfo bookinfo = new BookInfo(); bookinfo.BookInfo(); System.out.println(bookinfo.isbnInfo[0]); 如何从主菜单修改(添加、删除、编辑等)
import java.util.Scanner;
public class InvMenu {
public static void addBook(){
System.out.println("\nYou selected Add a Book\n");
BookInfo bookinfo = new BookInfo();
bookinfo.BookInfo(); // only these two are working but I cannot modify arrays at all
System.out.println(bookinfo.isbnInfo[0]);
}
public static void editBook(){
System.out.println("\nYou selected Edit a Book's Record\n");
}
public static void deleteBook(){
System.out.println("\nYou selected Delete a Book\n");
}
public static void printInvMenu(){
String choice;
int x = 0;
boolean b;
char letter;
boolean menu = true;
Scanner keyboard = new Scanner(System.in);
System.out.println("Serendipity Booksellers");
System.out.println("Inventory Database\n");
System.out.println(" 1. Look Up a Book");
System.out.println(" 2. Add a Book");
System.out.println(" 3. Edit a Book's Record");
System.out.println(" 4. Delete a Book");
System.out.println(" 5. Return to the Main Menu\n");
do{
System.out.print("Enter your choice: ");
choice = keyboard.nextLine();
b = true;
try {
x = Integer.parseInt(choice);
System.out.println(x);
}
catch(NumberFormatException nFE) {
b = false;
System.out.println("You did not enter a valid choice. Try again!\n");
}
}while(b == false);
do{
else if(x == 1){
addBook();
}
else if(x == 2){
editBook();
}
else if(x == 3){
deleteBook();
}
else if(x == 4){
System.out.println("Returning to the Main Menu\n");
break;
}
else{
System.out.println("\nYou did not enter a valid choice. Try again!\n");
}
printInvMenu();
}while(x == 5);
}
}
我可以从其他类主菜单轻松访问一些功能: BookInfo bookinfo = new BookInfo(); bookinfo.BookInfo(); System.out.println(bookinfo.isbnInfo[0]); 如何从主菜单修改(添加、删除、编辑等)?任何想法,建议都非常感谢!