In this exercise, you will create an
OverdraftException that is thrown by the
withdraw method in the
Account class. In the previous design, the
deposit and
withdraw methods return a Boolean flag to indicate
whether the operation was successful or not. This design has several flaws, one of which is that a false return value
does not give the calling client any indication of why the operation was not successful. In the new design, you will
use exceptions to indicate operation failure.
Figure 8-1 shows the UML class diagram of this new design. In UML parlance, the
<<send>>
dependency indicates that the
method on the source operation (the withdraw method) may
throw an OverdraftException.
Preparation
Before you begin, make sure that you have changed directories to
projects/BankingPrj/ using the cd command in the terminal
window.
cd ~/ projects/ BankingPrj/
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.
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.
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.
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