我现在正在制作3件事;一个司机,一个班级和一个例外。
目前我的书店类在识别我的书类上的例外时遇到了麻烦。
我做错了什么?
驱动程序:由@Satoshi Kouno提供的给定建议编辑
import java.io.*;
import java.util.*;
public class BookStore{
public static void main(String arg[ ]) throws Exception{
Scanner sc = new Scanner(System.in);
int isbn=0;
int quantity = 0;
String title = "";
Book oneBook;
boolean exit = false;
List<Book> bookList = new ArrayList<Book>(); //here
while(true){
try{
sc = new Scanner(System.in);
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();
// Validation Codes
// Condition
if(isbn !=0 && quantity != 0 && !"".equals(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;
}
}
catch(InputMismatchException ime){
System.out.println("you did not enter a number");
}
catch (BookException be){
System.out.println(be.getMessage( )); //
}
}
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 Exception{
//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;
}
}
}
我的异常类:编辑#2
public class BookException extends Exception {
//instance variable
private String message = "";
public BookException(String message) {
super(message);
}
public void setMessage(String newMessage) {
this.message = newMessage;
}
public String getMessage() {
return this.message;
}
} 试图让我的班级与我的司机一起工作,但它不起作用。
修改!! PSA 我忘了指出司机班的要求
我的书和异常类已经不需要更改了
他们只是希望我让我自己的驱动程序与它们和它们的例外运行
答案 0 :(得分:3)
你的班级没有这样做:
// Validation Codes
// Condition
if(isbn !=0 && quantity != 0 && title != null && title != "")
title != ""
此条件永远不会被验证,因此不会输入您创建Book
对象的代码部分:
要比较字符串值,只需将其替换为:
!"".equals(title);
或使用apache lang类(可能你必须导入它):
StringUtils.isNotEmpty(title);
<强>此外:强>
我建议您修改添加构造的自定义异常类:
public class BookException extends Exception {
public BookException(String message) {
super(message);
}
}
在Book#setTitle
中,另一位制定者将其提升:
throw new BookException("Title cannot be blank!");
希望得到这个帮助。
<强>更新强>
分析代码我检查了其他类似的东西:
if (isbn <= 1000 || isbn >= 10000) {
this.isbn = newisbn;
} else {
BookException be = new BookException( );
be.setMessage("ISBN should be between 1000 and 10000.");
throw be;
}
这个检查对我来说是错误的,可能你想这样做:
isbn >= 1000 && isbn <= 10000