AP Computer Science --- Haas --- InheritanceBankAccount

Complete the classes CheckingAccount and SavingsAccount which are both subclasses of the BankAccount class.



/** <<< THIS CLASS IS COMPLETE >>>
*   A bank account has a balance that can be changed by
*   deposits and withdrawals.
*/
public class BankAccount
{
   private double balance;

   /**
   * Constructs a bank account with a zero balance.
   */
   public BankAccount()   {
      balance = 0;
   }

   /**   
   * Constructs a bank account with a given balance.
   */
   public BankAccount(double initialBalance)  {
      balance = initialBalance;
   }

   /**
   *  Deposits money into the bank account.
   */
   public void deposit(double amount)  {
      balance = balance + amount;
   }

   /**
   *  Withdraws money from the bank account.
   */
   public void withdraw(double amount)   {
      balance = balance - amount;
   }

   /**
   *  Gets the current balance of the bank account.
   */
   public double getBalance()   {
      return balance;
   }

   /**
   *   Transfers money from the bank account to another account
   */
   public void transfer(double amount, BankAccount other)  {
      withdraw(amount);
      other.deposit(amount);
   }
}


/** 
*       <<< THIS CODE IS NOT COMPLETE >>>
*  CheckingAccount is a Subclass of BankAccount: 
*  All methods are inherited.
*  Private instance fields must be accesed using getters
*  and setters.
*  
*  Note: methods can be overriden, but instance fields can NOT.
*  
*  COMPLETE METHODS: deposit, withdraw, deductFees
*/
public class CheckingAccount extends BankAccount
{
   /**
   * keeps track of the number of account transactions made
   */
   private int transactionCount = 0; 
   
   /**
   * The bank allows 3 free transactions per period,
   * 2 dollars is charged per transaction after
   */
   private static final int FREE_TRANSACTIONS = 3;
   private static final double TRANSACTION_FEE = 2.0;

   /**
   *  Constructs a checking account with a given balance.
   */
   public CheckingAccount(double initialBalance)
   {
      // call to superclass constructor
      // if used must be first statement in constructor
      super(initialBalance);

     // Initialize transaction count
      transactionCount = 0;
   }

   /**
    * <<< COMPLETE THE METHOD BELOW >>>
    * overrides deposit in superclass
    * increment the number of transactions 
    * and deposite amount into account
    */
   public void deposit(double amount)  {

      // <<< Code Not Complete >>>   
      
    
   }

   /**
    * <<< COMPLETE THE METHOD BELOW >>>
    * overrides withdraw in superclass
    * increment the number of transactions 
    * and withdraw amount from account
    */
    public void withdraw(double amount)  {
        
        // <<< Code Not Complete >>>
  

   }

  /**
   * <<< COMPLETE THE METHOD BELOW >>>
   * resets transaction count to 0, and deducts 2 dollars 
   * for each transaction exceeding number of 
   * FREE_TRANSACTIONS from the bank account
   */
   public void deductFees()
   {
       //  <<< Code Not Complete >>>
   

   }
}


/**     
*       <<< THIS CODE IS NOT COMPLETE >>>
*   An account that earns interest at a fixed rate.
*/
public class SavingsAccount extends BankAccount
{
   private double interestRate;
   public static final double MINIMUM_BALANCE = 10;
   
   /**
   *   Constructs a bank account with a given interest rate.
   */
   public SavingsAccount(double rate)  {
       super();
       interestRate = rate;
   }

   /**   <<< COMPLETE THIS METHOD >>>
    *   Constructs a bank account with a given balance
    *   and a given interest rate.
    */
   public SavingsAccount(double initialBalance, double rate) {
       
       // <<<  CODE NOT COMPLETE >>>
       
      

   }
   
   
   /**  <<< COMPLETE THIS METHOD >>>
   *   Adds the earned interest to the account balance.
   */
   public void addInterest()   {
       // <<< CODE NOT COMPLETE >>>
     
  
   }
   
   /** <<< COMPLETE THIS METHOD>>>
    *  overrides withdraw in the superclass
    *  only allows withdrawel to occur if 
    *  resulting balance > MINIMUM_BALANCE
    */
    public void withdraw(double amount)  {
        
      // <<< Code Not Complete >>>
      

   }

   
}


/**     <<< THIS CODE IS COMPLETE >>>
*  This program tests the BankAccount class and its subclasses.
*/
 public class BankAccountTester
{
   public static void main(String[] args)
   {
      CheckingAccount joesAccount = new CheckingAccount(1000);
      joesAccount.withdraw(300);
      joesAccount.deposit(500);
      joesAccount.withdraw(100);
      joesAccount.withdraw(200);
      joesAccount.deposit(300);
      joesAccount.deductFees();
      // Balance should be $1196
      System.out.println("Joe's balance = $" + joesAccount.getBalance());
      
      SavingsAccount janesAccount = new SavingsAccount(2000,4.2);
      janesAccount.withdraw(1000);
      // Balance should be $1000
      System.out.println("Janes's balance = $" + janesAccount.getBalance());
      janesAccount.withdraw(1000); // withdrawel not allowed
      // Balance should be $1000
      System.out.println("Janes's balance = $" + janesAccount.getBalance());
      janesAccount.withdraw(995); // withdrawel not allowed
      // Balance should be $1000
      System.out.println("Janes's balance = $" + janesAccount.getBalance());
      janesAccount.addInterest();
      // Balance should be $1042
      System.out.println("Janes's balance = $" + janesAccount.getBalance());

      
   }
}