Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. private val map = new java.util.HashMap[String, Account]()
  2. map.put("from", Account("from", 500))
  3. map.put("to", Account("to", 500))
  4.  
  5. val accountsRef = Ref(map)
  6.  
  7.   def transfer(fromNo: String, toNo: String, amount: Int): Unit = {
  8.     atomic{ implicit t =>
  9.       var accounts = accountsRef()
  10.       val (from, to) = (accounts.get(fromNo), accounts.get(toNo))
  11.       if(from.balance > amount){
  12.         accounts.put(to.accountNo, to.credit(amount))
  13.         accounts.put(from.accountNo, from.debit(amount))
  14.         accountsRef() = accounts
  15.       }
  16.     }
  17.   }