Lesson 7:
Program Design
The book has taken you through the design of an ATM program. Let's look
at three stages of this program a little more carefully.
- After the specification stage came the time to decide what classes are
needed and to lay everything out visually on the screen. None of the
widgets have any functionality yet. We have created a class called
Keypad to hold the button widgets of our numeric key pad.
a) Download ATM1.java and Keypad1.java from the web page and try them out at
this point. Feel free to make changes to the layouts or other
properties so that you are sure you understand the code so far.
- Now we are going to add some functionality. The ATM needs to know
whether we are just starting out, ready to make a deposit, or ready to make
a withdrawal. We have added an instance variable mode which
will keep track of whether we are in START_MODE, DEPOSIT_MODE, or
WITHDRAW_MODE. We have added methods introStart(), introDeposit(),
and introWithdraw(). Finally we have added the actionPerformed
method to the applet to handle actions from the deposit, withdraw,
clear, and enter buttons. The methods handleDeposit
and handleWithdraw are left as "stubs" or in other
words, their bodies are left blank for now.
a) Download ATM2.java and Keypad2.java from the web page and try them
out.
b) Look at the methods introStart, introDeposit, and introWithdraw
carefully. Make sure that you can answer the following
questions:
What is being done with the
help.setText(...) and help.append(...) method calls?
Why are we performing an
assignment to the mode instance variable?
What is being done with the setEnabled
method for the various buttons?
c) Look at the actionPerformed method carefully. Make sure you
can answer the following questions:
How are we checking what button
caused the action event?
Why are we using a switch
statement for the modes?
For each mode, why are we
checking if some buttons are pushed and not others?
- Now we finish up the ATM applet. We add a listener for the
Keypad. We also change the display from a TextField to a new class
NumField which extends TextField. The NumField class only displays
legal numeric values. We now can fill in the stubs for handleDeposit
and handleWithdraw.
a) Download ATM.java, Keypad.java, and NumField.java from the web page and
try them out.
b) Look carefully at the class NumField and make sure that you can answer
the following questions:
Why does the method isLegal need
to use an instance variable hasDecimalPoint?
Where is the value of
hasDecimalPoint changed?
What does the command
super.setText(...) mean?
Do you understand what all
the methods of NumField do?
c) Look at the changes to Keypad and the actionPerformed method of
ATM. Make sure that you can answer the following questions:
What class is the listener for
the Keypad?
How does actionPerformed
know that a button from the pad was pressed?
How does actionPerformed
display the number off the pad button that was pressed?
d) Look at the handleDeposit and handleWithdraw methods
carefully and make sure that you understand what they are doing.