You are require to complete a Shopping Cart class that implements a shopping cart as an array of Item objects (Refer to
Posted: Fri May 20, 2022 11:49 am
You are require to complete a Shopping Cart class that implements a shopping cart as an array of Item objects (Refer to File: Shopping Cart.java). Another file named as Item.java that contains the definition of a class named Item that models an item one would purchase. An item has a name, price, and quantity (the quantity purchased). The Item.java file is shown in Figure 1. Given skeleton of Shopping Cart class. Complete the class by doing the following (i) - (iii): i. Declare an instance variable cart to be an array of Items and instantiate cart in the constructor to be an array holding capacity Items. (Note: capacity is an instance variable, initialized to 5). ii. Fill in the code for the addToCart method. This method should add the item to the cart and tests the size of the cart. If true, increase Size method will be called. iii. Fill in the code for the increaseSize method. Increases the capacity of the shopping cart by doubling it and update the cart. When compiling and run the class, you are checking for syntax errors in your Shopping Cart class. (Note: No tester or driver class has been written yet.) RRRR V/Item.java *Represents an item in a shopping cart //***** import java.text.NumberFormat; public class Item { private String name; private double price; private int quantity; public Item (String itemName, double itemPrice, int numPurchased) { name = itemName; price = itemPrice; quantity = numPurchased; } public String toString 0 1
public class Item private String name; private double price; private int quantity; public Item (String itemName, double itemPrice, int numPurchased) name = itemName; price = itemPrice; quantity numPurchased; } public String toString 0 { NumberFormat fit NumberFormat.getCurrencyInstance(); return (name + "t" + fmt.format(price) + "" + quantity + "t" + fmt.format(price*quantity)); } public double getPrice() return price; } public String getName() { return name; } public int getQuantity { return quantity; } } Figure 1 //****** //Shopping Cart.java
Figure 1 //********* //shopping Cart.java /Represents a shopping cart as an array of items //****** import java.text.NumberFormat; public class Shopping Cart { private int itemCount; // total number of items in the cart private double totalPrice; // total price of items in the cart private int capacity; // current cart capacity // (ia) Declare actual array of items to store things in the cart. private item [] cart; private item[]; private item cart; T (1b) Provide values to the instance variable of capacity.
private int itemCount; // total number of items in the cart private double totalPrice; // total price of items in the cart private int capacity; // current cart capacity // (ia) Declare actual array of items to store things in the cart. public Shopping Cart() { // (ib) Provide values to the instance variable of capacity. capacity = itemCount = 0; totalPrice = 0.0; // (ic) Declare an instance variable cart to be an array of items and instantiate cart to be an array holding capacity items. } public void addToCart(String itemName, double price, int quantity)
// (ic) Declare an instance variable cart to be an array of Items and instantiate cart to be an array holding capacity items. public void addToCart(String itemName, double price, int quantity) // ii a) Add item's name, price and quantity to the cart. cart[itemCount++] = totalPrice += price*quantity; // (iib) Check if full, increase the size of the cart. if ( increaseSize(); public String toString() NumberFormat fmt = NumberFormat.getCurrencyInstance(); String contents = "\nShopping Cart\n"; contents += "\nItem \tPrice\tQty\tTotal\n"; for (int i = 0; i < itemCount; i++) contents += cart.toString() + "\n";
if ( increaseSize(); public String toString() { NumberFormat fmt = NumberFormat.getCurrencylnstance(); String contents = "\nShopping Cart\n"; contents += "\nItem\tPrice|tQty\tTotal\n"; for (int i = 0; i < itemCount; i++) contents += cart.toString() + "\n"; contents += "\nTotal Price:" + fmt.format(totalPrice); contents += "\n"; + return contents; private void increaseSize() { Item[] templtem = new Item(capacity); // (iii a) Provide an operation to increases the capacity of the shopping cart by doubling it.
{ - Item[] templtem = new Item(capacityl; // (ii a) Provide an operation to increases the capacity of the shopping cart by doubling it. for (int i=0; i< itemCount; i++) { templtem = cart; } cart = new Item(capacity]; for (int i=0; i< itemCount; i++) { // (iii b) Update the cart. } } public double getTotalPrice() { return totalPrice; } }
cart[itemCount++] = totalPrice += price* quantity; // (iib) Check if full, increase the size of the cart. if ind itemCount == capacity } itemCount => capacity itemCount > capacity public String toString() { NumberFormat fmt = NumberFormat.getCurrencylnstance(); String contents = "\nShopping Cart\n";