Question: language: Java In this project, the shopping cart should be implemented using list, instead of array in assignment 2. In addition, the menu should look

language: Java

In this project, the shopping cart should be implemented using list, instead of array in assignment 2. In addition, the menu should look like follow:

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 Delete a book from cart

6 Delete a DVD from cart

7 - View cart

8 - Checkout

9 Done Shopping

Continue this looping until option 9 is selected. When option 9 is entered, return to the main role selection menu .

Assignment 2 (this is what i did for assignment two)

import java.util.Scanner;

import java.util.Arrays; // used to empty out the array

class OnlineStore

{

public static void main(String[] args)

{

String[] books = {"Intro to Java", "Intro to C++", "Python", "Perl", "C#"}; // string array1- contains all the books

double[] bookPrices = {45.99, 89.34, 100.00, 25.00, 49.99}; // double array1- to store book price corresponding to each item

String[] dvds = {"Snow White", "Cinderella", "Dumbo", "Bambi", "Frozen"}; // string array2-contains all the dvds

double[] dvdsPrices = {19.99, 24.99, 17.99, 21.99, 24.99};// double array2- to store dvd prices corresponding to each item

String[] TotalChosenItems = new String[5]; // max total item size

double[] TotalPrice = new double[5]; // max total price size

int TotalNumItems = 0; //initialized it to zero-number of items might increase

int invNum;

Scanner input = new Scanner(System.in);

int Choice; // created to allow the user to pick their option

for(;true;) // create an infinite loop until it flows through a break

{

displayMenu();// displayMenu method- will display the menu

System.out.print("Enter your choice: ");// asks the user

Choice = input.nextInt();

if (Choice == 1) // beginning of an if else statement- depending on what the user pick it

{

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

System.out.printf("%-20s%-20s%-20s ","Inventory Number", "Books", "Prices"); // printf for formating the table

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

displayArrays(books, bookPrices, "Books"); // displayArrays- display book inventory in order low to high in terms of price

}

else if (Choice == 2) // if user choice = 2

{

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

System.out.printf("%-20s%-20s%-20s ","Inverntory Number", "Movies", "Prices"); // formating

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

displayArrays(dvds, dvdsPrices, "DVDs"); // display dvd inventory in order low to high in terms of price

}

else if (Choice == 3) //if user choice = 3

{

invNum = getInventoryNumber(); // this method will read and return the number entered by the user

if ( invNum != -1) // if the user enters does not enter -1 then this will

{

TotalChosenItems[TotalNumItems] = books[invNum-1]; // takes the inventory number and removes one from the index

TotalPrice[TotalNumItems] = bookPrices[invNum-1];

TotalNumItems++;

}

else // if the user enters -1 it will cause the loop to immediately go to the next iteration of the loop

{

continue; // in this case it will redisplay the menu and not select an inventory

}

}

else if (Choice == 4) //if user choice = 4

{

invNum = getInventoryNumber();

if ( invNum != -1) // same as choice 3 but for dvds

{

TotalChosenItems[TotalNumItems] = dvds[invNum-1];

TotalPrice[TotalNumItems] = dvdsPrices[invNum-1]; // takes the inventory number and removes one from the index

TotalNumItems++;

}

else

{

continue;

}

}

else if (Choice == 5) //if user choice = 5

{

System.out.println("");

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

System.out.printf("%-20s%-15s ","Items", "Prices"); // formating

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

displayArrays(TotalChosenItems, TotalPrice, TotalNumItems); // display everything the user selected

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

System.out.println( "\t\t** Go to checkout for Total + Tax (Choice 5) **"); // guide the user to checkout

}

else if (Choice == 6) //if user choice = 6

{

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

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

displayArrays(TotalChosenItems, TotalPrice, TotalNumItems);

System.out.printf(" Total + tax: \t%4.2f ",getTotal(TotalPrice, TotalNumItems));

clearArrays(TotalChosenItems, TotalPrice, TotalNumItems); //if the checkout is cleared

TotalNumItems = 0;

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

}

else if (Choice == 7) //if user choice = 7

{

clearArrays(TotalChosenItems, TotalPrice, TotalNumItems); //clearArrays methods- purpose is to remove all the items that have been added in the list

TotalNumItems = 0;

System.out.println("**All of the items which you have added have been removed!**");

}

else if (Choice == 8) //if user choice = 8

{

System.out.println("Thank you for shopping!");

break; // this will terminate the infinite loop

}

else

{

System.out.println("Please enter an acceptable option."); // if the user does not select a give choice than this will be executed

}

}

}

public static void displayMenu() // displays the menu(users options)

{

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

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");

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

}

public static void displayArrays(String[] itemsArray, double[] pricesArray, String itemType)

{

// this will sort the inventory in the order of low to high in terms of price for both DVDs and Books

int Plength = pricesArray.length; // the length of an array depending on number of items in the cart

int[] NumberInventory = new int[Plength];

for(int num = 0; num < Plength; num++)

NumberInventory[num] = num; //Inventory num = 1 ex.NumberInventory[0] = 0 ...

int num1 = 0;

while( num1 < (Plength) ) //condition: num1 must be less then the length of price array

{

int numless = num1; // a smaller size

for(int num2 = num1; num2 < Plength; num2++)

while(pricesArray[NumberInventory[numless]] > pricesArray[NumberInventory[num2]]) // numless has less value than num2 then it will do the following

numless = num2;

int value = NumberInventory[numless]; // this will sort the inventory from high to low in terms of price

NumberInventory[numless] = NumberInventory[num1]; // this will shift low to the top

NumberInventory[num1] = value;

num1++; // increment until reaches num1 < (Plength)

}

int value = 0;

while(value < Plength) //// value must be less than the length if the price array

{

int InvNum = NumberInventory[value++]; //the value will increment until value < Plength

//next line will display the inventory in order with proper formatting

System.out.printf("%-20s%-20s\t%3.2f ", (InvNum + 1), itemsArray[InvNum], pricesArray[InvNum]);

}

}

public static void displayArrays(String[] TotalItems, double[] TotalPrice, int TotalTotalNumItems) //overloading

{

double TotalPlusTax = getTotal(TotalPrice, TotalTotalNumItems); //calls getTotal

for(int i = 0; i < TotalTotalNumItems; i++) // for loop to calculate the total

System.out.printf("%-16s%3.2f ", TotalItems[i], TotalPrice[i]);// format and display the array

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

System.out.printf("Total: \t\t%4.2f", TotalPlusTax/1.0825);// this will display the total with proper format

//I divided TotalPlusTax (is a method-returns total+ tax) by the tax so it will display the total without tax

}

public static int getInventoryNumber()

{

System.out.println("Enter the inventory number you wish to purchase (Enter -1 to redisplay the menu): "); // ask the user to enter a inventory num

Scanner input = new Scanner(System.in);

int InventoryNumber = input.nextInt();

return InventoryNumber; //return the inventory number the user entered

}

public static double getTotal(double[] TotalPrice, int TotalItems) // this method will calculate the total with tax

{

double total = 0;

for(double num: TotalPrice) //enhanced for loop is used to add price items and multiply it with the tax and return the total price

total = total + num; // add all the prices in the cart

return total * 1.0825; // returns the total price with tax

}

public static void clearArrays(String[] TotalItems, double[] TotalPrices, int TotalTotalNumItems)

{

TotalItems = new String[TotalItems.length];

TotalItems = new String[0];

Arrays.fill( TotalItems, "" ); // this will empty the string array

Arrays.fill( TotalPrices, 0 ); // this will empty the double array

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!