Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming question: Develop and online store which sells DVDs and Books,

ID: 3596946 • Letter: J

Question

Java Programming question:

Develop and online store which sells DVDs and Books, except this time,

we will also be selling AudioBooks which are specialized versions of Books (i.e., extension will come into play here). Use of multiple classes is a must for this Assignment and the perspective will be that of a store owner maintaining a Catalog.

The updated requirements for entities are as follows:

• Each CatalogItem has a title of type String.

• Each CatalogItem has a price of type double.

• Each Book (is a CatalogItem) has an author which is of type String.

•Each Book has an ISBN number which is of type int. Every book has a unique ISBN number.

• Each AudioBook has all of the properties of a Book, and in addition has a property named

runningTime of type double (conceptually you can think of this as minutes, but it doesn’t matter

for the purposes of the assignment).

• Each DVD (is a CatalogItem) has director which is of type String.

• Each DVD has a year which is of type int.

• Each DVD has an dvdcode which is of type int and is unique for all DVDs.

Each of the properties is mandatory (cannot be a default) and private, and only retrieved via public getter methods (for example: getPrice which would return the price of a Book or DVD), and the properties can only be set via constructor functions.

Additionally, AudioBooks are special – when the price is retrieved via the method getPrice, the method in

the parent (Book) class is overridden, and instead the price is retrieved as the regular price of the book with a 10% discount (i.e., 90% of the price).

Each of the classes must define a toString method which *returns* information about the entity as follows:

For Books it would be:

Title: <title> | Author: <author> | Price: <price> | ISBN: <isbn>

For AudioBooks it would be the same for books, except the price would be the discounted price and we

would append: | RunningTime: <runningtime>.

For DVDs it would be:

Title: <title> | Director: <director> | Price: <price> | Year: <year> | DvdCode: <dvdcode>

We do not have a limit on the number of Books/AudioBooks/DVDs that our store can hold, and thus, ArrayLists would be useful here. You should have two ArrayLists: one for Books and one for DVDs and the ArrayLists should be typed accordingly (i.e., the code should not be declared unsafe upon compilation). You are *not allowed* to have a separate ArrayList for AudioBooks.

The distinction between Books and AudioBooks should be easy enough using the instanceof operator as discussed in class. Also there is no such thing as a standalone CatalogItem [Hint: this isn’t a concrete class]. Meaning it’s Abstract!

The menu option displayed would now look like:

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

Then, within a loop, if the user enters an option that is not 1, 2, 3, 4, 5, 6, or 9, display the message “This option is not acceptable” and loop back to redisplay the menu. Continue in this manner until a correct option is input.

You may implement the methods however you want as long as you satisfy the class relationships, and satisfy the following behavior:

If options 1, 2 or 3 are entered, follow-up by asking the User to enter the required details. Invalid values are not allowed: for example, a book must have a non-empty title and author, and a positive running time and price. An AudioBook must also have a positive running time. Similar restrictions apply on DVDs. If the User enters something invalid, prompt them to enter it again. If the User tries to add a Book or DVD that already exists (i.e., we already have a Book with that ISBN number or a DVD with that dvdcode) then let the User know that this is the case and return to the main menu. [Hint: Therefore, when options 1, 2 or 3 are entered, the first thing to ask the User for would be the ISBN number or the DVD code respectively].

If option 4 is selected, ask the User to enter the ISBN number to remove: if it exists in the Catalog,t hen remove it; and if it doesn’t then let the User know by saying “The Book doesn’t exist in the Catalog” (and then return to the main menu). Similar behavior applies to option 5, except this time with DVDs. After successful removal, display the Catalog (i.e., the behavior of option 6, and redisplay the main menu).

If option 6 is selected then display the entire Catalog to the User by displaying all the Book

information followed by all the DVD information, with the following line to separate the two.

The information for each Book or DVD must be displayed using the respective toString methods.

Continue the looping until option 9 is selected, and when option 9 is entered, end the program.

Explanation / Answer

Created an abstract Super class CatalogItem with title,price attributes along with parameterized constructor and getter methods. Then the Book class is derived from CatalogItem; implemented all the requirements. AudioBook class is derived from Book class; and overridden the getPrice() method to implement a 10% discount. DVD class is derived from CatalogItem. The toString() method is defined for Book,DVD and AudioBook classes. Then a Test class is created which contains the main function. Two ArrayListsbooklist and dvdlist is created and initialized. Menu is displayed on the screen and the user is prompted to enter a choice. Each functionalities – adding a book, adding an audiobook, adding a dvd, removing a book, removing a dvd, displaying the catalog are defined in separate methods to ensure object oriented programming standards. Exception handling is also done to ensure proper handling of user inputs. You can find more info in Javadoc comments included.

//CatalogItem.java

public abstract class CatalogItem {

      private String title;

      private double price;

      public CatalogItem(String title, double price) {

            /**

            * Constructor to initialize all the variables

            */

            this.title = title;

            this.price = price;

      }

      /**

      * returns the title of the item

      */

      public String getTitle() {

            return title;

      }

      /**

      * returns the price of the item

      */

      public double getPrice() {

            return price;

      }

}

//Book.java

public class Book extends CatalogItem {

      private int ISBN;/* book id */

      private String author;/* book author */

      public Book(String title, double price,int ISBN,String author) {

            super(title, price); /* passing values to the superclass */

            this.ISBN=ISBN;

            this.author=author;

      }

      @Override

      public boolean equals(Object obj) {

            /**

            * this method can check if two Book objects are same. Helpful when

            * using with ArrayList method contains()

            */

            if (obj instanceof Book) {

                  Book book = (Book) obj;

                  if (this.ISBN == book.ISBN) {

                        return true;

                  }

            }

            return false;

      }

      public int getISBN() {

            return ISBN;

      }

      public String getAuthor() {

            return author;

      }

      @Override

      public String toString() {

            /**

            * will return a String with all the details of a Book

            */

            String str="Title: "+getTitle();

            str+=" | Author: "+getAuthor();

            str+=" | Price: "+getPrice();

            str+=" | ISBN: "+getISBN();

            return str;

      }

}

//AudioBook.java

public class AudioBook extends Book {

      private double runningTime;

      public AudioBook(String title, double price, int ISBN, String author,

                  double runningTime) {

            /**

            * Passing values to superclass Consructor

            */

            super(title, price, ISBN, author);

            this.runningTime = runningTime;

      }

      public double getRunningTime() {

            return runningTime;

      }

      @Override

      public double getPrice() {

            /**

            * overriding the superclass method and returning 90% of the orginal

            * cost. ie. 10% discount

            */

            return super.getPrice() * .9;

      }

      @Override

      public String toString() {

            /**

            * will return a String with all the details of a Book + runningtime

            */

            String str = super.toString() + " | Runningtime: " + getRunningTime();

            return str;

      }

}

//DVD.java

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) {

            /**

            * Passing values to superclass Consructor

            */

            super(title, price);

            this.director = director;

            this.dvdcode = dvdcode;

            this.year = year;

      }

      public String getDirector() {

            return director;

      }

      public int getYear() {

            return year;

      }

      public int getDvdcode() {

            return dvdcode;

      }

      @Override

      public boolean equals(Object obj) {

            /**

            * this method can check if two DVD objects are same. Helpful when using

            * with ArrayList method contains()

            */

            if (obj instanceof DVD) {

                  DVD dvd = (DVD) obj;

                  if (this.dvdcode == dvd.dvdcode) {

                        return true;

                  }

            }

            return false;

      }

      @Override

      public String toString() {

            /**

            * will return a String with all the details of a Book

            */

            String str = "Title: " + getTitle();

            str += " | Director: " + getDirector();

            str += " | Price: " + getPrice();

            str += " | Year: " + getYear();

            str += " | Dvdcode: " + getDvdcode();

            return str;

      }

}

//Test.java

import java.util.ArrayList;

import java.util.Scanner;

public class Test {

      static ArrayList<Book> booklist;

      /**

      * An arraylist to store Books and

      * Audiobooks

      */

      static ArrayList<DVD> dvdlist;

      /** An arraylist to store DVDs */

      static Scanner scanner=new Scanner(System.in);/**

      * A scanner object to get the

      * user input; can be accessed from anywhere in the

      * program

      */

      public static void main(String[] args) {

            /**

            * Initializing the lists

            */

            booklist=new ArrayList<Book>();

            dvdlist=new ArrayList<DVD>();

            int choice=0;

           

            while(choice!=9){

                  /**

                  * Displaying the menu

                  */

                  System.out.println("**Welcome to the Comets Books and DVDs Store (Catalog Section)**");

                  System.out.println("Choose from the following options: 1 – Add Book 2 – Add AudioBook");

                  System.out.println("3 – Add DVD 4 – Remove Book 5 – Remove DVD 6 – Display Catalog");     

                  System.out.println("9 - Exit store");

                  try{

                        choice=Integer.parseInt(scanner.nextLine()); /*getting the choice*/

                        switch (choice) {

                        case 1:

                              addBook();

                              break;

                        case 2:

                              addAudioBook();

                              break;

                        case 3:

                              addDVD();

                              break;

                        case 4:

                              removeBook();

                              break;

                        case 5:

                              removeDVD();

                              break;

                        case 6:

                              displayCatalog();

                              break;

                        case 9:System.out.println("Thank you..."); /*the loop will end*/

                              break;

                        default: throw new Exception(); /*Invalid input*/

                             

                        }

                  }catch (Exception e) {

                        /**

                        * In case of invalid inputs; ie not a number

                        */

                        System.out.println("Invalid input,try again");

                  }                                                          

            }

      }

      /**

      * method to add a Book

      */

      public static void addBook(){

            System.out.println("Enter book title:");

            String title = scanner.nextLine();

            System.out.println("Enter book ISBN (ID):");

            int ISBN = Integer.parseInt(scanner.nextLine());

            System.out.println("Enter author name:");

            String auth=scanner.nextLine();

            System.out.println("Enter price:");

            double price = Double.parseDouble(scanner.nextLine());

            Book book=new Book(title, price, ISBN, auth);

            if(booklist.contains(book)){

                  System.out.println("Error: The catalog already have the same book");

            }else{

                  booklist.add(book);

                  System.out.println("Book added successfully");

            }

      }

      /**

      * method to add an AudioBook

      */

      public static void addAudioBook(){

            System.out.println("Enter audio book title:");

            String title = scanner.nextLine();

            System.out.println("Enter audio book ISBN (ID):");

            int ISBN = Integer.parseInt(scanner.nextLine());

            System.out.println("Enter author name:");

            String auth=scanner.nextLine();

            System.out.println("Enter price:");

            double price = Double.parseDouble(scanner.nextLine());

            System.out.println("Enter runningtime:");

            double runningTime = Double.parseDouble(scanner.nextLine());

            AudioBook audioBook=new AudioBook(title, price, ISBN, auth, runningTime);

            if(booklist.contains(audioBook)){

                  System.out.println("Error: The catalog already have the same audio book");

            }else{

                  booklist.add(audioBook);

                  System.out.println("Audio book added successfully");

            }

      }

      /**

      * method to add a DVD

      */

      public static void addDVD(){

            System.out.println("Enter DVD title:");

            String title = scanner.nextLine();

            System.out.println("Enter DVDcode:");

            int dvdcode = Integer.parseInt(scanner.nextLine());

            System.out.println("Enter director name:");

            String dir=scanner.nextLine();

            System.out.println("Enter Year:");

            int year = Integer.parseInt(scanner.nextLine());

            System.out.println("Enter price:");

            double price = Double.parseDouble(scanner.nextLine());

            DVD dvd=new DVD(title, price, dir, year, dvdcode);

            if(dvdlist.contains(dvd)){

                  System.out.println("Error: The catalog already have the same DVD");

            }else{

                  dvdlist.add(dvd);

                  System.out.println("DVD added successfully");

            }

      }

      /**

      * method to remove a Book

      */

      public static void removeBook(){

            System.out.println("Enter the book ISBN: ");

            int isbn=Integer.parseInt(scanner.nextLine());

            boolean found=false;

            for (Book book : booklist) {

                  if(book.getISBN()==isbn){

                        booklist.remove(book);

                        System.out.println("Book removed successfully");

                        found=true;

                  }

            }

            if(!found){

                  System.out.println("The book doesn't exist in the catalog");

            }

      }

      /**

      * method to remove a DVD

      */

      public static void removeDVD(){

            System.out.println("Enter the DVDcode: ");

            int code=Integer.parseInt(scanner.nextLine());

            boolean found=false;

            for (DVD dvd : dvdlist) {

                  if(dvd.getDvdcode()==code){

                        booklist.remove(dvd);

                        System.out.println("DVD removed successfully");

                        found=true;

                  }

            }

            if(!found){

                  System.out.println("The DVD doesn't exist in the catalog");

            }

      }

      /**

      * method to display the entire catalog

      */

      public static void displayCatalog(){

            System.out.println("Catalog");

            System.out.println("-----------------------------------------");

            System.out.println("Books");

            System.out.println("-----------------------------------------");

            for (Book book : booklist) {

                  if(! (book instanceof AudioBook)){

                        System.out.println(book.toString());

                  }

            }

            System.out.println("-----------------------------------------");

            System.out.println("AudioBooks");

            System.out.println("-----------------------------------------");

            for (Book book : booklist) {

                  if(book instanceof AudioBook){

                        System.out.println(book.toString());

                  }

            }

            System.out.println("-----------------------------------------");

            System.out.println("DVDs");

            System.out.println("-----------------------------------------");

            for(DVD dvd: dvdlist){

                  System.out.println(dvd.toString());

            }

      }

}

//Output

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

1

Enter book title:

Alice in Wonderland

Enter book ISBN (ID):

102

Enter author name:

Thomas

Enter price:

150

Book added successfully

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

2

Enter audio book title:

Hello

Enter audio book ISBN (ID):

103

Enter author name:

Adele

Enter price:

100

Enter runningtime:

85

Audio book added successfully

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

6

Catalog

-----------------------------------------

Books

-----------------------------------------

Title: Alice in Wonderland | Author: Thomas | Price: 150.0 | ISBN: 102

-----------------------------------------

AudioBooks

-----------------------------------------

Title: Hello | Author: Adele | Price: 90.0 | ISBN: 103 | Runningtime: 85.0

-----------------------------------------

DVDs

-----------------------------------------

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

4

Enter the book ISBN:

104

The book doesn't exist in the catalog

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

4

Enter the book ISBN:

102

Book removed successfully

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

6

Catalog

-----------------------------------------

Books

-----------------------------------------

-----------------------------------------

AudioBooks

-----------------------------------------

Title: Hello | Author: Adele | Price: 90.0 | ISBN: 103 | Runningtime: 85.0

-----------------------------------------

DVDs

-----------------------------------------

**Welcome to the Comets Books and DVDs Store (Catalog Section)**

Choose from the following options:

1 – Add Book

2 – Add AudioBook

3 – Add DVD

4 – Remove Book

5 – Remove DVD

6 – Display Catalog

9 - Exit store

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote