import java.text.NumberFormat;
/**********************************************************************
 * MarketBasket.java
 *
 * Represents a market basket.
 ********************************************************************/
public class MarketBasket
{
	/** total number of items actually in the basket */
   private int itemCount; 
   /** total cost of all of the items in the basket */
   private double totalCost; 
   /** Items in the basket */
   private Item[] basket; 

   /***************************************************************
    * Creates an empty shopping basket with a capacity of 
    * of 15 items.  
    ***************************************************************/
   public MarketBasket()
   {
   	this.basket = new Item[15];
		this.itemCount = 0;
		this.totalCost = 0;
   }
   /***************************************************************
    * Adds an item to the basket, updating all pertinent totals.   
    ***************************************************************/
   public void addItem(Item anItem)
   {
		// error check in case we have reached past the end	
		if (itemCount < basket.length)
		{
			basket[itemCount] = anItem;
			itemCount++;
			totalCost = totalCost + anItem.getItemCost();
		}
		else 
			System.out.println("Error adding item " + anItem.toString() +
				". Basket full.");
	}

   /***************************************************************
    * Returns a String representation of this shopping basket
    * @return String representation of the basket, with all items
    *          listed.  
    ***************************************************************/
   public String toString()
   {
      NumberFormat fmt; // number format object
      String contents;  // return String

      fmt = NumberFormat.getCurrencyInstance();
      contents = "\nMarket Basket\n-------------";

      contents += String.format("\n%-21s%-9s%10s%10s\n", "Item", "Each", "Quantity", "Total");

      for (int i = 0; i < this.itemCount; i++)
         contents += this.basket[i].toString() + "\n";
   
      contents += "\nTotal Cost: " + fmt.format(totalCost);
      contents += "\n";
		
      return contents;
   }
}
