Created
November 27, 2017 15:51
-
-
Save timmolderez/7cb60d703ffb5f2ba475dbf9c5594503 to your computer and use it in GitHub Desktop.
Design patterns exercise: decorator, simple factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object SandwichShop { | |
trait Sandwich { | |
def getPrice: Double | |
def getIngredients: Array[String] | |
} | |
class CheeseSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 2 | |
override def getIngredients: Array[String] = Array[String]("bread", "cheese") | |
} | |
class CheeseMayoSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 2.5 | |
override def getIngredients: Array[String] = Array[String]("bread", "cheese", "mayonnaise") | |
} | |
class CheeseVegetableSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 2.5 | |
override def getIngredients: Array[String] = Array[String]("bread", "cheese", "lettuce", "carrots", "tomatoes") | |
} | |
class CheeseMayoVegetableSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 3 | |
override def getIngredients: Array[String] = Array[String]("bread", "cheese", "mayonnaise", "lettuce", "carrots", "tomatoes") | |
} | |
class HamSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 2 | |
override def getIngredients: Array[String] = Array[String]("bread", "ham") | |
} | |
class HamMayoSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 2.5 | |
override def getIngredients: Array[String] = Array[String]("bread", "ham", "mayonnaise") | |
} | |
class HamVegetableSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 2.5 | |
override def getIngredients: Array[String] = Array[String]("bread", "ham", "lettuce", "carrots", "tomatoes") | |
} | |
class HamMayoVegetableSandwich extends SandwichShop.Sandwich { | |
override def getPrice = 3 | |
override def getIngredients: Array[String] = Array[String]("bread", "ham", "mayonnaise", "lettuce", "carrots", "tomatoes") | |
} | |
def main(args: Array[String]): Unit = { | |
val order = Array("cheese", "cheese with mayo", "ham with vegetables") | |
var total = 0.0 | |
for (item <- order) { | |
var s = null : Sandwich | |
item match { | |
case "cheese" => | |
s = new SandwichShop.CheeseSandwich | |
case "cheese with mayo" => | |
s = new SandwichShop.CheeseMayoSandwich | |
case "cheese with vegetables" => | |
s = new SandwichShop.CheeseVegetableSandwich | |
case "cheese with mayo and vegetables" => | |
s = new SandwichShop.CheeseMayoVegetableSandwich | |
case "ham" => | |
s = new SandwichShop.HamSandwich | |
case "ham with mayo" => | |
s = new SandwichShop.HamMayoSandwich | |
case "ham with vegetables" => | |
s = new SandwichShop.HamVegetableSandwich | |
case "ham with mayo and vegetables" => | |
s = new SandwichShop.HamMayoVegetableSandwich | |
} | |
total += s.getPrice | |
} | |
System.out.println("Total price of order: " + total + " Eur") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment