Answered step by step
Verified Expert Solution
Link Copied!

Question

...
1 Approved Answer

Start with this java files. ShoppingCart.java, Item.java, ShoppingCartTester.java You will model ShoppingCart as java Class. ShoppingCart class will store Items in the cart. Item will

Start with this java files. ShoppingCart.java, Item.java, ShoppingCartTester.java

You will model ShoppingCart as java Class.

ShoppingCart class will store Items in the cart. Item will be another java class. Item is identified by Id attribute. If the Item with the same Id is added multiple times in the cart, change the quantity and DO NOT add multiple Item objects with same Id.

The program should prompt various actions on ShoppingCart

addItemToCart Two overloads

updateItemQuantity

removeItemFromCart

calculateItemBasedDiscount

getTotalCost

Based on the selected action ask for further input. For example, if user selects to addItemToCart, prompt user to enter

itemId

itemName

itemPrice

itemQuantity

Implement all methods in ShoppingCart.java

Write test in ShoppingCartTester.java. Use scanner to implement userInputSimulator() method. See sample output for sample run.

Before you start working on user input, implement the tester methods as provided in the starter code. Once you have everything working enable the user input and comment out the tester code.

Gracefully exit the program

Sample Output

WELCOME TO SHOPPING CART APPLICATION.

PLEASE ENTER YOUR NAME:

Joe Peters

Welcome Joe Peters. Your Shopping Cart is created.

-----------------------------------------------------------------------------

Select one of the following options:

1. Add Item in ShoppingCart

2. Update Quantity for Item in ShoppingCart

3. Remove Item from Shopping Cart

4. Calculate Item Based Discount

5. Get total cost

6. Exit

1

You have selected to Add Item in Shopping Cart

Please Enter Item Id:

123

Please Enter Item Price:

20.50

Please Enter Item Quantity:

20

Item "Id: 123, Cost: $20.50" is added in ShoppingCart with Quantity 20

Select one of the following options:

1. Add Item in ShoppingCart

2. Update Quantity for Item in ShoppingCart

3. Remove Item from Shopping Cart

4. Calculate Item Based Discount

5. Get total cost

6. Exit

2

You have selected to update Quantity for Item in ShoppingCart

Please Enter Item Id for which you want to change Quantity:

123

Please Enter new Quantity:

25

Quantity for Item "Id: 123, Cost: $20.50" is changed to 25

Select one of the following options:

1. Add Item in ShoppingCart

2. Update Quantity for Item in ShoppingCart

3. Remove Item from Shopping Cart

4. Calculate Item Based Discount

5. Get total cost

6. Exit

6

Are you sure you want to Exit(y/n)?

y

Good Bye Have a Nice Day!

Make sure you create helper methods instead of putting entire testing code in Main method for getting options and calling appropriate function once you receive the options.

Total Possible Points: 100

Program runs as submitted - 5 points

Constructor for ShoppingCart and Item is implemented 5 points

Program accepts all valid option (1-6) only and handle errors properly - 15 points

Program adds Item to shopping cart (along with check for Id to avoid duplicate items in the cart)- 10 points

Program updates quantity of Item in ShoppingCart - 10 points

Program removes Item from cart - 10 points

Program calculates the discount based on total quantity ACROSS ALL items in the cart - 10 points

Program calculates total cost applying discount - 10 points

Program exits without problems with proper message - 5 points

Code is well documented using Javadocs - 10 points

Program handles errors properly - 10 points

Shows error for user input (validate double, string, char, etc)

Shows error when Item not found for removing an item

Show error for: - user enters negative values for Item Qty and Item Price

----------------------------------------

/** Order class maintains the Items that are part of the order. */

//add package staement here import java.util.ArrayList;

public class ShoppingCart { public static final double LOW_DISCOUNT = 0.10; public static final double MEDIUM_DISCOUNT = 0.25; public static final double HIGH_DISCOUNT = 0.50; ArrayList itemArray = new ArrayList(); private String customerName; public String getCustomerName() { return customerName; }

public void setCustomerName(String customerName) { this.customerName = customerName; }

public ShoppingCart(String customerName){ //implement here } public void addItemToCart(Item item, int quanity) { //If item already exists then update the quantity of that item by calling updateItemQuantity //If a new item is added, add the item to itemArray and fetch the details of the item added by calling toString() in Item; //print the fetched string here //donot display the details here if the item is updated, that should be done inside updateItemQuantity() } public void addItemToCart(int itemId, double itemPrice, int itemQuantity) { //Create Object of Item //Call addItemToCart(Item item, int quanity) }

public void updateItemQuantity(int itemID, int newQuantity) { //implement code to change the quantity of an item

String item_updated = "Quantity for Item Id: " + String.valueOf(itemID) + ", Cost: $"; //modify item_updated to display data in the following format- 'Quantity for Item Id: 123, Cost: $20.50 is changed to 25' //print item_updated } public void removeItemFromCart(int itemID) { //implement here } public double calculateItemBasedDiscount() { double discount = 0; //If total number of quantity ACROSS ALL Items is less than 11 , apply LOW_DISCOUNT (10% discount) //If total number of quantity ACROSS ALL Items is more than 10 but less than 26, apply MEDIUM_DISCOUNT (25% discount) //If total number of quantity ACROSS ALL Items is more than 26, apply HIGH_DISCOUNT (50% discount) return discount; }

/** * * @return */ public double getTotalCost() { double totalFinalCost = 0; //Compute the cost by adding cost of each item. Make sure to multiply quantity and price per unit for cost of each item. //Find applicable discount using calculateItemBasedDiscount and apply the discount to find the final cost

return totalFinalCost; } }

-------------------------------------

//add package statement here

public class Item {

private int id; private double price;

public Item(int id, double price){ this.id = id; this.price = price; }

public int getId() { return id; }

public void setId(int id) { this.id = id; }

public double getPrice() { return price; }

public void setPrice(double price) { this.price = price; } public String toString(){ String item_desc = "Item Id: " + String.valueOf(id) + ", Cost: $"; //modify item_desc to contain item description in the following format- Item Id: 123, Cost: $20.50 added in ShoppingCart with Quantity 20 return item_desc; } }

------------------------------

//add package statement here public class ShoppingCartTester {

public static void main(String[] args) { //Add code here to display main menu along with all the options //Keep on looping until the user enters 6 to exit. DO NOT EXIT otherwise //If any number other than 1-6 is entered, display an error message "Enter input in the range 1-6"; and again ask for input

//Demo code /* ShoppingCart shoppingCart= new ShoppingCart();

System.out.println("Add Item Id: 1"); System.out.println("Add Item Cost per unit: 1.5");

int id = 1; double costPerUnit = 1.5; int quantity = 10;

Item item = new Item(id, costPerUnit); shoppingCart.addItemToCart(item, quantity); userInputSimulator(); */ } public static void userInputSimulator() {

//Use Scanner to get input for item quantity and item cost ShoppingCart shoppingCart = new ShoppingCart();

shoppingCart.addItemToCart(item, quanity);(); //System.out.print("Discount Code should be 0.5 : " + discountLevel);

double finalCost = order.getTotalCost(); //System.out.print("FInal cost should be : " + finalCost); //Additional testing //Add sensible function calls to test functionality of ALL methods in ShoppingCart.java //Display the final cost }

} /* Note: Perform error handling. Eg: donot accept values outside the range(1-6) donot accept negative values for QUANTITY or PRICE of an ITEM */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started