Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.
This is my prior code of which needs implementation:
This is my prior code of which needs implementation:
// CatalogItem class to store title and price of Book or DVD
abstract public class CatalogItem {
private String title;
private double price;
public CatalogItem(String title,double price)
{
this.title = title;
this.price = price;
}
public String getTitle()
{
return title;
}
public double getPrice() {
return price;
}
public String toString()
{
return "Title : "+title +" Price :"+price;
}
}
// End of CatalogItem class
// Book class to store details of a Book
public class Book extends CatalogItem {
private String author;
private int isbn;
public Book(String author, int isbn, String title, double price) {
super(title, price);
this.author = author;
this.isbn = isbn;
}
public String getAuthor()
{
return author;
}
public int getIsbn()
{
return isbn;
}
public String toString()
{
return super.toString() + " Author : "+author +" ISBN : "+isbn;
}
}
// End of Book class
// AudioBook class to store details of an Audio Book
public class AudioBook extends Book {
double runningTime;
public AudioBook(String author, int isbn, String title, double price, double runningTime) {
super(author, isbn, title, price);
this.runningTime = runningTime;
}
public double getPrice() {
return (0.9*getPrice());
}
public double getRunningTime()
{
return runningTime;
}
public String toString()
{
return super.toString() + " Running time : "+runningTime;
}
}
// End of AudioBook class
// DVD class to store details of a DVD
public class DVD extends CatalogItem{
String director;
int year;
int dvdCode;
public DVD(String title, double price, String director,int year,int dvdCode) {
super(title, price);
this.director = director;
this.year = year;
this.dvdCode = dvdCode;
}
public String getDirector()
{
return director;
}
public int getYear()
{
return year;
}
public int getdvdCode()
{
return dvdCode;
}
public String toString()
{
return super.toString() + " Director : "+ director+" Year : "+year+" Dvd Code: "+dvdCode;
}
}
// End of DVD class
// Java program to implement a Catalog for a Book and DVD store
import java.util.ArrayList;
import java.util.Scanner;
public class CommetsStore {
public static void displayMenu() {
System.out.println("** Welcome to the Commets Book and DVDs Store (Catalog Section) **");
System.out.println(" Choose from the following options: ");
System.out.println(" 1- Add Book");
System.out.println(" 2- Add AudioBook");
System.out.println(" 3- Add DVD");
System.out.println(" 4- Remove Book");
System.out.println(" 5- Remove DVD");
System.out.println(" 6- Display Catalog");
System.out.println(" 9- Exit");
}
public static int addBook(ArrayList<Book> list,int id, boolean isAudioBook)
{
String title,author;
double price,runningTime = 0;
Scanner scan = new Scanner(System.in);
System.out.print("n Enter title of the book : ");
title = scan.nextLine();
System.out.print("n Enter author of the book : ");
author = scan.nextLine();
System.out.print("n Enter price of the book : $");
price = scan.nextDouble();
scan.nextLine();
while(price < 0)
{
System.out.print("n Invalid price. n Enter the price of the book : $");
price = scan.nextDouble();
scan.nextLine();
}
//scan.nextLine();
if(isAudioBook)
{
System.out.print("n Enter the running time of the book : ");
runningTime = scan.nextDouble();
scan.nextLine();
while(runningTime < 0)
{
System.out.print("n Invalid running time . n Enter the running time of the book : ");
runningTime = scan.nextDouble();
scan.nextLine();
}
}
int bookId = isBookPresent(list,title,author);
if(bookId != -1)
{
System.out.println("n Book already exists with ISBN : "+bookId);
}else {
if(isAudioBook)
list.add(new AudioBook(author,id,title,price,runningTime));
else
list.add(new Book(author,id,title,price));
System.out.println("n Your book with has been added with ISBN :"+id);
id++;
}
//scan.close();
return id;
}
public static int isBookPresent(ArrayList<Book> list,String title,String author)
{
for(int i=0;i<list.size();i++)
{
if(list.get(i).getAuthor().equalsIgnoreCase(author) && list.get(i).getTitle().equalsIgnoreCase(title))
return list.get(i).getIsbn();
}
return -1;
}
public static int addDVD(ArrayList<DVD> list,int id)
{
String title,director;
double price;
int year;
Scanner scan = new Scanner(System.in);
System.out.print("n Enter title of the DVD : ");
title = scan.nextLine();
System.out.print("n Enter director of the DVD : ");
director = scan.nextLine();
System.out.print("n Enter price of the DVD : $");
price = scan.nextDouble();
scan.nextLine();
while(price < 0)
{
System.out.print("n Invalid price. n Enter the price of the DVD : $");
price = scan.nextDouble();
scan.nextLine();
}
System.out.print("n Enter the year of the DVD : ");
year = scan.nextInt();
scan.nextLine();
while(year < 0)
{
System.out.print("n Invalid year. n Enter the year of the DVD : ");
price = scan.nextInt();
scan.nextLine();
}
int dvdId = isDVDPresent(list, title, director ,year);
if(dvdId != -1)
{
System.out.println("n Dvd already exists with dvdCode : "+dvdId);
}else {
list.add(new DVD(title,price,director,year,id));
System.out.println("n Your DVD with has been added with code :"+id);
id++;
}
//scan.close();
return id;
}
public static int isDVDPresent(ArrayList<DVD> list,String title,String director,int year)
{
for(int i=0;i<list.size();i++)
if(list.get(i).getTitle().equalsIgnoreCase(title) && list.get(i).getDirector().equalsIgnoreCase(director) && list.get(i).getYear() == year)
return list.get(i).getdvdCode();
return -1;
}
public static void removeBook(ArrayList<Book> list)
{
int id;
boolean bookFound = false;
Scanner scan = new Scanner(System.in);
System.out.print("n Enter the ISBN of the book : ");
id = scan.nextInt();
scan.nextLine();
while(id < 0)
{
System.out.print("n Invalid ISBN .n Enter the ISBN of the book : ");
id = scan.nextInt();
scan.nextLine();
}
for(int i=0;i<list.size();i++)
{
if(list.get(i).getIsbn() == id) {
list.remove(i);
System.out.println("n Book with ISBN "+id+" removed");
bookFound = true;
break;
}
}
if(!bookFound)
System.out.println("n Book with ISBN : "+id+" doesn't exist ");
//scan.close();
}
public static void removeDVD(ArrayList<DVD> list)
{
int id;
boolean dvdFound = false;
Scanner scan = new Scanner(System.in);
System.out.print("n Enter the code of the DVD : ");
id = scan.nextInt();
scan.nextLine();
while(id < 0)
{
System.out.print("n Invalid code .n Enter the code of the DVD : ");
id = scan.nextInt();
scan.nextLine();
}
for(int i=0;i<list.size();i++)
{
if(list.get(i).getdvdCode() == id) {
list.remove(i);
System.out.println("n Book with Code "+id+" removed");
dvdFound = true;
break;
}
}
if(!dvdFound)
System.out.println("n DVD with code : "+id+" doesn't exist ");
//scan.close();
}
public static void displayCatalog(ArrayList<Book> bookList, ArrayList<DVD> dvdList)
{
System.out.println(" ****Catalog**** ");
System.out.println(" Books & Audio Books : ");
for(int i=0;i<bookList.size();i++)
System.out.println(bookList.get(i));
System.out.println(" DVDs : ");
for(int i=0;i<dvdList.size();i++)
System.out.println(dvdList.get(i));
}
public static void main(String[] args) {
ArrayList<Book> bookList = new ArrayList<Book>();
ArrayList<DVD> dvdList = new ArrayList<DVD>();
int bookId = 1;
int dvdCode = 1;
int response;
Scanner scan = new Scanner(System.in);
do {
displayMenu();
System.out.print("Enter your choice : ");
response = scan.nextInt();
scan.nextLine();
switch(response)
{
case 1: bookId = addBook(bookList,bookId,false);
break;
case 2: bookId = addBook(bookList,bookId,true);
break;
case 3: dvdCode = addDVD(dvdList,dvdCode);
break;
case 4: removeBook(bookList);
break;
case 5 : removeDVD(dvdList);
break;
case 6: displayCatalog(bookList,dvdList);
break;
case 9:
break;
default : System.out.println("n Invalid option ");
break;
}
}while(response != 9);
scan.close();
}
}
// End of CommetsStore class
- Attachment 1
- Attachment 2
- Attachment 3