Lab 6e: A Shopping Cart

Lab 6e: A Shopping Cart

In this exercise you will complete a class that implements a shopping cart as an array of items. The file Item6e.java contains the definition of a class named Item6ethat models an item one would purchase. An item has a name, price, and quantity (thequantity purchased). The file ShoppingCart6e.java implements the shopping cart as an array of Item objects.

  1. Fill out ShoppingCart6e -Complete the ShoppingCart6e class by doing the following:
  2. Create Array - Declare an instance variable cart to be an array of Item6e objects
  3. Instantiate Array - Instantiate cart in the constructor to be an array holding 5Item6e objects.
  4. Increase size of array by 3 - Fill in the code for the increaseSize() method. Your code should be similar to that in Lab 6d of the text but instead of doubling the size just increase it by 3 elements.
  5. Add item to cart - Fill in the code for the addToCart() method. This method should:
  6. If necessary, increase size - If the cart is maxed out, call increaseSize()to extend the array size.
  7. Add Item - Add the item to the cart.
  8. Update total price - Update the totalPrice instance variable (NOTE: this variable takes into account both the price ANDthe quantity of the item).
  9. Update itemCount - Update the itemCount. NOTE: this variable DOES NOT take into account the quantity of the item, it only counts the instances of Item6eadded to the array).
  1. Compile your class.
  1. Fill out GoingShopping6eRunner class -Write a program that simulates shopping.
  2. The program should have a loop that continues as long as the user wants to shop. Each time through the loop read in the name, price, and quantity of the item the user wants to add to the cart.
  3. When the user is finished adding items to the cart, the cart contents should be printed.

Item6e class

// ***************************************************************
// Item6e.java [Lab 6e]
// Student Name: <name>
//
// Represents an item in a shopping cart.
// ***************************************************************
importjava.text.NumberFormat;
publicclass Item6e
{
private String name;
privatedouble price;
privateint quantity;
// ------
// Create a new item with the given attributes.
// ------
publicItem6e( String itemName, doubleitemPrice, intnumPurchased )
{
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()
{
NumberFormatfmt = 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 );
}
}

ShoppingCart6e class

// **********************************************************************
// ShoppingCart6e.java [Lab 6e]
// Student Name: <name>
//
// Represents a shopping cart as an array of items
// **********************************************************************
importjava.text.NumberFormat;
publicclass ShoppingCart6e
{
privateintitemCount; // total number of items in the cart
privatedoubletotalPrice; // total price of items in the cart
// ------
// TODO #1a: Create an instance variable cart
// ------
// Creates an empty shopping cart with an initial capacity of
// 5 items.
// ------
publicShoppingCart6e()
{
itemCount = 0;
totalPrice = 0.0;
// ------
// TODO #1b: Initialize cart Array
}
// ------
// TODO #1d: Adds an item to the shopping cart.
// ------
publicvoidaddToCart( String itemName, double price, int quantity )
{
// ------
// TODO 1di#: Remember to increase the size if the cart is
// already maxed out (use itemCount and cart.length)
// ------
// TODO 1dii#: Add item
// TODO 1diii#: Update totalPrice
// TODO 1div#: Update itemCount
}
// ------
// Returns the contents of the cart together with
// summary information.
// ------
@Override
public String toString()
{
NumberFormatfmt = NumberFormat.getCurrencyInstance();
String contents = "\nShopping Cart\n";
contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n";
for ( inti = 0; iitemCount; i++ )
{
contents += cart[i].toString() + "\n";
}
contents += "\nTotal Price: " + fmt.format( totalPrice );
contents += "\n";
return contents;
}
// ------
// TODO #1c: Increases the capacity of the shopping cart by 3
// ------
privatevoidincreaseSize()
{
}
}

GoingShopping6eclass

// **********************************************************************
// GoingShopping6e.java [Lab 6e]
// Student Name: <name>
//
// Execute a shopping trip by adding new items and costs. Once the items
// are
// **********************************************************************
publicclass GoingShopping6e
{
publicstaticvoidmain( String[] args )
{
ShoppingCart6e scCart = new ShoppingCart6e();
// ------
// TODO #2a: Loop and enter new items
// ------
// TODO #2b: Print out content of shopping cart
}
}

Sample Output #1

Enter (1) to keep shopping or (0) to quit
1
Enter the name of the item:
Stuffed Dog
Enter the price of the item:
10.98
Enter the quantity of the item:
3
Enter (1) to keep shopping or (0) to quit
1
Enter the name of the item:
Laptop
Enter the price of the item:
458.20
Enter the quantity of the item:
1
Enter (1) to keep shopping or (0) to quit
1
Enter the name of the item:
Basketball
Enter the price of the item:
15.99
Enter the quantity of the item:
4
Enter (1) to keep shopping or (0) to quit
1
Enter the name of the item:
Uno Deck
Enter the price of the item:
7.29
Enter the quantity of the item:
2
Enter (1) to keep shopping or (0) to quit
1
Enter the name of the item:
Shirt
Enter the price of the item:
7.89
Enter the quantity of the item:
4
Enter (1) to keep shopping or (0) to quit
1
Enter the name of the item:
Socks
Enter the price of the item:
2.98
Enter the quantity of the item:
3
Enter (1) to keep shopping or (0) to quit
0
Shopping Cart
ItemUnit PriceQuantityTotal
Stuffed Dog$10.983$32.94
Laptop$458.201$458.20
Basketball$15.994$63.96
Uno Deck$7.292$14.58
Shirt$7.894$31.56
Socks$2.983$8.94
Total Price: $503.33
Page 1 of 4 / Arrays of Objects