You are on page 1of 98

1

Tutorial 26 ATM Application


Introducing Database Programming and Using
Command-Line Arguments
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
2

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.

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
4

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
5

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
6

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
7

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
8

26.1 IBM Cloudscape Database (Cont.)


Figure 26.5 Choosing the Cloudscape installation directory.
(Courtesy of IBM Corporation.)

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
9

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
10

26.1 IBM Cloudscape Database (Cont.)


Figure 26.7 Confirming the location of the Cloudscape installation.
(Courtesy of IBM Corporation.)

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
11

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
12

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
13

26.1 IBM Cloudscape Database (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
14

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.

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
16

26.2 Test-Driving the ATM Application (Cont.)

Figure 26.11 Setting the CLASSPATH environment variable.

Result of running setCP.bat

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
17

26.2 Test-Driving the ATM Application (Cont.)

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
18

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 All JButtons are


or a withdrawal amount disabled initially

JComboBox that displays


account numbers

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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
Done JButton is enabled

JComboBox is disabled

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
20

26.2 Test-Driving the ATM Application (Cont.)


Figure 26.15 Entering the PIN for the selected account.

An asterisk is displayed here


for each keypad JButton
pressed for the PIN
Enter JButton is enabled

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
21

26.2 Test-Driving the ATM Application (Cont.)


Figure 26.16 ATM displaying welcome message.

Welcome message displays


in JTextArea when the user
enters the correct PIN

Enter JButton is disabled

Keypad JButtons Balance JButton is enabled


are disabled
Withdraw JButton is enabled

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
22

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
23

26.2 Test-Driving the ATM Application (Cont.)


Figure 26.17 Viewing the account balance.

Balance displays
in JTextArea

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
24

26.2 Test-Driving the ATM Application (Cont.)


Figure 26.18 Withdrawing money from the account.

Ask the user to enter


a withdrawal amount

Keypad JButtons Balance JButton is disabled


are enabled Withdraw JButton is disabled

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
25

26.2 Test-Driving the ATM Application (Cont.)


Figure 26.19 ATM application displaying the withdrawal amount.

Display the
withdrawal amount

Keypad JButtons Balance JButton is enabled


are disabled Withdraw JButton is enabled

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
26

26.2 Test-Driving the ATM Application (Cont.)


Figure 26.20 Checking new balance.

Display new balance

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
27

26.2 Test-Driving the ATM Application (Cont.)


Figure 26.21 ATM application ready for next customer.

Displays instructions
and messages to the user

Keypad JButtons JButtons are disabled


are disabled

JComboBox is enabled

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
28

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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 numbers 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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
30

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
31

26.3 Planning the ATM Application (Cont.)

Action Component Event


User selects an account accountNumberJComboBox User selects item
number from the JComboBox from
Account Numbers:
JComboBox
Disable the JComboBox accountNumberJComboBox

Prompt user to enter a PIN messageJTextArea


Clear the JTextField for the numberJTextField
PIN
Enable the keypad JButtons zeroJButton, oneJButton,
twoJButton,
threeJButton, fourJButton,
fiveJButton, sixJButton,
sevenJButton,
eightJButton,
nineJButton

Enable the Done JButton doneJButton

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
32

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method


User enters the PIN zeroJButton, User clicks a keypad
oneJButton, JButton
twoJButton,
threeJButton,
fourJButton,
fiveJButton,
sixJButton,
sevenJButton,
eightJButton,
nineJButton
Enable the Enter JButton enterJButton

Append the number to the PIN numberJTextField


User clicks the Enter JButton to enterJButton User clicks Enter
submit the PIN JButton
Search database for the account myStatement, executeQuery, close,
numbers corresponding myResultSet next, getString,
account information getDouble
User provided a correct PIN
Figure 26.22 ACE table for the ATM application (Part 2 of 6).

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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 balanceJButton,
JButtons withdrawJButton
Display status to the user messageJTextArea

User did not provide a correct


PIN
Clear the JTextField numberJTextField

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
34

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method


Prompt the user to enter a valid messageJTextArea
PIN

User clicks the Balance JButton balanceJButton User clicks Balance


JButton
Display the balance messageJTextArea
User clicks the Withdraw withdrawJButton User clicks Withdraw
JButton JButton
Disable the Balance and balanceJButton,
Withdraw JButtons withdrawJButton
Enable the keypad JButtons zeroJButton,
oneJButton,
twoJButton,
threeJButton,
fourJButton,
fiveJButton,
sixJButton,
sevenJButton,
eightJButton,
nineJButton
Prompt the user to enter the messageJTextArea
withdrawal amount
Figure 26.22 ACE table for the ATM application (Part 4 of 6).

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
35

26.3 Planning the ATM Application (Cont.)

Action Component Event/Method


User clicks the Enter JButton to enterJButton User clicks Enter
submit the withdrawal amount JButton

Disable the Enter JButton enterJButton

Disable the keypad JButtons zeroJButton,


oneJButton,
twoJButton,
threeJButton,
fourJButton,
fiveJButton,
sixJButton,
sevenJButton,
eightJButton,
nineJButton
Process the withdrawal and myStatement,
display the withdrawal amount messageJTextArea
Clear withdrawal amount in the numberJTextField
JTextField
Enable the Balance and Withdraw balanceJButton,
JButtons withdrawJButton
User clicks the Done JButton doneJButton User clicks Done
JButton
Figure 26.22 ACE table for the ATM application (Part 5 of 6).

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
36

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, enterJButton,
Withdraw and Done JButtons balanceJButton,
withdrawJButton,
doneJButton
Enable the JComboBox accountNumber-
JComboBox
Display instructions for the next messageJTextArea
customer in the JTextArea
Figure 26.22 ACE table for the ATM application (Part 6 of 6).

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
37
26.4 Relational Database Overview: The ATM
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
38
26.4 Relational Database Overview: The ATM
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
39
26.4 Relational Database Overview: The ATM
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.

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
40

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
41

26.5 SQL (Cont.)

SELECT accountNumber, firstName FROM accountInformation

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.

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
42

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
44

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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.

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
46

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
47

26.6 Using Command-Line Arguments (Cont.)

Figure 26.27 Viewing the main method.

Start the main method


declaration
Check the number of
command-line arguments
Get command-line arguments
Pass command-line
arguments to the constructor
Display a line of text
that indicates the syntax
to run the application

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
48

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
49

26.7 Creating Database Connections (Cont.)

Figure 26.28 Importing the java.sql package .

Importing the
java.sql package

java.sql package
Perform database processing

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
50

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
51

26.7 Creating Database Connections (Cont.)

Figure 26.29 Declaring instance variables for database processing.

Declaring instance
variables for managing
the database connection

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
52

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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
thrown from line 83

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
54

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
56

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
58

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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
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

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
60

26.8 Programming the ATM Application (Cont.)

Figure 26.34 Closing the database connection.

Close Statement and


Connection to release
database resources
Catch any SQLExceptions
thrown from the try block
Terminate the application

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
61

26.8 Programming the ATM Application (Cont.)


Figure 26.35 Running the completed ATM application.

Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson


Education Inc. All Rights Reserved.
62
1 // Tutorial 26: ATM.java
2 // ATM application allows users to access an account,
Outline
3 // view the balance and withdraw money from the account.
4 import java.awt.*;
ATM.java (1 of 37)
5 import java.awt.event.*;
6 import java.sql.*;
7 import java.text.*; Importing
8 import javax.swing.*; java.sql package
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;

2004 Prentice Hall, Inc.


All rights reserved.
63
26 private JButton fourJButton;
27 private JButton fiveJButton;
Outline
28 private JButton sixJButton;
29 private JButton sevenJButton;
ATM.java (2 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
64
49 // JLabel and JComboBox for account numbers
50 private JLabel accountNumberJLabel;
Outline
51 private JComboBox accountNumberJComboBox;
52
ATM.java (3 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
65
71 // instance variables used to manipulate database
72 private Connection myConnection;
Outline
73 private Statement myStatement;
74 private ResultSet myResultSet;
ATM.java (4 of 37)
75
76 // constructor
77 public ATM( String databaseDriver, String databaseURL ) Declaring instance
78 { variables to manage
79 // establish connection to database
the database
80 try
81 {
connection
82 // load Cloudscape driver
Loading database
83 Class.forName( databaseDriver );
driver class (com.
84
ibm.db2j.jdbc.
85 // connect to database
86 myConnection =
DB2jDriver)
87 DriverManager.getConnection( databaseURL );
Connecting to ATM
88
89 // create Statement for executing SQL
database
90 myStatement = myConnection.createStatement(); (jdbc:db2j:ATM)
91 }
92 catch ( SQLException exception )
Creating a
93 { Statement object
94 exception.printStackTrace();
95 }

2004 Prentice Hall, Inc.


All rights reserved.
66
96 catch ( ClassNotFoundException exception )
97 {
Outline
98 exception.printStackTrace();
99 }
ATM.java (5 of 37)
100
101 createUserInterface(); // set up GUI
102
103 } // end constructor
104
Catching ClassNotFound-
105 // create and position GUI components; register event handler Exception from line 83
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() );

2004 Prentice Hall, Inc.


All rights reserved.
67
121 messageJTextArea.setEditable( false );
122 contentPane.add( messageJTextArea );
Outline
123
124 // set up numberJTextField
ATM.java (6 of 37)
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 );

2004 Prentice Hall, Inc.


All rights reserved.
68
146 oneJButton.addActionListener(
147
Outline
148 new ActionListener() // anonymous inner class
149 {
ATM.java (7 of 37)
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 {

2004 Prentice Hall, Inc.


All rights reserved.
69
171 // event handler called when twoJButton is clicked
172 public void actionPerformed( ActionEvent event )
Outline
173 {
174 twoJButtonActionPerformed( event );
ATM.java (8 of 37)
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 );

2004 Prentice Hall, Inc.


All rights reserved.
70
196 }
197
Outline
198 } // end anonymous inner class
199
ATM.java (9 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
71
221 ); // end call to addActionListener
222
Outline
223 // set up fiveJButton
224 fiveJButton = new JButton();
ATM.java (10 of 37)
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();

2004 Prentice Hall, Inc.


All rights reserved.
72
246 sixJButton.setBounds( 101, 52, 24, 24 );
247 sixJButton.setText( "6" );
Outline
248 sixJButton.setBorder(
249 BorderFactory.createRaisedBevelBorder() );
ATM.java (11 of 37)
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() );

2004 Prentice Hall, Inc.


All rights reserved.
73
271 buttonsJPanel.add( sevenJButton );
272 sevenJButton.addActionListener(
Outline
273
274 new ActionListener() // anonymous inner class
ATM.java (12 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
74
296 {
297 // event handler called when eightJButton is clicked
Outline
298 public void actionPerformed( ActionEvent event )
299 {
ATM.java (13 of 37)
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 {

2004 Prentice Hall, Inc.


All rights reserved.
75
321 nineJButtonActionPerformed( event );
322 }
Outline
323
324 } // end anonymous inner class
ATM.java (14 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
76
346
347 ); // end call to addActionListener
Outline
348
349 disableKeyPad(); // disable numeric JButtons
ATM.java (15 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
77
371 ); // end call to addActionListener
372
Outline
373 // set up balanceJButton
374 balanceJButton = new JButton();
ATM.java (16 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
78
396 withdrawJButton = new JButton();
397 withdrawJButton.setBounds( 149, 81, 72, 24 );
Outline
398 withdrawJButton.setText( "Withdraw" );
399 withdrawJButton.setBorder(
ATM.java (17 of 37)
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" );

2004 Prentice Hall, Inc.


All rights reserved.
79
421 doneJButton.setBorder(
422 BorderFactory.createRaisedBevelBorder() );
Outline
423 doneJButton.setEnabled( false );
424 buttonsJPanel.add( doneJButton );
ATM.java (18 of 37)
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 );

2004 Prentice Hall, Inc.


All rights reserved.
80
446
447 // set up accountNumberJLabel
Outline
448 accountNumberJLabel = new JLabel();
449 accountNumberJLabel.setBounds( 25, 15, 100, 20 );
ATM.java (19 of 37)
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

2004 Prentice Hall, Inc.


All rights reserved.
81
471 ); // end call to addItemListener
472
Outline
473 // read account numbers from database and
474 // place them in accountNumberJComboBox
ATM.java (20 of 37)
475 loadAccountNumbers();
476
477 // set properties of application's window
478 setTitle( "ATM" ); // set title bar string
479 setSize( 375, 410 ); // set window size
480 setVisible( true ); // display window
481
482 // ensure database connection is closed
483 // when user closes application window
484 addWindowListener(
485
486 new WindowAdapter() // anonymous inner class
487 {
488 public void windowClosing( WindowEvent event )
489 {
490 frameWindowClosing( event );
491 }
492
493 } // end anonymous inner class
494
495 ); // end addWindowListener

2004 Prentice Hall, Inc.


All rights reserved.
82
496
497 } // end method createUserInterface
Outline
498
499 // process oneJButton click
ATM.java (21 of 37)
500 private void oneJButtonActionPerformed( ActionEvent event )
501 {
502 zeroToNineJButtonActionPerformed( "1" );
503
504 } // end method oneJButtonActionPerformed
505
506 // process twoJButton click
507 private void twoJButtonActionPerformed( ActionEvent event )
508 {
509 zeroToNineJButtonActionPerformed( "2" );
510
511 } // end method twoJButtonActionPerformed
512
513 // process threeJButton click
514 private void threeJButtonActionPerformed( ActionEvent event )
515 {
516 zeroToNineJButtonActionPerformed( "3" );
517
518 } // end method threeJButtonActionPerformed
519
520 // process fourJButton click

2004 Prentice Hall, Inc.


All rights reserved.
83
521 private void fourJButtonActionPerformed( ActionEvent event )
522 {
Outline
523 zeroToNineJButtonActionPerformed( "4" );
524
ATM.java (22 of 37)
525 } // end method fourJButtonActionPerformed
526
527 // process fiveJButton click
528 private void fiveJButtonActionPerformed( ActionEvent event )
529 {
530 zeroToNineJButtonActionPerformed( "5" );
531
532 } // end method fiveJButtonActionPerformed
533
534 // process sixJButton click
535 private void sixJButtonActionPerformed( ActionEvent event )
536 {
537 zeroToNineJButtonActionPerformed( "6" );
538
539 } // end method sixJButtonActionPerformed
540
541 // process sevenJButton click
542 private void sevenJButtonActionPerformed( ActionEvent event )
543 {
544 zeroToNineJButtonActionPerformed( "7" );
545

2004 Prentice Hall, Inc.


All rights reserved.
84
546 } // end method sevenJButtonActionPerformed
547
Outline
548 // process eightJButton click
549 private void eightJButtonActionPerformed( ActionEvent event )
ATM.java (23 of 37)
550 {
551 zeroToNineJButtonActionPerformed( "8" );
552
553 } // end method eightJButtonActionPerformed
554
555 // process nineJButton click
556 private void nineJButtonActionPerformed( ActionEvent event )
557 {
558 zeroToNineJButtonActionPerformed( "9" );
559
560 } // end method nineJButtonActionPerformed
561
562 // process zeroJButton click
563 private void zeroJButtonActionPerformed( ActionEvent event )
564 {
565 zeroToNineJButtonActionPerformed( "0" );
566
567 } // end method zeroJButtonActionPerformed
568
569 // process clicks of a numeric JButton
570 private void zeroToNineJButtonActionPerformed( String number )

2004 Prentice Hall, Inc.


All rights reserved.
85
571 {
572 // enable enterJButton if it is disabled
Outline
573 if ( !enterJButton.isEnabled() )
574 {
ATM.java (24 of 37)
575 enterJButton.setEnabled( true );
576 }
577
578 // if user is entering PIN number display * to conceal PIN
579 if ( action == ENTER_PIN )
580 {
581 userPIN += number; // append number to current PIN
582 numberJTextField.setText(
583 numberJTextField.getText() + "*" );
584 }
585
586 else // otherwise display number of JButton user clicked
587 {
588 numberJTextField.setText(
589 numberJTextField.getText() + number );
590 }
591
592 } // end method zeroToNineJButtonsActionPerformed
593
594 // verify PIN or withdraw from account
595 private void enterJButtonActionPerformed( ActionEvent event )

2004 Prentice Hall, Inc.


All rights reserved.
86
596 {
597 if ( action == ENTER_PIN ) // checking PIN
Outline
598 {
599 // get pin, first name and balance for account number
ATM.java (25 of 37)
600 // selected in accountNumberJComboBox
601 retrieveAccountInformation();
602
603 numberJTextField.setText( "" ); // clear numberJTextField
604
605 // correct PIN number
606 if ( userPIN.equals( pin ) )
607 {
608 // disable enterJButton
609 enterJButton.setEnabled( false );
610
611 disableKeyPad(); // disable numeric JButtons
612
613 // enable balanceJButton and withdrawJButton
614 balanceJButton.setEnabled( true );
615 withdrawJButton.setEnabled( true );
616
617 // display status to user
618 messageJTextArea.setText(
619 "Welcome " + firstName + ", select a transaction." );
620

2004 Prentice Hall, Inc.


All rights reserved.
87
621 } // end if part of if...else
622
Outline
623 else // wrong PIN number
624 {
ATM.java (26 of 37)
625 // indicate that incorrect PIN was provided
626 messageJTextArea.setText(
627 "Sorry, PIN number is incorrect." +
628 "\nPlease re-enter the PIN number." );
629
630 userPIN = ""; // clear user's previous PIN entry
631
632 } // end else part of if...else
633
634 } // end if that processes PIN
635
636 else if ( action == WITHDRAWAL ) // process withdrawal
637 {
638 enterJButton.setEnabled( false ); // disable enterJButton
639
640 disableKeyPad(); // disable numeric JButtons
641
642 // process withdrawal
643 withdraw(
644 Double.parseDouble( numberJTextField.getText() ) );
645

2004 Prentice Hall, Inc.


All rights reserved.
88
646 numberJTextField.setText( "" ); // clear numberJTextField
647
Outline
648 // enable balanceJButton and withdrawJButton
649 balanceJButton.setEnabled( true );
ATM.java (27 of 37)
650 withdrawJButton.setEnabled( true );
651
652 } // end if that processes withdrawal
653
654 } // end method enterJButtonActionPerformed
655
656 // display account balance
657 private void balanceJButtonActionPerformed( ActionEvent event )
658 {
659 // define display format
660 DecimalFormat dollars = new DecimalFormat( "0.00" );
661
662 // display user's balance
663 messageJTextArea.setText( "Your current balance is $" +
664 dollars.format( balance ) + "." );
665
666 } // end method balanceJButtonActionPerformed
667
668 // display withdraw action
669 private void withdrawJButtonActionPerformed( ActionEvent event )
670 {

2004 Prentice Hall, Inc.


All rights reserved.
89
671 // disable Balance and Withdraw JButtons
672 balanceJButton.setEnabled( false );
Outline
673 withdrawJButton.setEnabled( false );
674
ATM.java (28 of 37)
675 enableKeyPad(); // enable numeric JButtons
676
677 // display message to user
678 messageJTextArea.setText(
679 "Enter the amount you would like to withdraw" );
680
681 // change action to indicate user will provide
682 // withdrawal amount
683 action = WITHDRAWAL;
684
685 } // end method withdrawJButtonActionPerformed
686
687 // reset GUI
688 private void doneJButtonActionPerformed( ActionEvent event )
689 {
690 userPIN = ""; // clear userPIN
691
692 disableKeyPad(); // disable numeric JButtons
693
694 // disable OK, Balance, Withdraw and Done JButtons
695 enterJButton.setEnabled( false );

2004 Prentice Hall, Inc.


All rights reserved.
90
696 balanceJButton.setEnabled( false );
697 withdrawJButton.setEnabled( false );
Outline
698 doneJButton.setEnabled( false );
699
ATM.java (29 of 37)
700 // enable and reset accountNumberJComboBox
701 accountNumberJComboBox.setEnabled( true );
702 accountNumberJComboBox.setSelectedIndex( 0 );
703
704 // reset messageJTextArea
705 messageJTextArea.setText(
706 "Please select your account number." );
707
708 } // end method doneJButtonActionPerformed
709
710 // get account number and enable OK and Done JButtons
711 private void accountNumberJComboBoxItemStateChanged(
712 ItemEvent event )
713 {
714 // get user selected account number if no transaction is
715 // in process
716 if ( ( event.getStateChange() == ItemEvent.SELECTED ) &&
717 ( accountNumberJComboBox.getSelectedIndex() != 0 ) )
718 {
719 // disable accountNumberJComboBox
720 accountNumberJComboBox.setEnabled( false );

2004 Prentice Hall, Inc.


All rights reserved.
91
721
722 // get selected account number
Outline
723 userAccountNumber =
724 ( String ) accountNumberJComboBox.getSelectedItem();
ATM.java (30 of 37)
725
726 // change action to indicate that user will provide
727 // PIN number
728 action = ENTER_PIN;
729 userPIN = "";
730
731 // prompt user to enter PIN number
732 messageJTextArea.setText(
733 "Please enter your PIN number." );
734
735 numberJTextField.setText( "" ); // clear numberJTextField
736 enableKeyPad(); // enable numeric JButtons
737 doneJButton.setEnabled( true ); // enable doneJButton
738
739 } // end if
740
741 } // end method accountNumberJComboBoxItemStateChanged
742
743 // enable numeric JButtons
744 private void enableKeyPad()
745 {

2004 Prentice Hall, Inc.


All rights reserved.
92
746 oneJButton.setEnabled( true ); // enable oneJButton
747 twoJButton.setEnabled( true ); // enable twoJButton
Outline
748 threeJButton.setEnabled( true ); // enable threeJButton
749 fourJButton.setEnabled( true ); // enable fourJButton
ATM.java (31 of 37)
750 fiveJButton.setEnabled( true ); // enable fiveJButton
751 sixJButton.setEnabled( true ); // enable sixJButton
752 sevenJButton.setEnabled( true ); // enable sevenJButton
753 eightJButton.setEnabled( true ); // enable eightJButton
754 nineJButton.setEnabled( true ); // enable nineJButton
755 zeroJButton.setEnabled( true ); // enable zeroJButton
756
757 } // end method enableKeyPad
758
759 // disable numeric JButtons
760 private void disableKeyPad()
761 {
762 oneJButton.setEnabled( false ); // disable oneJButton
763 twoJButton.setEnabled( false ); // disable twoJButton
764 threeJButton.setEnabled( false ); // disable threeJButton
765 fourJButton.setEnabled( false ); // disable fourJButton
766 fiveJButton.setEnabled( false ); // disable fiveJButton
767 sixJButton.setEnabled( false ); // disable sixJButton
768 sevenJButton.setEnabled( false ); // disable sevenJButton
769 eightJButton.setEnabled( false ); // disable eightJButton
770 nineJButton.setEnabled( false ); // disable nineJButton

2004 Prentice Hall, Inc.


All rights reserved.
93
771 zeroJButton.setEnabled( false ); // disable zeroJButton
772
Outline
773 } // end method disableKeyPad
774
ATM.java (32 of 37)
775 // withdraw amount from account
776 private void withdraw( double withdrawAmount )
777 {
778 // determine if amount can be withdrawn
779 if ( withdrawAmount <= balance )
780 {
781 balance -= withdrawAmount; // calculate new balance
782
783 updateBalance(); // update row in database
784
785 // define display format
786 DecimalFormat dollars = new DecimalFormat( "0.00" );
787
788 // display balance information to user
789 messageJTextArea.setText( "The withdrawal amount is $" +
790 dollars.format( withdrawAmount ) + "." );
791 }
792 else // amount cannot be withdrawn
793 {
794 messageJTextArea.setText(
795 "The withdrawal amount is too large." +

2004 Prentice Hall, Inc.


All rights reserved.
94
796 "\nSelect Withdraw and enter a different amount." );
797 }
Outline
798
799 } // end method withdraw
ATM.java (33 of 37)
800
801 // load account numbers to accountNumberJComboBox
802 private void loadAccountNumbers()
803 {
804 // get all account numbers from database
805 try Submit a query that
806 {
selects the account
807 myResultSet = myStatement.executeQuery(
numbers from the
808 "SELECT accountNumber from accountInformation" );
809
accountInformation
810 // add account numbers to accountNumberJComboBox table
811 while ( myResultSet.next() )
Process the ResultSet and
812 {
813 accountNumberJComboBox.addItem(
fill accountNumberJCom-
814 myResultSet.getString( "accountNumber" ) );
boBox with account numbers
815 }
816
Close myResultSet to
817 myResultSet.close(); // close myResultSet
818
release database resources
819 } // end try
820

2004 Prentice Hall, Inc.


All rights reserved.
95
821 catch ( SQLException exception )
822 {
Outline
823 exception.printStackTrace();
824 }
ATM.java (34 of 37)
825
826 } // end method loadAccountNumbers Catch any
827 SQLExceptions thrown
828 // get account information from database
from the try block
829 private void retrieveAccountInformation()
830 {
Submit a query that
831 // get account information
832 try
selects the pin,
833 { firstName and
834 myResultSet = myStatement.executeQuery( "SELECT pin, " + balanceAmount values
835 "firstName, balanceAmount FROM accountInformation " + for the specified
836 "WHERE accountNumber = '" + userAccountNumber + "'" ); account number
837
838 // get next result
839 if ( myResultSet.next() )
840 { Get the pin, firstName
841 pin = myResultSet.getString( pin" );
and balanceAmount values
842 firstName = myResultSet.getString( "firstName" );
from the ResultSet
843 balance = myResultSet.getDouble( "balanceAmount" );
844 }
845

2004 Prentice Hall, Inc.


All rights reserved.
96
846 myResultSet.close(); // close myResultSet
847
Outline
848 } // end try
849
ATM.java (35 of 37)
850 catch ( SQLException exception )
851 { Close myResultSet to
852 exception.printStackTrace();
release database resources
853 }
854
855 } // end method retrieveAccountInformation Catch any
856 SQLExceptions thrown
857 // update database after withdrawing from the try block
858 private void updateBalance()
859 {
860 // update balance in database
Submit a SQL statement
861 try that updates the
862 { balanceAmount in the
863 myStatement.executeUpdate( "UPDATE accountInformation" + accountInformation
864 " SET balanceAmount = " + balance + " WHERE " + table for the row with the
865 "accountNumber = '" + userAccountNumber + "'" ); specified
866 } accountNumber

2004 Prentice Hall, Inc.


All rights reserved.
97
867 catch ( SQLException exception )
868 {
Outline
869 exception.printStackTrace();
870 }
ATM.java (36 of 37)
871
872 } // end method updateBalance Catch any
873 SQLExceptions thrown
874 // close statement and database connection from the try block
875 private void frameWindowClosing( WindowEvent event )
876 {
877 // close myStatement and database connection Close myStatement and
878 try myConnection to release
879 { database resources
880 myStatement.close();
881 myConnection.close();
882 }
883 catch ( SQLException sqlException )
Catch any
884 {
SQLExceptions thrown
885 sqlException.printStackTrace();
886 } from the try block
887 finally
888 {
889 System.exit( 0 ); Terminate the application
890 }
891

2004 Prentice Hall, Inc.


All rights reserved.
98
892 } // end method frameWindowClosing
893
Outline
894 // method main
895 public static void main( String[] args )
ATM.java (37 of 37)
896 {
897 // check command-line arguments
898 if ( args.length == 2 ) Check command-
899 { line arguments
900 // get command-line arguments
901 String databaseDriver = args[ 0 ];
Get command-line arguments
902 String databaseURL = args[ 1 ];
903
904 // create new ATM
905 ATM atm = new ATM( databaseDriver, databaseURL ); Create new ATM instance
906 }
907 else // invalid command-line arguments
908 { Display a line of text that
909 System.out.println( indicates the syntax to run
910 "Usage: java ATM databaseDriver databaseURL" );
the application
911 }
912
913 } // end method main
914
915 } // end class ATM

2004 Prentice Hall, Inc.


All rights reserved.

You might also like