Created
February 4, 2013 15:56
-
-
Save ojacquemart/4707608 to your computer and use it in GitHub Desktop.
Some problem inspired by fsarradin, from https://groups.google.com/forum/?hl=fr&fromgroups=#!topic/paris-scala-user-group/7dqP4ubUyDY (https://groups.google.com/forum/?hl=fr&fromgroups=#%21topic/paris-scala-user-group/7dqP4ubUyDY)
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
/** | |
Robin est le genre de personne à avoir plusieurs comptes dans plusieurs banques. | |
Elle s'est créée un petit programme en Scala pour pouvoir récupérer le solde de ses comptes auprès | |
des différentes banques, à partir de son propriétaire. | |
*/ | |
case class Account(id: String, bankName: String, owner: String, amount: Int) | |
object AccountRepository extends App { | |
val banks: Map[String, Set[Account]] = | |
Map( | |
"GNB" -> Set(Account("CC1", "GNB", "Robin", 5000), Account("CC2", "GNB", "Barney", 9015000)), | |
"La Boob Postale" -> Set(Account("CP1", "La Boob Postale", "Robin", 2000)), | |
"Tortue Genaiale" -> Set(Account("TGN1", "Tortue Genaiale", "Ted", 150)), | |
"Credit Rural" -> Set(Account("CRL1", "Credit Rural", "Lily & Marshall", 50000)) | |
) | |
def findAccountsByOwner(owner: String): List[(String, Int)] = { | |
val ownerAccounts: List[Account] = banks.flatMap(b => b._2.filter(_.owner == owner)).toList | |
ownerAccounts.map(acc => (acc.bankName, acc.amount)) | |
} | |
println(findAccountsByOwner("Robin")) // List((GNB,5000), (La Boob Postale,2000)) | |
println(findAccountsByOwner("Barney")) // List((GNB,9015000)) | |
println(findAccountsByOwner("Tondeuse")) //) List() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment