I NEED THE NECESSARY JAVA SOURCE FILES. YOU CAN REFER TO THE FIRST PART AT https
ID: 3604832 • Letter: I
Question
I NEED THE NECESSARY JAVA SOURCE FILES. YOU CAN REFER TO THE FIRST PART AT https://www.chegg.com/homework-help/questions-and-answers/please-provide-necessary-java-source-files-needs-bear-first-part-followed-second-part-q24690396
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 CatalogItem has a title of type String. Each Catalogltem has a price of type double . . Each Book (is a Catalogltem) 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 assi gnment) 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: | Author: | Price: | ISBN: For AudioBooks it would be the same for books, except the price would be the discounted price and we would append: 1 RunningTime: For DVDs it would be: Title: | Director: | Price: | Year: | 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.Explanation / Answer
Here is the basic code for you:
import java.io.*;
import java.util.*;
class OnlineStore
{
public static void displayMenu()
{
System.out.println("**Welcome to the Comets Books and DVSs 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)
{
int[] inventoryNumbers = new int[prices.length];
for(int i = 0; i < prices.length; i++)
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 displayArrays(String[] itemsArray, double[] pricesArray, String itemType)
{
int[] inventoryNumbers = sort(pricesArray);
for(int i = 0; i < pricesArray.length; i++)
System.out.printf("%d %-20s $%3.2f ", inventoryNumbers[i]+1, itemsArray[inventoryNumbers[i]], pricesArray[inventoryNumbers[i]]);
}
public static int getInventoryNumber()
{
System.out.println("Enter the inventory number you wish to purchase (-1 to redisplay the menu.): ");
Scanner sc = new Scanner(System.in);
int inventoryNumber = sc.nextInt();
return inventoryNumber;
}
public static void displayArrays(String[] cartItems, double[] cartPrices, int numOfItems)
{
for(int i = 0; i < numOfItems; i++)
System.out.printf("%-20s $%3.2f ", cartItems[i], cartPrices[i]);
System.out.printf("Total + tax $%4.2f ", getTotal(cartPrices, numOfItems));
}
public static double getTotal(double[] prices, int numOfItems)
{
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)
{
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++"};
double[] bookPrices = {45.99, 89.34, 100.00, 25.00, 49.99};
String[] dvds = {"Snow White", "Cinderella", "Dumbo", "Bambi", "Frozen"};
double[] dvdsPrices = {19.99, 24.99, 17.99, 21.99, 24.99};
String[] cartItems = new String[5];
double[] cartPrices = new double[5];
int numOfItems = 0, invNum;
Scanner sc = new Scanner(System.in);
int choice;
while(true)
{
displayMenu();
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch(choice)
{
case 1: displayArrays(books, bookPrices, "Books");
break;
case 2: displayArrays(dvds, dvdsPrices, "DVDs");
break;
case 3: invNum = getInventoryNumber();
cartItems[numOfItems] = books[invNum-1];
cartPrices[numOfItems] = bookPrices[invNum-1];
numOfItems++;
break;
case 4: invNum = getInventoryNumber();
cartItems[numOfItems] = dvds[invNum-1];
cartPrices[numOfItems] = dvdsPrices[invNum-1];
numOfItems++;
break;
case 5: displayArrays(cartItems, cartPrices, numOfItems);
break;
case 6: displayArrays(cartItems, cartPrices, numOfItems);
clearArrays(cartItems, cartPrices, numOfItems);
numOfItems = 0;
break;
case 7: clearArrays(cartItems, cartPrices, numOfItems);
numOfItems = 0;
break;
case 8: return;
default: System.out.println("This option is not acceptable.");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.