M206 - TMA05 Question 1 Part 1
i) a) Copy of Class Reporter
Reporter KidsAccount subclass of Account
-----------------------------------------------------------------
- instance variables:
- dateAppliedFor The date the account was applied for.
- instance methods:
- dateAppliedFor:
dateAppliedFor - class variables:
- no class variables
- class-instance variables:
- no class-instance variables
- class methods: no class methods
KidsAccount class comment
-------------------------------------------
The class KidsAccount differs from Account in that objects of the subclass pay favourable interest (8.5% per annum) on balances that are greater than zero and that have been applied for at least 180 days before (approximately six months). (The date applied for is used rather than the date the account was opened as there is often a delay in opening the account, which should not penalise the account holder.) By offering this sort of account, a bank hopes to encourage children into a saving habit; hence the name of the subclass.
Example usage: "The following expression series -creates myKidsAccount with all the instance variables referencing nil. More realistic objects are then referenced by holder and overLimit, £50 is paid in and the amount in the account is reported and the date the account was applied for is also recorded."
- MyKidsAccount:=KidsAccount new.
- myKidsAccount holder:'John Smith'
- overLimit: 50;
- credit: 50;
- dateAppliedFor: (a Date:= Date newDay:22 month:#May year: 1998).
- Dialog warn: ('You have £', (myKidsAccount balance) printString, ' in this account.')
"The following expression returns the date on which the account was opened."
- myKidsAccount dateAppliedFor
"The following expression checks whether or not interest can be added applying the criterion mentioned above. Answers true if Ok and false if not"
"The following expression sets the date the account was applied for to 30 December 1998."
- myKidsAccount dateAppliedFor:(aDate:= Date newDay:30 month:#December year:1998).
KidsAccount instance methods code
dateAppliedFor
"Answer the dateAppliedFor".
- ^aDateAppliedFor
dateAppliedFor:aDate
Set the dateAppliedFor to aDate(an instance of Date), if the date is valid set to aDate else set to todays date. Answer the receiver."
- (aDate class = Date)
- ifTrue: [dateAppliedFor:= aDate]
- ifFalse:[dateAppliedFor:= Date today].
b) Expressions used to create the KidsAccount object and test dateAppliedFor:aDate
An instance of KidsAccount was created by evaluating the expression testAcc:= KidsAccount new. The variables were set using the following expression series:
- testAcc:= 'Test Account'; overLimit: 50; credit: 50; dateApplied For: (a Date:= Date newDay:22 month:#May year.. 1998).
To test the implementation of dateAppliedFor: the following expressions were used:
- testAcc dateAppliedFor:'22/06/99'
Invalid date, which in effect produces today's date confirmed by inspecting testAcc class confirmed by inspection of dateAppliedFor.
- testAcc dateAppliedFor: (aDate:= Date newDay:22 month:#May year: 1998)
Produced message answer 22 May,1998 class confirmed by inspection of message answer. No other dates were tried as the pre-condition is only checking for an argument of class Date.