• No results found

Tutorial 26 –

N/A
N/A
Protected

Academic year: 2021

Share "Tutorial 26 –"

Copied!
98
0
0

Loading.... (view fulltext now)

Full text

(1)

Outline

26.1 IBM Cloudscape Database

26.2 Test-Driving the ATM Application 26.3 Planning the ATM Application

26.4 Relational Database Overview: The ATM Database 26.5 SQL

26.6 Using Command-Line Arguments 26.7 Creating Database Connections

26.8 Programming the Screen Saver Application 26.9 Wrap-Up

Tutorial 26 – ATM Application

Introducing Database Programming and Using

Command-Line Arguments

(2)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Objectives

• In this tutorial, you will learn to:

– Install the Cloudscape database.

– Connect to databases.

– Create SQL queries.

– Retrieve and update information in databases.

– Use command-line arguments to pass options to an

application as it begins executing.

(3)

26.1 IBM Cloudscape Database

• Cloudscape

– Pure-Java embedded database management system.

• Installing Cloudscape

– Type

java –jar

D:\software\Cloudscape513\cloudscape_eval_513.jar

to begin the installation process

(4)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.1 IBM Cloudscape Database (Cont.)

Figure 26.1 Welcome page of Cloudscape installer. (Courtesy of IBM Corporation.)

(5)

26.1 IBM Cloudscape Database (Cont.)

Figure 26.2 Options to view the release notes. (Courtesy of IBM Corporation.)

(6)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.1 IBM Cloudscape Database (Cont.)

Figure 26.3 Cloudscape 5.1 release notes. (Courtesy of IBM Corporation.)

(7)

26.1 IBM Cloudscape Database (Cont.)

Figure 26.4 Accepting the Cloudscape license agreement. (Courtesy of IBM Corporation.)

(8)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.1 IBM Cloudscape Database (Cont.)

Figure 26.5 Choosing the Cloudscape installation directory.

(Courtesy of IBM Corporation.)

(9)

26.1 IBM Cloudscape Database (Cont.)

Figure 26.6 Choosing the default setup type. (Courtesy of IBM Corporation.)

(10)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.1 IBM Cloudscape Database (Cont.)

Figure 26.7 Confirming the location of the Cloudscape installation.

(Courtesy of IBM Corporation.)

(11)

26.1 IBM Cloudscape Database (Cont.)

Figure 26.8 Cloudscape installation process dialog. (Courtesy of IBM Corporation.)

(12)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.1 IBM Cloudscape Database (Cont.)

Figure 26.9 Completing the Cloudscape installation. (Courtesy of IBM Corporation.)

(13)

26.1 IBM Cloudscape Database (Cont.)

Figure 26.10 Exiting the installer wizard. (Courtesy of IBM Corporation.)

(14)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application

Application Requirements

A local bank has asked you to create a prototype automated teller machine (ATM) application to access a database that contains sample customer records. Each record consists of an account number, Personal Identification Number (PIN), first name and balance amount. For testing purposes, valid account numbers will be provided in a JComboBox. The ATM application should allow the user to log in to an account by providing a valid PIN. Once logged in, the user should be able to view the account balance and withdraw money from the account (if the account contains sufficient funds). If money is withdrawn, the application should update the database.

(15)

26.2 Test-Driving the ATM Application (Cont.)

• Setting the CLASSPATH environment variable

– Set this variable so Java can interact with Cloudscape

• Running the ATM application

– Type java ATM com.ibm.db2j.jdbc.DB2jDriver jdbc:db2j:ATM in the Command Prompt window

• Command-line arguments

(16)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.11 Setting the CLASSPATH environment variable.

Result of running setCP.bat

(17)

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.12 Passing command-line arguments to the ATM application.

(18)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.13 ATM application.

Displays instructions and messages to the user

Keypad for entering a PIN

or a withdrawal amount All JButtons are

disabled initially

JComboBox that displays account numbers

(19)

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.14 Selecting an account number from the JComboBox.

Prompt the user to provide a PIN

Keypad JButtons are enabled

JComboBox is disabled

Done JButton is enabled

(20)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.15 Entering the PIN for the selected account.

Enter JButton is enabled An asterisk is displayed here

for each keypad JButton

pressed for the PIN

(21)

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.16 ATM displaying welcome message.

Enter JButton is disabled

Balance JButton is enabled

WithdrawJButton is enabled Keypad JButtons

are disabled Welcome message displays in JTextArea when the user enters the correct PIN

(22)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application (Cont.)

• Confirming the use of the database

– Click the Balance JButton to check the account balance – Withdrawing money

• Click the

Withdrawal JButton

• Input an amount to withdraw and click the

Enter JButton

– Click the Balance JButton again

• Notice that the balance reflects the withdrawal you performed

(23)

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.17 Viewing the account balance.

Balance displays in JTextArea

(24)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.18 Withdrawing money from the account.

Balance JButton is disabled

WithdrawJButton is disabled Keypad JButtons

are enabled Ask the user to enter a withdrawal amount

(25)

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.19 ATM application displaying the withdrawal amount.

Keypad JButtons are disabled

Balance JButton is enabled

WithdrawJButton is enabled Display the

withdrawal amount

(26)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.20 Checking new balance.

Display new balance

(27)

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.21 ATM application ready for next customer.

JComboBox is enabled

JButtons are disabled Displays instructions

and messages to the user

Keypad JButtons are disabled

(28)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application

When the user selects an account number from the JComboBox Disable the JComboBox

Prompt the user to enter a PIN Clear the JTextField for the PIN Enable the keypad Jbuttons Enable the Done JButton When the user enters the PIN

Enable the Enter Jbutton

Append the number to the PIN

(29)

26.3 Planning the ATM Application (Cont.)

When the user clicks the Enter JButton to submit the PIN

Search the database for the account number’s corresponding account information If the user provided a correct PIN

Clear the JTextField Disable the Enter Jbutton Disable the keypad Jbuttons

Enable the Balance and Withdraw Jbuttons Display the status to the user

Else

Clear the JTextField

Prompt the user to enter a valid PIN When the user clicks the Balance Jbutton

Display the balance

(30)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

When the user clicks the Withdraw Jbutton

Disable the Balance and Withdraw Jbuttons Enable the keypad Jbuttons

Prompt the user to enter the withdrawal amount

When the user clicks the Enter JButton to submit the withdrawal amount Disable the Enter Jbutton

Disable the keypad Jbuttons

Process the withdrawal and display the withdrawal amount Clear the withdrawal amount in the JTextField

Enable the Balance and Withdraw JButtons When the user clicks the Done Jbutton

Disable the keypad Jbuttons

Disable the Enter, Balance, Withdraw and Done Jbuttons Enable the JComboBox

Display instructions for the next customer in the JTextArea

(31)

26.3 Planning the ATM Application (Cont.)

Action Component Event

User selects an account

number from the JComboBox

accountNumberJComboBox User selects item from

Account Numbers:

JComboBox

Disable the JComboBox accountNumberJComboBox

Prompt user to enter a PIN messageJTextArea

Clear the JTextField for the PIN

numberJTextField

Enable the keypad JButtons zeroJButton, oneJButton,

twoJButton,

threeJButton, fourJButton,

fiveJButton, sixJButton,

sevenJButton,

eightJButton,

nineJButton

Enable the Done JButton doneJButton

(32)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method

User enters the PIN zeroJButton,

oneJButton,

twoJButton,

threeJButton,

fourJButton,

fiveJButton,

sixJButton,

sevenJButton,

eightJButton,

nineJButton

User clicks a keypad

JButton

Enable the Enter JButton enterJButton

Append the number to the PIN numberJTextField

User clicks the Enter JButton to submit the PIN

enterJButton User clicksEnter

JButton

Search database for the account number’s corresponding

account information

myStatement,

myResultSet executeQuery, close,

next, getString,

getDouble

User provided a correct PIN

Figure 26.22 ACE table for the ATM application (Part 2 of 6).

(33)

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method

Clear the JTextField numberJTextField

Disable the Enter JButton enterJButton

Disable the keypad JButtons zeroJButton,

oneJButton,

twoJButton,

threeJButton,

fourJButton,

fiveJButton,

sixJButton,

sevenJButton,

eightJButton,

nineJButton

Enable the Balance and Withdraw

JButtons balanceJButton,

withdrawJButton

Display status to the user messageJTextArea

User did not provide a correct PIN

(34)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method

Prompt the user to enter a valid PIN

messageJTextArea

User clicks the Balance JButton balanceJButton User clicksBalance

JButton

Display the balance messageJTextArea

User clicks the Withdraw JButton

withdrawJButton User clicksWithdraw JButton

Disable the Balance and

Withdraw JButtons balanceJButton,

withdrawJButton

Enable the keypad JButtons zeroJButton,

oneJButton,

twoJButton,

threeJButton,

fourJButton,

fiveJButton,

sixJButton,

sevenJButton,

eightJButton,

nineJButton

Prompt the user to enter the withdrawal amount

messageJTextArea

Figure 26.22 ACE table for the ATM application (Part 4 of 6).

(35)

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method

User clicks the Enter JButton to submit the withdrawal amount

enterJButton User clicksEnter

JButton

Disable the Enter JButton enterJButton

Disable the keypad JButtons zeroJButton,

oneJButton,

twoJButton,

threeJButton,

fourJButton,

fiveJButton,

sixJButton,

sevenJButton,

eightJButton,

nineJButton

Process the withdrawal and

display the withdrawal amount myStatement,

messageJTextArea

Clear withdrawal amount in the JTextField

numberJTextField

Enable the Balance and Withdraw

JButtons balanceJButton,

withdrawJButton

User clicks the Done JButton doneJButton User clicksDone

(36)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method

Disable the keypad JButtons zeroJButton,

oneJButton,

twoJButton,

threeJButton,

fourJButton,

fiveJButton,

sixJButton,

sevenJButton,

eightJButton,

nineJButton

Disable the Enter, Balance,

Withdraw and Done JButtons enterJButton,

balanceJButton,

withdrawJButton,

doneJButton

Enable the JComboBox accountNumber- JComboBox

Display instructions for the next customer in the JTextArea

messageJTextArea

Figure 26.22 ACE table for the ATM application (Part 6 of 6).

(37)

Database

• Database

– Organized collection of data

– Database management system (DBMS)

• enables applications to access and store data without worrying

about how the data is organized

(38)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Database (Cont.)

• Relational database

– Stores data in tables

• Tables store data in rows and columns

– Primary key - field that contains unique values used to distinguish records from one another

• Structured Query Language – Perform queries

– Manipulate data

(39)

Database (Cont.)

accountNumber pin firstName balanceAmount

12548693 1234 John 980.0

24578648 8568 Susan 125.0

35682458 5689 Joseph 3400.99

45632598 8790 Michael 1254.76

52489635 2940 Donna 9200.02

55698632 3457 Elizabeth 788.9

69857425 6765 Jennifer 677.87

71869534 5678 Al 7799.24

88965723 1245 Ben 736.78

98657425 2456 Bob 946.09

Figure 26.23 accountInformation table of the ATM database.

(40)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.5 SQL

• Basic SELECT Query

– Selects rows and columns from a table – Performed by SELECT

• Basic form:

SELECT * FROM TableName

– *

indicates all columns from

TableName

will be selected

– *

can be replaced by comma-separated list of columns to retrieve

• e.g.

SELECT accountNumber FROM accountInformation

– FROM

indicates the table from which to retrieve data

(41)

26.5 SQL (Cont.)

accountNumber firstName

12548693 John

24578648 Susan

35682458 Joseph

45632598 Michael

52489635 Donna

55698632 Elizabeth

69857425 Jennifer

71869534 Al

88965723 Ben

98657425 Bob

Figure 26.24 Selecting the accountNumber and firstName columns of the accountInformation table.

SELECT accountNumber, firstName FROM accountInformation

(42)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.5 SQL (Cont.)

• WHERE Clause

– Specify the selection criteria for a query

• Only rows that satisfy the criteria will be selected

– Combined with a SELECT statement – General form:

• SELECT

columnName1, columnName1, … FROM TableName WHERE criteria

– Example

• SELECT pin, firstName, balanceAmount FROM accountInformation

WHERE accountNumber = ’12548693’

(43)

26.5 SQL (Cont.)

pin firstName balanceAmount

1234 John 980.0

Figure 26.25 Selecting the pin, firstName and balanceAmount for the person with accountNumber 12548693

(44)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.5 SQL (Cont.)

• UPDATE Statement

– Modify data in a table – Basic form:

• UPDATE TableName

SET columnName1 = value1, columnName2 = value2 … WHERE criteria

• TableName specifies table to update

• SET

keyword is followed by list of column name/value pairs

• WHERE

clause determines which rows to update

– Example

• UPDATE accountInformation SET balanceAmount = 1000

WHERE accountNumber = ‘12548693

(45)

26.5 SQL (Cont.)

accountNumber pin firstName balanceAmount

12548693 1234 John 1000.0

24578648 8568 Susan 125.0

35682458 5689 Joseph 3400.99

45632598 8790 Michael 1254.76

52489635 2940 Donna 9200.02

55698632 3457 Elizabeth 788.9

69857425 6765 Jennifer 677.87

71869534 5678 Al 7799.24

88965723 1245 Ben 736.78

98657425 2456 Bob 946.09

Figure 26.26 accountInformation table after executing an UPDATE statement.

(46)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.6 Using Command-Line Arguments

• main method

– Command line arguments

• Passed to

main

as

String

array

args

• Length of

args

• Standard output object

– System.out

– Display text in the Command Prompt window

(47)

26.6 Using Command-Line Arguments (Cont.)

Figure 26.27 Viewing the main method.

Display a line of text that indicates the syntax to run the application Get command-line arguments

Pass command-line arguments to the constructor

Check the number of command-line arguments

Start the main method declaration

(48)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.7 Creating Database Connections

• JDBC API

– Communicate and manipulate databases

• JDBC Driver

– Provided by a DMNS vendor

– Enable Java applications to access a particular database

(49)

26.7 Creating Database Connections (Cont.)

Importing the

java.sql package

Figure 26.28 Importing the java.sql package .

• java.sql package

– Perform database processing

(50)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.7 Creating Database Connections (Cont.)

• Connection object

– Manages connection between Java application and database – Allows applications to create SQL statements

• Statement object

– Enables applications to execute SQL statements

• ResultSet object

– Returned by executing a query

– Contain rows and columns selected

– Rows of table returned in sequence

– One ResultSet per Statement

(51)

26.7 Creating Database Connections (Cont.)

Figure 26.29 Declaring instance variables for database processing.

Declaring instance variables for managing the database connection

(52)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.7 Creating Database Connections (Cont.)

• Connecting to a database

– Load database driver

• forName

method of class

Class

• DriverManager

class

– Manages JDBC drivers and establishes connections to databases

– getConnection

method connects to database – JDBC URL

• Form: protocol:subprotocol:subname

• Specifies protocol and subprotocol for communication and name of database

• Protocols and subprotocols define how data is

transferred between a Java application and a database

(53)

26.7 Creating Database Connections (Cont.)

Figure 26.30 Adding a database connection.

Load database driver class (com.ibm.db2j.jdbc.JB2j

Driver) Connect to ATM database

(jdbc:db2j:ATM) Create a Statement object Catch any SQLExceptions thrown from lines 86-87 or line 90

Catch

ClassNotFoundException

(54)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.8 Programming the ATM Application

• executeQuery method of Statement

– Takes a String representing a SQL query – Returns a ResultSet

• ResultSet

– ResultSet

cursor

• Points to a row in a ResultSet

• Needs to be positioned to the first row before processing data – next

method

• Returns true if cursor can be positioned in next row

• Returns false if no more rows

– Extract data from

ResultSet

as a specific Java type

• Methods getString, getInt and getDouble

– Return the data as a String, int or double, respectively.

• close method closes the ResultSet and releases its resources

(55)

26.8 Programming the ATM Application (Cont.)

Figure 26.31 Filling the accountNumberJComboBox with account numbers.

Submit a query that selects the account numbers from table accountInformation

Process the ResultSet and fill the

accountNumberJComboBox

with account numbers Close myResultSet to release database resources Catch any SQLExceptions

thrown from the try block

(56)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.8 Programming the ATM Application (Cont.)

Figure 26.32 Retrieving account information from the database.

Submit a query that selects the pin, firstName and

balanceAmount values for the specified account number

Get the pin, firstName and

balanceAmount values from the ResultSet

Close myResultSet to release database resources Catch any SQLExceptions

thrown from the try block

(57)

26.8 Programming the ATM Application (Cont.)

• executeUpdate method of Statement

– Submits a SQL statement that updates a database – Takes a String indicating SQL to execute

– Returns an int specifying how many rows were updated

(58)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.8 Programming the ATM Application (Cont.)

Figure 26.33 Updating the balanceAmount column.

Submit a SQL statement that updates the balanceAmount in table accountInformation

for the row with the specified

accountNumber

Catch any SQLExceptions thrown from the try block

(59)

26.8 Programming the ATM Application (Cont.)

• Closing database connections

– close method of Statement

• Closes

Statement

object – Releases resources

– Prevents further SQL from being executed – close method of Connection

• Closes

Connection

object

– Terminates connection between application and database

(60)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

26.8 Programming the ATM Application (Cont.)

Figure 26.34 Closing the database connection.

Close Statement and

Connection to release database resources

Terminate the application Catch any SQLExceptions thrown from the try block

(61)

26.8 Programming the ATM Application (Cont.)

Figure 26.35 Running the completed ATM application.

(62)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (1 of 37)

3 // view the balance and withdraw money from the account.

4 import java.awt.*;

5 import java.awt.event.*;

6 import java.sql.*;

7 import java.text.*;

8 import javax.swing.*;

9 import javax.swing.event.*;

10

11 public class ATM extends JFrame 12 {

13 // JTextArea to display message 14 private JTextArea messageJTextArea;

15

16 // JTextField to enter PIN or withdrawal amount 17 private JTextField numberJTextField;

18

19 // JPanel for number JButtons 20 private JPanel buttonsJPanel;

21

22 // JButtons for input of PIN or withdrawal amount 23 private JButton oneJButton;

24 private JButton twoJButton;

25 private JButton threeJButton;

Importing

java.sql package

(63)

ATM.java (2 of 37)

28 private JButton sixJButton;

29 private JButton sevenJButton;

30 private JButton eightJButton;

31 private JButton nineJButton;

32 private JButton zeroJButton;

33

34 // JButton to submit PIN or withdrawal amount 35 private JButton enterJButton;

36

37 // JButton to view balance

38 private JButton balanceJButton;

39

40 // JButton to withdraw from account 41 private JButton withdrawJButton;

42

43 // JButton to close the transaction 44 private JButton doneJButton;

45

46 // JPanel to get account numbers 47 private JPanel accountNumberJPanel;

48

(64)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (3 of 37)

51 private JComboBox accountNumberJComboBox;

52

53 // constants for user action

54 private final static int ENTER_PIN = 1;

55 private final static int WITHDRAWAL = 2;

56

57 // instance variables used to store PIN and 58 // firstName from database

59 private String pin, firstName;

60

61 // instance variable used to distinguish user action 62 private int action;

63

64 // instance variables used to store user selected account number 65 // and PIN

66 private String userAccountNumber, userPIN;

67

68 // instance variable used to store account balance 69 private double balance;

70

(65)

ATM.java (4 of 37)

73 private Statement myStatement;

74 private ResultSet myResultSet;

75

76 // constructor

77 public ATM( String databaseDriver, String databaseURL ) 78 {

79 // establish connection to database 80 try

81 {

82 // load Cloudscape driver 83 Class.forName( databaseDriver );

84

85 // connect to database 86 myConnection = 87 DriverManager.getConnection( databaseURL );

88

89 // create Statement for executing SQL 90 myStatement = myConnection.createStatement();

91 }

92 catch ( SQLException exception ) 93 {

94 exception.printStackTrace();

95 }

Declaring instance variables to manage the database

connection

Loading database driver class (com.

ibm.db2j.jdbc.

DB2jDriver)

Creating a

Statement object Connecting to ATM

database

(jdbc:db2j:ATM)

(66)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (5 of 37)

98 exception.printStackTrace();

99 } 100

101 createUserInterface(); // set up GUI 102

103 } // end constructor 104

105 // create and position GUI components; register event handler 106 private void createUserInterface()

107 {

108 // get content pane for attaching GUI components 109 Container contentPane = getContentPane();

110

111 // enable explicit positioning of GUI components 112 contentPane.setLayout( null );

113

114 // set up messageJTextArea

115 messageJTextArea = new JTextArea();

116 messageJTextArea.setBounds( 40, 16, 288, 88 );

117 messageJTextArea.setText(

118 "Please select your account number." );

119 messageJTextArea.setBorder(

120 BorderFactory.createLoweredBevelBorder() );

Catching ClassNotFound- Exception from line 83

(67)

ATM.java (6 of 37)

123

124 // set up numberJTextField

125 numberJTextField = new JTextField();

126 numberJTextField.setBounds( 110, 120, 128, 21 );

127 numberJTextField.setBorder(

128 BorderFactory.createLoweredBevelBorder() );

129 numberJTextField.setEditable( false );

130 contentPane.add( numberJTextField );

131

132 // set up buttonsJPanel

133 buttonsJPanel = new JPanel();

134 buttonsJPanel.setBounds( 44, 160, 276, 150 );

135 buttonsJPanel.setBorder( BorderFactory.createEtchedBorder() );

136 buttonsJPanel.setLayout( null );

137 contentPane.add( buttonsJPanel );

138

139 // set up oneJButton

140 oneJButton = new JButton();

141 oneJButton.setBounds( 53, 28, 24, 24 );

142 oneJButton.setText( "1" );

143 oneJButton.setBorder(

144 BorderFactory.createRaisedBevelBorder() );

145 buttonsJPanel.add( oneJButton );

(68)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (7 of 37)

148 new ActionListener() // anonymous inner class 149 {

150 // event handler called when oneJButton is clicked 151 public void actionPerformed( ActionEvent event ) 152 {

153 oneJButtonActionPerformed( event );

154 } 155

156 } // end anonymous inner class 157

158 ); // end call to addActionListener 159

160 // set up twoJButton

161 twoJButton = new JButton();

162 twoJButton.setBounds( 77, 28, 24, 24 );

163 twoJButton.setText( "2" );

164 twoJButton.setBorder(

165 BorderFactory.createRaisedBevelBorder() );

166 buttonsJPanel.add( twoJButton );

167 twoJButton.addActionListener(

168

169 new ActionListener() // anonymous inner class 170 {

(69)

ATM.java (8 of 37)

173 {

174 twoJButtonActionPerformed( event );

175 } 176

177 } // end anonymous inner class 178

179 ); // end call to addActionListener 180

181 // set up threeJButton

182 threeJButton = new JButton();

183 threeJButton.setBounds( 101, 28, 24, 24 );

184 threeJButton.setText( "3" );

185 threeJButton.setBorder(

186 BorderFactory.createRaisedBevelBorder() );

187 buttonsJPanel.add( threeJButton );

188 threeJButton.addActionListener(

189

190 new ActionListener() // anonymous inner class 191 {

192 // event handler called when threeJButton is clicked 193 public void actionPerformed( ActionEvent event ) 194 {

195 threeJButtonActionPerformed( event );

(70)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (9 of 37)

198 } // end anonymous inner class 199

200 ); // end call to addActionListener 201

202 // set up fourJButton

203 fourJButton = new JButton();

204 fourJButton.setBounds( 53, 52, 24, 24 );

205 fourJButton.setText( "4" );

206 fourJButton.setBorder(

207 BorderFactory.createRaisedBevelBorder() );

208 buttonsJPanel.add( fourJButton );

209 fourJButton.addActionListener(

210

211 new ActionListener() // anonymous inner class 212 {

213 // event handler called when fourJButton is clicked 214 public void actionPerformed( ActionEvent event ) 215 {

216 fourJButtonActionPerformed( event );

217 } 218

219 } // end anonymous inner class 220

(71)

ATM.java (10 of 37)

223 // set up fiveJButton

224 fiveJButton = new JButton();

225 fiveJButton.setBounds( 77, 52, 24, 24 );

226 fiveJButton.setText( "5" );

227 fiveJButton.setBorder(

228 BorderFactory.createRaisedBevelBorder() );

229 buttonsJPanel.add( fiveJButton );

230 fiveJButton.addActionListener(

231

232 new ActionListener() // anonymous inner class 233 {

234 // event handler called when fiveJButton is clicked 235 public void actionPerformed( ActionEvent event ) 236 {

237 fiveJButtonActionPerformed( event );

238 } 239

240 } // end anonymous inner class 241

242 ); // end call to addActionListener 243

244 // set up sixJButton

245 sixJButton = new JButton();

(72)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (11 of 37)

248 sixJButton.setBorder(

249 BorderFactory.createRaisedBevelBorder() );

250 buttonsJPanel.add( sixJButton );

251 sixJButton.addActionListener(

252

253 new ActionListener() // anonymous inner class 254 {

255 // event handler called when sixJButton is clicked 256 public void actionPerformed( ActionEvent event ) 257 {

258 sixJButtonActionPerformed( event );

259 } 260

261 } // end anonymous inner class 262

263 ); // end call to addActionListener 264

265 // set up sevenJButton

266 sevenJButton = new JButton();

267 sevenJButton.setBounds( 53, 76, 24, 24 );

268 sevenJButton.setText( "7" );

269 sevenJButton.setBorder(

270 BorderFactory.createRaisedBevelBorder() );

(73)

ATM.java (12 of 37)

273

274 new ActionListener() // anonymous inner class 275 {

276 // event handler called when sevenJButton is clicked 277 public void actionPerformed( ActionEvent event ) 278 {

279 sevenJButtonActionPerformed( event );

280 } 281

282 } // end anonymous inner class 283

284 ); // end call to addActionListener 285

286 // set up eightJButton

287 eightJButton = new JButton();

288 eightJButton.setBounds( 77, 76, 24, 24 );

289 eightJButton.setText( "8" );

290 eightJButton.setBorder(

291 BorderFactory.createRaisedBevelBorder() );

292 buttonsJPanel.add( eightJButton );

293 eightJButton.addActionListener(

294

295 new ActionListener() // anonymous inner class

(74)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (13 of 37)

298 public void actionPerformed( ActionEvent event ) 299 {

300 eightJButtonActionPerformed( event );

301 } 302

303 } // end anonymous inner class 304

305 ); // end call to addActionListener 306

307 // set up nineJButton

308 nineJButton = new JButton();

309 nineJButton.setBounds( 101, 76, 24, 24 );

310 nineJButton.setText( "9" );

311 nineJButton.setBorder(

312 BorderFactory.createRaisedBevelBorder() );

313 buttonsJPanel.add( nineJButton );

314 nineJButton.addActionListener(

315

316 new ActionListener() // anonymous inner class 317 {

318 // event handler called when nineJButton is clicked 319 public void actionPerformed( ActionEvent event ) 320 {

(75)

ATM.java (14 of 37)

323

324 } // end anonymous inner class 325

326 ); // end call to addActionListener 327

328 // set up zeroJButton

329 zeroJButton = new JButton();

330 zeroJButton.setBounds( 77, 100, 24, 24 );

331 zeroJButton.setText( "0" );

332 zeroJButton.setBorder(

333 BorderFactory.createRaisedBevelBorder() );

334 buttonsJPanel.add( zeroJButton );

335 zeroJButton.addActionListener(

336

337 new ActionListener() // anonymous inner class 338 {

339 // event handler called when zeroJButton is clicked 340 public void actionPerformed( ActionEvent event ) 341 {

342 zeroJButtonActionPerformed( event );

343 } 344

345 } // end anonymous inner class

(76)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (15 of 37)

348

349 disableKeyPad(); // disable numeric JButtons 350

351 // set up enterJButton

352 enterJButton = new JButton();

353 enterJButton.setBounds( 149, 17, 72, 24 );

354 enterJButton.setText( "Enter" );

355 enterJButton.setBorder(

356 BorderFactory.createRaisedBevelBorder() );

357 buttonsJPanel.add( enterJButton );

358 enterJButton.setEnabled( false );

359 enterJButton.addActionListener(

360

361 new ActionListener() // anonymous inner class 362 {

363 // event handler called when enterJButton is clicked 364 public void actionPerformed( ActionEvent event ) 365 {

366 enterJButtonActionPerformed( event );

367 } 368

369 } // end anonymous inner class 370

(77)

ATM.java (16 of 37)

373 // set up balanceJButton

374 balanceJButton = new JButton();

375 balanceJButton.setBounds( 149, 49, 72, 24 );

376 balanceJButton.setText( "Balance" );

377 balanceJButton.setBorder(

378 BorderFactory.createRaisedBevelBorder() );

379 buttonsJPanel.add( balanceJButton );

380 balanceJButton.setEnabled( false );

381 balanceJButton.addActionListener(

382

383 new ActionListener() // anonymous inner class 384 {

385 // event handler called when balanceJButton is clicked 386 public void actionPerformed( ActionEvent event )

387 {

388 balanceJButtonActionPerformed( event );

389 } 390

391 } // end anonymous inner class 392

393 ); // end call to addActionListener 394

395 // set up withdrawJButton

(78)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (17 of 37)

398 withdrawJButton.setText( "Withdraw" );

399 withdrawJButton.setBorder(

400 BorderFactory.createRaisedBevelBorder() );

401 withdrawJButton.setEnabled( false );

402 buttonsJPanel.add( withdrawJButton );

403 withdrawJButton.addActionListener(

404

405 new ActionListener() // anonymous inner class 406 {

407 // event handler called when withdrawJButton is clicked 408 public void actionPerformed( ActionEvent event )

409 {

410 withdrawJButtonActionPerformed( event );

411 } 412

413 } // end anonymous inner class 414

415 ); // end call to addActionListener 416

417 // set up doneJButton

418 doneJButton = new JButton();

419 doneJButton.setBounds( 149, 113, 72, 24 );

420 doneJButton.setText( "Done" );

(79)

ATM.java (18 of 37)

423 doneJButton.setEnabled( false );

424 buttonsJPanel.add( doneJButton );

425 doneJButton.addActionListener(

426

427 new ActionListener() // anonymous inner class 428 {

429 // event handler called when doneJButton is clicked 430 public void actionPerformed( ActionEvent event ) 431 {

432 doneJButtonActionPerformed( event );

433 } 434

435 } // end anonymous inner class 436

437 ); // end call to addActionListener 438

439 // set up accountNumberJPanel

440 accountNumberJPanel = new JPanel();

441 accountNumberJPanel.setBounds( 44, 320, 276, 48 );

442 accountNumberJPanel.setBorder(

443 BorderFactory.createEtchedBorder() );

444 accountNumberJPanel.setLayout( null );

445 contentPane.add( accountNumberJPanel );

(80)

 2004 Prentice Hall, Inc.

All rights reserved.

ATM.java (19 of 37)

448 accountNumberJLabel = new JLabel();

449 accountNumberJLabel.setBounds( 25, 15, 100, 20 );

450 accountNumberJLabel.setText( "Account Number:" );

451 accountNumberJPanel.add( accountNumberJLabel );

452

453 // set up accountNumberJComboBox

454 accountNumberJComboBox = new JComboBox();

455 accountNumberJComboBox.setBounds( 150, 12, 96, 25 );

456 accountNumberJComboBox.addItem( "" );

457 accountNumberJComboBox.setSelectedIndex( 0 );

458 accountNumberJPanel.add( accountNumberJComboBox );

459 accountNumberJComboBox.addItemListener(

460

461 new ItemListener() // anonymous inner class 462 {

463 // event handler called when account number is chosen 464 public void itemStateChanged( ItemEvent event )

465 {

466 accountNumberJComboBoxItemStateChanged( event );

467 } 468

469 } // end anonymous inner class 470

References

Related documents

Then, insert a new record into the RENTAL table identifying the booked date as today's date, the copy ID number, the member ID number, the title ID number and the expected

Interim Assistant Dean-College of Education and Human Services; Associate Professor and Coordinator of Special Education Programs, Psychology, Counseling, and Special Education,

Highest microbial diversity was re- vealed by primer pair 515F-806R using two alpha diversity indices (number of observed species, Shannon) for diverse source compart- ments along

pdedrnpne pdebbuenne adeos jnfdrn 'oidm in`nf jdgoiupne sdgnro/ jdgoiupne sdgnro/ gnro. gnro

In this case the user needs to bear in mind that by attempting to enter the incorrect PIN repeatedly may cause the PIN to lock and therefore the smart card. Error: The PIN

1) Failed PIN creation via SecurID with Web authentication. When a user fails to enter a PIN that matches the PIN criteria the first time they will be prompted again to create a

But as Yugoslavia moved away from the Soviet Union, turning toward the West, and as it started to change from a poverty stricken, largely agrarian society, ravaged by the

Digital Bunny the steel on their Bunny among The Tuft on The thing Game Pin the Tail Easter Kid corner Party Games Template from Peony Blush Designs.. See more freedom in minutes,