In this section, you will run and interact with your first Java application. You will begin by running an ATM application that simulates the transactions that take place when using an ATM machine (e.g.,
withdrawing
money, making deposits and checking account
balances
). You will learn how to build this application in the optional, object-oriented case study included in Chapters 18 and 10. Figure 1.10 at the end of this section suggests many additional interesting applications that you may also want to test-drive after completing the ATM test-drive.
In the following steps, you will run the application and perform various transactions. The elements and functionality you see in this application are typical of what you will learn to program in this book. [
Note:
We use fonts to distinguish between features you see on a screen (e.g., the
Command Prompt
) and elements that are not directly
related
to a screen. Our convention is to
emphasize
screen features like titles and
menus
(e.g., the
File
menu) in a semibold
sans-serif Helvetica
font and to emphasize non-screen elements, such as file
names
or input (e.g.,
ProgramName.java
) in a
sans-serif Lucida
font. As you have already noticed, the defining occurrence of each
term
is set in blue, heavy bold. In the figures in this section, we highlight in yellow the
user
input required by each step and point out significant
parts
of the application with lines and text. To make these features more visible, we have modified the background
color
of the
Command Prompt
windows
.]
|
1.
|
Checking your setup.
Read the
For Students and Instructors: Important Information Before You Begin
section to confirm that you have set up Java properly on your computer and that you have
copied
the book's examples to your hard drive.
|
|
2.
|
Locating the completed application.
Open a
Command Prompt
window. For readers using Windows 95, 98 or 2000, this can be done by selecting
Start > Programs
> Accessories > Command Prompt
. For Windows XP users, select
Start > All Programs
> Accessories > Command Prompt
. Change to your completed ATM application directory by typing
cd C:\examples\ch01\ATM
and then pressing
Enter
(Fig. 1.2). The command
cd
is used to change directories.
|
|
3.
|
Running the ATM application.
Now that you are in the directory that contains the ATM application, type the command
java ATMCaseStudy
(Fig. 1.3) and press
Enter
. Remember from the
preceding
section that the java command, followed by the
name
of the application's
.class
file (in this case,
ATMCaseStudy
), executes the application. It is not necessary to specify the
.class
extension when using the
java
command. [
Note:
Java commands are case sensitive. It is important to type the name of this application with a capital A, T and M in "ATM," a capital C in "Case" and a capital S in "Study." Otherwise, the application will not execute.]
|
|
|
|
|
4.
|
Entering an account number.
When the application first executes, it displays a "
Welcome!
" greeting and prompts you for an account number. Type
12345
at the "
Please enter your account number
:" prompt (Fig. 1.4) and press
Enter
.
[Page 18]
|
|
5.
|
Entering a PIN.
Once a valid account number is entered, the application displays the prompt "
Enter your PIN:
". Type "
54321
" as your valid PIN (Personal Identification Number) and press
Enter
. The ATM main menu containing a list of options will be displayed (Fig. 1.5).
|
|
6.
|
Viewing the account balance.
Select option
1
, "
View my balance
", from the ATM menu. The application then displays two numbersthe
Available balance ($1000.00)
and the
Total balance ($1,200.00)
. The available balance is the maximum amount of money in your account which is available for withdrawal at a given time. In some cases, certain funds, such as recent deposits, are not immediately available for the user to withdraw, so the available balance may be less than the total balance, as it is here. After the account balance information is shown, the application's main menu is displayed again (Fig. 1.6).
|
|
|
[Page 19]
|
|
7.
|
Withdrawing money from the account.
Select option
2
, "
Withdraw cash
", from the application menu. You are then presented (Fig. 1.7) with a list of dollar amounts (e.g.,
20
,
40
,
60
,
100
and
200
). You are also given the option to cancel the transaction and return to the main menu. Withdraw $100 by selecting option 4. The application displays "
Please take your cash now
." and returns to the main menu. [
Note:
Unfortunately, this application only
simulates
the behavior of a real ATM and thus does not actually dispense money.]
|
|
8.
|
Confirming that the account information has been updated.
From the main menu, select option
1
again to view your current account balance. Note that both the available balance and the total balance have been updated to reflect your withdrawal transaction (Fig. 1.8).
|
|
|
|
|
9.
|
Ending the transaction.
To end your current ATM session, select option
4
, "
Exit
" from the main menu. The ATM will exit the system and display a
goodbye
message to the user. The application will then return to its original prompt asking for the
next
user's account number (Fig. 1.9).
|
|
|
[Page 20]
|
|
10.
|
Exiting the ATM application and closing the
Command Prompt
window.
Most applications provide an option to exit and return to the
Command Prompt
directory from which the application was run. A real ATM does not provide a user with the option to
turn
off the ATM machine. Rather, when a user has completed all desired transactions and chooses the menu option to exit, the ATM resets itself and displays a prompt for the next user's account number. As Fig. 1.9 illustrates, the ATM application here behaves similarly. Choosing the menu option to exit ends only the current user's ATM session, not the entire ATM application. To actually exit the ATM application, click the close (
x
) button in the upper-right corner of the
Command Prompt
window. Closing the window causes the running application to terminate.
|