1 /**
2 A checking account that charges transaction fees.
3 */
4 public class CheckingAccount extends BankAccount
5 {
6 private static final int FREE_TRANSACTIONS = 3;
7 private static final double TRANSACTION_FEE = 2.0;
8
9 private int transactionCount;
10
11 /**
12 Constructs a checking account with a given balance.
13 @param initialBalance the initial balance
14 */
15 public CheckingAccount(double initialBalance)
16 {
17 // Construct superclass
18 super(initialBalance);
19
20 // Initialize transaction count
21 transactionCount = 0;
22 }
23
24 public void deposit(double amount)
25 {
26 transactionCount++;
27 // Now add amount to balance
28 super.deposit(amount);
29 }
30
31 public void withdraw(double amount)
32 {
33 transactionCount++;
34 // Now subtract amount from balance
35 super.withdraw(amount);
36 }
37
38 /**
39 Deducts the accumulated fees and resets the
40 transaction count.
41 */
42 public void deductFees()
43 {
44 if (transactionCount > FREE_TRANSACTIONS)
45 {
46 double fees = TRANSACTION_FEE *
47 (transactionCount - FREE_TRANSACTIONS);
48 super.withdraw(fees);
49 }
50 transactionCount = 0;
51 }
52 }