home / study / engineering / computer science / questions and answers / a shoppi
ID: 3830646 • Letter: H
Question
home / study / engineering / computer science / questions and answers / a shopping cart using the arraylist class in this ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: A Shopping Cart Using the ArrayList Class In this ... Bookmark A Shopping Cart Using the ArrayList Class 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 Chapter 5: Conditionals and Loops // *** 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
package io;
/**
* The java proram that prompts user to enter
* name, price and quantity and conintues to
* prompt until user enters n to exit.
* The print the total cost of items
* in the item list to console.
* */
//Shop.java
import java.util.ArrayList;
import java.util.Scanner;
public class Shop
{
public static void main (String[] args)
{
//declare Item variable
Item item;
String itemName;
double itemPrice;
int quantity;
//set total cost=0
double totalCost=0;
//create an arraylist of type Item class
ArrayList<Item>itemList=new ArrayList<Item>();
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();
//read new line character and ignore it
scan.nextLine();
//create an instance of item with name ,price and quantity
item=new Item(itemName, itemPrice, quantity);
//add item object to list
itemList.add(item);
System.out.printf("%-15s%-15s%-15s ","Name","Price","Quantity");
//for each loop to print itemobjets to console
for (Item itemObject : itemList)
{
//add cost to total cost
totalCost+=(itemObject.getPrice()*itemObject.getQuantity());
System.out.println(itemObject);
}
System.out.print ("Continue shopping (y/n)? ");
keepShopping = scan.nextLine();
}while (keepShopping.equals("y"));
//print total cost to console
System.out.printf("Total cost of items in list:% 5.2f",totalCost);
}
}
--------------------------------------------------------------------------------------------------------
package io;
//Item.java
public class Item
{
//private data members
private String name;
private double price;
private int quantity;
//Constructor to set name, price and quantity
public Item (String name, double price, int quantity)
{
this.name = name;
this.price = price;
this.quantity = quantity;
}
public double getPrice()
{
return price;
}
public String getName()
{
return name;
}
public int getQuantity()
{
return quantity;
}
public String toString ()
{
return String.format("%-15s%-15.2f%-15d",name,price,quantity);
}
} //end of Item class
--------------------------------------------------------------------------------------------------------
Sample output:
Enter the name of the item: tv
Enter the unit price: 100
Enter the quantity: 5
Name Price Quantity
tv 100.00 5
Continue shopping (y/n)? y
Enter the name of the item: mixer
Enter the unit price: 50
Enter the quantity: 10
Name Price Quantity
tv 100.00 5
mixer 50.00 10
Continue shopping (y/n)? n
Total cost of items in list: 1500.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.