Question
JAVA please explain with comments Use the same Java time/local date library as used in the code below 1) Modify the Book and all sub-classes
JAVA
please explain with comments
Use the same Java time/local date library as used in the code below
1) Modify the Book and all sub-classes of the Book class constructors to throw a generic InvalidDateException when any of the following Publish dates are entered:
if the publish date is in the future (1 point)
if year is before 1200 (1 point)
if month is out of range (1 point)
if day is out of range according to the month (1 point)
if day falls on the weekend (1 point) (books arent published on weekends)
The exception should have a message with the book details and a short description of the problem (2 points).
2) When an exception is encountered, the Test class should print out the error message and continue as normal with the following books. (2 points: each test date is worth 0.25 point)
Test this by using the following dates:
Date: 2018, 11, 30
Date: 2020, 8, 4
Date: 1996, 2, 29
Date: 2010, 13, 9
Date: 2003, 1, 18
Date: 1999, 2, 30
Date: 1165, 12, 1
Date: 2015, 7, 8
Test.java
// Java core packages
import java.text.DecimalFormat;
import java.time.LocalDate;
// 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, LocalDate.of(1992, 3, 26));
EducationBook educationbook = new EducationBook("My Maths 3", "James Jones", 15.6, 180654203, 4,
LocalDate.of(1913, 3, 26));
Classic classic = new Classic("The Great Gatsby", "F. Scott Fitzgerald", 35.0, LocalDate.of(1800, 3, 26));
DecimalFormat precision2 = new DecimalFormat("0.00");
// book reference to a novel
book = novel;
try {
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()) + " ";
} catch (Exception e) {
System.out.println(e.getMessage());
}
Book books[] = new Book[5];
books[0] = novel;
books[1] = educationbook;
books[2] = classic;
EducationBook educationbook1 = new EducationBook("My Maths 4", "James Jones", 15.6, 180654203, -4,
LocalDate.of(1913, 3, 26));
Classic classic2 = new Classic("The Greatest Harry", "F. SNEwewcott Fitzgerald", 35.0,
LocalDate.of(2018, 3, 26));
books[3] = educationbook1;
books[4] = classic2;
double totalCost = 0;
for (int i = 0; i < books.length; i++) {
try {
if (books[i] instanceof Classic) {
if (books[i].getPublishDate().compareTo(LocalDate.of(books[i].getPublishDate().getYear() - 50,
books[i].getPublishDate().getMonth(), books[i].getPublishDate().getDayOfMonth())) > 0) {
totalCost = totalCost + (books[i].cost()*0.35 + books[i].cost());
} else {
totalCost = totalCost + books[i].cost();
}
} else {
if (books[i].getPublishDate().compareTo(LocalDate.of(books[i].getPublishDate().getYear() - 5,
books[i].getPublishDate().getMonth(), books[i].getPublishDate().getDayOfMonth())) > 0) {
totalCost = totalCost + (books[i].cost()*0.12 + books[i].cost());
} else {
totalCost = totalCost + books[i].cost();
}
}
output = output + "Book : "+(i+1)+books[i]+" , cost : "+books[i].cost() + " ";
}catch(Exception e) {
output = output + books[i].toString()+" Cost Error: "+e.getMessage() + " ";
}
}
output = output + "Total Cost : "+totalCost;
JOptionPane.showMessageDialog(null, output, "Demonstrating Polymorphism", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
} // end class Test
Book.java
import java.time.LocalDate;
// Abstract base class Book.
public abstract class Book {
private String bookTitle;
private String bookAuthor;
private LocalDate publishDate;
private static int bookId = 0;
private int id;
// constructor
public Book(String title, String author, LocalDate pubDate) {
bookTitle = title;
bookAuthor = author;
publishDate = pubDate;
bookId++;
id = bookId;
}
// get bookTitle
public String getbookTitle() {
return bookTitle;
}
// get bookAuthor
public String getbookAuthor() {
return bookAuthor;
}
public int getId() {
return id;
}
public String toString() {
return id + " " + bookTitle + " " + bookAuthor + " "+ publishDate.toString();
}
public LocalDate getPublishDate() {
return publishDate;
}
public abstract double cost() throws Exception;
}
Novel.java
import java.time.LocalDate;
// Novel class derived from Book1.
public final class Novel extends Book {
private double bookCost;
// constructor for class Novel
public Novel(String title, String author, double cost, LocalDate pubDate) {
super(title, author, pubDate); // call superclass constructor
setBookCost(cost);
}
// set Novel's cost
public void setBookCost(double cost) {
bookCost = cost;
}
// get Novel's cost
public double cost() throws Exception {
if(bookCost < 0) {
throw new Exception("Total Book Cost cannot be negative.");
}
return bookCost;
}
// get String representation of book details
public String toString() {
return "Novel: " + super.toString();
}
} // end class Novel
Classic.java
import java.time.LocalDate;
// 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, LocalDate pubDate) {
super(title, author, pubDate); // call superclass constructor
setBookCost(cost);
}
// Set the cost of the book
public void setBookCost(double cost) {
bookCost = cost;
}
// Get the Classic book's cost
public double cost() throws Exception {
if(bookCost < 0) {
throw new Exception("Total Book Cost cannot be negative.");
}
return bookCost;
}
public String toString() {
return "Classic: " + super.toString();
}
}
EducationBook.java
import java.time.LocalDate;
// 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,LocalDate pubDate) {
super(title, author,pubDate); // call superclass constructor
setBookCost(cost);
setIsbn(isbn);
setQuantity(quantity);
}
// set EducationBook's cost
public void setBookCost(double cost) {
bookCost = cost;
}
// 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;
}
// determine EducationBook's total cost
public double cost() throws Exception {
if(bookCost * quantity < 0) {
throw new Exception("Total Book Cost cannot be negative.");
}
return bookCost * quantity;
}
// get String representation of EducationBook's details
public String toString() {
return "Education Book: " + super.toString();
}
} // end class EducationBook
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started