Exercise: Creating Your Own Exception (Level 3)

In this exercise, you will create an OverdraftException that is thrown by the withdraw method in the Account class.

Task 1 - Creating the OverdraftException Class

Using a text editor, create the OverdraftException class source file in the src/com/mybank/domain/ directory. This class must satisfy the UML diagram in Figure 8-1 on page Lab 8-2.

  1. Create a public class, called OverdraftException, in the com.mybank.domain package. This class extends the Exception class.

    package com.mybank.domain;
    public class OverdraftException extends Exception {
      // insert code here
    }

  2. Add a private attribute called deficit that holds a double.

    private final double deficit;

  3. Add a public constructor that takes two arguments: message and deficit. The message parameter initializes the deficit attribute.

    public OverdraftException(String msg, double deficit) {
      super(msg);
      this.deficit = deficit;
    }

  4. Add a public accesser called getDeficit.

    public double getDeficit() {
      return deficit;
    }

Task 2 - Modifying the Account Class

Using a text editor, modify the Account class source file in the src/com/mybank/domain/ directory. This class must satisfy the UML diagram in Figure 8-1 on page Lab 8-2. In particular, the deposit and withdraw methods should not return a Boolean flag.

  1. Modify the deposit method so that it does not return a value (that is, void). This operation should never fail, so it does not need to throw any exceptions.

    public void deposit(double amt) {
      balance = balance + amt;
    }

  2. Modify the withdraw method so that it does not return a value (that is, void). Declare that this method throws the OverdraftException.
    Modify the code to throw a new exception that specifies Insufficient funds and the deficit (the amount requested subtracted by the current balance).

    public void withdraw(double amt) throws OverdraftException {
      if ( amt <= balance ) {
        balance = balance - amt;
      } else {
        throw new OverdraftException("Insufficient funds", amt - balance);
      }
    }

Task 3 - Modifying the CheckingAccount Class

Using a text editor, modify the CheckingAccount class source file in the src/com/mybank/domain/ directory. This class must satisfy the UML diagram in Figure 8-1 on page Lab 8-2.

Modify the withdraw method so that it does not return a value (that is, void). Declare that this method throws the OverdraftException. Modify the code to throw an exception when the OverdraftProtection amount is not sufficient to cover the deficit; use the message Insufficient funds for overdraft protection for this exception.

public void withdraw(double amount) throws OverdraftException {
  if ( balance < amount ) {
    double overdraftNeeded = amount - balance;
    if ( overdraftAmount < overdraftNeeded ) {
        throw new OverdraftException("Insufficient funds for overdraft protection", overdraftNeeded);
    } else {
        balance = 0.0;
        overdraftAmount -= overdraftNeeded;
    }
  } else {
    balance = balance - amount;
  }
}

Task 4 - Copying the TestBanking Class

Copy the TestBanking.java file from the resources/mod08_except/ directory into the src/com/mybank/test/ directory.

cp ~/resources/mod08_except/TestBanking.java
      src/com/mybank/test/

Task 5 - Compiling the TestBanking Program

On the command line, use the javac command to compile the test program.

cd src
javac -d ../classes com/mybank/test/TestBanking.java

Task 6 - Running the TestBanking Program

On the command line, use the java command to run the test program.

java -cp ../classes com.mybank.test.TestBanking

You should see the following output:

Customer [Simms, Jane] has a checking balance of 200.0 with a 500.00 overdraft protection.
Checking Acct [Jane Simms] : withdraw 150.00
Checking Acct [Jane Simms] : deposit 22.50
Checking Acct [Jane Simms] : withdraw 147.62
Checking Acct [Jane Simms] : withdraw 470.00
Exception: Insufficient funds for overdraft protection   Deficit: 470.0
Customer [Simms, Jane] has a checking balance of 0.0

Customer [Bryant, Owen] has a savings balance of 200.0
Savings Acct [Owen Bryant] : withdraw 100.00
Savings Acct [Owen Bryant] : deposit 25.00
Savings Acct [Owen Bryant] : withdraw 175.00
Exception: Insufficient funds   Deficit: 50.0
Customer [Bryant, Owen] has a savings balance of 125.0