Tuesday, October 26, 2010

LabTest#2: Revised Solution -- Section 6 -- Class: Shopping Cart

public class ShoppingCart 
{
 public Item[] theItems;
 public int item_count;
 public int TotalPrice;
 
 public ShoppingCart()
 {
  theItems = new Item[1000];
  item_count = 0;
  TotalPrice = 0;
 }
 
 /* ****************************************************************************************** */
 
 // Setters and Getters
 
 public int getItemCount()
 {
  return item_count;
 }
 
 public void setItemCount(int ic)
 {
  item_count = ic;
 }
 
 public void setTotalPrice(int t)
 {
  this.TotalPrice = t;
 }
 
 public int getTotalPrice()
 {
  return TotalPrice;
 }
 
 public void setTheItems(Item[] t)
 {
  this.theItems = t;
 }
 
 public Item[] getTheItems()
 {
  return theItems;
 }
 
 /* ****************************************************************************************** */
 
 public void addItems(Item anItem)
 {
  theItems[item_count] = anItem;
  
  System.out.println("Item Added:" + anItem); 
  
  item_count++; 
 }
 
 /* ****************************************************************************************** */
 
 public boolean removeItem(Item e)
 {
  boolean returnValue = false;
  int matched_item;
  
  if((matched_item = this.contains(e)) >= 0)
  {
   for(int i = matched_item; i < item_count - 1; i++)
   {
    theItems[i] = theItems[i+1];
   }
   
   item_count--;
   
   this.setTotalPrice(this.getTotalPrice() - e.getPrice());
   returnValue = true;
  }
  
  if(returnValue == false)
  {
   System.out.println("Item not found");
  }
  else
  {
   System.out.println("Item Removed:" + e); 
  }
  return returnValue;
 }
 
 /* ****************************************************************************************** */
 
 public int contains(Item e)
 {
  int item_match = -1;
  
  for(int j = 0; j < this.getItemCount(); j++)
  {
   if(theItems[j].getName().equals(e.getName()) && theItems[j].getPrice() == e.getPrice())
   {
    item_match = j;
   }
  }
  return item_match;
 }
 
 /* ****************************************************************************************** */
 
 public int calculateTotalPrice()
 {
  this.setTotalPrice(0);
  
  for(int i = 0 ; i < this.getItemCount(); i++)
  {
   TotalPrice += theItems[i].getPrice();
  }
  
  return this.getTotalPrice();
 }
 
 /* ****************************************************************************************** */
 
 public void clear()
 {
  for(int j = 0; j < this.getItemCount(); j++)
  {
   theItems[j] = new Item("", 0);
  }
  
  // Create an empty array of Item type : Item[]
  Item[] freshEmptyItems = new Item[1000];
  
  // Set theItems to this freshEmptyItems
  this.setTheItems(freshEmptyItems);
  
  this.setItemCount(0);
  this.setTotalPrice(0);
  
  System.out.println("Cart Cleared");
 }
 
 /* ****************************************************************************************** */
 
 public String toString()
 {
  boolean encountered_before;
  int the_item_count;
  String str = "";
  
  for(int i = 0; i < this.getItemCount(); i++)
  {
   encountered_before = false;
   
   // Check to see if encountered before
   for(int j = 0; j < i; j++)
   {
    if(theItems[i].getName().equalsIgnoreCase(theItems[j].getName()))
    {
     encountered_before = true;
    }
   }
   
   // if not encountered before then calculate its count
   if(encountered_before == false)
   {
    // calculate the item_counts of each item 
    the_item_count = 1;
    for(int k = i+1; k < this.getItemCount(); k++)
    {
     if(theItems[i].getName().equalsIgnoreCase(theItems[k].getName()))
     {
      the_item_count++;
     }
    }
    
    // display
    //System.out.println("Quantity = " + the_item_count + " " + theItems[i].getName());
    //str += "Quantity = " + the_item_count + " " + theItems[i].getName() + "\n";
    str += String.format("Quantity = %d %s %n", the_item_count, theItems[i].getName());
   }
   
  }
  return str;
 }
 
 /* ****************************************************************************************** */

}


No comments: