Created
September 24, 2015 12:44
-
-
Save pram/16d67e743150fc7a1577 to your computer and use it in GitHub Desktop.
Scala Worksheet
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
class Coffee(val flavour: String, val price: Int = 25) { | |
override def toString: String = flavour + " " + price.toString | |
} | |
class CreditCard(val number: Int) { | |
override def toString: String = number.toString | |
} | |
case class Charge(card: CreditCard,cost: Double) { | |
def combine(other: Charge): Charge = | |
if (card == other.card) | |
Charge(card, cost + other.cost) | |
else | |
throw new Exception("Can't Combine") | |
} | |
val mocha = new Coffee("Mocha",35) | |
class Cafe { | |
def buyCoffee(c: Coffee, cc: CreditCard): (Coffee, Charge) = { | |
(c, Charge(cc,c.price)) | |
} | |
def buyCoffees(cc: CreditCard, n: Int): (List[Coffee], Charge) = { | |
val purchases: List[(Coffee, Charge)] = List.fill(n)(buyCoffee(mocha,cc)) | |
val (coffees, charges) = purchases.unzip | |
(coffees, charges.reduce((c1,c2) => c1.combine(c2))) | |
} | |
} | |
val shmoe = new Cafe | |
val (drinks, stuff) = shmoe.buyCoffees(new CreditCard(1234567890), 5) | |
drinks | |
stuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment