package com.java.jikexueyuan.coffeebar;
public abstract class Drink {
public String description="";
private float price=0f;;
public void setDescription(String description)
{
this.description=description;
}
public String getDescription()
{
return description+"-"+this.getPrice();
}
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price=price;
}
public abstract float cost();
}
package com.java.jikexueyuan.coffeebar.coffee;
import com.java.jikexueyuan.coffeebar.Drink;
public class Coffee extends Drink {
@Override
public float cost() {
// TODO Auto-generated method stub
return super.getPrice();
}
}
package com.java.jikexueyuan.coffeebar.coffee;
public class Decaf extends Coffee {
public Decaf()
{
super.setDescription("Decaf");
super.setPrice(3.0f);
}
}
package com.java.jikexueyuan.coffeebar.coffee;
public class Espresso extends Coffee{
public Espresso()
{
super.setDescription("Espresso");
super.setPrice(4.0f);
}
}
package com.java.jikexueyuan.coffeebar.coffee;
public class LongBlack extends Coffee{
public LongBlack()
{
super.setDescription("LongBlack");
super.setPrice(6.0f);
}
}
package com.java.jikexueyuan.coffeebar.coffee;
public class ShortBlack extends Coffee{
public ShortBlack()
{
super.setDescription("ShortBlack");
super.setPrice(5.0f);
}
}
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.Drink;
public class Decorator extends Drink {
private Drink Obj;
public Decorator(Drink Obj){
this.Obj=Obj;
};
@Override
public float cost() {
// TODO Auto-generated method stub
return super.getPrice()+Obj.cost();
}
@Override
public String getDescription()
{
return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription();
}
}
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.Drink;
public class Milk extends Decorator {
public Milk(Drink Obj) {
super(Obj);
// TODO Auto-generated constructor stub
super.setDescription("Milk");
super.setPrice(2.0f);
}
}
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.Drink;
public class Chocolate extends Decorator {
public Chocolate(Drink Obj) {
super(Obj);
// TODO Auto-generated constructor stub
super.setDescription("Chocolate");
super.setPrice(3.0f);
}
}
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.Drink;
public class Soy extends Decorator {
public Soy(Drink Obj) {
super(Obj);
// TODO Auto-generated constructor stub
super.setDescription("Soy");
super.setPrice(1.5f);
}
}
package com.java.jikexueyuan.coffeebar;
import com.java.jikexueyuan.coffeebar.coffee.Decaf;
import com.java.jikexueyuan.coffeebar.coffee.LongBlack;
import com.java.jikexueyuan.coffeebar.decorator.Chocolate;
import com.java.jikexueyuan.coffeebar.decorator.Milk;
public class CoffeeBar {
public static void main(String[] args) {
Drink order;
order=new Decaf();
System.out.println("order1 price:"+order.cost());
System.out.println("order1 desc:"+order.getDescription());
System.out.println("****************");
order=new LongBlack();
order=new Milk(order);
order=new Chocolate(order);
order=new Chocolate(order);
System.out.println("order2 price:"+order.cost());
System.out.println("order2 desc:"+order.getDescription());
}
}