Question: Continue developing the online store from Assignment 2 which sells DVDs and Books, except this time, we will also be selling AudioBooks which are specialized
Continue developing the online store from Assignment 2 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 Book 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 Book has a title of type String. Each Book has a price of type double. 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 doesnt matter for the purposes of the assignment). Each DVD has director which is of type String. Each DVD has a title 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 DVD has a price of type double 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 title of a Book or DVD), and the properties can only be set via constructor functions. The one exception is the quantity property which can be set via a Setter method (for example: setQuantity). 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:
************************************************************************************************************
The original Code:
import java.util.Scanner; class BooksAndDVDs { public static void displayMenu() // Original startup menu for user prompt { System.out.println("**Welcome to the Comets Books and DVDs Store**"); System.out.println(" Choose from the following options:"); System.out.println("1 - Browse books inventory (price low to high)"); System.out.println("2 - Browse DVDs inventory (price low to high)"); System.out.println("3 - Add a book to the cart"); System.out.println("4 - Add a DVD to the cart"); System.out.println("5 - View cart"); System.out.println("6 - Checkout"); System.out.println("7 - Cancel Order"); System.out.println("8 - Exit store"); } public static int[] sort(double[] prices) // Selection sorting method for array { int[] inventoryNumbers = new int[prices.length]; for(int i = 0; i < prices.length; i++) // Find the minimum in the list inventoryNumbers[i] = i; for(int i = 0; i < prices.length-1; i++) { int small = i; for(int j = i; j < prices.length; j++) if(prices[inventoryNumbers[j]] < prices[inventoryNumbers[small]]) small = j; int temp = inventoryNumbers[small]; inventoryNumbers[small] = inventoryNumbers[i]; inventoryNumbers[i] = temp; } return inventoryNumbers; } public static void displayArray(String[] itemsArray, double[] pricesArray, String itemType) // Method for displaying arrays { int[] inventoryNumbers = sort(pricesArray); // Call sorting method for prices for(int i = 0; i < pricesArray.length; i++) System.out.printf("%d\t%-15s\t$%3.2f ", inventoryNumbers[i]+1, itemsArray[inventoryNumbers[i]], pricesArray[inventoryNumbers[i]]); } public static int getInventoryNumber() // Prompt for inventory number when buying DVD or Books { System.out.println("Enter the inventory number you wish to purchase (-1 to redisplay the menu.): "); Scanner sc = new Scanner(System.in); int invNumber = sc.nextInt(); return invNumber; } public static void displayArray(String[] cartItems, double[] cartPrices, int numOfItems) // Displaying Cart Items { for(int i = 0; i < numOfItems; i++) System.out.printf("%-15s\t$%3.2f ", cartItems[i], cartPrices[i]); //Aligning display System.out.printf("Total + tax\t$%4.2f ", getTotal(cartPrices, numOfItems)); } public static double getTotal(double[] prices, int numOfItems) // Sum up all cart items and add tax { double total = 0; for(int i = 0; i < numOfItems; i++) total += prices[i]; return total * 1.0825; } public static void clearArrays(String[] items, double[] prices, int numOfItems) // Method to empty cart { for(int i = 0; i < numOfItems; i++) { items[i] = null; prices[i] = 0; } } public static void main(String[] args) { String[] books = {"Intro to Java", "Intro to C++", "Python", "Perl", "C++"}; // list of books double[] bookPrices = {45.99, 89.34, 100.00, 25.00, 49.99}; // prices of books String[] dvds = {"Snow White", "Cinderella", "Dumbo", "Bambi", "Frozen"}; // list of DVDs double[] dvdsPrices = {19.99, 24.99, 17.99, 21.99, 24.99}; //prices of dvds String[] cartItems = new String[5]; double[] cartPrices = new double[5]; int numOfItems = 0, invInput; Scanner sc = new Scanner(System.in); int choice; while(true) { displayMenu(); //Call intro display System.out.print("Enter your choice: "); choice = sc.nextInt(); switch(choice) // Switch method for selection { case 1: displayArray(books, bookPrices, "Books"); //Call displayArray for book in order break; case 2: displayArray(dvds, dvdsPrices, "DVDs"); // Call displayArray for DVDs in order break; case 3: invInput = getInventoryNumber(); // Getting input from user about buying a book cartItems[numOfItems] = books[invInput-1]; // adding the book into the shopping cart cartPrices[numOfItems] = bookPrices[invInput-1]; // adding the price of book into total numOfItems++; break; case 4: invInput = getInventoryNumber(); cartItems[numOfItems] = dvds[invInput-1]; cartPrices[numOfItems] = dvdsPrices[invInput-1]; numOfItems++; break; case 5: displayArray(cartItems, cartPrices, numOfItems); // Display Cart break; case 6: displayArray(cartItems, cartPrices, numOfItems); // Display cart clearArrays(cartItems, cartPrices, numOfItems); // Clear cart after "checking out" numOfItems = 0; System.out.println("Thank you for shopping with us!"); break; case 7: clearArrays(cartItems, cartPrices, numOfItems); // Clear cart and cancel order numOfItems = 0; System.out.println("Your order has been cancelled."); break; case 8: System.out.println("Thanks for stopping by, see you next time!"); return; default: System.out.println("This option is not acceptable."); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
