0% found this document useful (0 votes)
332 views3 pages

Lab 6k A Shopping Cart Using The ArrayList Class-0

This document provides instructions for a lab exercise to implement a shopping cart using an ArrayList class. Students are asked to: 1) create an empty ArrayList to represent the cart, and add new Item6k objects to the cart; and 2) loop through the cart to print out each item and calculate the total price of all items. Sample output showing 3 added items and a final total is also provided.

Uploaded by

ANIL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
332 views3 pages

Lab 6k A Shopping Cart Using The ArrayList Class-0

This document provides instructions for a lab exercise to implement a shopping cart using an ArrayList class. Students are asked to: 1) create an empty ArrayList to represent the cart, and add new Item6k objects to the cart; and 2) loop through the cart to print out each item and calculate the total price of all items. Sample output showing 3 added items and a final total is also provided.

Uploaded by

ANIL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 Item6k that models an item one would purchase (this class was used in an earlier lab). An Item6k has a name, price,
and quantity (the quantity purchased). The file RunShop6k.java is an incomplete program that models shopping.

1. Fill in RunShop6k Class


a. Create a cart - Declare and instantiate a variable cart to be an empty ArrayList.
b. 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.

2. Print out contents of the cart – After adding the cart:


a. Create a loop – Create a loop that gets each item from the cart.
i. Print each item - Print each item.
ii. 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.
b. Print out cart totals – Once the loop is complete, use System.out.println() to output the total price of all
the items in the cart.

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;

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)? " );

Page 1 of 3 ArrayList Class


Lab 6k: A Shopping Cart Using the ArrayList Class

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;

public class Item6k


{
private String name;

private double price;

private int quantity;

// -------------------------------------------------------
// 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()
{

Page 2 of 3 ArrayList Class


Lab 6k: A Shopping Cart Using the ArrayList Class

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 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

Final Shopping Cart totals


Plush Dog $5.50 3 $16.50
Kitty Litter $6.00 2 $12.00
Soy Sauce $3.75 3 $11.25
Total $ Amount in Cart: $39.75

Page 3 of 3 ArrayList Class

You might also like