lab 12 9 1

background image

1 - 5

Fundamentals of Java Programming Lab 12.9.1

Copyright

 2003, Cisco Systems, Inc.

Lab 12.9.1 Exceptions for the JBANK Application


Estimated Time: 60 minutes

Learning Objectives:

• In this lab activity, the student will create user-defined exceptions based on the business

rules for the JBANK application, throw, and test the exceptions using a try-catch block.


Description:

• Dealing with Exceptions

• Defining and using custom Exceptions

• Create custom Exceptions by extending the core Java Exception class
• Use try-catch blocks to handle the exceptions


Business rules:

• Customer cannot have more than one of each type of Account.

• To deposit or withdraw money from a particular account type, the corresponding account

type should exist for the customer.

• A customer is not permitted to withdraw money from an account in an amount more than

the balance or credit limit.


File Management

Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New
Project window and in the Look in: list box select c:\. Now, double click the javacourse
folder listed in the text window and a different New Project window opens with javacourse
in the Look in: list box. Now, double click the chap12 folder listed in the text window and a
different New Project window opens with chap12 in the Look in: list box. Type lab12.9.1
in the File name text box and click on Create to create a lab12.9.1 subfolder in the
chap12 folder. Import the JBANK classes.


Tasks:

Step 1 Defining your own Exception classes

a. Create a class named AmountOverDrawnException that extends from the Java core

Exception class.

b. Define an attribute that holds the account type using char acctype.
c. Define a constructor that accepts a char for the account type.

AmountOverDrawnException(char acctype). In the constructor code pass a
String message to the parent constructor. The call to super(“Insufficient
funds”); will assign this String to the private message attribute of the Exception.
The message attribute is a String to hold any message designed by the programmer. As
this is a private method, a getMessage() method is provided in the parent class.
Also assign the argument from the constructor to the acctype attribute of the Exception

class.

d. Override the inherited getMessage() method. This method is defined in the parent

class Throwable. The getMessage() method returns a String. As this Exception will be
used by all of the Account sub classes, implement the logic using a switch statement.
All instances of this class will hold an account type. Use this attribute to test its value,

and then concatenate the message of the Exception object with messages specific to
each account. An example for one of the tests is provided:
S- "in Savings Account"
I - "in Investment Account"

background image

2 - 5

Fundamentals of Java Programming Lab 12.9.1

Copyright

 2003, Cisco Systems, Inc.

L- "in Line-Of-Credit Account"
O- "in Overdraft Protect Account"

Sample code:

switch (acctype){

case 'S' :

return super.getMessage() + "in Savings Account";

case ‘L’:

// rest of switch statements.

// Do not for get the default statement.

Step 2 More user-defined Exception classes

a. Create

AccountTypeAlreadyExistsException class extending from Exception. Define an

attribute of type char to hold the account type called acctype. Implement a constructor
that accepts a char for the account type, AccountTypeAlreadyExistsException
(char acctype). In the constructor code pass a message String to the parent
constructor. The call to super(" account exists. Unable to create a
duplicate account ") will assign this String to the private message attribute of
the Exception.

b. Override the inherited getMessage() method. As this Exception will be used by all of

the Account sub classes, include logic using a switch . All instances of this class will
hold an account type. Use this attribute to test its value, and then concatenate the
message of the exception object with messages specific to each account. An example
for one of the tests is provided:

switch (acctype){
case

'S':

return "Savings" + super.getMessage();

case

'I':

return "Investment" + super.getMessage();

// rest of switch code. Do not forget the default statement.

c. Create

AccountTypeNotFoundException class extending from Exception. Implement a

constructor that accepts a char for the account type,

AccountTypeNotFoundException (char acctype)
In the constructor code pass a message string to the parent constructor. The call to
super(" account does not exist ") will assign this string to the private

message attribute of your exception.

d. Override the inherited getMessage() method. As this Exception will be used by all of

the Account sub classes, include logic using a switch. All instances of this class will
hold an account type. Use this attribute to test its value, and then concatenate the
message of the exception object with messages specific to each account. An example
for one of the tests is provided:

switch (acctype){
case

'S':

return "Savings" + super.getMessage();

case

'I':

return "Investment" + super.getMessage();

// rest of switch code. Do not forget the default statement.

background image

3 - 5

Fundamentals of Java Programming Lab 12.9.1

Copyright

 2003, Cisco Systems, Inc.


Step 3 Throwing AmountOverDrawnException in classes that implement the withdraw()
method

a. In the Account class use the throws keyword to show that withdraw() method throws

the AmountOverDrawnException.

Code sample:

public abstract double withdraw(double amt) throws

AmountOverDrawnException


The abstract withdraw() method is implemented in the concrete classes (Savings,

OverdraftProtect, LineOfCredit, Investment). Make sure that the withdraw method of
these classes is modified to include throws AmountOverDrawnException and throw
an instance of the AmountOverDrawnException. For example in the withdraw()
method of the Savings class, replace the statement return false; with the statement

throw new AmountOverDrawnException (getAcctType());

Note: The constructor for the AmountOverDrawnException requires a char representing
the account type (S, I, L, O).

Repeat this for each of the concrete classes that implement the withdraw method.

Step 4 Throwing AccountTypeAlreadyExistsException and
AccountTypeNotFoundException in classes that create and use account objects

a. In the Customer class modify the addAccount() method. This method throws an

AccountTypeAlreadyExistsException if the account type of the account to be created
already exits and an AccountTypeNotfoundException if an OverdraftProtect account is
created without a matching savings account existing for the customer. The value passed
to the constructor is the char ‘S’. (Note: Business rule states that the Overdraft protect
account will cover withdrawals in excess of the balance in the account if there is money in
the savings account. This requires that the overdraft protection account be associated
with the savings account). This method also throws an AccountTypeNotfoundException if
none of the values provided for account type are correct. In this case the
AccountTypeNotFoundException constructor is passed the value of the addAccount()

method of argument ‘type’.


b. Modify

the

getAccount() method to throw AccountTypeNotFoundException if a

particular account type does not exist. Remember to declare the throws
AccountTypeNotFoundException as part of the method signature.

Step 5 Handling Exceptions

a. In the ATMButtonHandlerclass use try and catch blocks to handle the exceptions

thrown by withdraw() method, addAccount(), and getAccount() methods. In the
catch block of each try-catch block, use the getMessage() method of the
Exception object to display the exception message in the TextArea of the ATMGUI. Test
and run the program. Use test data that will cause the exceptions to be thrown.

b. Revise

the

catch block from the previous step to print the stacktrace.

background image

4 - 5

Fundamentals of Java Programming Lab 12.9.1

Copyright

 2003, Cisco Systems, Inc.

Step 6 Review Questions

a. What blocks are used for exception handling?


b. What statement is used to throw an exception?






Step 7 Documentation

Using the Document “How to use UMLTestTool”, follow the instructions to verify your
JBANK classes match the JBANK UML diagram shown below.

Write javadoc comments to the classes introduced in this lab.


background image

5 - 5

Fundamentals of Java Programming Lab 12.9.1

Copyright

 2003, Cisco Systems, Inc.


Document Outline


Wyszukiwarka

Podobne podstrony:
lab 8 9 12
CHEMIA 12, Chemia fizyczna AGH laborki, lab 12
lab 12
IE RS lab 12 solutions
SPRAWOZ4, Chemia fizyczna AGH laborki, lab 12
Lab 12
CHEMIA 12, Chemia fizyczna AGH laborki, lab 12
lab.12, Cel ˙wiczenia:
lab.12, Cel ˙wiczenia:
PW LAB 12
lab 9 12 1
Lab 9 12, Studia - Politechnika Opolska, Semestr 2, Informatyka
Lab 7 12, Studia - Politechnika Opolska, Semestr 2, Informatyka
HYDRA LAB 12, sgsp, Hydromechanika, HYDROMECHANIKA 1, CI GI
Lab 12
Lab 12 13 2007 2008
Harm cwicz lab 12 BM EN
Biohydrometalurgia instrukcje2012, Lab 9 12
ALUMINIUM I STOPY ALUMINIUM - Lab 12, Studia, Materiałoznastwo, Metaloznastwo i Podstawy Obrobki Cie

więcej podobnych podstron