Created
July 6, 2011 15:00
-
-
Save dwhitney/1067443 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
| private val map = new java.util.HashMap[String, Account]() | |
| map.put("from", Account("from", 500)) | |
| map.put("to", Account("to", 500)) | |
| val accountsRef = Ref(map) | |
| def transfer(fromNo: String, toNo: String, amount: Int): Unit = { | |
| atomic{ implicit t => | |
| var accounts = accountsRef() | |
| val (from, to) = (accounts.get(fromNo), accounts.get(toNo)) | |
| if(from.balance > amount){ | |
| accounts = accounts.put(to.accountNo, to.credit(amount)) | |
| accounts = accounts.put(from.accountNo, from.debit(amount)) | |
| accountsRef() = accounts | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment