Created
March 8, 2021 19:38
-
-
Save yigitgenc/40dae9f5afdb298726232b8b857e6139 to your computer and use it in GitHub Desktop.
Pizza
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
sealed trait Topping | |
case object Cheese extends Topping | |
case object Pepperoni extends Topping | |
case object Mushrooms extends Topping | |
case object Onions extends Topping | |
sealed trait CrustSize | |
case object SmallCrustSize extends CrustSize | |
case object MediumCrustSize extends CrustSize | |
case object LargeCrustSize extends CrustSize | |
sealed trait CrustType | |
case object RegularCrustType extends CrustType | |
case object ThinCrustType extends CrustType | |
case object ThickCrustType extends CrustType | |
class Pizza ( | |
var crustSize: CrustSize = MediumCrustSize, | |
var crustType: CrustType = RegularCrustType | |
) { | |
// ArrayBuffer is a mutable sequence (List) | |
val toppings = scala.collection.mutable.ArrayBuffer[Topping]() | |
def addTopping(topping: Topping): Unit = toppings += topping | |
def removeTopping(topping: Topping): Unit = toppings -= topping | |
def removeAllToppings(): Unit = toppings.clear() | |
override def toString(): String = { | |
s""" | |
|Crust Size: $crustSize | |
|Crust Type: $crustType | |
|Toppings: $toppings | |
|""".stripMargin | |
} | |
} | |
// a litte "driver" app | |
object PizzaTest extends App { | |
var p = new Pizza | |
p.addTopping(Cheese) | |
p.addTopping(Mushrooms) | |
p.addTopping(Onions) | |
println(p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment