In: Computer Science
Demonstrate the following:
Instructions
Pages
//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;
}
}
/**
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<Item> itemArray = new ArrayList<Item>();
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 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
*/
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
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:
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:
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;
private int itemQuantity;
public Item(int id, double price, int qty) {
this.id = id;
       this.price = price;
       this.itemQuantity = qty;
}
   public int getItemQuantity() {
       return itemQuantity;
   }
   public void setItemQuantity(int itemQuantity)
{
       this.itemQuantity =
itemQuantity;
   }
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: $"
          
    + price + " added in ShoppingCart with Quantity
"
          
    + itemQuantity;
// 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
/**
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<Item> itemArray = new ArrayList<Item>();
private String customerName;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public ShoppingCart(String customerName) {
       // implement here
       setCustomerName(customerName);
}
   public void addItemToCart(Item item, int quanity)
{
       int flag = 0;
       // 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
       // do not display the details
here if the item is updated, that should
       // be done inside
updateItemQuantity()
       for (int i = 0; i <
itemArray.size(); i++) {
           if
(itemArray.get(i).getId() == item.getId()) {
          
    flag = 1;
          
    break;
           }
       }
       if (flag == 1) {
          
updateItemQuantity(item.getId(), quanity);
       } else {
          
addItemToCart(item.getId(), item.getPrice(),
item.getItemQuantity());
          
System.out.println(item);
       }
   }
public void addItemToCart(int itemId, double itemPrice, int itemQuantity) {
       // Create Object of Item
       Item i = new Item(itemId,
itemPrice, itemQuantity);
       // Call addItemToCart(Item item,
int quanity)
}
public void updateItemQuantity(int itemID, int newQuantity)
{
       int indx = 0;
       // implement code to change the
quantity of an item
       for (int i = 0; i <
itemArray.size(); i++) {
           if
(itemArray.get(i).getId() == itemID) {
          
    indx = i;
          
    break;
           }
       }
       String item_updated = "Quantity
for Item Id: "
          
    +
String.valueOf(itemArray.get(indx).getId())
+ ", Cost: $" + itemArray.get(indx).getPrice();
itemArray.get(indx).setItemQuantity(newQuantity);
// modify item_updated to display data in the following format-
item_updated += " is changed to " + itemArray.get(indx).getPrice();
       // print item_updated
      
System.out.println(item_updated);
}
   public void removeItemFromCart(int itemID) {
       int indx = 0;
       // implement code to change the
quantity of an item
       for (int i = 0; i <
itemArray.size(); i++) {
           if
(itemArray.get(i).getId() == itemID) {
          
    indx = i;
          
    break;
           }
       }
       itemArray.remove(indx);
}
public double calculateItemBasedDiscount()
{
       double discount = 0;
       int totQty = 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)
       for (int i = 0; i <
itemArray.size(); i++) {
           totQty +=
itemArray.get(i).getItemQuantity();
       }
       if (totQty < 11) {
           discount =
10;
       } else if (totQty > 10
&& totQty <= 26) {
           discount =
26;
       } else if (totQty > 26) {
           discount =
50;
       }
       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.
       for (int i = 0; i <
itemArray.size(); i++) {
           totalFinalCost
+= itemArray.get(i).getPrice()
          
        *
itemArray.get(i).getItemQuantity();
       }
       // Find applicable discount using
calculateItemBasedDiscount and apply
       // the discount to find the final
cost
       totalFinalCost = totalFinalCost +
(calculateItemBasedDiscount() / 100)
          
    * totalFinalCost;
return totalFinalCost;
}
}
___________________________
// ShoppingCartTester.java
import java.util.Scanner;
public class ShoppingCartTester {
public static void main(String[] args) {
       String customerName;
       /*
       * Creating an Scanner class object
which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new
Scanner(System.in);
       // Getting the input entered by
the user
       System.out.println("WELCOME TO
SHOPPING CART APPLICATION.");
       System.out.print("PLEASE ENTER YOUR
NAME:");
       customerName = sc.nextLine();
       ShoppingCart shopcart = new
ShoppingCart(customerName);
       System.out.println("Welcome " +
shopcart.getCustomerName()
          
    + ". Your Shopping Cart is created.");
       // 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
       char yesorno;
while(true)
{
   while (true) {
           
System.out.println("Select one of the following options:");
           
System.out.println("1 - Add Item in ShoppingCart");
           
System.out.println("2 - Update Quantity for Item in
ShoppingCart");
           
System.out.println("3 - Remove Item from Shopping Cart");
           
System.out.println("4 - Calculate Item Based Discount");
           
System.out.println("5 - Get total cost");
           
System.out.println("6 - Exit");
            int
choice = sc.nextInt();
            switch
(choice) {
            case 1:
{
           
    int id,qty;
           
    double price;
           
    System.out.println("You have selected to Add
Item in Shopping Cart");
           
    System.out.print("Please Enter Item Id:");
           
    id=sc.nextInt();
           
    System.out.print("Please Enter Item
Price:");
           
    price=sc.nextDouble();
           
    System.out.print("Please Enter Item
Quantity:");
           
    qty=sc.nextInt();
           
   
           
    shopcart.addItemToCart(id,price,qty);
           
    continue;
            }
            case 2:
{
           
    System.out.println("You have selected to update
Quantity for Item in ShoppingCart");
           
    System.out.print("Please Enter Item Id for which
you want to change Quantity:");
int id=sc.nextInt();
System.out.println("Please Enter new Quantity:");
int qty=sc.nextInt();
shopcart.updateItemQuantity(id,qty);
           
    continue;
            }
            case 3:
{
           
    System.out.println("You have selected to Remove
Item in ShoppingCart");
           
    System.out.print("Please Enter Item Id for which
you want to remove:");
int id=sc.nextInt();
shopcart.removeItemFromCart(id);
           
    continue;
            }
            case 4:
{
           
    System.out.println("You have selected to Display
ItemBasedDiscount in ShoppingCart");
           
    System.out.print("Discount
:%"+shopcart.calculateItemBasedDiscount());
  
           
    continue;
            }
            case 5:
{
           
   
        System.out.print("Total Cost
:$"+shopcart.getTotalCost());
  
           
    continue;
            }
            case 6:
{
           
    break;
            }
            default:
{
           
    System.out.println("** Invalid Choice
**");
           
    continue;
            }
            }
           
break;
        }
     
   System.out.print("Are you sure you want to
Exit(y/n)?");
   yesorno=sc.next(".").charAt(0);
   if(yesorno=='y' || yesorno=='Y')
   {
       break;
   }
}
System.out.println("Good Bye Have a Nice Day!");
      
   }
}
________________________________Thank You