import java.text.NumberFormat; import java.util.ArrayList; import javax.swing.JOptionPane; /** * * @author Bill Kraynek */ public class IceCreamShop0 { interface Item { public String getDescription(); public double getCost(); } class ItemWrapper implements Item { Item item; public ItemWrapper(Item item) { this.item = item; } @Override public String getDescription() { return item.getDescription(); } @Override public double getCost() { return item.getCost(); } } class Cone implements Item { @Override public String getDescription() { return "Cone with"; } @Override public double getCost() { return 1.00; } } class Dish implements Item { @Override public String getDescription() { return "Dish of"; } @Override public double getCost() { return 0.10; } } class Vanilla extends ItemWrapper { public Vanilla(Item item) { super(item); } public String getDescription() { return item.getDescription() + " Vanilla "; } @Override public double getCost() { return item.getCost() + 3.00; } } class Chocolate extends ItemWrapper { public Chocolate(Item item) { super(item); } @Override public String getDescription() { return item.getDescription() + " Chocolate "; } @Override public double getCost() { return item.getCost() + 3.25; } } class Strawberry extends ItemWrapper { public Strawberry(Item item) { super(item); } public String getDescription() { return item.getDescription() + " Strawberry "; } public double getCost() { return item.getCost() + 4.50; } } class ChocolateSyrup extends ItemWrapper { public ChocolateSyrup(Item item) { super(item); } public String getDescription() { return item.getDescription() + " with Chocolate Syrup"; } public double getCost() { return item.getCost() + 1.50; } } class WhippedCream extends ItemWrapper { public WhippedCream(Item item) { super(item); } public String getDescription() { return item.getDescription() + " with Whipped Cream"; } public double getCost() { return item.getCost() + 0.75; } } class Flavor extends ItemWrapper { String flavor; double cost; public Flavor(Item item, String flavor, double cost) { super(item); this.flavor = flavor; this.cost = cost; } public String getDescription() { return item.getDescription() + " " + flavor + " "; } public double getCost() { return item.getCost() + cost; } } class Topping extends ItemWrapper { String topping; double cost; public Topping(Item item, String topping, double cost) { super(item); this.topping = topping; this.cost = cost; } public String getDescription() { return item.getDescription() + " " + topping + " "; } public double getCost() { return item.getCost() + cost; } } public IceCreamShop0() { NumberFormat money = NumberFormat.getCurrencyInstance(); Item icItem = new Dish(); icItem = new Vanilla(icItem); icItem = new Chocolate(icItem); icItem = new WhippedCream(icItem); JOptionPane.showMessageDialog(null, icItem.getDescription() + " " + money.format(icItem.getCost())); icItem = ((WhippedCream)icItem).item; JOptionPane.showMessageDialog(null, icItem.getDescription() + " " + money.format(icItem.getCost())); icItem = new Dish(); icItem = new Flavor(icItem,"Vanilla",3.00); icItem = new Flavor(icItem, "Chocolate", 3.25); icItem = new Topping(icItem,"Whipped Cream",0.75); JOptionPane.showMessageDialog(null, icItem.getDescription() + " " + money.format(icItem.getCost())); icItem = ((Topping)icItem).item; JOptionPane.showMessageDialog(null, icItem.getDescription() + " " + money.format(icItem.getCost())); } /** * @param args the command line arguments */ public static void main(String[] args) { new IceCreamShop0(); } }