2010 Cookie Order

<< 2011 RouteCipher | APQuestionsTrailIndex | 2010APLine >>

question

CookieOrder.java

public class CookieOrder 
{
  private String variety;
  private int numBoxes;
  /** Constructs a new CookieOrder object. */
  public CookieOrder(String variety, int numBoxes)
  {
      this.variety=variety;
      this.numBoxes=numBoxes;
  }

 /** @return the variety of cookie being ordered */
 public String getVariety()    
  { 
      return variety;
  }
  /** @return the number of boxes being ordered */
  public int getNumBoxes()
  { 
      return numBoxes;
    }
}

MasterOrder.java

import java.util.ArrayList;
import java.util.List;
public class MasterOrder
{
    private List<CookieOrder> orders;

    public MasterOrder()
    {
        orders= new ArrayList<CookieOrder>();
    }
    public void addOrder ( CookieOrder theOrder)
    {
        orders.add(theOrder);
    }
    public int getTotalBoxes(){
        // part (a)
        return 0;
    }
    public int removeVariety(String cookieVar){
        //part (b)
        return 0;
    }
    public String toString(){
        String result="";
        for (CookieOrder c:(ArrayList<CookieOrder>)(orders))
           result+=c.getVariety()+" "+c.getNumBoxes()+"\n";
        return result;
    }
}

CookieTester.java

public class CookieTester
{

    public static void main(String[] args)
    {
       MasterOrder goodies = new MasterOrder(); 
       goodies.addOrder(new CookieOrder("Chocolate Chip", 1)); 
       goodies.addOrder(new CookieOrder("Shortbread", 5)); 
       goodies.addOrder(new CookieOrder("Macaroon", 2)); 
       goodies.addOrder(new CookieOrder("Chocolate Chip", 3));
       System.out.println("Should have a total of 11 Boxes: "+goodies.getTotalBoxes() );
       System.out.println("Removing Choc Chip-- should be 4: "+goodies.removeVariety("Chocolate Chip"));
       System.out.println("Master Order:\n"+goodies);
       System.out.println("Removing Brownie-- should be 0: "+goodies.removeVariety("Brownie"));
        System.out.println("Master Order:\n"+goodies);
    }
}