Skip to content

Instantly share code, notes, and snippets.

@agushuley
Created October 2, 2012 06:53
Show Gist options
  • Save agushuley/3816922 to your computer and use it in GitHub Desktop.
Save agushuley/3816922 to your computer and use it in GitHub Desktop.
Scala for impatients, chapter 5, 2th task
class Account {
private var balance: Double = 0;
def credit = if ( balance < 0 ) -balance else 0
def credit_=( value: Int ) {
balance = -value
}
def debit = if ( balance > 0 ) balance else 0
def debit_=( value: Int ) {
balance = value
}
}
var myAccount = new Account()
myAccount.credit = 1
println( "debit" -> myAccount.debit, "credit" -> myAccount.credit )
myAccount.debit = 1
println( "debit" -> myAccount.debit, "credit" -> myAccount.credit )
myAccount.credit = -1
println( "debit" -> myAccount.debit, "credit" -> myAccount.credit )
myAccount.credit = 0
println( "debit" -> myAccount.debit, "credit" -> myAccount.credit )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment