Navigation:  Tutorials > Creating a GUI Application > Designing the Domain Models >

PersonalAccountTransaction

Previous pageReturn to chapter overviewNext page

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 '

classVariableNames: ''

poolDictionaries: ''

 

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.

initialize

"Private - Initialize the receiver"

       date := Date today.

       description := String new.

       amount := 0.0.

       isDebit := true.

 

The initialization and accessor methods are again straightforward.

amount

       "Answer the amount of the receiver"

       ^amount.

 

amount: aNumber

       "Set the amount of the receiver to aNumber"

       amount := aNumber.

 

date

       "Answer the date of the receiver"

       ^date.

 

date: aDate

       "Set the date of the receiver to aDate"

       date := aDate.

 

description

       "Answer the String description of the receiver"

       ^description.

 

description: aString

       "Set the description of the receiver to aString"

       description := aString.

 

isDebit

       "Answer true if the receiver is a Debit transaction"

       ^isDebit.

 

isDebit: aBoolean

       "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.

actualAmount

       "Answer the actual amount of the receiver taking into consideration

       whether it is a debit or credit transaction"

 

       ^self isDebit

               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.

printOn: aStream

       "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: $).

 

displayOn: aStream

       "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.