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

In this exercise you will implement a shopping cart using the ArrayList class. T

ID: 3805542 • Letter: I

Question

In this exercise you will implement a shopping cart using the ArrayList class. The file Item.java contains the definition of a class named Item that models an item one would purchase (this class was used in an earlier lab). An item has a name, price, and quantity (the quantity purchased). The file Shop.java is an incomplete program that models shopping.

1. Complete Shop.java as follows:

a. Declare and instantiate a variable cart to be an empty ArrayList.

b. Fill in the statements in the loop to add an item to the cart and to print the cart contents (using the default toString in the ArrayList class). Comments in the code indicate where these statements go.

c. Compile your program and run it.

2. You should have observed two problems with using the default printing for the cart object: the output doesn’t look very good and the total price of the goods in the cart is not computed or printed.

Modify the program to correct these problems by replacing the print statement with a loop that does the following:

a. gets each item from the cart and prints the item

b. computes the total price of the items in the cart (you need to use the getPrice and getQuantity methods of the Item class). The total price should be printed after the loop.

3. Compile and run your program.

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

// Shop.java

//

// Uses the Item class to create items and add them to a shopping

// cart stored in an ArrayList.

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

import java.util.ArrayList;

import java.util.Scanner;

public class Shop {

public static void main (String[] args)

{

Item item;

String itemName;

double itemPrice;

int quantity;

Scanner scan = new Scanner(System.in);

String keepShopping = "y";

do

{

System.out.print ("Enter the name of the item: ");

itemName = scan.nextLine();

System.out.print ("Enter the unit price: ");

itemPrice = scan.nextDouble();

System.out.print ("Enter the quantity: ");

quantity = scan.nextInt();

// *** create a new item and add it to the cart 78

// *** print the contents of the cart object using println

System.out.print ("Continue shopping (y/n)? ");

keepShopping = scan.nextLine();

}

while (keepShopping.equals("y"));

}

}

Explanation / Answer

//Output has been formatted

Item.java

public class Item {

   private String name;

   private Double price;

   private Integer quantity;

  

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public Double getPrice() {

       return price;

   }

   public void setPrice(Double price) {

       this.price = price;

   }

   public Integer getQuantity() {

       return quantity;

   }

   public void setQuantity(Integer quantity) {

       this.quantity = quantity;

   }

  

   public Item()       //Default Constructor

   {

      

   }

  

   public Item(String name, Double price, Integer quantity) {       //Constructor to initialize object with values passed as input

       super();

       this.name = name;

       this.price = price;

       this.quantity = quantity;

   }

  

  

}

Shop.java

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Shop {

   public static List<Item> cart = new ArrayList<Item>();

   public static void main (String[] args)

  

{

       Item item;

       String itemName;

       double itemPrice;

       double total=0;

       int quantity;

       Scanner scan = new Scanner(System.in);

       String keepShopping = "y";

       do

       {

           System.out.println();

           System.out.print ("Enter the name of the item: ");

           itemName = scan.nextLine();

           System.out.print ("Enter the unit price: ");

           itemPrice = scan.nextDouble();scan.nextLine();

           System.out.print ("Enter the quantity: ");

           quantity = scan.nextInt();scan.nextLine();

           // *** create a new item and add it to the cart 78

           item = new Item(itemName,itemPrice,quantity);

           Shop.cart.add(item);

           // *** print the contents of the cart object using println

           System.out.println();

           System.out.println("Your shopping cart");

           System.out.println("------------------");

           for(Item cartItem: cart)           //Iterating the cart to get items

           {

               System.out.println("Name: "+cartItem.getName());

               System.out.println("Price: "+cartItem.getPrice());

               System.out.println("Quantity: "+cartItem.getQuantity());

               System.out.println();

               total += getTotal(cartItem.getPrice(),cartItem.getQuantity());

           }

           System.out.print ("Continue shopping (y/n)? ");

           keepShopping = scan.nextLine();

       }

       while (keepShopping.equals("y"));

       System.out.println("__________________");

       System.out.println("Total: "+total);       //Get Total using price and quantity

       scan.close();

   }

   private static Double getTotal(Double price, Integer quantity) {

       return price*quantity;

   }

}

Sample Output

Enter the name of the item: books

Enter the unit price: 100

Enter the quantity: 5

Your shopping cart

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

Name: books

Price: 100.0

Quantity: 5

Continue shopping (y/n)? y

Enter the name of the item: watches

Enter the unit price: 120

Enter the quantity: 6

Your shopping cart

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

Name: books

Price: 100.0

Quantity: 5

Name: watches

Price: 120.0

Quantity: 6

Continue shopping (y/n)? n

__________________

Total: 1720.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote