//*******************************************************************
//
//   Accounts.java          In Text          Application
//
//   Authors:  Lewis and Loftus
//
//   Classes:  Accounts
//             Savings_Account
//             Bonus_Saver_Account
//
//*******************************************************************


//-------------------------------------------------------------------
//
//  Class Accounts contains the driver of a program that manages
//  two types of savings accounts.
//
//  Methods:
//
//     public static void main (String[] args)
//
//-------------------------------------------------------------------

class Accounts {

   //===========================================================
   //  Creates two accounts and performs various operations on
   //  them.
   //===========================================================
   public static void main (String[] args) {

      Savings_Account savings =
         new Savings_Account (4321, 8921.39, 0.02);
      Bonus_Saver_Account big_savings =
         new Bonus_Saver_Account (6543, 1225.00, 0.02);

      savings.add_interest();
      big_savings.add_interest();

      savings.deposit (390.23);
      big_savings.deposit (250.45);

      savings.withdrawal (432.34);
      big_savings.withdrawal (875.95);

   }  // method main

}  // class Accounts

//-------------------------------------------------------------------
//
//  Class Savings_Account represents a single savings account with
//  the ability to make deposits and withdrawals.  The account earns
//  interest.
//
//  Constructors:
//
//     public Savings_Account (int account_num, double initial_balance,
//                             double interest_rate)
//
//  Methods:
//
//     public void deposit (double amount)
//     public boolean withdrawal (double amount)
//     public void add_interest ()
//
//-------------------------------------------------------------------

class Savings_Account {

   protected int account;
   protected double balance;
   protected double rate;

   //===========================================================
   //  Sets up a savings account using the specified values.
   //===========================================================
   public Savings_Account (int account_num, double initial_balance,
                           double interest_rate) {

      account = account_num;
      balance = initial_balance;
      rate = interest_rate;

   }  // constructor Savings_Account

   //===========================================================
   //  Deposits the specified amount into the account.
   //===========================================================
   public void deposit (double amount) {

      balance += amount;
      System.out.println ("Deposit into account " + account);
      System.out.println ("Amount: " + amount);
      System.out.println ("New balance: " + balance);
      System.out.println ();

   }  // method deposit

   //===========================================================
   //  Withdraws the specified amount from the account,
   //  printing a message if the balance is not high enough.
   //===========================================================
   public boolean withdrawal (double amount) {

      boolean result = false;

      System.out.println ("Withdrawl from account " + account);
      System.out.println ("Amount: " + amount);

      if (amount > balance)
         System.out.println ("Insufficient funds.");
      else {
         balance -= amount;
         System.out.println ("New balance: " + balance);
         result = true;
      }
      System.out.println();

      return result;

   }  // method withdrawal

   //===========================================================
   //  Adds interest to the balance of the account.
   //===========================================================
   public void add_interest () {

      balance += balance * rate;
      System.out.println ("Interest added to account: " + account);
      System.out.println ("New balance: " + balance);
      System.out.println();

   }  // method add_interest

}  // class Savings_Account

//-------------------------------------------------------------------
//
//  Class Bonus_Saver_Account represents a special type of savings
//  account that earns extra interest, but has a penalty for
//  withdrawals.
//
//  Constructors:
//
//     public Bonus_Saver_Account (int account_num,
//               double initial_balance, double interest_rate)
//
//  Methods:
//
//     public boolean withdrawal (double amount)
//     public void add_interest ()
//
//-------------------------------------------------------------------

class Bonus_Saver_Account extends Savings_Account {

   private final int PENALTY = 25;
   private final double BONUS_RATE = 0.03;

   //===========================================================
   //  Sets up a bonus account using the specified values.
   //===========================================================
   public Bonus_Saver_Account (int account_num,
             double initial_balance, double interest_rate) {

      super (account_num, initial_balance, interest_rate);

   }  // constructor Super_Saver_Account

   //===========================================================
   //  Withdraws the specified amount, plus the penalty for
   //  withdrawing from a bonus account.  Overrides the
   //  withdrawal method of the Savings_Account class, but uses
   //  it to perform the actual withdrawal operation.
   //===========================================================
   public boolean withdrawal (double amount) {

      System.out.println ("Penalty incurred: " + PENALTY);
      return super.withdrawal (amount+PENALTY);

   }  // method withdrawal

   //===========================================================
   //  Adds interest to the balance of the account, including
   //  the bonus rate.  Overrides the add_interest method of
   //  the Savings_Account class.
   //===========================================================
   public void add_interest () {

      balance += balance * (rate + BONUS_RATE);
      System.out.println ("Interest added to account: " + account);
      System.out.println ("New balance: " + balance);
      System.out.println();

   }  // method add_interest

}  // class Bonus_Saver_Account


