Lab: Develop program for an online store which sells dvds and honks (items). The
ID: 3890259 • Letter: L
Question
Lab: Develop program for an online store which sells dvds and honks (items). The store will have cart which will cantain the items the user wants to purchase and their prics. The inventory for hooks and dvds will be stored in twn separate bring arrays which will coatzin the names of the bonks and the names of the dvds. In addition, twa separate arys of type doubie wil heused to siore the price corespending to each it You will create the arrays and populare them with data ar the heginning of your program. This is how the aays will lnok 45.99 89.14 25.00 49.99 ds nom dwdsPrioes 9.99 24.99 1.99 24.99 Murtover, you will impl ment the cart as tao errays too. Thu first array is for he narnes uflhe items the user wishes to purchase (hks dvds). The sacond is for the earresponding prices You oan assume a maximum can size af Sitesia, the User cannet have mare than 5 items in their cart. Afleryuu create an initlize all the arays and aray liss, the prugram will thum display anumu for thousur by calling be mathod dLsplayNenu { } . Th' 1nethod will display the following Ire8 wt called *Wdicune to the Cormets Beoss snN DVDs Store Chooae from the following aptians 1- Brawse hecks inventory price loa ta high 2 - Browse DVDs sory price low to hiph - Adda honk to the cart s-Viow cart 5 -Checkout s - Exit sture Then, within loup, iftus user enters an optioc dat is not 1, 2, 3, 4, 5, 6,7 or 8. display the message This option is not acceptable" and loop hock :n redispay the menu. Car tinue in this mannar mil a coeet option is input Ir option 1 is entered, call the methed displaayswith the following sigzature: ta display the inverory, Also keep in mind that we want to disply the inventory in the arda of low to high-in terms of price. You can implement any sorting algorithm EXCEPT bubble surt) thst we talked sbout in clsss to sort toe arrays You shoald t pass the correct two srays and a stringDooks" or "DVDto the methedas shuwn abuve depunding cm which oplin gels selected. Thmethod should display be arrays ine leQowing format(the books and bar prices are used hereas the example; the same formatting applies to DVDs: Path to j*va Fythan If option 3 is seled, call the method gutitnbr the methud should ask thes tu unter the inventory number they wish to purchase (from the list in Option 1) and-l if dey wish to redisplny the menu and nnt select nn inventary. Then the methad will read the invemary mumber kd rerarn the numbx entened by the user. You can take the inventory number wiich gets rcturned and remove 1 rom il to r the correct index into the array, Use the index to retrieve the coree item and coresponding pre, and then aid them to the art array The abuve dicussion applies fur option 4 (and corrspondingly optiun 2) as well If option 5 is seletad, you can overload the diaplayh=aral > method (or crnale 1 separate method) to accept just the arrays and display thu cari cartenis similar tu tho way you have displayed the inventory. Here is what the outpur should loo like format-wise yoa ean call the getTotal0 etotoget the ent total, see option 6 below 43.99 421.99 I tho array lists are mpty display yurcart is emply lowed by the menu. If option 6 is selerred, call the method gutzot l which aocepts as input, the price array und returns the tutal by luoping uver prices and aiculating the total. XMake sure you also add an 8.25 tax to the tocal. Dispay the intal for the user and clear the array (se oprion 5 se an arhanced tor loopto iterse ove prices in his method and End tee totl. Ir option 7 is selecod, call the cleareays mehod and pass it the cagtItens snd prices arrays, In the method, you will clear the scTRys to remove all he ite wich bave bom added 10 can the ordur. Cuntinue this looping until opticm B is selucte. When eptiun is entered, end the prugram. Subcnit only your source cude .java file(sN!! U- Remember to cuament your code wellExplanation / Answer
Shop.java
import java.io.*;
public class Shop {
//display menu
public void displayMenu() throws Exception
{
System.out.println("***Welcome to the comets Book & DVD store***");
System.out.println(" Choose the following option:");
System.out.println("1 - Brows books inventory( Price Low to High )");
System.out.println("2 - Brows DVDs inventory( Price Low to High )");
System.out.println("3 - Add a book to cart");
System.out.println("4 - Add a DVD to cart");
System.out.println("5 - View cart");
System.out.println("6 - Check out");
System.out.println(" 7 - Cancel order");
System.out.println("8 - Exit Store");
}
//sorting method
public void selectionSort(double[] array){
for (int i = 0; i < array.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < array.length; j++){
if (array[j] < array[index]){
index = j;
}
}
double lessNo = array[index];
array[index] = array[i];
array[i] = lessNo;
}
}
//display array method
public void displayArrays(String itemArray[],double priceArray[],String itemType)
{
double temparray[]= new double[5];
for(int i=0;i<5;i++)
{
temparray[i]=priceArray[i];
}
int arrayIndex[] = new int[5];
Shop shop = new Shop();
if(itemType.equals("cart"))
{
System.out.println("Item Prices");
System.out.println("-------------------------");
for(int i=0;i<5;i++)
{
if(itemArray[i] == null)
{
System.out.println("Your cart is empty");
break;
}
System.out.println(itemArray[i]+" "+priceArray[i]);
}
System.out.println("--------------------------");
System.out.println("Tolal + Tax "+shop.getTotal(priceArray));
}
else
{
shop.selectionSort(temparray);
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(temparray[i] == priceArray[j])
{
arrayIndex[i] = (j+1);
}
}
}
System.out.println("Inventory Number Book Price");
System.out.println("-----------------------------------------------------");
for(int i=0;i<5;i++)
{
int index = arrayIndex[i];
System.out.println(index+" "+itemArray[index-1]+" "+priceArray[index-1]);
}
}
}
//get inventory mrthod
public int getInventoryNumber() throws Exception
{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
System.out.println(" Enter the inventory number of the book, or press -1 to see the menu");
int invNo = Integer.parseInt(br.readLine());
return invNo;
}
//get total method
public double getTotal(double priceArray[])
{
double total = 0;
for(int i=0;i<5;i++)
{
total=total+priceArray[i];
}
total = total + (total*8.25)/100;
return total;
}
//cleararray method
public void clearArrays(String itemArray[],double priceArray[])
{
for(int i=0;i<5;i++)
{
itemArray[i]=null;
priceArray[i]=0;
}
}
public static void main(String[] args) throws Exception {
//array initilization
String books[] = new String[]{"Intro to JAVA","Intro to C++","Python","Perl","C#"};
double booksPrices[] = new double[]{45.99,89.34,100.00,25.00,49.99};
String dvds[] = new String[]{"Snow White","Cinderella","Dumbo","Bambi","Frozen"};
double dvdsPrices[] = new double[]{19.99,24.34,17.00,21.00,24.99};
String cartItem[] = new String[5];
double cartPrices[] = new double[5];
Shop shop = new Shop();
int choice=0,cartIndex=0,invNo;
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
//infinite loop
while(true)
{
shop.displayMenu();
System.out.println(" Enter your choice:");
choice = Integer.parseInt(br.readLine());
if(choice>=1 && choice<=8)
{
if(choice == 1)
{
shop.displayArrays(books, booksPrices, "books");
}
if(choice == 2)
{
shop.displayArrays(dvds, dvdsPrices, "dvds");
}
if(choice == 3)
{
invNo = shop.getInventoryNumber();
if((invNo == -1) ||(invNo>=1 && invNo<=5) )
{
if(invNo>=1 && invNo<=5)
{
cartItem[cartIndex] = books[invNo];
cartPrices[cartIndex] = booksPrices[invNo];
cartIndex++;
}
}
else
{
System.out.println(" This option is not acceptable.");
}
}
if(choice == 4)
{
invNo = shop.getInventoryNumber();
if((invNo == -1) ||(invNo>=1 && invNo<=5) )
{
if(invNo>=1 && invNo<=5)
{
cartItem[cartIndex] = dvds[invNo];
cartPrices[cartIndex] = dvdsPrices[invNo];
cartIndex++;
}
}
else
{
System.out.println(" This option is not acceptable.");
}
}
if(choice == 5)
{
shop.displayArrays(cartItem, cartPrices, "cart");
}
if(choice == 6)
{
System.out.println("Total price including taxes: "+shop.getTotal(cartPrices));
}
if(choice == 7)
{
shop.clearArrays(cartItem, cartPrices);
}
if(choice == 8)
{
break;
}
}
else
{
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.