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.
-
Create a public class, called OverdraftException, in
the com.mybank.domain package. This class extends the
Exception class.
-
Add a private attribute called deficit that holds a
double.
-
Add a public constructor that takes two arguments: message and deficit. The message parameter initializes the
deficit attribute.
-
Add a public accesser called getDeficit.
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.
-
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.
-
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).
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.
Task 4 - Copying the
TestBanking Class
Copy the TestBanking.java file from the
resources/mod08_except/ directory into the
src/com/mybank/test/ directory.
Task 5 - Compiling the
TestBanking Program
On the command line, use the javac command to compile the
test program.
Task 6 - Running the
TestBanking Program
On the command line, use the java command to run the test
program. 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