Execute the following sequence of actions on a bank account:
1. Read in and show the balance of a bank account. |
2. Deposit an amount, withdraw
an amount, and show the balance. Read in the amounts
from the user. |
3. Add interest for one day, using an annual interest rate of 4.5%. Show the balance. |
There is one class in this system, class ACCOUNT. The data in an
account are the balance and the interest rate. The system
goals are to read in and store the balance, deposit money, withdraw money,
and show the balance.
The creation routine make contains all the code. This is not a good OO solution, but it is the only thing we can do until routines are covered in the next chapter. The client chart is unchanged from the previous part of the case study.
A new variable named amount is used to hold the amounts to deposit
and withdraw. The amount is read from the user, used to change the balance,
and that value is not used again. The amount to deposit (withdraw) is not
part of the state of the object; the state of the object is defined by
its balance and rate. The variable amount should be local,
but it has to be defined as a class attribute until routines are covered
in the next chapter.
The listing for class ACCOUNT is given below. This is the root class
for the system, with one routine named make that contains all the
executable code, so the Ace file is unchanged.
class ACCOUNT
creation
make
feature
balance: REAL
rate: REAL is
4.5
amount: REAL
make is
-- use the account
do
io.new_line
io.putstring ("%TInitial account balance: $")
io.readreal
balance := io.lastreal
io.putstring ("%NThe balance is $")
io.putreal (balance)
io.putstring ("%Tamount to deposit: $")
io.readreal
amount := io.lastreal
balance := balance + amount
io.putstring ("%Tamount to withdraw: $")
io.readreal
amount := io.lastreal
balance := balance - amount
io.new_line
io.putstring ("%NThe balance is $")
io.putreal (balance)
balance := balance + balance * (rate / 100.0) / 365.25 -- add interest today
io.new_line
io.new_line
io.putstring ("%NThe balance is $")
io.putreal (balance)
io.new_line
end -- make
end -- class ACCOUNT