In this exercise you will implement a shopping cart using the ArrayList class. T
ID: 3683242 • 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
// *** 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
Shop.java:
import java.util.ArrayList;
import java.util.Scanner;
class Shop
{
public static void main(String[] args)
{
Item item;
ArrayList<Item> cart = new ArrayList();
String itemName;
double itemPrice;
int quantity;
double totalPrice=0;
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();
//Consumer the new line present in the buffer
scan.nextLine();
//Create new object of item and add to cart
cart.add(new Item(itemName,itemPrice,quantity));
System.out.println ("Now Cart contains : ");
for(int i=0;i<cart.size();i++)
{
//Output the cart details.
System.out.println("item name : "+cart.get(i).getName()+" Price : "
+cart.get(i).getPrice()+" Quantity : "+cart.get(i).getQuantity());
totalPrice=totalPrice+(cart.get(i).getPrice()*cart.get(i).getQuantity());
}
//Output the total amount upto now
System.out.println("Total Price : "+totalPrice);
System.out.print ("Continue shopping (y/n)? ");
keepShopping = scan.nextLine();
}while(keepShopping.equals("y"));
}
}
Item.java:
public class Item
{
private int quantity;
private String name;
private double price;
Item(String name,double price,int quantity)
{
this.name=name;
this.price=price;
this.quantity=quantity;
}
public double getPrice()
{
return price;
}
public int getQuantity()
{
return quantity;
}
public String getName()
{
return name;
}
}
Output:
Enter the name of the item: pen
Enter the unit price: 3
Enter the quantity: 2
Now Cart contains :
item name : pen Price : 3.0 Quantity : 2
Total Price : 6.0
Continue shopping (y/n)? y
Enter the name of the item: pencil
Enter the unit price: 1
Enter the quantity: 4
Now Cart contains :
item name : pen Price : 3.0 Quantity : 2
item name : pencil Price : 1.0 Quantity : 4
Total Price : 16.0
Continue shopping (y/n)? n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.