public void prepare(Beverage b) { if (b.getBeverage() == null) System.out.println("Add Coffee\nAdd Water"); else { if (b.getDescription().equals("milk")) { System.out.println("Steaming milk"); prepare(b.getBeverage()); System.out.println("Mixing milk and coffee"); } else { prepare(b.getBeverage()); System.out.println("Adding " + b.getDescription()); } } }
public static void main(String[] args) { Beverage expresso = new Expresso(); System.out.println(expresso.getDescription()); System.out.println(expresso.cost()); Beverage milk = new Milk(expresso); System.out.println(milk.getDescription()); System.out.println(milk.cost()); Beverage mocha = new Mocha(milk); System.out.println(mocha.getDescription()); System.out.println(mocha.cost()); }
public static void main(String[] args) { Beverage beverage = new NongSuoCoffee(); System.out.println(beverage.getDescription() + " | " + beverage.cost()); Beverage beverage2 = new XingBingLe(); beverage2 = new MochaDecorator(beverage2); beverage2 = new MochaDecorator(beverage2); beverage2 = new MilkDecorator(beverage2); System.out.println(beverage2.getDescription() + " | " + beverage2.cost()); Beverage beverage3 = new XingBingLe(); beverage3 = new MilkDecorator(beverage3); System.out.println(beverage3.getDescription() + " | " + beverage3.cost()); }
public static void main(String[] args) { DarkRoast darkRoast = new DarkRoast(); System.out.println( "Beverage: \t" + darkRoast.getDescription() + ", Cost: \t" + darkRoast.getCost()); Milk milk = new Milk(darkRoast); System.out.println("Beverage: \t" + milk.getDescription() + ", Cost: \t" + milk.getCost()); System.out.println("\n##########################################\n"); Beverage beverage = new HouseBlend(); beverage = new Milk(beverage); beverage = new Milk(beverage); beverage = new Milk(beverage); beverage = new Milk(beverage); beverage = new Mocha(beverage); System.out.println( "Beverage: \t" + beverage.getDescription() + ", Cost: \t" + beverage.getCost()); }
public double cost() { double cost = beverage.cost(); int size = getSize(); if (size == Beverage.TALL) { cost += 1.00; } else if (size == Beverage.GRANDE) { cost += 2.00; } else { cost += 3.00; } return cost; }
public void onGetCostClick(View view) { double cost; String description = ""; Beverage beverage = null; RadioGroup rg = (RadioGroup) findViewById(R.id.beverageType); switch (rg.getCheckedRadioButtonId()) { case R.id.rbEspresso: beverage = new Espresso(); break; case R.id.rbHouseBlend: beverage = new HouseBlend(); } if (((Switch) (findViewById(R.id.swWhip))).isChecked()) { beverage = new Whip(beverage); } if (((Switch) (findViewById(R.id.swSoy))).isChecked()) { beverage = new Soy(beverage); } if (((Switch) (findViewById(R.id.swMocha))).isChecked()) { beverage = new Mocha(beverage); } if (beverage != null) { String order = "You ordered: " + beverage.getDescription() + ", which costs: " + Double.toString(beverage.cost()); Toast.makeText(this, order, Toast.LENGTH_SHORT).show(); } Intent intent = new Intent(this, MyService.class); sendBroadcast(intent); }
public static void main(String[] args) { // Espresso Beverage espresso = new Espresso(); System.out.println(espresso.getDescription() + " : $" + espresso.cost()); // DarkRoast + Mocka + Mocha + Whip Beverage darkRoast = new DarkRoast(); darkRoast = new Mocha(darkRoast); darkRoast = new Mocha(darkRoast); darkRoast = new Whip(darkRoast); System.out.println(darkRoast.getDescription() + " : $" + darkRoast.cost()); // HouseBlend + Soy + Mocha + Whip Beverage houseblend = new HouseBlend(); houseblend = new Soy(houseblend); houseblend = new Mocha(houseblend); houseblend = new Whip(houseblend); System.out.println(houseblend.getDescription() + " : $" + houseblend.cost()); }
public double cost() { double cost = beverage.cost(); if (getSize() == Size.TALL) { System.out.print("TALL "); cost += 0.10; } else if (getSize() == Size.GRANDE) { System.out.print("GRANDE "); cost += 0.15; } else if (getSize() == Size.VENTI) { System.out.print("VENTI "); cost += 0.20; } return cost; }
public double cost() { if (beverage.getSize() == 1) { return 0.2 + beverage.cost(); } else if (beverage.getSize() == 2) { return 0.3 + beverage.cost(); } else if (beverage.getSize() == 3) { return 0.4 + beverage.cost(); } else { return 0; } }
@Override public double cost() { return beverage.cost() + 0.10; }
// 此接口是继承自抽象组件Beverage的cos方法 @Override public double cost() { return 0.5 + beverage.cost(); }
// First we delegate the call to the object we're decorating, so that it can // compute the cost, and then add the cost of this condiment to the result public double cost() { return beverage.cost() + 1.00; }
public int getSize() { return beverage.getSize(); }
@Override public double cost() { return PRICE + mBenverage.cost(); }
public Size getSize() { return beverage.getSize(); }
@Override public double cost() { return 2 + be.cost(); }
@Override public String getDescription() { return beverage.getDescription() + ", Milk"; }
public String getDescription() { return beverage.getDescription() + " Soy"; }
@Override public String getDescription() { return mBenverage.getDescription() + "Soy"; }
public String getDescription() // interface of CondimentDecorator { return beverage.getDescription() + ", MOCHA"; }
public Mocha(Beverage beverage) { this.beverage = beverage; System.out.println("Mocha is created with size=" + beverage.getSize()); }
public int getSize() // interface of CondimentDecorator { return beverage.getSize(); }
public double cost() { return beverage.cost() + .15; }
public int cost() { return 5 + beverage.cost(); }
@Override public double cost() { return beverage.cost() + .12; }
public static void main(String[] args) { Beverage drank = new Sugar(new Chocolate(new Milk(new Coffee()))); System.out.println(drank.cost()); }
@Override public String getDescript() { return be.getDescript() + ", Milk"; }
@Override public BigDecimal cost() { return beverage.cost().add(this.cost); }
@Override public String getDescription() { return beverage.getDescription() + " with " + this.description; }
public String getDescription() { return beverage.getDescription() + ", Mocha"; }