In: Computer Science
this won't compile
package com.test;
public class CatalogItem {
private String title;
private double price;
public CatalogItem(String title, double price) {
super();
this.title = title;
this.price = price;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
}
//Book.java
package com.test;
public class Book extends CatalogItem {
private String author;
private int ISBN;
public Book(String title, double price, String author, int iSBN) {
super(title, price);
this.author = author;
ISBN = iSBN;
}
public String getAuthor() {
return author;
}
public int getISBN() {
return ISBN;
}
@Override
public String toString() {
return "Title: " + getTitle() + " | Author: "+author + " | Price: " + getPrice() + " ISBN: "+ISBN;
}
}
//AudioBook.java
package com.test;
public class AudioBook extends Book {
private double runningTime;
public AudioBook(String title, double price, String author, int iSBN, double runningTime) {
super(title, price, author, iSBN);
this.runningTime = runningTime;
}
public double getRunningTime() {
return runningTime;
}
public double getP() {
return getPrice()*0.90;
}
@Override
public String toString() {
return "Title: " + getTitle() + " | Author: "+getAuthor() + " | Price: " + getP() + " | ISBN: "+getISBN() + " | running time: "+runningTime;
}
}
//DVD.java
package com.test;
public class DVD extends CatalogItem {
private String director;
private int year;
private 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;
}
@Override
public String toString() {
return "Title: " + getTitle() + " | Director: "+director + " | Price: " + getPrice() + " | Year: "+year + " | DvdCode: "+dvdcode;
}
}
//Driver.java
package com.test;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static ArrayList<Book> booklist = new ArrayList<>();
public static ArrayList<DVD> dvdlist = new ArrayList<>();
public static void display()
{
for(int i=0;i<booklist.size();i++)
{
if(booklist.get(i) instanceof AudioBook)
System.out.println((AudioBook)(booklist.get(i)));
else
System.out.println(booklist.get(i));
}
System.out.println("-------------------------------------------------------------------------------");
for(int i=0;i<dvdlist.size();i++)
{
System.out.println(dvdlist.get(i));
}
}
//method to find book
public static boolean findBook(int isbn)
{
for(int i=0;i<booklist.size();i++)
{
if(booklist.get(i).getISBN() == isbn)
return true;
}
return false;
}
//method to find book
public static boolean findDVD(int dvdcode)
{
for(int i=0;i<dvdlist.size();i++)
{
if(dvdlist.get(i).getDvdcode() == dvdcode)
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println("Welcome to the comets Books and DVDs store(Catalog Section)");
int option;
String title;
double price;
String author;
int isbn;
double runningTime;
String director;
int year;
int dvdcode;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Choose from the following option:");
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 Store");
option = sc.nextInt();
if(option == 1)
{
System.out.println("Please enter Book isbn");
isbn = sc.nextInt();
if(findBook(isbn))
continue;
System.out.println("Please enter Book title");
title = sc.next();
System.out.println("Please enter Book price");
price = sc.nextDouble();
while(price < 0) {
System.out.println("Please enter Book valid price");
price = sc.nextDouble();
}
System.out.println("Please enter Book author");
author = sc.next();
Book b = new Book(title, price, author, isbn);
booklist.add(b);
}
else if(option == 2)
{
System.out.println("Please enter Audio Book isbn");
isbn = sc.nextInt();
if(findBook(isbn))
continue;
System.out.println("Please enter Audio Book title");
title = sc.next();
System.out.println("Please enter Audio Book price");
price = sc.nextDouble();
while(price < 0) {
System.out.println("Please enter Audio Book valid price");
price = sc.nextDouble();
}
System.out.println("Please enter Audio Book author");
author = sc.next();
System.out.println("Please enter Audio Book running time");
runningTime = sc.nextDouble();
while(runningTime < 0) {
System.out.println("Please enter Audio Book valid running time");
runningTime = sc.nextDouble();
}
AudioBook b = new AudioBook(title, price, author, isbn,runningTime);
booklist.add(b);
}
else if(option == 3)
{
System.out.println("Please enter DVD code");
dvdcode = sc.nextInt();
if(findDVD(dvdcode))
continue;
System.out.println("Please enter DVD title");
title = sc.next();
System.out.println("Please enter DVD price");
price = sc.nextDouble();
System.out.println("Please enter DVD director");
director = sc.next();
System.out.println("Please enter DVD year");
year = sc.nextInt();
DVD d = new DVD(title, price, director, year, dvdcode);
dvdlist.add(d);
}
else if(option == 4)
{
System.out.println("Enter isbn number to delete book");
isbn = sc.nextInt();
boolean remove = false;
for(int i=0;i<booklist.size();i++){
if(booklist.get(i).getISBN() == isbn){
booklist.remove(i);
remove = true;
}
}
if(remove == false)
System.out.println("The Book doesn't exist in catalog");
display();
}
else if(option == 5)
{
System.out.println("Enter dvd code to delete DVD");
dvdcode = sc.nextInt();
boolean remove = false;
for(int i=0;i<dvdlist.size();i++){
if(dvdlist.get(i).getDvdcode() == dvdcode){
dvdlist.remove(i);
remove = true;
}
}
if(remove == false)
System.out.println("The DVD doesn't exist in catalog");
display();
}
else if(option == 6)
{
display();
}
else if(option == 9)
break;
else
System.out.println("This option is not acceptable");
}
}
}
Code
CatalogItem.java
package com.test;
public class CatalogItem {
private String title;
private double price;
public CatalogItem(String title, double price) {
super();
this.title = title;
this.price = price;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
}
Book.java
package com.test;
public class Book extends CatalogItem {
private String author;
private int ISBN;
public Book(String title, double price, String author, int iSBN)
{
super(title, price);
this.author = author;
ISBN = iSBN;
}
public String getAuthor() {
return author;
}
public int getISBN() {
return ISBN;
}
@Override
public String toString() {
return "Title: " + getTitle() + " | Author: "+author + " | Price: "
+ getPrice() + " ISBN: "+ISBN;
}
}
AudioBook.java
package com.test;
public class AudioBook extends Book {
private double runningTime;
public AudioBook(String title, double price, String author, int
iSBN, double runningTime) {
super(title, price, author, iSBN);
this.runningTime = runningTime;
}
public double getRunningTime() {
return runningTime;
}
public double getP() {
return getPrice()*0.90;
}
@Override
public String toString() {
return "Title: " + getTitle() + " | Author: "+getAuthor() + " |
Price: " + getP() + " | ISBN: "+getISBN() + " | running time:
"+runningTime;
}
}
Dvd.java
package com.test;
public class DVD extends CatalogItem {
private String director;
private int year;
private 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;
}
@Override
public String toString() {
return "Title: " + getTitle() + " | Director: "+director + " |
Price: " + getPrice() + " | Year: "+year + " | DvdCode:
"+dvdcode;
}
}
Drver,java
package com.test;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver
{
public static ArrayList<Book> booklist = new
ArrayList<>();
public static ArrayList<DVD> dvdlist = new
ArrayList<>();
public static void display()
{
for(int i=0;i<booklist.size();i++)
{
if(booklist.get(i) instanceof AudioBook)
System.out.println((AudioBook)(booklist.get(i)));
else
System.out.println(booklist.get(i));
}
System.out.println("-------------------------------------------------------------------------------");
for(int i=0;i<dvdlist.size();i++)
{
System.out.println(dvdlist.get(i));
}
}
//method to find book
public static boolean findBook(int isbn)
{
for(int i=0;i<booklist.size();i++)
{
if(booklist.get(i).getISBN() == isbn)
return true;
}
return false;
}
//method to find book
public static boolean findDVD(int dvdcode)
{
for(int i=0;i<dvdlist.size();i++)
{
if(dvdlist.get(i).getDvdcode() == dvdcode)
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println("Welcome to the comets Books and DVDs
store(Catalog Section)");
int option;
String title;
double price;
String author;
int isbn;
double runningTime;
String director;
int year;
int dvdcode;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Choose from the following option:");
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 Store");
option = sc.nextInt();
if(option == 1)
{
System.out.println("Please enter Book isbn");
isbn = sc.nextInt();
if(findBook(isbn))
continue;
System.out.println("Please enter Book title");
title = sc.next();
System.out.println("Please enter Book price");
price = sc.nextDouble();
while(price < 0) {
System.out.println("Please enter Book valid price");
price = sc.nextDouble();
}
System.out.println("Please enter Book author");
author = sc.next();
Book b = new Book(title, price, author, isbn);
booklist.add(b);
}
else if(option == 2)
{
System.out.println("Please enter Audio Book isbn");
isbn = sc.nextInt();
if(findBook(isbn))
continue;
System.out.println("Please enter Audio Book title");
title = sc.next();
System.out.println("Please enter Audio Book price");
price = sc.nextDouble();
while(price < 0) {
System.out.println("Please enter Audio Book valid price");
price = sc.nextDouble();
}
System.out.println("Please enter Audio Book author");
author = sc.next();
System.out.println("Please enter Audio Book running time");
runningTime = sc.nextDouble();
while(runningTime < 0) {
System.out.println("Please enter Audio Book valid running
time");
runningTime = sc.nextDouble();
}
AudioBook b = new AudioBook(title, price, author,
isbn,runningTime);
booklist.add(b);
}
else if(option == 3)
{
System.out.println("Please enter DVD code");
dvdcode = sc.nextInt();
if(findDVD(dvdcode))
continue;
System.out.println("Please enter DVD title");
title = sc.next();
System.out.println("Please enter DVD price");
price = sc.nextDouble();
System.out.println("Please enter DVD director");
director = sc.next();
System.out.println("Please enter DVD year");
year = sc.nextInt();
DVD d = new DVD(title, price, director, year, dvdcode);
dvdlist.add(d);
}
else if(option == 4)
{
System.out.println("Enter isbn number to delete book");
isbn = sc.nextInt();
boolean remove = false;
for(int i=0;i<booklist.size();i++){
if(booklist.get(i).getISBN() == isbn){
booklist.remove(i);
remove = true;
}
}
if(remove == false)
System.out.println("The Book doesn't exist in catalog");
display();
}
else if(option == 5)
{
System.out.println("Enter dvd code to delete DVD");
dvdcode = sc.nextInt();
boolean remove = false;
for(int i=0;i<dvdlist.size();i++){
if(dvdlist.get(i).getDvdcode() == dvdcode){
dvdlist.remove(i);
remove = true;
}
}
if(remove == false)
System.out.println("The DVD doesn't exist in catalog");
display();
}
else if(option == 6)
{
display();
}
else if(option == 9)
break;
else
System.out.println("This option is not acceptable");
}
}
}
outputs
There is no errors in you program its runnig well see this outputs.
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.