previous | start | next

File 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 }


previous | start | next