2015-12-03 18 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