Ejercicio De Cafeteria Utilizando El Patron Decorate

Un programa capaz de calcular el precio de un café, contemplando que el cliente pueda pedir extras al mismo.

Interfaz

public interface Beverage {
    public String getName();
    public double getPrice();
    public void setPrice(double price);
    public void setDescription(String description);

}

Nuestra clase docoradora

public class CondimentDecorator implements Beverage{

    public CondimentDecorator(Beverage beverage){
        super();
        this.beverage=beverage;
    }
    Beverage beverage;

    private String description;
    private int price;

    public String getName() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public double getPrice() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setPrice(double price){
        System.out.println(price);
    }

    public void setDescription(String description) {
        System.out.println(description);
    }

}

Condimentos (decreteDecorator)

public class ChocoChips extends CondimentDecorator{

    private String description;
    private double price;

    public ChocoChips(Beverage beverage){
        super(beverage);
        this.beverage = beverage;
    }

    @Override
    public String getName() {
        description = beverage.getName()+ ", ChocoChips";
        return description;
    }

    @Override
    public double getPrice() {
        price = 3+beverage.getPrice();
       // System.out.print(" "+price);
        return price;
    }

}
public class Cream extends CondimentDecorator{

    private String description;
    private double price;

    public Cream(Beverage beverage){
        super(beverage);
        this.beverage = beverage;
    }

    @Override
    public String getName() {
        description = beverage.getName()+ ", Cream";
        System.out.print(", "+description);
        return description;
    }

    @Override
    public double getPrice() {
        price = 5+beverage.getPrice();
        System.out.print(" "+description);
        return price;
    }

}
public class Milk extends CondimentDecorator{

    private String description;
    private double price;

    public Milk(Beverage beverage){
        super(beverage);

        this.beverage = beverage;
    }

    @Override
    public String getName() {
        description = beverage.getName()+ ", Milk";
        return description;
    }

    @Override
    public double getPrice() {
        price = 5+beverage.getPrice();
        return price;
    }
public class Whip extends CondimentDecorator{

    private String description;
    private double price;

    public Whip(Beverage beverage){
        super(beverage);
        this.beverage = beverage;
    }

    @Override
    public String getName() {
        description = beverage.getName()+ ", Whip";
        return description;
    }

    @Override
    public double getPrice() {
        price = 4+beverage.getPrice();
        return price;
    }
}

Mis tipos de cafes y algunos cafes

public class DarkRoast implements Beverage{

    public DarkRoast(){}

    private String description;

    public String getName() {
        description = "Cafe tipo DarkRoast";
        System.out.print(description);
        return description;
    }

    public double getPrice() {
        return 0;
    }

    public void setPrice(double price) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setDescription(String description) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
public class Decaf extends DarkRoast{

    public Decaf(){
        super();
    }

    private String description;
    private double price;

    @Override
    public String getName() {
        description = "Decaf";
        return description;
    }

    @Override
    public double getPrice() {
        price=15;
        return price;
    }

}
public class HouseBlend implements Beverage{

    public HouseBlend(){}

    private String description;

    public String getName() {
        description = "Cafe tipo HouseBlend";
        System.out.print(description);
        return description;
    }

    public double getPrice() {
        return 0;
    }

    public void setPrice(double price) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setDescription(String description) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
public class Express extends HouseBlend{

    public Express(){
        super();
    }

    private String description;
    private double price;

    @Override
    public String getName() {
        description = "Express";
        return description;
    }

    @Override
    public double getPrice() {
        price=12;
        return price;
    }
}

Finalmente nuestro Main

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        double price;
        String description;

        Beverage bev1=new Decaf();
        bev1 = new Milk(bev1);
        bev1 = new ChocoChips(bev1);
        bev1 = new Whip(bev1);
        bev1 = new Milk(bev1);
        description = bev1.getName();
        price = bev1.getPrice();
        bev1.setDescription(description);
        bev1.setPrice(price);

        Beverage bev2=new Express();
        bev2 = new Milk(bev2);
        description = bev2.getName();
        price = bev2.getPrice();
        bev2.setDescription(description);
        bev2.setPrice(price);
    }

}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License