Skip to content
Snippets Groups Projects

Add alternative solution for orderDec

Open Matt Bovel requested to merge mb/alt-orderdec into main
1 file
+ 17
0
Compare changes
  • Side-by-side
  • Inline
@@ -30,6 +30,23 @@ trait Problem3:
soldCards.flatMap { _ => buyListOfCards(wantedDeck) }
}.flatten
/** Alternative implementation of orderDeck.
*/
def orderDecAlt(
bag: MoneyBag,
cardsToSell: List[Card],
wantedDeck: List[String]
): Future[List[Card]] =
val totalGivenMoney =
cardsToSell.map(c => valueOf(c.name)).sum + moneyIn(bag)
val totalNeededMoney = wantedDeck.map(valueOf).sum
if totalGivenMoney < totalNeededMoney then
return Future.failed(new NotEnoughMoneyException())
val soldCards: Future[Any] =
if moneyIn(bag) != 0 then sellListOfCards(cardsToSell).zip(deposit(bag))
else sellListOfCards(cardsToSell)
soldCards.flatMap { _ => buyListOfCards(wantedDeck) }
/** This helper function will sell the provided list of cards and put the
* money on your personal bank account. It returns a Future of Unit, which
* indicates when all sales are completed.
Loading