FRQ 3 - Arrays/ArrayLists
Cookie Order
public class CookieOrder
{
private String variety;
private int boxes;
/** Constructs a new CookieOrder object. */
public CookieOrder(String variety, int numBoxes){
this.variety = variety;
this.boxes = numBoxes;
}
/** @return the variety of cookie being ordered
*/
public String getVariety(){
return variety;
}
/** @return the number of boxes being ordered
*/
public int getNumBoxes(){
return boxes;
}
// There may be instance variables, constructors, and methods that are not shown.
}
public class MasterOrder
{
/** The list of all cookie orders */
private List<CookieOrder> orders;
/** Constructs a new MasterOrder object. */
public MasterOrder(){
orders = new ArrayList<CookieOrder>();
}
/** Adds theOrder to the master order.
* @param theOrder the cookie order to add to the master order
*/
public void addOrder(CookieOrder theOrder)
{ orders.add(theOrder); }
/** @return the sum of the number of boxes of all of the cookie orders
*/
public int getTotalBoxes(){
int count = 0;
for(CookieOrder cookie : orders){ //for each cookie in the list orders, add the num boxes to count
count += cookie.getNumBoxes();
}
return count;
}
/** Removes all cookie orders from the master order that have the same variety of
* cookie as cookieVar and returns the total number of boxes that were removed.
* @param cookieVar the variety of cookies to remove from the master order
* @return the total number of boxes of cookieVar in the cookie orders removed
*/
public int removeVariety(String cookieVar)
{
int removed = 0;
for(CookieOrder cookie: orders){ //for all cookies in orders list
if(cookie.getVariety().equals(cookieVar)){ //if the variety of the cookie equals the name of the cookie
orders.remove(cookie); //remove the cookie and then add the number of boxes removed
removed += cookie.getNumBoxes();
}
}
return removed;
}
// There may be instance variables, constructors, and methods that are not shown.
}
public class CookieTester{
public static void main(String[] args)
{
MasterOrder orderList = new MasterOrder();
//makes a new order list
CookieOrder order1 = new CookieOrder("chocolate chip", 2);
CookieOrder order2 = new CookieOrder("shortbread", 3);
//makes two new cookie order objects
orderList.addOrder(order1);
orderList.addOrder(order2);
//adds objects to list
System.out.println("Order 1:");
System.out.println();
System.out.println("Type: " + order1.getVariety() + "; Number of Boxes: "+ order1.getNumBoxes());
System.out.println("Type: " + order2.getVariety() + "; Number of Boxes: "+ order2.getNumBoxes());
//gets variety and number of boxes of each added object
System.out.println("Total number of boxes in orderList: " + orderList.getTotalBoxes() + " boxes");
//uses get total boxes method
System.out.println("------------------------------");
System.out.println("Remove order chocolate chip:");
System.out.println();
System.out.println("Number of boxes removed: " + orderList.removeVariety("chocolate chip"));
System.out.println("Number of boxes left: " + orderList.getTotalBoxes());
//removes boxes from chocolate chip and returns new total
}
}
CookieTester.main(null);