2015-12-03 84 views
1

我試圖使用對象的數組列表創建驅動程序類,它要求我:驅動程序類做什麼(陣列)

  • 從用戶
  • 閱讀書名閱讀本書的ISBN從用戶
  • 從用戶
  • 程序應繼續讀取用戶的圖書信息,直到從該用戶的所有項目的所有字段閱讀本書庫存量爲空白或零。
  • 程序將有效的書籍對象存儲到一個ArrayList中(僅限有效的對象)
  • 然後,程序將打印出用戶輸入書籍的相反順序輸入的所有「有效」書籍。
  • 當用戶輸入信息時,程序應提供反饋,例如報告項目已添加到ArrayList,或報告發現的任何錯誤。
  • 書無效條目未添加到ArrayList,因此當ArrayList中印

在這裏,他們將不被打印出來是我當前的代碼,到目前爲止我的驅動程序:(我有點福利局在這使) 編輯:給定

這裏的答案是什麼我現在

import java.io.*; 
 
import java.util.*; 
 

 
public class Bookstore2{ 
 
    
 
    
 
     
 
    public static void main(String arg[ ]) throws Exception{ 
 
    
 
     Scanner sc = new Scanner(System.in); 
 
     int isbn=0; 
 
     int quantity = 0; 
 
     String title = ""; 
 
     Book oneBook; 
 
     List<Book> bookList = new ArrayList<Book>(); //here 
 
    
 
    while(true){ 
 
    System.out.print("Enter title: "); 
 
    title = sc.nextLine(); 
 
    sc = new Scanner(System.in); 
 
    System.out.println(); 
 
    System.out.print("Enter isbn: "); 
 
    isbn = sc.nextInt(); 
 
    sc = new Scanner(System.in); 
 
    System.out.println(); 
 
    System.out.print("Enter quantity: "); 
 
    quantity = sc.nextInt(); 
 
    sc = new Scanner(System.in); 
 
    sc = new Scanner(System.in); 
 
    System.out.println(); 
 

 
// WRITE YOUR VALIDATION CODE HERE 
 
// Change condition according to your requirements. 
 
    if(isbn !=0 && quantity != 0 && title != null && title != "") 
 
    { 
 
     oneBook = new Book(title, isbn, quantity); 
 
    bookList.add(oneBook); //create a list in main 
 
    System.out.println("Book added in the list."); 
 
    } 
 
    else 
 
    { 
 
     System.out.println("Book not added"); 
 
     break; 
 
     } 
 

 
} 
 
    for(int i = bookList.size()-1; i >= 0; i--){ 
 
    System.out.println(bookList.get(i)); 
 
    } 
 
    } //main method 
 
} //class

了3210

錯誤現在避免,但它不是同時利用我的異常和書籍類好像

這裏是我的課,我的異常,將使用新的驅動程序類

-----類

運行
public class Book{ 
//instance variables 
private String title = ""; 
private int isbn; 
private int quantity; 

public Book (String title, int isbn, int quantity)throws BookException{  
//constructors 

    setTitle(title); 
    setIsbn(isbn); 
    setQuantity(quantity); 

    } 
public String toString(){ //toString Method 

    String s = ""; 
    s = s + "Title: " + this.title + "\nISBN: " + this.isbn + "\nQuantity: " + this.quantity + "\n"; 
    return s; 

    } 

public String getTitle(){ 
    return this.title; 
    } 
public int getisbn(){ 
    return this.isbn; 
    } 
public int getquantity(){ 
    return this.quantity; 
    } 

//mutator methods 
public void setTitle(String newtitle)throws BookException{ 
    if(newtitle.length()<1){ 
    BookException be = new BookException(); 
    be.setMessage("Title cannot be blank"); 
    throw be; 
    } 
    else{ 
    this.title=newtitle; 
    } 
} 

public void setIsbn(int newisbn)throws BookException{ 
    if (isbn <= 1000 || isbn >= 10000) { 
    this.isbn = newisbn; 
    } 
    else{ 
    BookException be = new BookException(); 
    be.setMessage("ISBN should be between 1000 and 10000."); 
    throw be; 
    } 
} 

public void setQuantity(int newquantity)throws BookException{ 
    if(newquantity>=0){ 
    this.quantity = newquantity; 
    } 
    else{ 
    BookException be = new BookException(); 
    be.setMessage("Quantity can't be a negative number."); 
    throw be; 
    } 
    } 

} 

------異常類

public class BookException extends Exception { 
    //instance variable 
    private String message = ""; 

    public void setMessage(String newMessage) { 
     this.message = newMessage; 
    } 

    public String getMessage() { 
     return this.message; 
    } 
} 
+0

「列表」在哪裏?你確切的問題是什麼?我不能從問題中弄清楚。 – SomeJavaGuy

+0

我正在創建一個使用數組書對象的驅動程序類,並且如上所述,我需要遵循這些要求。 – aldz24

+0

我的確切問題是:如何製作一個驅動程序類,遵循頂部使用所述數組的指令進行操作?我已經列出了我的書類和例外類..我只需要驅動程序類,但我不知道下一步要放什麼 – aldz24

回答

2

首先使用的:當(真)循環來迭代,直到用戶輸入的0爲所有領域。

while(true) 
{ 
    // Scanner Code i.e. read input from the user. 
    if(//check all the inputs) 
    { 
     //create a new book and insert it into array list 
    } 
    else 
    { 
    // If input is 0, break from the loop 
    } 
} 

其次,切勿在您的bean類中執行驗證。創建一個單獨的類或方法來驗證輸入。之後,只有輸入驗證然後創建一個新的對象。

希望這會幫助你。

正確的代碼:

 sc = new Scanner(System.in); 
    while(true){ 
    System.out.print("Enter title: "); 
    title = sc.nextLine(); 
      System.out.println(); 
    System.out.print("Enter isbn: "); 
    isbn = sc.nextInt(); 
    System.out.println(); 
    System.out.print("Enter quantity: "); 
    quantity = sc.nextInt(); 
    System.out.println(); 

// WRITE YOUR VALIDATION CODE HERE 
// Change condition according to your requirements. 
    if(isbn !=0 && quantity != 0 && title != null && title != "") 
    { 
     oneBook = new Book(title, isbn, quantity); 
    bookList.add(oneBook); //create a list in main 
    System.out.println("Book added in the list."); 
    } 
    else 
    { 
     System.out.println("Book not added"); 
     break; 
     } 

} 
    for(int i = bookList.size()-1; i >= 0; i--){ 
    System.out.println(bookList.get(i)); 
    } 
+0

+1對於簡單循環指令 - 無限循環(''while(true)'')with''break'','' ''''我反轉''''我'和'''' - 每個循環涵蓋了最重複的問題,並且易於閱讀,理解和改變。 –

+0

我可能會考慮你的選擇Ash ......它看起來像代碼形式完全一樣嗎?從我的代碼基地? – aldz24

+0

謝謝!我使用你的代碼編輯它,但它並沒有利用我的書籍類和異常類在底部看起來像..我試圖讓他們三個一起工作。也會在驅動程序類中增加inputmismatchexception necesarry,或者書類會照顧它嗎? – aldz24

0

您發佈的評論:

while(title != null || title.equals("0") || isbn != null || isbn != 0 || quantity 

ISBN爲int類型,即基本類型,我們如何能夠用空進行比較的。數量也是int類型。 int的默認值,即基元類型爲0.並且,原始類型永遠不能與null進行比較。

0

由於沒有對碼了這麼多的混亂,這裏是一個完整的解決方案的任務:

Bookstore.java

public class Bookstore { 
    static final Scanner in = new Scanner(System.in); 
    static List<Book> books = new ArrayList<>(); 

    public static void main(String arg[]) throws Exception { 
     while (true) { 
      // read book information 
      Book book = new Book(); 
      System.out.print("Enter title: "); 
      book.title = in.nextLine(); 
      System.out.println(); 

      System.out.print("Enter ISBN: "); 
      book.isbn = readInt(); 
      System.out.println(); 

      System.out.print("Enter quantity: "); 
      book.quantity = readInt(); 
      System.out.println(); 

      // exit condition: "blank book" entered 
      if (book.title.isEmpty() && book.isbn == 0 && book.quantity == 0) { 
       System.out.println("Goodbye!"); 
       break; 
      } 

      //validate and add book 
      try { 
       validateBook(book); 
       books.add(book); 
       System.out.println("Book successfully added to the list."); 
      } catch (IllegalStateException ex) { 
       System.err.println("Book is not valid: " + ex.getMessage()); 
       continue; 
      } 

      // print book list 
      for (int i = books.size() - 1; i >= 0; i--) { 
       System.out.println(books.get(i)); 
       System.out.println(); 
      } 
     } 
    } 

    static int readInt() { 
     while (true) { 
      String input = in.nextLine(); 
      if(input.isEmpty()) { 
       return 0; 
      } 
      try { 
       return Integer.parseInt(input); 
      } catch (NumberFormatException ex) { 
       System.err.println("Expected a valid integer: " + input); 
      } 
     } 
    } 

    static void validateBook(Book book) { 
     if (book.title == null || book.title.isEmpty()) { 
      throw new IllegalStateException("Book title must not be blank."); 
     } 
     if (book.isbn < 1000 || book.isbn > 10000) { 
      throw new IllegalStateException("Book ISBN must be between 1000 and 10000."); 
     } 
     if (book.quantity < 0) { 
      throw new IllegalStateException("Book quantity must be positive."); 
     } 
    } 
} 

Book.java

public class Book { 
    public String title; 
    public int isbn; 
    public int quantity; 

    @Override 
    public String toString() { 
     return String.join("\n", 
       "Title: " + title, 
       "ISBN: " + isbn, 
       "Quantity: " + quantity 
     ); 
    } 
} 
+0

雖然我想用你的書店的建議,但我必須將它與我的書和異常類相匹配,我有:(而且它不會從我的其他類中挑選例外 – aldz24