Lab 6k A Shopping Cart Using The ArrayList Class-0
Lab 6k A Shopping Cart Using The ArrayList Class-0
RunShop6k class
// ***************************************************************
// 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;
// ----------------------------------------------
// 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();
// ----------------------------------------------
// TODO #1b: Create an new Item6k and add it to
// the cart
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;
// -------------------------------------------------------
// Create a new item with the given attributes.
// -------------------------------------------------------
public Item6k( 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
// -------------------------------------------------
public double getPrice()
{
return price;
}
// -------------------------------------------------
// Returns the quantity of the item
// -------------------------------------------------
public int getQuantity()
{
return quantity;
}
// -------------------------------------------------------
// Return a string with the information about the item
// -------------------------------------------------------
@Override
public String toString()
{
Sample output
Enter the name of the item: Plush Dog
Enter 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