The question asks:
Enhance the addInterest method of the SavingsAccount class to compute the interest on the minimum balance since the last call to addInterest. Hint: You need to modify the withdraw method as well, and you need to add an instance variable to remember the minimum balance.
(Oh, and this has to do with inheritance)
This is what I have so far:
BankAccount.java
1 /**
2 A bank account has a balance that can be changed by
3 deposits and withdrawals.
4 */
5 public class BankAccount
6 {
7 /**
8 Constructs a bank account with a zero balance
9 */
10 public BankAccount()
11 {
12 balance = 0;
13 }
14
15 /**
16 Constructs a bank account with a given balance
17 @param initialBalance the initial balance
18 */
19 public BankAccount(double initialBalance)
20 {
21 balance = initialBalance;
22 }
23
24 /**
25 Deposits money into the bank account.
26 @param amount the amount to deposit
27 */
28 public void deposit(double amount)
29 {
30 balance = balance + amount;
31 }
32
33 /**
34 Withdraws money from the bank account.
35 @param amount the amount to withdraw
36 */
37 public void withdraw(double amount)
38 {
39 balance = balance - amount;
40 }
41
42 /**
43 Gets the current balance of the bank account.
44 @return the current balance
45 */
46 public double getBalance()
47 {
48 return balance;
49 }
50
51 /**
52 Transfers money from the bank account to another account
53 @param other the other account
54 @param amount the amount to transfer
55 */
56 public void transfer(BankAccount other, double amount)
57 {
58 withdraw(amount);
59 other.deposit(amount);
60 }
61
62 private double balance;
63 }
CheckingAccount.java
1 /**
2 A checking account that charges transaction fees.
3 */
4 public class CheckingAccount extends BankAccount
5 {
6 /**
7 Constructs a checking account with a given balance
8 @param initialBalance the initial balance
9 */
10 public CheckingAccount(int initialBalance)
11 {
12 // construct superclass
13 super(initialBalance);
14
15 // initialize transaction count
16 transactionCount = 0;
17 }
18
19 public void deposit(double amount)
20 {
21 transactionCount++;
22 // now add amount to balance
23 super.deposit(amount);
24 }
25
26 public void withdraw(double amount)
27 {
28 transactionCount++;
29 // now subtract amount from balance
30 super.withdraw(amount);
31 }
32
33 /**
34 Deducts the accumulated fees and resets the
35 transaction count.
36 */
37 public void deductFees()
38 {
39 if (transactionCount > FREE_TRANSACTIONS)
40 {
41 double fees = TRANSACTION_FEE *
42 (transactionCount - FREE_TRANSACTIONS);
43 super.withdraw(fees);
44 }
45 transactionCount = 0;
46 }
47
48 private int transactionCount;
49
50 private static final int FREE_TRANSACTIONS = 3;
51 private static final double TRANSACTION_FEE = 2.0;
52 }
SavingsAccount.java
1 /**
2 An account that earns interest at a fixed rate.
3 */
4 public class SavingsAccount extends BankAccount
5 {
6 /**
7 Constructs a bank account with a given interest rate
8 @param rate the interest rate
9 */
10 public SavingsAccount(double rate)
11 {
12 interestRate = rate;
13 }
14
15 /**
16 Adds the earned interest to the account balance.
17 */
18 public void addInterest()
19 {
20 double interest = getBalance() * interestRate / 100;
21 deposit(interest);
22 }
23
24 private double interestRate;
25 }