使用Object和Driver类的Java自定义异常处理 [英] Java Custom Exception handling with Object and Driver class

查看:175
本文介绍了使用Object和Driver类的Java自定义异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题,我的自定义书籍异常与我的程序进行交互,创建一个书籍对象,并且最终与我的驱动程序类Bookstore.java进行交互。我的驱动程序类没有发现不一致。喜欢:

I am having trouble with my custom book exception to interact with my program that creates a book object and for that too finally interact with my driver class Bookstore.java. My driver class doesn't catch the inconsistencies from happening. Like:


  • 标题不应为空白或仅包含空白

  • isbn应该是1000和10000(含)

  • 数量不应为负数(零表示确定,表示缺货)

当我运行我的驱动程序类BookStore.java时,它没有捕获我在book.java中写的上述错误。

When I run my driver class BookStore.java, it does not catch the above errors that I have written in book.java.

--- -------------------------------创建书对象

----------------------------------Creates book Object

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;
}
}

}

---------------------------------------------定制书例外

-----------------------------------------------Custom Book 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;
}
}

----------- -------------------------------------------司机级别

------------------------------------------------------ Driver Class

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






我真的很感激帮助和提示!


I would really appreciate the help and tips!

推荐答案

你的构造函数总是通过。从

Your constructor always passes. Change from

this.title = title;
this.isbn = isbn;
this.quantity = quantity;

setTitle(title);
setIsbn(isbn);
setQuantity(quantity);

这篇关于使用Object和Driver类的Java自定义异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆