Shopping Cart – Part 8
In this workbook, you will add interactivity to the application.
Task 1
Remove all the code inside main()
.
Task 2
Create one object of the Store
class and one object of the Cart
class. Make both objects class
variables.
Task 3
Inside Main
, define a function named loadItems
.
/**
* Name: loadItems
* @param fileName (String)
* @throws FileNotFoundException
*
* Inside the function:
* 1. loads items from <fileName>.
* - while loop runs through every line in <fileName>
* - scan.nextLine() picks up the entire line.
* - splits each String using the ";" separator.
* - splits both fields in each String using the "=" separator.
* - Parse each price into a Double.
* 2. adds all items to the store object's items field.
*/
The function receives the fileName
from which you will load items.
/**
* Name: loadItems
* @param fileName (String) <-------
* @throws FileNotFoundException
*
* Inside the function:
* 1. loads items from <fileName>.
* - while loop runs through every line in <fileName>
* - scan.nextLine() picks up the entire line.
* - splits each String using the ";" separator.
* - splits both fields in each String using the "=" separator.
* - Parse each price into a Double.
* 2. adds all items to the store object's items field.
*/
The function throws a FileNotFoundException
.
/**
* Name: loadItems
* @param fileName (String)
* @throws FileNotFoundException <--------
*
* Inside the function:
* 1. loads items from <fileName>.
* - while loop runs through every line in <fileName>
* - scan.nextLine() picks up the entire line.
* - splits each String using the ";" separator.
* - splits both fields in each String using the "=" separator.
* - Parse each price into a Double.
* 2. adds all items to the store object's items field.
*/
Follow steps 1 and 2 to complete the function. Step 1 assumes you're familiar with split()
and Double.parse()
. If not, please revisit the Movie Store workbook.
/**
* Name: loadItems
* @param fileName (String)
* @throws FileNotFoundException
*
* Inside the function: <-----------
* 1. loads items from <fileName>.
* - while loop runs through every line in <fileName>
* - scan.nextLine() picks up the entire line.
* - splits each String using the ";" separator.
* - splits both fields in each String using the "=" separator.
* - Parses each price into a Double.
* 2. adds all items to the store object's items field.
*/
From main()
, try
to call loadItems
. After,
-
System.out.println("\n\t******************************JAVA GROCERS******************************\n");
-
System.out.println(store);
In case of a failure, catch
the FileNotFoundException
.
Expected Output:
Once you get this output, remove the two println()
statements.
Task 4 – manageItems
Inside Main
, add a method called manageItems
and call it inside the try
block.
/**
* Name: manageItems <------
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. Creates an infinite loop:
* - The user can choose to a) add or b) remove c) checkout.
* - case a: asks for the aisle and item number. Then, adds item to cart.
* - case b: asks for the name. Then, removes item from cart.
* - case c: prints the receipt and closes Scanner.
* - Prints the updated shopping cart.
*/
Create a Scanner
object.
/**
* Name: manageItems
* Inside the function:
* - 1. Starts a new instance of Scanner; <------
* - 2. Creates an infinite loop:
* - The user can choose to a) add or b) remove c) checkout.
* - case a: asks for the aisle and item number. Then, adds item to cart.
* - case b: asks for the name. Then, removes item from cart.
* - case c: prints the receipt and closes Scanner.
* - Prints the updated shopping cart.
*/
Make a while
loop that runs infinitely. Print the following at the beginning of each loop:
println("\n\t******************************JAVA GROCERS******************************\n");
println(store);
/**
* Name: manageItems
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. Creates an infinite loop: <--------
* - The user can choose to a) add or b) remove c) checkout.
* - case a: asks for the aisle and item number. Then, adds item to cart.
* - case b: asks for the name. Then, removes item from cart.
* - case c: prints the receipt and closes Scanner.
* - Prints the updated shopping cart.
*/
During each run, let the user choose between add to cart, remove from cart, or check-out.
println("Options: \n\ta) Add to cart\n\tb) Remove from cart \n\tc) Checkout");
/**
* Name: manageItems
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. Creates an infinite loop:
* - The user can choose to a) add or b) remove c) checkout. <-------------
* - case a: asks for the aisle and item number. Then, adds item to cart.
* - case b: asks for the name. Then, removes item from cart.
* - case c: prints the receipt and closes Scanner.
* - Prints the updated shopping cart.
*/
Case a:
-
Pick up the aisle (row) number.
-
System.out.print("\nChoose an aisle number between: 1 – 7: ");
-
Note: the row index goes from 0 to 6.
-
-
Pick up the item number.
-
System.out.print("Choose an item number between: 1 – 3: ");
-
Note: the index of each element goes from 0 to 2.
-
-
If the
item
is not already in the shopping cart:-
add it to the cart
-
System.out.println(item.getName() + " was added to your shopping cart.");
-
-
otherwise:
System.out.println(item.getName() + " is already in your shopping cart.");
-
Important: Since we're using
nextLine()
andnextInt()
in the same loop, add a throwawaynextLine()
after eachnextInt()
. The throwaways will get consumed anyway.
/**
* Name: manageItems
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. Creates an infinite loop:
* - The user can choose to a) add or b) remove c) checkout.
* - case a: asks for the aisle and item number. Then, adds item to cart. <--------
* - case b: asks for the name. Then, removes item from cart.
* - case c: prints the receipt and closes Scanner.
* - Prints the updated shopping cart.
*/
Case b:
-
Pick up the item's name:
System.out.print("Enter the item you'd like to remove: ");
-
Remove the item from the cart.
/**
* Name: manageItems
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. Creates an infinite loop:
* - The user can choose to a) add or b) remove c) checkout.
* - case a: asks for the aisle and item number. Then, adds item to cart.
* - case b: asks for the name. Then, removes item from cart. <----------
* - case c: prints the receipt and closes Scanner.
* - Prints the updated shopping cart.
*/
Case c: print the receipt and close Scanner()
.
/**
* Name: manageItems
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. Creates an infinite loop:
* - The user can choose to a) add or b) remove c) checkout.
* - case a: asks for the aisle and item number. Then, adds item to cart.
* - case b: asks for the name. Then, removes item from cart.
* - case c: prints the receipt and closes Scanner. <---------
* - Prints the updated shopping cart.
*/
-
At the end of each run, print the updated shopping cart:
System.out.println("\n\nSHOPPING CART\n\n" + cart);
-
Prompt the user to continue:
System.out.print("Enter anything to continue: ");
/**
* Name: manageItems
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. Creates an infinite loop:
* - The user can choose to a) add or b) remove c) checkout.
* - case a: asks for the aisle and item number. Then, adds item to cart.
* - case b: asks for the name. Then, removes item from cart.
* - case c: prints the receipt and closes Scanner.
* - Prints the updated shopping cart. <---------
*/
Final output: