Description Write a Java program that mimics the framework for an online book in
ID: 3600800 • Letter: D
Question
Description Write a Java program that mimics the framework for an online book information system, similar to GoodR to provide basic functionality for the system. Use the provided UML, the class descrip the test output to determine the proper behavior for your classes. eads, written in Java. Create an abstract base class, two subclasses, and one interface Create an interface Rateable that: Provides a method rate(double numRatings, double totalRating) Create an abstract class Book that Implements the Rateable and Comparable interfaces Provides the data fields: title, author, releaseYear, genre the appropriate getter/setter methods. numRatings and totalRating should default to 0 Provides constructors , numRatings, and totalRating with o Default, no-arg constructor Convenience constructor that passes the: title, author, releaseYear, and genre o .Provides an abstract getlnfo() method Provides a concrete toString() method that returns the only book title Provides a concrete getRating() method that returns o 1 if numRatings is 0, o totalRating/numRatings otherwise Implements the Rateable interface's rate() method by increasing the existing values of numRatings and totalRating by the amount passed in the appropriate parameter Implements the Comparable interface's compareTo) method by comparing ratings. Books should be sorted in descending order, meaning that a higher rating should be considered "less than" a lower rating Create a concrete class Fiction that: Automatically sets the genre to Fiction in its constructors ibute for sub-genre, subGenre, with the appropriate getter/setter methods Provides a getinfol) method that returns "title by author, genre/subGenre, releaseYear (rating)" with the appropriate values filled inExplanation / Answer
Rateable.java
/**
* Rateable Interface
* @author
*/
public interface Rateable {
/**
* @param numRatings
* @param totalRating
*/
public void rate(double numRatings, double totalRating);
}
Book.java
/**
* @author
*Abstract class Book implements Rateable and Comparable interfaces
*/
public abstract class Book implements Rateable, Comparable<Book> {
//data fields
private String title;
private String author;
private int releaseYear;
private String genre;
private double numRatings;
private double totalRating;
/**
*Constructor with no params
*/
public Book(){
numRatings=0;
totalRating=0;
}
/**Constructor with parameters
* @param title
* @param author
* @param releaseYear
* @param genre
*/
public Book(String title, String author, int releaseYear, String genre){
this.title=title;
this.author=author;
this.releaseYear=releaseYear;
this.genre=genre;
numRatings=0;
totalRating=0;
}
/**
* Abstract method getinfo
* @return String
*/
public abstract String getInfo();
/*
* implements rate(double, double) of Rateable interface
*/
@Override
public void rate(double numRatings, double totalRating) {
this.totalRating=totalRating;
this.numRatings=numRatings;
}
/**
* Calculates the ratings
* @return double
*/
public double getRating(){
if(numRatings==0)
return -1;
else
return totalRating/numRatings;
}
/*
* Compares two books using their ratings and sorts in descending order
*/
@Override
public int compareTo(Book book) {
if(getRating()<= book.getRating()){
return 1;
}else{
return -1;
}
}
/**
* getter setters for the fields
*/
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public double getNumRatings() {
return numRatings;
}
public void setNumRatings(double numRatings) {
this.numRatings = numRatings;
}
public double getTotalRating() {
return totalRating;
}
public void setTotalRating(double totalRating) {
this.totalRating = totalRating;
}
public String toString(){
return title;
}
}
Fiction.java
import java.text.DecimalFormat;
/**
* @author
*Fiction class extends Book
*/
public class Fiction extends Book {
private String subGenre;
//To format the double value up to two decimals
DecimalFormat df=new DecimalFormat("0.00");
/**
* Constructor with parameters
* @param title
* @param author
* @param releaseYear
* @param subGenre
*/
public Fiction(String title, String author, int releaseYear, String subGenre){
super(title,author,releaseYear,"Fiction");
this.subGenre=subGenre;
}
public String getSubGenre() {
return subGenre;
}
public void setSubGenre(String subGenre) {
this.subGenre = subGenre;
}
@Override
public String getInfo() {
//Defaults to No Rating
String rating="No ratings";
//If book has ratings returns value with formatting to 2 decimals
if(getRating()>-1){
rating=df.format(getRating());
}
return getTitle()+" by "+getAuthor()+", Fiction/"+getSubGenre()+", "+getReleaseYear()+" ("+rating+")";
}
}
NonFiction.java
import java.text.DecimalFormat;
/**
* @author
*Non-Fiction class extends Book
*/
public class NonFiction extends Book {
private String topic;
//To format the double value up to two decimals
DecimalFormat df = new DecimalFormat("0.00");
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
/**
* Constructor with Parameters
* @param title
* @param author
* @param releaseYear
* @param topic
*/
public NonFiction(String title, String author, int releaseYear, String topic) {
super(title, author, releaseYear, "Non-Fiction");
this.topic=topic;
}
@Override
public String getInfo() {
//Defaults to No Rating
String rating="No ratings";
//If book has ratings returns value with formatting to 2 decimals
if(getRating()>-1){
rating=df.format(getRating());
}
return getTitle()+" by "+getAuthor()+", Non-Fiction, "+getReleaseYear()+" ("+rating+"), Topic: "+getTopic();
}
}
PAssign7.java
import java.util.ArrayList;
import java.util.Collections;
/**
* @author
*PAssign7.java contains main method with test data to test books related to Fiction, Non-Fiction
*/
public class PAssign7 {
public static void main(String[] args) {
//Arraylist to store books
ArrayList<Book> books=new ArrayList<Book>();
//Create Fiction books with data
Book fiction1=new Fiction("The Catcher in the Rye", "J.D.Salinger",1951, "Classics");
Book fiction2=new Fiction("1984", "George Orwell",1949, "Classics");
Book fiction3=new Fiction("The Fault in our stars", "John Green",2012, "Young Adult");
//Create non fiction Books with data
Book nonFiction1=new NonFiction("The Immortal Life of Henrietta Lacks", "Rebecca Skloot",2010, "Medical History, HeLa Cell line");
Book nonFiction2=new NonFiction("Between the World and Me", "Ta-Nehisi Coates",2015, "American History, Race Relations");
Book nonFiction3=new NonFiction("Blink", "Malcom Gladwell",2005, "Pop Science, Thinking/Neuroscience");
//Assign rates to each book
fiction1.rate(2151135, 8152802);
fiction2.rate(2101011, 8698186);
fiction3.rate(2538804, 10815305);
nonFiction1.rate(414113, 1673017);
nonFiction3.rate(366808, 1426833);
//Add non-fiction and fiction books to Books ArrayList
books.add(fiction1);
books.add(fiction2);
books.add(fiction3);
books.add(nonFiction1);
books.add(nonFiction2);
books.add(nonFiction3);
//Sort the books using Collections sort method, uses CompareTo method implemented in Book.java
Collections.sort(books);
for (Book book : books) {
System.out.println(book.getInfo());
}
}
}
Sample Output:
The Fault in our stars by John Green, Fiction/Young Adult, 2012 (4.26)
1984 by George Orwell, Fiction/Classics, 1949 (4.14)
The Immortal Life of Henrietta Lacks by Rebecca Skloot, Non-Fiction, 2010 (4.04), Topic: Medical History, HeLa Cell line
Blink by Malcom Gladwell, Non-Fiction, 2005 (3.89), Topic: Pop Science, Thinking/Neuroscience
The Catcher in the Rye by J.D.Salinger, Fiction/Classics, 1951 (3.79)
Between the World and Me by Ta-Nehisi Coates, Non-Fiction, 2015 (No ratings), Topic: American History, Race Relations