我有以下两个java类(如下所示)类BookInfo声明静态数组块
public class BookInfo {
// Global arrays accessible by all methods
private static String[] isbnInfo;
private static String[] bookTitleInfo;
private static String[] authorInfo;
private static String[] publisherInfo;
private static String[] dateAddedInfo;;
private static int[] qtyOnHandInfo;
private static double[] wholesaleInfo;
private 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]);如何从主菜单修改(添加,删除,编辑等)?任何想法,建议都非常感谢!
答案 0 :(得分:2)
我认为你需要重新考虑一下你的设计。我提供了一个示例,说明如何使用当前代码处理某些功能,但这样做会产生一些非常讨厌的代码。
静态通常用于在此信息唯一的实例之间共享信息。通常的例子是作为实例计数器。因此,每次创建实例时,它都会递增值,以便您可以跟踪唯一的实例。
问题是bookInfo不知道它是什么意思。它希望成为数据存储,但也描述了一个唯一的对象。想想如果你想创建另一个BookInfo会发生什么。编辑一个实例的任何静态都会影响其他实例。
您可以轻松编辑静态字符串[]中的信息。
public int getBookLocation(Sting name){
return bookTitleInfo.IndexOf(name)
}
然后您可以随意操作特定条目。
public void SetBookName(String oldname, String newname){
int index = getBookLocation(oldname);
if(index > 0){
bookTitleInfo[index] = newname;
}
这对编辑来说并不算太糟糕,但删除一个条目会非常混乱。这是一些伪代码
first have to find the bookindes. - getBookLocation(...)
For each entry in static string[]
create a new one of size-1,
For each entry in String[]
add existing entry to new string[].
您需要为每个阵列执行此操作。即,isbnInfo,bookInfoName,...., 那是很多不必要的迭代。
以下内容虽然不能解决您的问题,但却是一种更好的设计方法,将问题分为几类。
另一个将代表书店,它将存储书籍对象列表,用书籍初始化数据,提供添加,删除,编辑书店书籍的功能。
公共类BookInfo {
//Class variables
private String isbnInfo;
private String bookTitleInfo;
//....
private double wholesaleInfo;
private double retailInfo;
//Constructors
BookInfo(){
//put default behaviour here
}
BookInfo(String isbnInfo, String bookTitleInfo, .....){
this.isbnInfo = isbnInfo;
this.bookTitleInfo = bookTitleInfo;
this.authorInfo = authorInfo;
//....
//Setter Method
public String getIsbnInfo(){
return isbnInfo;
}
//Getter Method
public void setBookTitleInfo(String isbnInfo){
this.isbnInfo = isbnInfo;
}
//.....
所以现在你可以创建一个Book信息并设置/获取所有变量,以OOP方式封装。
接下来,我们将查看BookInfos的存储/访问类。
public class BookStore {
private isbnInfo = new String[] {"978-0060014018", "978-0449221431", "978-0545132060",
"978-0312474881", "978-0547745527" };
private bookTitleInfo = new String[] { "The Greatest Stories", "The Novel", "Smile",
"The Bedford Introduction to Drama",
"AWOL on the Appalachian Trail" };
//...rest of strings
//here is where we store our book objects
//we will create methods to add, remove, edit, later
private BookInfo booklist = new List<BookInfo>();
private String storename;
//Default Constructor initialises booklist based on stored values
public BookStore(){
for(int i = 0; i < isbnInfo.lenght; i++){
AddBook(isbnInfo [i], bookTitleInfo[i],.....,retailInfo[i]);
)
}
//overloaded constructors you probably want to read from external file, db etc eventually.
public BookStore(String filelocation){
//Do stuff to read from a file and add books to booklist
}
//Add a new book to the list by passing in all the info
public void AddBook(String isbn, String title,
BookInfo newbook = new BookInfo(isbn, title, ....)
booklist.add(newbook);
//Add a new book by passing in a book object.
public void AddBook(BookInfo newbook){
booklist.add(newbook);
}
//Find Book
public int getBook(String isbn){
for(int i=0; i++; i < booklist.lenght){
if(book.getIsbn() == isbn)
return i;
}
return -1;
//Remove Book from list
public void removeBook(String isbn){
int loc = getbook(isbn);
if(loc > -1){
booklist.remove(loc);
}
else{
System.out.println(Could not find book);
}
}
public void DisplayBookList(){
//.. print list
}
}
现在,BookStore所需的所有必需功能都位于需要的位置。您现在可以根据需要添加任何其他功能。
您的主要驱动程序类也需要更改。您的驱动程序类需要创建BookStore。
如果用户想要删除图书,他们会输入图书名称。然后你打电话
bookstore.removeBook(userstring);
添加一本书需要用户输入更多的值,但基本上是相同的
String isbn = scanner.next();
String bookname = scanner.next();
.......
double wholesaleinfo = Double.ParseDouble(scanner.next());
然后通过传递信息和params
来添加你的罐头bookstore.addBook(isbn, bookname, ...., wholesaleinfo);
或书籍对象。
BookInfo newbook = new BookInfo(isbn, bookname, ...., wholesaleinfo)
bookstore.addBook(newbook);
我会以任何你想要的方式保留用户逻辑。希望这已经说明了事情。对我所做的事情肯定有所改进。