Java Custom Book Exception Hello, I am having trouble with my custom book except
ID: 656186 • Letter: J
Question
Java Custom Book Exception
Hello, 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:
title should not be blank or contain blanks only
isbn should be a number between 1000 and 10000 (inclusive)
quantity should not be negative (zero is ok, it means they are out of stock)
----------------------------------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 + " 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 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
//author: Blanca Polo
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();
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!
Explanation / Answer
Note:
The code was absolutely perfect.
The mistake was in Book class.
There are two ways to overcome the problem.
1)One way:
Since, when a Book object is created in the Bookstore class the values are passed directly to the parameterized constructor.
In the Book class, the parameterized constructor is directly assigning the values to the instance variable. Therefore, the Book class is not invoking the BookException class methods.
To overcome the problem, in the parameterized constructor call the setter methods to the values to the instance variables.
In the setter method, the conditions are checked. There by it invokes the BookException class methods respectively if errors exist.
2)Second way:
In the Bookstore class, create a Book class object by invoking the empty constructor and assign the values by calling the setter methods of Book object.
In the below code, the first way is applied. It is highlighted in bold letters. Some of the sample outputs are added. Hope this would be helpful to you.
Program code:
//Driver class: Bookstore.java
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();
>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
//Exceptions class: BookException.java
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;
}
}
//Base class: 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 Exception
{
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 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;
}
}
}
Sample Output:
With correct input data:
Enter title: Algorithms
Enter isbn: 2254
Enter quantity: 12
The book entered was:
Title: Algorithms
ISBN: 2254
Quantity: 12
Sample Output:
With wrong title input data:
Enter title:
Enter isbn: 2245
Enter quantity: 13
Title cannot be blank
Sample Output:
With wrong isbn input data:
Enter title: Algorithms
Enter isbn: 152
Enter quantity: 10
ISBN should be between 1000 and 10000.
Sample Output:
With wrong isbn input data:
Enter title: Algorithms
Enter isbn: 2245
Enter quantity: -9
Quantity can't be a negative number.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.