Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A Shopping Cart In this exercise you will complete a class that implements a sho

ID: 3861747 • Letter: A

Question

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 would purchase. An item has a name, price, and quantity (the quantity purchased). The file ShoppingCart.java implements the shopping cart as an array of Item objects.

1.    Complete the ShoppingCart class by doing the following:

a.     Declare an instance variable cart to be an array of Items and instantiate cart in the constructor to be an array holding capacity Items.

b.    Fill in the code for the increaseSize method. Your code should be similar to that in Listing 7.8 of the text but instead of doubling the size just increase it by 3 elements.

c.     Fill in the code for the addToCart method. This method should add the item to the cart and update the totalPrice instance variable (note this variable takes into account the quantity).

d.    Compile your class.

Write a program that simulates shopping. The program should have a loop that continues 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 the items in the cart

THISS CODE BELOW WAS GIVEN

// ***************************************************************

//   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;

    // -------------------------------------------------------

    // Create a new item with the given attributes.

    // -------------------------------------------------------

    public Item (String itemName, double itemPrice, int numPurchased)

    {

               name = itemName;

               price = itemPrice;

               quantity = numPurchased;

    }

    // -------------------------------------------------------

    //   Return a string with the information about the item

    // -------------------------------------------------------

    public String toString ()

    {

               NumberFormat fmt = NumberFormat.getCurrencyInstance();

               return (name + " " + fmt.format(price) + " " + quantity + " "

                              + fmt.format(price*quantity));

    }

    // -------------------------------------------------

    //   Returns the unit price of the item

    // -------------------------------------------------

    public double getPrice()

    {

               return price;

    }

    // -------------------------------------------------

    //   Returns the name of the item

    // -------------------------------------------------

    public String getName()

    {

               return name;

    }

    // -------------------------------------------------

    //   Returns the quantity of the item

    // -------------------------------------------------

    public int getQuantity()

    {

               return quantity;

    }

}

// **********************************************************************

//   ShoppingCart.java

//

//   Represents a shopping cart as an array of items

// **********************************************************************

import java.text.NumberFormat;

public class ShoppingCart

{

    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;

    }

    // -------------------------------------------------------

    // Adds an item to the shopping cart.

    // -------------------------------------------------------

    public void addToCart(String itemName, double price, int quantity)

    {

    }

    // -------------------------------------------------------

    // Returns the contents of the cart together with

    // summary information.

    // -------------------------------------------------------

    public String toString()

    {

               NumberFormat fmt = NumberFormat.getCurrencyInstance();

               String contents = " Shopping Cart ";

               contents += " Item Unit Price Quantity Total ";

               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()

    {

    }

}

Explanation / Answer

//=================================== Item.java ==================================
public class Item {
   private String name;
   private double price;
   private int purchQty;
  
   public String getName() {
       return name;
   }
   public Item(String n,double p,int q) {
       name = n;
       price = p;
       purchQty =q;
   }
   public void setName(String name) {
       this.name = name;
   }
   public double getPrice() {
       return price;
   }
   public void setPrice(double price) {
       this.price = price;
   }
   public int getPurchQty() {
       return purchQty;
   }
   public void setPurchQty(int purchQty) {
       this.purchQty = purchQty;
   }
   public String toString(){
       String str = ""+name+"   "+purchQty+"   "+price+" ";
       return str;
   }
}

//================================= ShopingCart.java ===================================

public class ShopingCart {
   Item[] items;
   private int numOfItem;
   private int cap;
   private double total;
  
   public ShopingCart(int cap){
       items = new Item[cap];
       this.cap = cap;
       setTotal(0.0d);
       numOfItem =0;
   }
  
   public void increaseSize(){
       Item[] temp = items;
       items = new Item[cap+3];
       for(int i=0;i<temp.length;i++){
           items[i] = temp[i];
       }
       cap += 3;
   }
   public void addToCart(Item item){
       if(numOfItem>=cap)
           increaseSize();
       items[numOfItem] = item;
       numOfItem++;
       setTotal(getTotal() + item.getPrice()*item.getPurchQty());
   }

   public double getTotal() {
       return total;
   }

   public void setTotal(double total) {
       this.total = total;
   }
   public String toString(){
       String str = " =============== CART ================ ";
       for(int i =0;i<numOfItem;i++){
           str += "#"+(i+1)+" "+items[i].toString()+"";
          
       }
       str += " ===================================== ";
       return str;
   }
}

//=================================== ShoppingCartApp.java ==========================

import java.util.Scanner;

public class ShoppingCartApp {

   public static void main(String[] args) {
       ShopingCart cart = new ShopingCart(10);
       Scanner sc = new Scanner(System.in);
       boolean quit = false;
       while(!quit){
           System.out.println("[1] Add Item to Cart");
           System.out.println("[2] Show Cart");
           System.out.println("[3] Check out");
           System.out.println("[4] Exit");
           System.out.println("Enter Your Choice :");
           int ch = sc.nextInt();
           switch(ch){
           case 1:
               System.out.println("Enter Item Name :");
               String name = sc.next();
               System.out.println("Enter Item Price :");
               double price = sc.nextDouble();
               System.out.println("Enter Item Quantiry :");
               int qty = sc.nextInt();
               cart.addToCart(new Item(name, price, qty));
               break;
           case 2:
               System.out.println(cart);
               break;
           case 3:
               System.out.println(cart);
               System.out.println("Please Pay :"+cart.getTotal()+" ");
               break;
           case 4:
               quit =true;
               break;
           }
       }
   }

}

//===================== OUTPUT =============================

[1] Add Item to Cart
[2] Show Cart
[3] Check out
[4] Exit
Enter Your Choice :
1
Enter Item Name :
BEAR
Enter Item Price :
100
Enter Item Quantiry :
4
[1] Add Item to Cart
[2] Show Cart
[3] Check out
[4] Exit
Enter Your Choice :
1
Enter Item Name :
SODA
Enter Item Price :
50
Enter Item Quantiry :
2
[1] Add Item to Cart
[2] Show Cart
[3] Check out
[4] Exit
Enter Your Choice :
2

=============== CART ================
#1 BEAR   4   100.0
#2 SODA   2   50.0

=====================================

[1] Add Item to Cart
[2] Show Cart
[3] Check out
[4] Exit
Enter Your Choice :
3

=============== CART ================
#1 BEAR   4   100.0
#2 SODA   2   50.0

=====================================

Please Pay :500.0


[1] Add Item to Cart
[2] Show Cart
[3] Check out
[4] Exit
Enter Your Choice :
4