In the two problems that follow, identify what pattern should be used, and finish the code so that it uses the pattern. Please turn this in by email (either an attachment or in the body of a message). Due Wednesday.
An ice cream parlor makes sundaes consisting in ice cream, syrup, and sprinkles. The customer can specify how many scoops, how many layers of syrup, and how many handfuls of sprinkles. The customer orders by saying something like "scoop, scoop, syrup, scoop, sprinkles, scoop." This means 4 scoops, 1 layer of syrup, 1 handful of sprinkles.
However, the three employees each have their own style of sundae. Gretchen first puts all the scoops, then all the syrup on top, and then all the sprinkles on top of that (in this case, scoop scoop scoop scoop syrup sprinkles). Isaac puts all the syrup on the bottom, puts the scoops on top of that, and then the sprinkles on top (syrup scoop scoop scoop scoop sprinkles). Sadie puts all the indicated layers of syrup and handfuls of sprinkles on each scoop (scoop syrup sprinkles scoop syrup sprinkles scoop syrup sprinkles scoop syrup sprinkles).
Complete the following code so that the OrderBot
can model what Isaac, Gretchen, and Sadie are doing.
Add classes and fill-in makeSundae
.
It is up to you to decide how OrderBot
knows which
employee to model, but do not subclass OrderBot
.
import java.util.*; interface SundaeIngredient {} class Scoop implements SundaeIngredient {} class Syrup implements SundaeIngredient {} class Sprinkles implements SundaeIngredient {} class Sundae { // holds ingredients from bottom to top private ArrayListingredients; public Sundae() { ingredients = new ArrayList (); } public void addIngredient(SundaeIngredient ingredient) { ingredients.add(ingredient); } } class OrderBot { public Sundae makeSundae(ArrayList order) { for (Iterator it = order.iterator(); it.hasNext(); ) { String next = it.next(); if (next.equals("scoop")) { // add scoop } else if (next.equals("syrup")) { // add syrup } else if (next.equals("sprinkles")) { // add sprinkles } } return null; } }
A restaurant offers three kinds of sandwiches: a breakfast sandwich with fried egg and bacon on an English muffin, a lunch sandwich with a burger patty and cheese on a Kaiser bun, and a dessert sandwich with ice cream and chocolate syrup between two chocolate chip cookies.
The interface for ordering each of these is the same: the customer indicates how many layers of the main ingredient and how many layers of the topping he or she wants.
Finish the following code to model the sandwiches.
Add appropriate classes and finish makeSandwich()
.
It is up to you to decide how OrderBot
knows
which kind of sandwich to make, but do not subclass OrderBot
.
import java.util.HashSet; interface Wrapper {} class EnglishMuffin implements Wrapper {} class KaiserBun implements Wrapper {} class ChocolateChipCookie implements Wrapper {} interface MainIngredient {} class FriedEgg implements MainIngredient {} class BurgerPatty implements MainIngredient {} class IceCreamSlab implements MainIngredient {} interface Topping {} class Bacon implements Topping {} class Cheese implements Topping {} class ChocolateSyrup implements Topping {} class Sandwich { private Wrapper w; private HashSetm; private HashSet t; public Sandwich(Wrapper w) { this.w = w; m = new HashSet (); t = new HashSet (); } public void addMain(MainIngredient mm) { m.add(mm); } public void addTopping(Topping tt) { t.add(tt); } } class OrderBot { public Sandwich makeSandwich(int numMain, int numTop) { return null; } }