如何将尺寸为i的arraylist的元素显示到案例切换系统中?

时间:2015-12-10 17:48:29

标签: java arraylist switch-statement

我一直致力于将图书对象存储到图书馆的程序,但我已陷入停滞状态。

我需要打印出来自我的arraylist,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");


        }
      }
      }

可根据要求将其他编码编辑到此问题中。

一个额外的问题是:如何在此之后编辑数组中Book对象的状态?

提前谢谢。

3 个答案:

答案 0 :(得分:0)

只需使用foreach声明

for (Book book : bookList){
    System.out.println(book.getTitle());
}

这个想法是,对于你在case语句中调用的每个方法,你的程序将遍历书籍列表,要么显示它们,要么允许用户修改它们

我认为这与你正在尝试的相反,我的理解是迭代列表并显示每本书的所有选项。

以相反的方式做到这一点

答案 1 :(得分:0)

可悲的是,switch case只接受编译时已知常量的值。 通常,如果编译器在switch语句中为每个case找到了很好的常量,那么编译器会为switch case建立一个跳转表。 请注意,switch仅适用于整数基元类型和枚举,因此如果您正在使用其他对象类型,则需要使用if / else语句。

答案 2 :(得分:0)

为什么要使用开关盒?

获取用户输入,例如bookId并搜索该对象的列表,然后编辑该对象详细信息。

步骤; 1.选择编辑书 2.询问书籍ID 3.从数组列表中获取带有id的书

List<Book> books = new ArrayList<Book>();

// Populate list

private Book getBookById(int id) {
    Iterator<Book> obj = books.iterator();
    while(obj.hasNext()) {
        Book book = obj.next();
        if(book.id == id)
            return book;
    }
    return null;
}