Question: JAVA: PLEASE EXPLAIN CODE WITH COMMENTS Modify the supplied book shop system to include a private instance variable called publishDate in class Book to represent

JAVA: PLEASE EXPLAIN CODE WITH COMMENTS

Modify the supplied book shop system to include a private instance variable called publishDate in class Book to represent when the book was published. Use LocalDate as the variable type. Use a static variable in the Book class to automatically assign each new book a unique (incremental) id number. Create an array of Book variables to store references to the various book objects. In a loop, calculate the cost of each book type and the sum total of the order. If the book is of classic type and is more than 50 years old, give the cost a 35% discount. If the book is of novel or education type and is more than 5 years old, give the cost a 12% discount. Change the cost() method in all sub-classes of Book to throw a user defined Exception if the total cost is less than zero. This exception should print an error message showing the details of the problem. Modify the Test class to be able to handle exceptions. When an exception is encountered while calculating the cost of a book, the Test class should print out the error message and continue as normal with the next books. This implementation shoud be tested by creating books with negative cost.

// Abstract base class Book.

public abstract class Book {

private String bookTitle; private String bookAuthor;

// constructor public Book(String title, String author) { bookTitle = title; bookAuthor = author; }

// get bookTitle public String getbookTitle() { return bookTitle; } // get bookAuthor public String getbookAuthor() { return bookAuthor; }

public String toString() { return bookTitle + ' ' + bookAuthor; }

public abstract double cost(); }

// Java core packages import java.text.DecimalFormat;

// Java extension packages import javax.swing.JOptionPane;

public class Test {

// test book hierarchy public static void main(String args[]) { Book book; // superclass reference String output = "";

Novel novel = new Novel("Harry Potter", "J.K Rowling", 20.0);

EducationBook educationbook = new EducationBook( "My Maths 3", "James Jones", 15.6, 180654203, 4);

Classic classic = new Classic("The Great Gatsby", "F. Scott Fitzgerald", 35.0);

DecimalFormat precision2 = new DecimalFormat("0.00");

// book reference to a novel book = novel;

output += book.toString() + " costs $" + precision2.format(book.cost()) + " " + novel.toString() + " costs $" + precision2.format(novel.cost()) + " ";

// book reference to a educationbook book = educationbook;

output += book.toString() + " costs $" + precision2.format(book.cost()) + " " + educationbook.toString() + " costs $" + precision2.format( educationbook.cost()) + " ";

// book reference to a Classic book = classic;

output += book.toString() + " costs $" + precision2.format(book.cost()) + " " + classic.toString() + " costs $" + precision2.format(classic.cost()) + " ";

JOptionPane.showMessageDialog(null, output, "Demonstrating Polymorphism", JOptionPane.INFORMATION_MESSAGE);

System.exit(0); } } // end class Test

// EducationBook class derived from Book

public final class EducationBook extends Book {

private double bookCost; // cost of the book private int bookIsbn; // the unique identifier of the education book private int quantity; // number of this book required

// constructor for class EducationBook public EducationBook(String title, String author, double cost, int isbn, int quantity) { super(title, author); // call superclass constructor setBookCost(cost); setIsbn(isbn); setQuantity(quantity); }

// set EducationBook's cost public void setBookCost(double cost) { bookCost = (cost > 0 ? cost : 0); }

// set EducationBook's isbn public void setIsbn(int isbn) { bookIsbn = (isbn > 0 ? isbn : 0); }

// set EducationBook's quantity sold public void setQuantity(int totalSold) { quantity = (totalSold > 0 ? totalSold : 0); }

// determine EducationBook's total cost public double cost() { return bookCost * quantity; }

// get String representation of EducationBook's details public String toString() { return "Education Book: " + super.toString(); }

} // end class EducationBook

// Novel class derived from Book.

public final class Novel extends Book {

private double bookCost;

// constructor for class Novel

public Novel(String title, String author, double cost) {

super(title, author); // call superclass constructor

setBookCost(cost);

}

// set Novel's cost

public void setBookCost(double cost) {

bookCost = (cost > 0 ? cost : 0);

}

// get Novel's cost

public double cost() {

return bookCost;

}

// get String representation of book details

public String toString() {

return "Novel: " + super.toString();

}

} // end class Novel

// Definition of class Classic

public final class Classic extends Book {

private double bookCost; // cost of the book

// constructor for class Classic public Classic(String title, String author, double cost) { super(title, author); // call superclass constructor setBookCost(cost); }

// Set the cost of the book public void setBookCost(double cost) { bookCost = (cost > 0 ? cost : 0); }

// Get the Classic book's cost public double cost() { return bookCost; }

public String toString() { return "Classic: " + super.toString(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!