Created
March 22, 2021 14:12
-
-
Save davegurnell/027a0c9c0200ce4b4599a5738691751f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// OBJECTS -------------------------------------- | |
object iceCream { | |
val name = "Sundae" | |
val inCone = false | |
val numCherries = 1 | |
def inGlass: Boolean = | |
!inCone | |
def hasRaspberrySauce: Boolean = | |
if (inCone) numCherries >= 2 else numCherries % 2 == 0 | |
} | |
iceCream.name | |
case class IceCream(name: String, inCone: Boolean, numCherries: Int) { | |
def inGlass: Boolean = | |
!inCone | |
def hasRaspberrySauce: Boolean = | |
if (inCone) numCherries >= 2 else numCherries % 2 == 0 | |
} | |
val foo1 = new IceCream("Magnum", false, 0) | |
val foo2 = new IceCream("Magnum", false, 10) | |
val bar = new IceCream("Cornetto", true, 1) | |
foo1 == foo2 | |
foo1.copy(name = "Bob") | |
object priceCalculator { | |
def price(iceCream: IceCream, eatingIn: Boolean): Int = { | |
val basePrice: Int = | |
(if (iceCream.inGlass) 100 else 50) + | |
(20 * iceCream.numCherries) + | |
(if (iceCream.hasRaspberrySauce) 80 else 0) | |
val taxMultiplier: Double = | |
(if (eatingIn) 1.2 else 1.0).toInt | |
(basePrice * taxMultiplier).toInt | |
} | |
} | |
priceCalculator.price(foo1, true) | |
priceCalculator.price(foo2, true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment