Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Instructions

Start with this java files.Item.java, ShoppingCart.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:

XXXXXXX

Welcome XXXXXX. Your Shopping Cart is created.

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

Select one of the following options:

Add Item in ShoppingCart

Update Quantity for Item in ShoppingCart

Remove Item from Shopping Cart

Calculate Item Based Discount

Get total cost

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:

Add Item in ShoppingCart

Update Quantity for Item in ShoppingCart

Remove Item from Shopping Cart

Calculate Item Based Discount

Get total cost

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:

Add Item in ShoppingCart

Update Quantity for Item in ShoppingCart

Remove Item from Shopping Cart

Calculate Item Based Discount

Get total cost

Exit

6

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

y

Good Bye Have a Nice Day!

Item.java

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;

}

}

ShoppingCart.java

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;

}

}

ShoppingCartTester.java

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:

Meticulously read the instructions explaining how marks will be awarded for this assignment.

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 to Expert-Tailored 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

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions