mardi 8 mars 2016

Java Pattern for enum

I have a class for salad and enum for salad weight. The question is how can i replace method orderOlivie with pattern and what type of pattern do I need? Or maybe i just need to reorganize this a bit?

public class Salad { private List olivie;

private SaladWeight saladWeight;

public SaladWeight getSaladWeight() {
    return saladWeight;
}

public void setSaladWeight(SaladWeight saladWeight) {
    this.saladWeight = saladWeight;
}

public List<Vegetable> getOlivie() {
    return Collections.unmodifiableList(olivie);
}

public void setOlivie(List<Vegetable> olivie) {
    this.olivie = olivie;
}

public void orderOlivie(SaladWeight sW){
    Carrot carrot = null;
    Cheese cheese = null;
    French french = null;
    White white = null;
    Onion onion = null;

    switch (sW){
        case Little: {
            carrot = new Carrot(1);
            cheese = new Cheese(50);
            french = new French(125);
            white = new White(75);
            onion = new Onion(1);
            break;
        }
        case Medium: {
            carrot = new Carrot(2);
            cheese = new Cheese(100);
            french = new French(250);
            white = new White(150);
            onion = new Onion(2);
            break;
        }
        case Big: {
            carrot = new Carrot(3);
            cheese = new Cheese(150);
            french = new French(375);
            white = new White(225);
            onion = new Onion(3);
            break;
        }
        default: throw new IllegalArgumentException();
    }

    olivie.add(carrot);
    olivie.add(cheese);
    olivie.add(french);
    olivie.add(white);
    olivie.add(onion);

    System.out.println("You ordered " + saladWeight + "gr of olivie!");
}

}

Aucun commentaire:

Enregistrer un commentaire