driver class what to do (array)
我正在尝试使用对象数组列表创建一个驱动程序类,它需要我:
- 从用户那里读取书名
- 阅读用户的书 ISBN
- 从用户处读取库存数量的图书
- 程序应该继续从用户那里读取图书信息,直到用户在所有字段中的所有条目都为空白或为零。
- 程序会将有效的书籍对象存储到一个 ArrayList 中(只有有效的对象)
- 然后,您的程序将以与输入书籍相反的顺序打印用户输入的所有"有效"书籍的列表。
- 当用户输入信息时,程序应该提供反馈,例如报告已将项目添加到 ArrayList,或报告发现的任何错误。
- 具有无效条目的书籍未添加到 ArrayList,因此在打印 ArrayList 时不会打印它们
这是我目前的驱动程序代码:(我对此有点陌生)
编辑:给出的答案
这是我现在得到的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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 |
现在避免了错误,但它似乎没有同时利用我的异常和书籍类
这是我的类和我的异常,它将与新的驱动程序类一起运行
-----类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 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 +"\ ISBN:" + this.isbn + "\ Quantity:" + this.quantity +"\ "; 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; } } } |
------异常类
1 2 3 4 5 6 7 8 9 10 11 12 |
首先使用:while(true) 循环迭代,直到用户为所有字段输入 0。
1 2 3 4 5 6 7 8 9 10 11 12 | 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 类中执行验证。创建一个单独的类或方法来验证输入。之后,输入验证才创建一个新对象。
希望对你有所帮助。
正确的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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)); } |
由于代码有很多混乱,这里有一个完整的任务解决方案:
书店.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
你发布了:
1 | while(title != null || title.equals("0") || isbn != null || isbn != 0 || quantity |
isbn 是 int 类型,即原始类型,我们如何将它与 null 进行比较。
数量也是 int 类型。
int 即原始类型的默认值为 0。并且,原始类型永远不能与 null 进行比较。