Skip to content

Instantly share code, notes, and snippets.

@timmolderez
Created December 8, 2016 00:39
Show Gist options
  • Save timmolderez/b9da787fe39011e19dc4c3f86e5f4044 to your computer and use it in GitHub Desktop.
Save timmolderez/b9da787fe39011e19dc4c3f86e5f4044 to your computer and use it in GitHub Desktop.
Design patterns exercise: decorator, simple factory
package be.ac.vub.patterns;
public class SandwichShop {
public static interface Sandwich {
public double getPrice();
public String[] getIngredients();
}
public static class CheeseSandwich implements Sandwich {
@Override
public double getPrice() {return 2;}
@Override
public String[] getIngredients() {return new String[]{"bread", "cheese"};}
}
public static class CheeseMayoSandwich implements Sandwich {
@Override
public double getPrice() {return 2.5;}
@Override
public String[] getIngredients() {return new String[]{"bread", "cheese", "mayonnaise"};}
}
public static class CheeseVegetableSandwich implements Sandwich {
@Override
public double getPrice() {return 2.5;}
@Override
public String[] getIngredients() {return new String[]{"bread", "cheese", "lettuce", "carrots", "tomatoes"};}
}
public static class CheeseMayoVegetableSandwich implements Sandwich {
@Override
public double getPrice() {return 3;}
@Override
public String[] getIngredients() {return new String[]{"bread", "cheese", "mayonnaise", "lettuce", "carrots", "tomatoes"};}
}
public static class HamSandwich implements Sandwich {
@Override
public double getPrice() {return 2;}
@Override
public String[] getIngredients() {return new String[]{"bread", "ham"};}
}
public static class HamMayoSandwich implements Sandwich {
@Override
public double getPrice() {return 2.5;}
@Override
public String[] getIngredients() {return new String[]{"bread", "ham", "mayonnaise"};}
}
public static class HamVegetableSandwich implements Sandwich {
@Override
public double getPrice() {return 2.5;}
@Override
public String[] getIngredients() {return new String[]{"bread", "ham", "lettuce", "carrots", "tomatoes"};}
}
public static class HamMayoVegetableSandwich implements Sandwich {
@Override
public double getPrice() {return 3;}
@Override
public String[] getIngredients() {return new String[]{"bread", "ham", "mayonnaise", "lettuce", "carrots", "tomatoes"};}
}
public static void main(String[] args) {
String[] order = {"cheese", "cheese with mayo", "ham with vegetables"};
double total = 0;
for (int i = 0; i < order.length; i++) {
String item = order[i];
Sandwich s = null;
switch(item) {
case "cheese": s = new CheeseSandwich(); break;
case "cheese with mayo": s = new CheeseMayoSandwich(); break;
case "cheese with vegetables": s = new CheeseVegetableSandwich(); break;
case "cheese with mayo and vegetables": s = new CheeseMayoVegetableSandwich(); break;
case "ham": s = new HamSandwich(); break;
case "ham with mayo": s = new HamMayoSandwich(); break;
case "ham with vegetables": s = new HamVegetableSandwich(); break;
case "ham with mayo and vegetables": s = new HamMayoVegetableSandwich(); break;
}
total += s.getPrice();
}
System.out.println("Total price of order: " + total);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment