Implementing the Non-constructor methods

  1. The root BankAccount class:

    BankAccount class
          public class BankAccount (extends Object !)
          {
             private double balance;
    
             public void deposit(double amount)
             {
                balance = balance + amount;
             }
    
             public void withdraw(double amount)
             {
                if ( amount <= balance )
                   balance = balance - amount;
                else
                   System.out.println("Insufficient funds");   
             }
    
             public double getBalance()
             {
                return(balance);
             }
          }
    

  2. The CheckingAccount class:

    Structure of the CheckingAccount class
        public CheckingAccount extends BankAccount
        {
           private int transactionCount;
    
           public void deposit(double amount) // Override
           {
    	  ....
           }
    
           public void withdraw(double amount) // Override   
           {
    	  ....
           }
    
           public void deductFees() // New method
           {
    	  ....
           }
        }
    

  3. First attempt at CheckingAccount:

    CheckingAccount class: Take 1
        public CheckingAccount extends BankAccount
        {
           private int transactionCount;
    
           public void deposit(double amount) // Override
           {
    	  balance = balance + amount;
    	  transactionCount++;
           }
    
           public void withdraw(double amount) // Override   
           {
    	  ....
           }
    
           public void deductFees() // New method
           {
    	  ....
           }
        }
    

    ERROR:

  4. Second attempt at CheckingAccount:

    CheckingAccount class: Take 2
        public CheckingAccount extends BankAccount
        {
           private int transactionCount;
    
           public void deposit(double amount) // Override
           {
    	  deposit(amount);
    	  transactionCount++;
           }
    
           public void withdraw(double amount) // Override   
           {
    	  ....
           }
    
           public void deductFees() // New method
           {
    	  ....
           }
        }
    

    ERROR:

  5. Third attempt at CheckingAccount:

    CheckingAccount class: Final Take
        public CheckingAccount extends BankAccount
        {
           private static final int FREE_TRANSACTIONS = 3;
           private static final double TRANSACTION_FEE = 2.0;
           private int transactionCount;
    
           public void deposit(double amount) // Override
           {
    	  super.deposit(amount);
    	  transactionCount++;
           }
    
           public void withdraw(double amount) // Override   
           {
    	  super.withdraw(amount);
    	  transactionCount++;
           }
    
           public void deductFees() // New method
           {
    	  double fees;
    
    	  if ( transactionCount > FREE_TRANSACTIONS )
    	  {
    	      fees = (transactionCount - FREE_TRANSACTIONS) *
    			TRANSACTION_FEE;
    	      super.withdraw(fees);
    	  }
    
    	  transactionCount = 0;    // Reset for new monthly count
           }
        }
    

  6. Implementing SavingsAccount:

    SavingsAccount class definition:
       public class SavingsAccount extends BankAccount
       {
          private double interestRate;
    
          public SavingsAccount(double rate)
          {
             interestRate = rate;
          }
    
          public void addInterest()
          {
             double interest;
    
             interest = getBalance() * interestRate/100;    
             deposit(interest);
          }
       }
    

  7. Implementing TimeDepositAccount:

    TimeDepositAccount class
       public class TimeDepositAccount extends SavingsAccount
       {
          private static double EARLY_WITHDRAWAL_PENALTY = 20.0;
          private int periodsToMaturity;
    
          public void withdraw(double amount)
          {
    	 super.withdraw(amount);
    
    	 if ( periodsToMaturity > 0 )
    	    super.withdraw(EARLY_WITHDRAWAL_PENALTY);
          }
    
          public void addInterest(double amount)
          {
    	 super.addInterest();
    	 periodsToMaturity--;
          }
       }