2015-12-02 60 views
0

我期待建立一個庫類將設立圖書對象的數組或數組列表,並進行適當的功能,如添加一本書,編輯書的細節,刪除一本書,借書和歸還書。另外,我希望實現一個完全測試解決方案的測試程序類。我希望將測試菜單的每個選項都編程爲在適當的情況下包含驗證,並調用庫類中創建的函數。到目前爲止,我有以下類:代碼不執行用戶的輸入,並從我的庫類調用函數

Book類:

public class Book { 

    // Instance variables 
    private int BookID; 
    private String Title; 
    private String Author; 
    private boolean On_Loan; 
    private int Number_of_Loans; 

    // Constructor 
    public Book(int BookID, String Title, String Author, boolean On_Loan, int Number_of_Loans) { 
     this.BookID = BookID; 
     this.Title = Title; 
     this.Author = Author; 
     this.On_Loan = On_Loan; 
     this.Number_of_Loans = Number_of_Loans; 
    } 

    public Book(int iD, String title2) { 
     // TODO Auto-generated constructor stub 
    } 

    // Mutator methods 
    public void setBookID(int BookID) { 
     this.BookID = BookID; 
    } 

    public void setTitle(String Title) { 
     this.Title = Title; 
    } 

    public void setAuthor(String Author) { 
     this.Author = Author; 
    } 

    public void setOn_Loan(boolean On_Loan) { 
     this.On_Loan = On_Loan; 
    } 

    public void setNumber_of_Loans(int Number_of_Loans) { 
     this.Number_of_Loans = Number_of_Loans; 
    } 

    // Accessor methods 
    public int getBookID() { 
     return BookID; 
    } 

    public String getTitle() { 
     return Title; 
    } 

    public String getAuthor() { 
     return Author; 
    } 

    public boolean getOn_Loan() { 
     return On_Loan; 
    } 

    public int getNumber_of_Loans() { 
     return Number_of_Loans; 
    } 
} 

圖書館類:

import java.util.ArrayList; 

public class Library { 

    private ArrayList<Book> books; 

    public Library(ArrayList<Book> books) { 
     super(); 
     this.books = books; 
    } 

    public ArrayList<Book> getBooks() { 
     return books; 
    } 

    public void setBooks(ArrayList<Book> books) { 
     this.books = books; 
    } 

    public void displayBooks() { 
     for (int i = 0; i < books.size(); i++) { 
      System.out.println("ID " + books.get(i).getBookID()); 
      System.out.println("Title " + books.get(i).getTitle()); 
     } 
     System.out.println("Displayed " + books.size() + " Books"); 
    } 

    public void addBook(int ID, String title) { 
     books.add(new Book(ID, title)); 

    } 

    public void loaning_A_Book(int bookIndex) { 
     Book book = books.get(bookIndex); 
     book.setOn_Loan(true); 
    } 

    public void removeBook(int id) { 
     boolean successful = false; 
     for (int i = 0; i < books.size(); i++) { 
      if (books.get(i).getBookID() == id) { 
       books.remove(i); 
       System.out.println("Book removal successful"); 
       successful = true; 
      } 
     } 

     if (!successful) { 
      System.out.println("Could not remove book id " + id); 
     } 
    } 

    public void editBook(int idToEdit, Scanner s) { 
     // TODO Auto-generated method stub 

    } 
} 

庫測試類:

import java.util.Scanner; 

public class Library_Tester { 

    public static void main(String args[]) { 

     Library lib = new Library(HelperUtilities.generateBooks()); 
     Scanner sc = new Scanner(System.in); 

     displayMenu(lib, sc); 
    } 

    static void displayMenu(Library i, Scanner s) { 
     System.out.println("--- Library Menu ---"); 
     System.out.println("--- Display Books ---"); 
     System.out.println("--- Add Book ---"); 
     System.out.println("--- Remove Book ---"); 
     System.out.println("--- Edit Book ---"); 
     System.out.println("--- EXIT ---"); 

     int option = s.nextInt(); 

     switch (option) { 
     case 1: 
      displayBooks(); 
      break; 

     case 2: 
      System.out.println("Enter an ID"); 
      int id = s.nextInt(); 
      System.out.println("Enter a title"); 
      String title = s.nextLine(); 
      i.addBook(id, title); 
      break; 

     case 3: 
      System.out.println("Enter an ID to remove"); 
      int idToRemove = s.nextInt(); 
      i.removeBook(idToRemove); 
      break; 

     case 4: 
      System.out.println("Enter an ID to edit"); 
      int idToEdit = s.nextInt(); 
      i.editBook(idToEdit, s); 
      break; 

     case 5: 
      System.out.println("EXITING..."); 
      System.exit(1); 
      break; 

     default: 
      System.out.println("ERROR: Invalid input"); 
      break; 

     } 

     displayMenu(i, s); 

    } 

    private static void displayBooks() { 
     // TODO Auto-generated method stub 

    } 
} 

HelperUtilities類:

import java.util.ArrayList; 

public class HelperUtilities { 

    private static String[] names = { "Harry Potter and the Philosopher's Stone", 
      "Harry Potter and the Chamber of Secrets", "Harry Potter and the Prisoner of Azkaban", 
      "Harry Potter and the Goblet of Fire", "Harry Potter and the Order of the Phoenix", 
      "Harry Potter and the Half-Blood Prince", "Harry Potter and the Deathly Hallows" }; 

    private static int[] ids = { 1000, 1001, 1002, 1003, 1004, 1005, 1006 }; 

    public static ArrayList<Book> generateBooks() { 
     ArrayList<Book> books = new ArrayList<Book>(); 

     for (int i = 0; i < names.length; i++) { 
      books.add(new Book(ids[i], names[i])); 
     } 

     return books; 
    } 

} 

我的問題是,最終,爲什麼我的代碼不能執行用戶輸入並從我的庫類中調用函數?任何幫助,將不勝感激。謝謝!

+0

你應該消耗換行符這很可能仍然在流中使用's.nextInt後();' – MadProgrammer

+0

@ Oh.phil看看我下面的解決方案。 – user3437460

回答

0

就像MadProgrammer提到的一樣,你需要在s.nextInt()之後加上s.nextLine()這會給你帶來問題。

如果你不能決定在哪裏插入s.nextLine(),更改所有s.nextInt()到:

int someVariable = Integer.parseInt(s.nextLine()); 

接收輸入字符串,然後解析爲相應的類型。這樣,你的輸入總是消耗換行符。

+0

謝謝。非常有用 –

+0

然而,當我嘗試運行代碼我得到以下異常: 異常線程「main」 java.util.InputMismatchException \t在java.util.Scanner.throwFor(來源不明) \t是java。 util.Scanner.next(未知來源) \t在java.util.Scanner.nextInt(未知來源) \t在java.util.Scanner.nextInt(未知來源) \t在Library_Tester.displayMenu(Library_Tester.java:21) \t在Library_Tester.main(Library_Tester.java:10) –

+0

@ Oh.phil有一個不同的問題了。可能是因爲您在嘗試將其解析爲整數時輸入了字符串。 – user3437460

相關問題