// Program Name                  ShoppingCart.java
// Course:                       CSE 1302J
// Student Name:                 Bradley Shedd
// Assignment Number:            Lab 2
// Due Date:                     09/1/2010
// Purpose:                      This program will work with a customer shopping at a store              
//
//
//  
// *******************************************************************// **********************************************************************
//   ShoppingCart.java
//
//   Represents a shopping cart as an array of items
// **********************************************************************

import java.text.NumberFormat;

public class ShoppingCart
{
   
private int itemCount;      // total number of items in the cart
    private double totalPrice;  // total price of items in the cart
    private int capacity;       // current cart capacity
    private Item[]cart;
   
// -----------------------------------------------------------
    //  Creates an empty shopping cart with a capacity of 5 items.
    // -----------------------------------------------------------
    public ShoppingCart()
    {
   capacity = 5;
   itemCount = 0;
   totalPrice = 0.0;
   cart =
new Item[capacity];
    }

   
// -------------------------------------------------------
    //  Adds an item to the shopping cart.
    // -------------------------------------------------------
    public void addToCart(String itemName, double price, int quantity)
    {
       
if(itemCount == cart.length)
          increaseSize();
         
        cart[itemCount] =
new Item (itemName, price, quantity);
        totalPrice += price*quantity;
        itemCount++;
    }
   
// -------------------------------------------------------
    //  Returns the total price of contents in the cart
    //
    // -------------------------------------------------------
      public double getTotalPrice()
      {
     
return this.totalPrice;
      }
   
// -------------------------------------------------------
    //  Returns the contents of the cart together with
    //  summary information.
    // -------------------------------------------------------
    public String toString()
    {
   NumberFormat fmt = NumberFormat.getCurrencyInstance();

   String contents =
"\nShopping Cart\n";
   contents +=
"\nItem\t\tUnit Price\tQuantity\tTotal\n";

  
for (int i = 0; i < itemCount; i++)
       contents += cart[i].toString() +
"\n";

   contents +=
"\nTotal Price: " + fmt.format(totalPrice);
   contents +=
"\n";

  
return contents;
    }
  

   
// ---------------------------------------------------------
    //  Increases the capacity of the shopping cart by 3
    // ---------------------------------------------------------
    private void increaseSize()
    {
      Item[] mycart =
new Item[cart.length + 3];
     
     
for( int item = 0; item < cart.length; item++)
         mycart[item] = cart[item];
        
      cart = mycart;
    }
}

Homepage