Dutnption Develop a program for an online store which sells dvds and books (item
ID: 3753112 • Letter: D
Question
Dutnption Develop a program for an online store which sells dvds and books (items). The store will have a cart which will contain the items the user wants to purchase and their prices The inventory for books and dvds will be stored in two separate string arrays which will contain the names of the books and the names of the dvds. In addition, two separate arrays of type double will be used to store the price corresponding to each item. You will create the arrays and populate them with data at the beginning of your program. This is how the arrays will look like: books | Intro to Java booksPrices I Intro to C++ Python C# l perl Perl CH 89.34 45.99 dvds Snow White Cinderella Dumbo BambiFrozen 25.00 49.99 dvdsPrices 19.99 24.99 17.99 21.99 24.99 Moreover, you will implement the shopping cart as two arrays too. The first array is for the names of the items the user wishes to purchase (books or dvds). The second is for the corresponding prices. After you create and initialize all the arrays and array lists, the program will then display a menu for the user by calling the method displayMenu (). The method will display the following lines when called: **Welcome to the Mustangs Books and DVDs Store Choose from the following options: 1- Browse books inventory (price low to high) 2- Browse DVDs inventory (price low to high) 3- add a book to the cart 4 - add a DVD to the cart 5 View cart 6-Checkout 7 Cancel Order 9 Exit storeExplanation / Answer
ScreenShot
-----------------------------------------
Program
//Packages for Inputting and arraylist usage
import java.util.ArrayList;
import java.util.Scanner;
public class OnlineStore {
//Scanner object
public static Scanner sc=new Scanner(System.in);
//Menu display method
public static void displayMenu() {
System.out.println("**Welcome to the Mustangs Books and DVDs Store** ");
System.out.println("Choose from the following options:");
System.out.println("1 - Brows books inventory (price low or high) 2 - Brows DVDs inventory (price low or high) 3 - Add a book to the cart 4 - Add a DVD to the cart 5 - View cart 6 - Checkout 7 - Cancel Order 9 - Exit store");
}
//Array sort and display method
public static void displayArrays(String[] itemArray,double[] priceArray,String itemType) {
double tempPrice;
String tempItem;
for (int i = 1; i < priceArray.length; i++) {
for (int j = i; j > 0; j--) {
if (priceArray[j] <priceArray [j - 1]) {
tempPrice = priceArray[j];
tempItem = itemArray[j];
priceArray[j] = priceArray[j - 1];
itemArray[j]=itemArray[j-1];
priceArray[j - 1] = tempPrice;
itemArray[j-1]=tempItem;
}
}
}
System.out.println("Inventory Number "+itemType+" Prices --------------------------------------------");
for (int i = 0; i < itemArray.length; i++) {
System.out.println((i+1)+String.format("%30s",itemArray[i])+" $"+priceArray[i]);
}
}
//Inventory number return method
public static int getInventoryNumber() {
int inp;
System.out.println("Enter the inventory number: ");
inp=sc.nextInt();
if(inp==-1) {
return -1;
}
else {
return inp;
}
}
//Overload array display method
public static void displayArrays(ArrayList<String> itemCart,ArrayList<Double> itemPrice) {
System.out.println("Items Prices ----------------------------------------");
for (int i = 0; i < itemCart.size(); i++) {
System.out.println(String.format(itemCart.get(i),"%30s")+" $"+itemPrice.get(i));
}
}
//Main method
public static void main(String[] args) {
int val = 0;
//Inventory arrays
String books[]= {"Intro to Java","Intro to C++","Python","Perl","C#"};
double booksPrices[]= {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};
//Lists for cart
ArrayList<String> itemCart=new ArrayList<String>();
ArrayList<Double> itemPrice=new ArrayList<Double>();
//Loop to menu input
int ch;
displayMenu();
System.out.print("Enter your choice: ");
ch=sc.nextInt();
//Choice wrong error message
while(ch!=1&&ch!=2&&ch!=3&&ch!=4&&ch!=5&&ch!=6&&ch!=7&& ch!=9) {
System.out.println("This option is not accepatable");
displayMenu();
System.out.print("Enter your choice: ");
ch=sc.nextInt();
}
//Otherwise different choices
while(ch!=9) {
switch(ch) {
//Display books details
case 1:
displayArrays(books,booksPrices,"Books");
break;
//Display dvds details
case 2:
displayArrays(dvds,dvdsPrices,"DVDs");
break;
//add books into cart
case 3:
val=getInventoryNumber();
while(val==-1) {
displayArrays(books,booksPrices,"Books");
val=getInventoryNumber();
}
while(val==0 ||val>5) {
System.out.println("Entered inventory number is wrong!!!");
val=getInventoryNumber();
}
itemCart.add(books[val-1]);
itemPrice.add(booksPrices[val-1]);
break;
//add DVDs into cart
case 4:
val=getInventoryNumber();
while(val==-1) {
displayArrays(dvds,dvdsPrices,"DVDs");
val=getInventoryNumber();
}
while(val==0 ||val>5) {
System.out.println("Entered inventory number is wrong!!!");
val=getInventoryNumber();
}
itemCart.add(dvds[val-1]);
itemPrice.add(dvdsPrices[val-1]);
break;
//Display your cart
case 5:
displayArrays(itemCart,itemPrice);
break;
case 6:
break;
//Cancel your purchase
case 7:
itemCart.clear();
itemPrice.clear();
break;
}
//Again ask for your permission
System.out.println("Do you want to continue(y/n): ");;
char cont=sc.next().charAt(0);
if(cont=='y'||cont=='Y') {
displayMenu();
System.out.print("Enter your choice: ");
ch=sc.nextInt();
}
else {
break;
}
}
//Exit from application
System.out.println("Thank you for your interest Bye!!!!");
System.exit(0);
}
}
-------------------------------------------------------------
Output
**Welcome to the Mustangs Books and DVDs Store**
Choose from the following options:
1 - Brows books inventory (price low or high)
2 - Brows DVDs inventory (price low or high)
3 - Add a book to the cart
4 - Add a DVD to the cart
5 - View cart
6 - Checkout
7 - Cancel Order
9 - Exit store
Enter your choice: 1
Inventory Number Books Prices
--------------------------------------------
1 Perl $25.0
2 Intro to Java $45.99
3 C# $49.99
4 Intro to C++ $89.34
5 Python $100.0
Do you want to continue(y/n):
y
**Welcome to the Mustangs Books and DVDs Store**
Choose from the following options:
1 - Brows books inventory (price low or high)
2 - Brows DVDs inventory (price low or high)
3 - Add a book to the cart
4 - Add a DVD to the cart
5 - View cart
6 - Checkout
7 - Cancel Order
9 - Exit store
Enter your choice: 2
Inventory Number DVDs Prices
--------------------------------------------
1 Dumbo $17.99
2 Snow White $19.99
3 Bambi $21.99
4 Cinderella $24.99
5 Frozen $24.99
Do you want to continue(y/n):
y
**Welcome to the Mustangs Books and DVDs Store**
Choose from the following options:
1 - Brows books inventory (price low or high)
2 - Brows DVDs inventory (price low or high)
3 - Add a book to the cart
4 - Add a DVD to the cart
5 - View cart
6 - Checkout
7 - Cancel Order
9 - Exit store
Enter your choice: 3
Enter the inventory number:
2
Do you want to continue(y/n):
y
**Welcome to the Mustangs Books and DVDs Store**
Choose from the following options:
1 - Brows books inventory (price low or high)
2 - Brows DVDs inventory (price low or high)
3 - Add a book to the cart
4 - Add a DVD to the cart
5 - View cart
6 - Checkout
7 - Cancel Order
9 - Exit store
Enter your choice: 4
Enter the inventory number:
3
Do you want to continue(y/n):
y
**Welcome to the Mustangs Books and DVDs Store**
Choose from the following options:
1 - Brows books inventory (price low or high)
2 - Brows DVDs inventory (price low or high)
3 - Add a book to the cart
4 - Add a DVD to the cart
5 - View cart
6 - Checkout
7 - Cancel Order
9 - Exit store
Enter your choice: 5
Items Prices
----------------------------------------
Intro to Java $45.99
Bambi $21.99
Do you want to continue(y/n):
n
Thank you for your interest
Bye!!!!
---------------------------------------------
Note
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.