我的自定义书籍异常无法与创建书籍对象的程序交互,并且最终也与我的驱动程序类 Bookstore.java 交互。我的驱动程序类无法捕获发生的不一致情况。喜欢:
- 标题不能为空或仅包含空格
- isbn 应该是 1000 到 10000(含)之间的数字
- 数量不能为负(零也可以,表示缺货)
当我运行驱动程序类 BookStore.java 时,它没有捕获我在 book.java 中编写的上述错误。
----------------------------------创建书籍对象
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
this.title = title;
this.isbn = isbn;
this.quantity = 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 Exception{
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 Exception{
if(newisbn>=1000 && newisbn>=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 Exception{
if(newquantity>=0){
this.quantity = newquantity;
}
else{
BookException be = new BookException( );
be.setMessage("Quantity can't be a negative number.");
throw be;
}
}
}
------------------------------------------------------------自定义图书异常(exception)
public class BookException extends Exception{
//instance variable
private String message = "";
public BookException( ){
//empty constructor
}
public void setMessage(String newMessage){
this.message = newMessage;
}
public String getMessage( ){
return this.message;
}
}
------------------------------------------------------------ ------- 驱动类
import java.io.*;
import java.util.*;
public class Bookstore{
//this program will read the information for one book
//it will validate it and print it if correct
public static void main(String arg[ ]) throws Exception{
Scanner sc = new Scanner(System.in);
int size = 3;
int isbn=0;
int quantity = 0;
String title = "";
int count=0;
boolean exit = false;
Book oneBook;
try{
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);
System.out.println();
oneBook = new Book(title, isbn, quantity); //attempting to create the book
//if any information about the book is inconsistent the BookExcpetion will be
//thrown by the Book constructor/set methods and it should be caught
System.out.println("The book entered was:");
System.out.println(oneBook.toString( ));
}
catch(InputMismatchException ime){
System.out.println("you did not enter a number");
}
catch (BookException be){
System.out.println(be.getMessage( )); //calling the getMessage from BookException.java
}
} //main method
} //class
<小时/>
我非常感谢您的帮助和提示!
最佳答案
你的构造函数总是会通过。更改自
this.title = title;
this.isbn = isbn;
this.quantity = quantity;
至
setTitle(title);
setIsbn(isbn);
setQuantity(quantity);
关于使用对象和驱动程序类进行 Java 自定义异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938646/