Skip to content

Instantly share code, notes, and snippets.

@dwhitney
Created July 5, 2011 20:54
Show Gist options
  • Select an option

  • Save dwhitney/1065895 to your computer and use it in GitHub Desktop.

Select an option

Save dwhitney/1065895 to your computer and use it in GitHub Desktop.
class Account(val accountNo: String, var balance: Int){
def credit(amount: Int) = balance = balance + amount
def debit(amount: Int) = balance = balance - amount
}
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))
if(from.balance > amount){
to.credit(amount)
from.debit(amount)
}
}
def main(args: Array[String]): Unit = {
Bank.transfer("from", "to", 50)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment