One more class to go and then we can do some experimentation. Now will define the class that represents a transaction on a PersonalAccount.
Model subclass: #PersonalAccountTransaction
instanceVariableNames: 'date description amount isDebit '
Each transaction holds a descriptive String and a numeric amount, together with the date of the transfer. The isDebit flag is a boolean to indicate whether the transaction is a debit or a credit operation on the account.
"Private - Initialize the receiver"
date := Date today.
description := String new.
amount := 0.0.
isDebit := true.
The initialization and accessor methods are again straightforward.
"Answer the amount of the receiver"
"Set the amount of the receiver to aNumber"
amount := aNumber.
"Answer the date of the receiver"
"Set the date of the receiver to aDate"
date := aDate.
"Answer the String description of the receiver"
"Set the description of the receiver to aString"
description := aString.
"Answer true if the receiver is a Debit transaction"
"Set the receiver to a Debit transaction according to aBoolean.
If aBoolean is true then the receiver is treated as a credit"
isDebit := aBoolean.
It will be useful to provide a proper method that answers the effective amount for the transaction taking in into consideration the isDebit flag.
"Answer the actual amount of the receiver taking into consideration
whether it is a debit or credit transaction"
ifTrue: [ self amount negated ]
ifFalse: [ self amount ].
Again we provide a printOn: method, but this time we also include a displayOn: method to determine how a transaction will appear when presented to the user as text. By default it is this method that is used to display an object within a list or text view on the screen.
"Append, to aStream, a String whose characters are a description of the receiver as a developer would want to see it."
self basicPrintOn: aStream.
aStream nextPut: $(.
self displayOn: aStream.
aStream nextPut: $).
"Append, to aStream, a String whose characters are a description of the receiver as a user would want to see it."
self date displayOn: aStream.
aStream nextPutAll: ': '.
aStream nextPutAll: self description.
aStream nextPutAll: (self isDebit
ifTrue: [' - debit ']
ifFalse: [ ' - credit ']).
self amount displayOn: aStream.