//======================================================================//  PROJECT:            _programming.java_//  FILE:               ATM.java//  PURPOSE:            Chapter 7 demo program, final version//  VERSION:            2.0//  TARGET:             Java v1.1//  UPDATE HISTORY:     2.0    11/2/98    1.1 event model//                      1.0    7/4/96     the version in _p.j_//======================================================================//------------------------------ IMPORTS -------------------------------import java.applet.*;import java.awt.*;import java.awt.event.*;import Utilities.*;       // Our own package, for the Keypad and NumField classes//============================= ATM CLASS ==============================/** * This applet simulates the action of an automatic teller machine, * consisting of a keypad, a collection of control buttons, a * numeric display field, and a text area for instructions. * <BR> * See _programming.java_, Chapter 7, for details and specification. */public class ATM extends Applet implements ActionListener{    private final int   START_MODE = 0,                        DEPOSIT_MODE = 1,                        WITHDRAW_MODE = 2;    private int         mode;	    private NumField    display = new NumField();	private Keypad      pad = new Keypad();	private Button      clear = new Button("Clear"),                        enter = new Button("Enter"),                        deposit = new Button("Deposit"),                        withdraw = new Button("Withdraw");    private TextArea    help = new TextArea(6, 35);	    public void init()    {        Panel p1 = new Panel();        p1.setLayout(new GridLayout(4, 1, 0, 3));        p1.add(clear);        p1.add(enter);        p1.add(deposit);        p1.add(withdraw);		        Panel p2 = new Panel();        p2.setLayout(new BorderLayout(3, 0));        p2.add("Center", pad);        p2.add("East", p1);		        Panel p3 = new Panel();        p3.setLayout(new BorderLayout());        p3.add("North", display);        p3.add("Center", p2);		        setLayout(new BorderLayout(5, 5));        add("Center", p3);        add("East", help);        help.setEditable(false);        this.setBackground(Color.lightGray);                clear.addActionListener(this);        enter.addActionListener(this);        deposit.addActionListener(this);        withdraw.addActionListener(this);        pad.addActionListener(this);		        introStart();    }	    public void actionPerformed(ActionEvent e)    {        Object source = e.getSource();        switch (mode)        {        case START_MODE:            if (source == deposit)            {                mode = DEPOSIT_MODE;                introDeposit();            }            else if (source == withdraw)            {                mode = WITHDRAW_MODE;                introWithdraw();            }            else if (source == clear)            {                doQuit();            }            else if (source == enter)            {                mode = START_MODE;                introStart();            }            break;			        case DEPOSIT_MODE:            if (source == enter)            {                handleDeposit();            }            else if (source == clear)            {                display.clear();            }            else            {                String name = ((Button)source).getLabel();                display.append(name);            }            break;			        case WITHDRAW_MODE:            if (source == enter)            {                handleWithdrawal();            }            else if (source == clear)            {                display.clear();            }            else            {                String name = ((Button)source).getLabel();                display.append(name);            }            break;        }    }	    private void introStart()    {        help.setText("             W E L C O M E\n\n");        help.appendText("Press the Deposit or Withdraw buttons\n");        help.appendText("to make a transaction.\n\n");        help.appendText("Press Clear to end this session.");		        display.clear();        clear.setEnabled(true);        enter.setEnabled(false);        deposit.setEnabled(true);        withdraw.setEnabled(true);    }	    private void introDeposit()    {        help.setText("               D E P O S I T\n\n");        help.appendText("Key in the amount of your deposit\n");        showGeneralMessage();		        display.clear();        clear.setEnabled(true);        enter.setEnabled(true);        deposit.setEnabled(false);        withdraw.setEnabled(false);    }	    private void introWithdraw()    {        help.setText("             W I T H D R A W A L\n\n");        help.appendText("Key in the amount of your withdrawal\n");        showGeneralMessage();		        display.clear();        clear.setEnabled(true);        enter.setEnabled(true);        deposit.setEnabled(false);        withdraw.setEnabled(false);    }        /**     * A deposit request has been made.  Check whether the amount in     * display is nonzero.  If so, just go back to start mode.  If not,     * display a help message.     */    private void handleDeposit()    {        if (display.getValue() == 0.0)        {            help.setText("*** oops!  The amount can't be zero\n\n");            help.appendText("Key in the amount of your deposit\n");            showGeneralMessage();        }        else        {            mode = START_MODE;            introStart();        }    }        /**     * A withdrawal request has been made.  Check whether the amount in     * display is nonzero.  If so, just go back to start mode.  If not,     * display a help message.     */    private void handleWithdrawal()    {        if (display.getValue() == 0.0)        {            help.setText("*** oops!  The amount can't be zero\n\n");            help.appendText("Key in the amount of your withdrawal\n");            showGeneralMessage();        }        else        {            mode = START_MODE;            introStart();        }    }	    private void doQuit()    {        help.setText("Thank you for your business.\n\n");        help.appendText("Have a nice day.\n\n\n");        help.appendText("Press the Enter button to start.");		        display.clear();        clear.setEnabled(false);        enter.setEnabled(true);        deposit.setEnabled(false);        withdraw.setEnabled(false);    }	    private void showGeneralMessage()    {        help.appendText("and then press Enter to finish.\n\n");        help.appendText("If you make a mistake, press Clear,\n");        help.appendText("key in your amount again,\n");        help.appendText("and then press Enter. ");    }}