【发布时间】:2015-12-10 17:48:29
【问题描述】:
我一直在研究将书籍对象存储到图书馆中的程序,但我陷入了停滞。
我需要从我的数组列表 BookList 中打印出所有大小未知的 Book 对象(由于可能包括将新书籍添加到系统中的功能)到案例切换系统中,以便您可以选择每个选项并进行编辑详情。
我是否需要找到 ArrayList.length 并使用它来确定 switch 语句选项的数量?
我的代码在下面
import java.util.Scanner;
import java.util.ArrayList;
public class Library_Tester {
public static void main (String[] args){
System.out.println(" ===========================================");
System.out.println("| == == ===== == == ==== |");
System.out.println("| == == == == == = = |");
System.out.println("| ====== ==== == == = = |");
System.out.println("| == == == == == = = |");
System.out.println("| == == ===== ===== ===== ==== |");
System.out.println(" ===========================================");
System.out.println(" =======================================");
System.out.println("| Library Systems Inc |");
System.out.println(" =======================================");
System.out.println("| Options: |");
System.out.println("| 1. Add a book |");
System.out.println("| 2. Edit a book's details |");
System.out.println("| 3. Delete a book |");
System.out.println("| 4. Loan a book |");
System.out.println("| 5. Return a book |");
System.out.println("| 6. Exit the Program |");
System.out.println("| 7. Display all books in library |");
System.out.println("| |");
System.out.println("| *Type a number to make a selection* |");
System.out.println(" =======================================");
System.out.println("");
System.out.print("Selection: ");
Book a = new Book();
a.setTitle("Random ");
a.setAuthor("Craig Robertson");
a.setBookID("1847398812");
a.setonLoan(false);
a.setNumberofLoans(3);
Book b = new Book();
b.setTitle("The Last Refuge");
b.setAuthor("Craig Robertson");
b.setBookID("1471127753");
b.setonLoan(false);
b.setNumberofLoans(2);
Book c = new Book();
c.setTitle("The Bird That Did Not Sing");
c.setAuthor("Alex Gray");
c.setBookID("0751548278");
c.setonLoan(true);
c.setNumberofLoans(7);
ArrayList<Book> BookList = new ArrayList<Book>();
BookList.add(a);
BookList.add(b);
BookList.add(c);
Scanner SC = new Scanner(System.in);
int Choice1;
Choice1 = SC.nextInt();
SC.close();
switch (Choice1) {
case 1:
Scanner JK = new Scanner(System.in);
System.out.println("'Add a book' selected");
System.out.println(" ");
break;
case 2:
System.out.println("'Edit a book's details' selected");
System.out.println("Which Book would you like to edit?");
System.out.println("");
break;
case 3:
System.out.println("'Delete a book' selected");
break;
case 4:
System.out.println("'Loan a book' selected");
break;
case 5:
System.out.println("'Return a book' selected");
break;
case 6:
System.out.println("Goodbye!");
System.exit(0);
break;
case 7:
System.out.println("Displaying Books");
System.out.println("");
Book.showBooks(BookList);
System.out.println("");
break;
default:
System.out.println("Invalid selection. Try again");
}
}
}
可根据要求将其他编码编辑到此问题中。
一个额外的问题是:在此之后如何编辑 Array 中 Book 对象的状态?
提前谢谢你。
【问题讨论】:
-
考虑循环遍历列表?
-
这不是你问的关于打印书籍的另一个问题的重复吗?
-
这里的确切问题是什么???我猜你需要阅读amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683
标签: java arraylist switch-statement