3 A Shopping Cart In this exercise you will complete a class
3. A Shopping Cart
In this exercise you will complete a class that implements a shopping cart as an array of items.The file Item.java contains the definition of a class named Item that models an item one wouldpurchase. An item has a name, price, and quantity (the quantity purchased). Item.java is provided.You do not need to modify it. The file ShoppingCart.java implements the shopping cart as anarray of Item objects.
1. Complete the ShoppingCart.java class by doing the following:
a. Fill in the code for the increaseSize method and just increase the old size by 3 elements.Remember to copy the old elements back to new array.
b. Fill in the code for the addToCart method. This method should add the item to the cart andupdate the totalPrice instance variable (note this variable takes into account the quantity).
2. Write a program shopping.java that simulates shopping. The program should have a loop thatcontinues as long as the user wants to shop. Each time through the loop read in the name, price,and quantity of the item the user wants to add to the cart. After adding an item to the cart, the cart contents should be printed. After the loop print a “Please pay ...” message with the total price of he items in the cart.
Solution
// **********************************************************************
// ShoppingCart.java
//
// Represents a shopping cart as an array of items
// **********************************************************************
import java.text.NumberFormat;
public class ShoppingCart
{
private Item[] cart; // an array of Items
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
// -----------------------------------------------------------
// Creates an empty shopping cart with a capacity of 5 items.
// -----------------------------------------------------------
public ShoppingCart()
{
capacity = 5;
itemCount = 0;
totalPrice = 0.0;
cart = new Item[capacity];
}
// -------------------------------------------------------
// Adds an item to the shopping cart.
// -------------------------------------------------------
public void addToCart(String itemName, double price, int quantity)
{
// Add your code here. If the itemCount == cart.length, call increaseSize function.
if (itemCount == cart.length)
{
increaseSize();
// Create a new Item object by calling new Item constructor and append it to the cart array.
cart[itemCount]= new Item (itemName, price,quantity);
//Increase itemCount and update the totalPrice
itemCount++;
totalPrice += (totalPrice*quantity);
}
}
// -------------------------------------------------------
// Returns the contents of the cart together with
// summary information.
// -------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String contents = \"\ Shopping Cart\ \";
contents += \"\ Item\\t\\tUnit Price\\tQuantity\\tTotal\ \";
for (int i = 0; i < itemCount; i++)
contents += cart[i].toString() + \"\ \";
contents += \"\ Total Price: \" + fmt.format(totalPrice);
contents += \"\ \";
return contents;
}
// ---------------------------------------------------------
// Increases the capacity of the shopping cart by 3
// ---------------------------------------------------------
private void increaseSize()
{
// Add your code here. Allocate a new array with cart.length + 3 size.
// In the for loop, copy the cart array element to this new array.
// Let the cart object points to the new array.
Item[] cart1 = new Item[cart.length + 3];
for (int j = 0; j < cart.length; j++){
cart1[j] = cart[j];
cart = cart1;
}
}
// ---------------------------------------------------------
// Returns the total price of the items in the cart.
// ---------------------------------------------------------
public double getTotal()
{
return totalPrice;
}
}
//Shopping.java
import java.util.Scanner;
public class Shopping {
public static void main(String[] args) {
String itemName ;
Long itemPrice;
int itemQuantity;
String response;
long order = 0;
ShoppingCart sc = new ShoppingCart();
Scanner input = new Scanner(System.in);
do {
//Prompts buyer to enter item details
System.out.println(\"Enter the name of item purchased: \");
itemName = input.nextLine();
System.out.println(\"\ Enter the price of item purchased: \");
itemPrice = input.nextLong();
System.out.println(\"\ Enter the quantity of item purchased: \");
itemQuantity = input.nextInt();
//Reads user\'s response to continue shopping or not
System.out.println(\"\ Enter Y to continue shopping\");
response =input.next();
//Adding items purchased by user to shopping cart
sc.addToCart(itemName, itemPrice, itemQuantity);
System.out.println(sc.toString());
//Printing cart contents
System.out.println(\"\ --------------------------------\");
System.out.println(itemName+ \" \" +itemQuantity+ \" \" +itemPrice);
//Calculating total order value
order += itemPrice * itemQuantity;
} while((response.equals(\"y\") || response.equals(\"Y\")) && (response != null));
//Printing total order value to be paid
System.out.println(\"\ Please pay \" +order );
}
}


