2013-12-17 62 views
0

數據進行超和陣列我有其是 菜單,LoanBook(超類),FictionBook 4個Java類的Java應用程序(延伸loanBook),NonFictionBook(延伸loanBook)添加使用方法

我卡上一種方法 - 該方法的目的是從我的數組中發佈一本書給學生。我的目標是搜索一本書,併爲該書添加學生細節。我無法弄清楚我的錯在哪裏,我只是無法理解這種方法。

請忽略任何其他變量,但考慮到一切,任何的建議是非常有益的,如果需要更多的信息.​​.

我的問題是爲別人看在這一點,告訴我什麼是不需要和我出錯的地方。有沒有更簡單的方法來做到這一點?

PS我已經改變了原來從下面的代碼,但這個我得到一個運行時錯誤後,我已經添加了名

static void issueBook() { 
     int choice,x = 0; 
     String title,name,date; 
     boolean found = false; 
     System.out.println("1.Fiction or 2.NonFiction?"); 
     choice = keyboard.nextInt(); 
     if (choice == 1){ 
      System.out.println("Please enter the title of the book to loan:"); 
      title = keyboard.next(); 
      System.out.println("Please enter your name:"); 
      name = keyboard.next(); 
      date = "todays date"; 
      do { 
       if (fictionBooksArray[x].getTitle().equals(title)){ 
        found = true; 
        fictionBooksArray[x].setOnLoan(true); 
        fictionBooksArray[location].setName(name); 
        fictionBooksArray[location].setDate(date); 
        System.out.println("Loan successful "); 
        } 
       else x++; 
      } 
      while ((x < fictionBooksArray.length)&& (!found)); 
      if (!found){ 
       System.out.println("This book title could not be located."); 
      } 

     } 
+0

有什麼實際問題? –

+0

嘗試重構一個採用LoanBook []和標題的方法,並在數組匹配中返回該書。這應該使它更具可讀性。 – user60561

+0

嗨,戴夫,我已經更新了這個問題。 User60561,我不知道你的意思 – user2854120

回答

2

這似乎是家庭作業,所以我將不會發布該怎麼辦這一點,但有些一般的方法結構可能會像這樣:

// Returns the book if it exists in books, otherwise returns null 
static LoanBook find(LoanBook[] books, String title) 

// Prints the prompt to the console, returns whatever the user types next 
static String getUserInput(String prompt) 

// Takes the input title and name, tries to find the book in the array 
// If it detects the find method has failed by returning null, it prompts 
// the user for new information 
static void takeOutBook(LoanBook[] books) 

// The big method is much clearer now, this depends on the other methods to work 
static void issueBook() { 
    int choice = Integer.parseInt(getUserInput("1. Fiction: 2. Non Fiction:")); 
    if (choice == 1) { 
     takeOutBook(fictionBooksArray); 
    } else if (choice == 0) { 
     takeOutBook(nonfictionBookArray); 
    } 
} 

編輯,按要求完整的例子:

隱含代碼:

static class LoanBook { 
    String getTitle() { return ""; } 
    boolean isOnLoan() { return true; } 
    void setOnLoan(boolean loan) { } 
    void setDate(String d){ } 
    void setName(String d){ } 
} 
static class FictionBook extends LoanBook { } 
static class NonFictionBook extends LoanBook { } 
static Scanner keyboard = new Scanner(System.in); 
static FictionBook[] fictionBooksArray = new FictionBook[10]; 
static NonFictionBook[] nonfictionBookArray = new NonFictionBook[10]; 

示例代碼:

static void issueBook() { 
    int choice = Integer.parseInt(getUserInput("1. Fiction: 2. Non Fiction:")); 
    if (choice == 1) { 
     takeOutBook(fictionBooksArray); 
    } else if (choice == 0) { 
     takeOutBook(nonfictionBookArray); 
    } 
} 

static LoanBook find(LoanBook[] books, String title) { 
    for (LoanBook book : books) { 
     if (book.getTitle().equals(title) && !book.isOnLoan()) { 
      return book; // When the book is found, exit the loop and return the book 
     } 
    } 
    return null; // Returns null if book not found 
} 

static String getUserInput(String prompt) { 
    System.out.println(prompt); 
    return keyboard.next(); // You can then use Integer.parseInt(String param) to get the int value 
} 

static void takeOutBook(LoanBook[] books) { 
    String title = getUserInput("Please enter title of the book to loan"); 
    String name = getUserInput("Please enter your name: "); 
    String date = "???"; 

    LoanBook book = find(fictionBooksArray, title); 
    if (book != null) { 
     book.setOnLoan(true); 
     book.setName(name); 
     book.setDate(date); 
    } else { 
     System.out.println("The title has not been found, please try again"); 
     takeOutBook(books); // The method calls itself in an loop until the user inserts valid information 
    } 
} 
+0

這是家庭作業,但只是一個較大程序的一小部分,包括書寫,添加書籍,加載文件,寫入文件,搜索排序,顯示書籍等等,所以更準確的答案會有幫助。我真的認爲它是我的初始陣列,這是問題。我在主要問題中更改了我的代碼,請查看 – user2854120

+0

'location'從哪裏來?堆棧跟蹤也可能有所幫助,以及setName方法的來源。 – user60561