Skip to content
Snippets Groups Projects
Commit 951607c3 authored by Matt Bovel's avatar Matt Bovel
Browse files

Add alternative solution for orderDec

parent 0f473913
No related tags found
1 merge request!3Add alternative solution for orderDec
Pipeline #158104 passed
...@@ -30,6 +30,23 @@ trait Problem3: ...@@ -30,6 +30,23 @@ trait Problem3:
soldCards.flatMap { _ => buyListOfCards(wantedDeck) } soldCards.flatMap { _ => buyListOfCards(wantedDeck) }
}.flatten }.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 /** 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 * money on your personal bank account. It returns a Future of Unit, which
* indicates when all sales are completed. * indicates when all sales are completed.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment