Lab 6k: A Shopping Cart Using the ArrayList Class
Lab 6k: A Shopping Cart Using the ArrayList Class
In this exercise you will implement a shopping cart using the ArrayList class. The file Item6k.java contains the definition of a class named Item6kthat models an item one would purchase (this class was used in an earlier lab). An Item6khas a name, price, and quantity (the quantity purchased). The file RunShop6k.java is an incomplete program that models shopping.
- Fill in RunShop6k Class
- Create a cart -Declare and instantiate a variable cart to be an empty ArrayList.
- Add item to cart -Fill in the statements in the loop to add an item to the cart
Comments in the code indicate where these statements go.
- Print out contents of the cart – After adding the cart:
- Create a loop – Create a loop that gets each item from the cart.
- Print each item - Print each item.
- Add up the total price– Add up the total price of each item [getPrice()] times the quantity of that item [getQuantity()]that is in the cart.
- Print out cart totals – Once the loop is complete, useSystem.out.println()to output the total price of all the items in the cart.
RunShop6kclass
// ***************************************************************// RunShop6k.java [Lab 6k]
// Student Name: <name>
//
// 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.List;
import java.text.NumberFormat;
import java.util.Scanner;
public class RunShop6k
{
public static void main( String[] args )
{
Item6k item;
String itemName;
double itemPrice;
int quantity;
Scanner scan = new Scanner( System.in );
String keepShopping = "y";
// ------
// TODO #1a: Create an ArrayList cart
// ------
// Loop and allow user to add new shopping cart items
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();
scan.nextLine();
// ------
// TODO #1b: Create an new Item6k and add it to
// the cart
System.out.print( "Continue shopping (y/n)? " );
keepShopping = scan.nextLine();
}
while ( keepShopping.equalsIgnoreCase( "y" ) );
// ------
// TODO #2a: Loop through the cart
// TODO #2ai: Print the item
// TODO #2aii: Add up the total amount of all the items in the cart
System.out.println();
System.out.println( "Final Shopping Cart totals" );
// ------
// TODO #2b: Output the total $ amount in cart
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
}
}
Item6k Class
// ***************************************************************// Item6k.java [Lab 6k]
// Student Name: <name>
//
// Represents an item in a shopping cart.
// ***************************************************************
import java.text.NumberFormat;
publicclass Item6k
{
private String name;
privatedouble price;
privateint quantity;
// ------
// Create a new item with the given attributes.
// ------
publicItem6k( String itemName, double itemPrice, int numPurchased )
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
// ------
// Returns the name of the item
// ------
public String getName()
{
return name;
}
// ------
// Returns the unit price of the item
// ------
publicdoublegetPrice()
{
return price;
}
// ------
// Returns the quantity of the item
// ------
publicintgetQuantity()
{
return quantity;
}
// ------
// Return a string with the information about the item
// ------
@Override
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String strTab = name.length() > 8 ? "\t" : "\t\t";
return name + strTab + fmt.format( price ) + "\t\t" + quantity + "\t\t" + fmt.format( price * quantity );
}
}
Sample output
Enter the name of the item: Plush DogEnter the unit price: 5.50
Enter the quantity: 3
Continue shopping (y/n)? y
Enter the name of the item: Kitty Litter
Enter the unit price: 6.00
Enter the quantity: 2
Continue shopping (y/n)? y
Enter the name of the item: Soy Sauce
Enter the unit price: 3.75
Enter the quantity: 3
Continue shopping (y/n)? n
Final Shopping Cart totals
Plush Dog$5.503$16.50
Kitty Litter$6.002$12.00
Soy Sauce$3.753$11.25
Total $ Amount in Cart: $39.75
Page 1 of 3 / ArrayList Class