The Java Program: CheckingAccount.java

  1 // Account.java -- example of inheritance adapted from Hostman, page 449
  2 
  3 class BankAccount {
  4    private double balance;
  5    public BankAccount ()         { this(0.0); }
  6    public BankAccount (double init) { balance = init; }
  7    public void deposit   (double amount) { balance += amount; }
  8    public void withdrawl (double amount) { deposit (-amount); }
  9    public double getBalance () { return balance; }
 10 }
 11 
 12 class SavingsAccount extends BankAccount {
 13    private double interestRate;
 14    public SavingsAccount (double rate) { interestRate = rate / 100; }
 15    public void addInterest() {
 16       double i = getBalance() * interestRate;
 17       deposit (i);
 18    }
 19 }
 20 
 21 public class CheckingAccount extends BankAccount {
 22 
 23    private static final int FREE = 3;
 24    private static final double FEE = 1.50;
 25    private int transactionCount;
 26    
 27    public CheckingAccount () { this (0.0); }
 28    public CheckingAccount (double init) { super (init); transactionCount=0;}
 29    public void deposit (double amount) {
 30       super.deposit (amount);
 31       transactionCount++;
 32    }
 33    public void deductFees() {
 34       if (transactionCount > FREE) {
 35          super.deposit (- (FEE *(transactionCount-FREE)));
 36       }
 37       transactionCount = 0;
 38    }
 39 }
 40