Created
July 5, 2011 22:02
-
-
Save dwhitney/1066072 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
| import scala.actors._ | |
| import Actor._ | |
| object Bank extends Actor{ | |
| private var accounts = Map("from" -> Account("from", 500), "to" -> Account("to", 500) ) | |
| def act = { | |
| loop{ | |
| react{ | |
| case Transfer(fromNo, toNo, amount) => | |
| val (from, to) = (accounts(fromNo), accounts(toNo)) | |
| if(from.balance > amount){ | |
| accounts = accounts + (to.accountNo -> to.copy(balance = (to.balance + amount))) | |
| accounts = accounts + (from.accountNo -> from.copy(balance = (from.balance - amount))) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| case class Transfer(fromNo: String, toNo: String, amount: Int) | |
| case class Account(val accountNo: String, balance: Int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment