Created
July 5, 2011 21:48
-
-
Save dwhitney/1066049 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{ | |
| val accounts = Map("from" -> new Account("from", 500), "to" -> new Account("to", 500) ) | |
| def transfer(fromNo: String, toNo: String, amount: Int): Unit = { | |
| val (from, to) = (accounts(fromNo), accounts(toNo)) | |
| val balance = (from !? Balance).asInstanceOf[Int] | |
| if(balance > amount){ | |
| to ! Credit(amount) | |
| from ! Debit(amount) | |
| } | |
| } | |
| } | |
| case class Credit(amount: Int) | |
| case class Debit(amount: Int) | |
| case object Balance | |
| class Account(val accountNo: String, beginningBalance: Int) extends Actor{ | |
| private var _balance = beginningBalance | |
| def act = { | |
| loop{ | |
| receive{ | |
| case Credit(amount) => _balance = _balance + amount | |
| case Debit(amount) => _balance = _balance - amount | |
| case Balance => reply(_balance) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment