我试图使用对象的数组列表创建驱动程序类,它要求我:驱动程序类做什么(阵列)
- 从用户
- 阅读书名阅读本书的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
错误现在避免,但它不是同时利用我的异常和书籍类好像
这里是我的课,我的异常,将使用新的驱动程序类
-----类
运行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;
}
}
“列表”在哪里?你确切的问题是什么?我不能从问题中弄清楚。 – SomeJavaGuy
我正在创建一个使用数组书对象的驱动程序类,并且如上所述,我需要遵循这些要求。 – aldz24
我的确切问题是:如何制作一个驱动程序类,遵循顶部使用所述数组的指令进行操作?我已经列出了我的书类和例外类..我只需要驱动程序类,但我不知道下一步要放什么 – aldz24