Skip to main content

L11

InsufficientFunds.java

public class InsufficientFunds extends RuntimeException{
    public InsufficientFunds(){

    }
    public InsufficientFunds(String msg){
        super(msg); // use superclass constructor
    }
}

BankAccount.java

public class BankAccount{
  private double balance;
  public BankAccount(){
    balance = 0;
  }
  public BankAccount(double initialBalance){
    balance = initialBalance;
  }
  public void deposit(double amount){
    balance = balance + amount;
  }
  public void withdraw(double amount){
    if(amount <= balance)
      balance = balance - amount;
    else{
      InsufficientFunds myEx = new InsufficientFunds("Amount Exceeds Balance");
      throw myEx;
    }
  }
  public double getBalance(){
    return balance;
  }
}

CheckingAccount.java

public class CheckingAccount extends BankAccount
{
  private int transactionCount;
  public CheckingAccount(){
    transactionCount = 0;
  }
  public CheckingAccount(double initialBalance){
    super(initialBalance); //use the super class constructor that's already coded
    transactionCount = 0;
  }
  public void deposit(double amount){
    super.deposit(amount);
    transactionCount = transactionCount + 1;
  }
  public void withdraw(double amount){
    super.withdraw(amount);
    transactionCount = transactionCount + 1;
  }
  public void deductFees(){
    if (transactionCount > 3){
      double fee = 2.0*(transactionCount - 3);
      super.withdraw(fee);
    }
    transactionCount = 0;
  }
}

BankAccountTest.java

import java.util.*;

public class BankAccountTest
{
  public static void main(String[] args)
  {
    Scanner myScanner = new Scanner(System.in);
    System.out.print("Enter initial checking account balance: ");
    double amount = Double.parseDouble(myScanner.nextLine());
    CheckingAccount myChecking = new CheckingAccount(amount);
    System.out.println("D - Deposit into checking");
    System.out.println("W - Withdraw from checking");
    System.out.println("P - End of month processing");
    System.out.println("S - Show account balance");
    System.out.println("E - Exit the program");
    boolean done = false;
    while (!done)
    {
      System.out.print("> ");
      String choice = myScanner.nextLine();
      if (choice.equalsIgnoreCase("D"))
      {
        System.out.print("Enter amount to deposit: ");
        amount = Double.parseDouble(myScanner.nextLine());
        myChecking.deposit(amount);
      }
      else if (choice.equalsIgnoreCase("W")) {
        try {
          System.out.print("Enter amount to withdraw: ");
          amount = Double.parseDouble(myScanner.nextLine());
          myChecking.withdraw(amount);
        } catch (InsufficientFunds myEx) {
            System.out.println(myEx.getMessage());
          System.out.println("Transaction Aborted!");
        }
      }
      else if (choice.equalsIgnoreCase("P"))
        myChecking.deductFees();
      else if (choice.equalsIgnoreCase("S"))
        System.out.println("Checking balance: " + myChecking.getBalance());
      else if (choice.equalsIgnoreCase("E"))
        done = true;
      else
        System.out.println("Invalid menu choice - please re-enter");
    }
    System.out.println("Goodbye!");
  }
}