Question: Creating Java Classes and checking inheritance. (Book and Bookcase classes are provided below) Suppose we want to make our bookstore sell also music albums. So,
Creating Java Classes and checking inheritance. (Book and Bookcase classes are provided below)
Suppose we want to make our bookstore sell also music albums. So, lets abstract the Album class, bearing in mind that even for music albums there is an identification number called ISMN (although there are other ways to identify an album as using the European EAN standard), as well as for books there is the ISBN identification number. Then check if there is an inheritance relationship between the Book class and the Album class. If it exists, implement a solution.
import java.util.*; //Book class contains name and genre class Book{ String name; String genre; //Parametrized constructor Book(String name,String genre){ this.name = name; this.genre = genre; } //setter for name public void setName(String name) { this.name = name; } //setter for genre public void setGenre(String genre) { this.genre = genre; } public String getGenre() { return genre; } //getter for name public String getName() { return name; } //return the string format of this class public String toString(){ return name; } } //BookCase which contains arraylist of book of same genre class Bookcase{ ArrayList bookcase = new ArrayList<>();; String genre; Bookcase(String genre){ this.genre = genre; } //this add book of same genre to the arraylist void addBook(Book book){ if(book.getGenre().compareTo(genre)==0){ bookcase.add(book); }else{ System.out.println("Book belong to different genre"); } } //setter for genre public void setGenre(String genre) { this.genre = genre; } //getter for genre public String getGenre() { return genre; } //setter for book arraylist public void setBookcase(ArrayList bookcase) { this.bookcase = bookcase; } //getter for bookcase arraylist public ArrayList getBookcase() { return bookcase; } //this will return the string form of this class public String toString(){ String output=""; output +=genre+ " : "; for(Book book:bookcase){ output += "\t"+book.toString()+" "; } output +=" "; return output; } } //BookStore class which is a singlton class //means class which has only one instance //this contains arraylist of bookcase //it contains one bookcase for each genre class Bookstore{ private static Bookstore instance = null; ArrayList bookstore = new ArrayList<>(); private Bookstore(){ } //it returns the instance public static Bookstore getInsatance(){ if(instance ==null){ instance = new Bookstore(); } return instance; } //this will the book case to the arraylist void addBookCase(Bookcase bc){ boolean flag = false; for(Bookcase bookcase : bookstore){ //if bookcase of coming genre already present then it bookstore then //it merge with existing one if(bookcase.getGenre().compareTo(bc.getGenre())==0){ System.out.println("This genre bookcase already present..."); System.out.println("Merging book case"); flag = true; bookcase.bookcase.addAll(bc.bookcase); System.out.println("done..."); } } //if new gerne comes then it simply add it if(flag==false){ bookstore.add(bc); } } //return the string form of this class public String toString(){ String output =""; for(Bookcase bc:bookstore){ output += bc.toString(); } return output; } } public class BookstoreTest{ public static void main(String[] args) { //make a instance of bookstore class Bookstore bookStore = Bookstore.getInsatance(); //create instance of all genres Bookcase bc_novel1 = new Bookcase("novel"); Bookcase bc_novel2 = new Bookcase("novel"); Bookcase bc_thriller = new Bookcase("thriller"); Bookcase bc_essay = new Bookcase("essay"); Bookcase bc_manual = new Bookcase("manual"); //add books of same genre to there bookcase Book b1 = new Book("Absalom, Absalom! by William Faulkner", "novel"); Book b2 = new Book("A Time to Kill by John Grisham", "novel"); bc_novel1.addBook(b1); bc_novel1.addBook(b2); Book b3 = new Book("The House of Mirth by Edith Wharton", "novel"); Book b4 = new Book("East of Eden by John Steinbeck", "novel"); bc_novel2.addBook(b3); bc_novel2.addBook(b4); Book b5 = new Book("And Then There Were None by Agatha Christie", "thriller"); Book b6 = new Book("A Simple Favor: A Novel by Darcey Bell", "thriller"); bc_thriller.addBook(b5); bc_thriller.addBook(b6); Book b7 = new Book("Aleksandar Hemon, The Book of My Lives","essay"); Book b8 = new Book("Robin Wall Kimmerer, Braiding Sweetgrass","essay"); bc_essay.addBook(b7); bc_essay.addBook(b8); Book b9 = new Book("IPhone: The Missing Manual","manual"); Book b10 = new Book("The Manual: A Philosopher's Guide to Life","manual"); bc_manual.addBook(b9); bc_manual.addBook(b10); //now add bookcases to the bookstore bookStore.addBookCase(bc_novel1); bookStore.addBookCase(bc_novel2); bookStore.addBookCase(bc_thriller); bookStore.addBookCase(bc_essay); bookStore.addBookCase(bc_manual); //print the bookstore System.out.println(bookStore); //Now we try to add essay gener book to add into manual bookcase bc_manual.addBook(b7); } } ===============================================
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
