1  import java.awt.FlowLayout;
  2  import java.awt.GridLayout;
  3  import java.awt.event.ActionEvent;
  4  import java.awt.event.ActionListener;
  5  import java.sql.SQLException;
  6  import javax.swing.JButton;
  7  import javax.swing.JFrame;
  8  import javax.swing.JOptionPane;
  9  import javax.swing.JPanel;
 10  import javax.swing.JTextArea;
 11  
 12  /**
 13     A frame displaying the components of an ATM.
 14  */
 15  public class ATMFrame extends JFrame
 16  {  
 17     private static final int FRAME_WIDTH = 300;
 18     private static final int FRAME_HEIGHT = 300;
 19  
 20     private JButton aButton;
 21     private JButton bButton;
 22     private JButton cButton;
 23     
 24     private KeyPad pad;
 25     private JTextArea display;
 26  
 27     private ATM theATM;
 28  
 29     /**
 30        Constructs the user interface of the ATM frame.
 31     */
 32     public ATMFrame(ATM anATM)
 33     {  
 34        theATM = anATM;
 35  
 36        // Construct components
 37  
 38        pad = new KeyPad();
 39  
 40        display = new JTextArea(4, 20);
 41        
 42        aButton = new JButton("  A  ");
 43        aButton.addActionListener(new AButtonListener());
 44  
 45        bButton = new JButton("  B  ");
 46        bButton.addActionListener(new BButtonListener());
 47  
 48        cButton = new JButton("  C  ");
 49        cButton.addActionListener(new CButtonListener());
 50        
 51        // Add components
 52  
 53        JPanel buttonPanel = new JPanel();
 54        buttonPanel.add(aButton);
 55        buttonPanel.add(bButton);
 56        buttonPanel.add(cButton);
 57        
 58        setLayout(new FlowLayout());
 59        add(pad);
 60        add(display);
 61        add(buttonPanel);
 62        showState();
 63  
 64        setSize(FRAME_WIDTH, FRAME_HEIGHT);
 65     }
 66     
 67     /** 
 68        Updates display message.
 69     */
 70     public void showState()
 71     {  
 72        int state = theATM.getState();
 73        pad.clear();
 74        if (state == ATM.START)
 75           display.setText("Enter customer number\nA = OK");
 76        else if (state == ATM.PIN)
 77           display.setText("Enter PIN\nA = OK");
 78        else if (state == ATM.ACCOUNT)
 79           display.setText("Select Account\n" 
 80                 + "A = Checking\nB = Savings\nC = Exit");
 81        else if (state == ATM.TRANSACT)
 82           try
 83           {
 84              display.setText("Balance = " 
 85                    + theATM.getBalance() 
 86                    + "\nEnter amount and select transaction\n"
 87                    + "A = Withdraw\nB = Deposit\nC = Cancel");
 88           }
 89           catch (SQLException exception)
 90           {
 91              JOptionPane.showMessageDialog(null,
 92                    "Database error");
 93           }
 94     }
 95     
 96     class AButtonListener implements ActionListener
 97     {  
 98        public void actionPerformed(ActionEvent event)
 99        {  
100           int state = theATM.getState();
101           if (state == ATM.START)
102              theATM.setCustomerNumber((int) pad.getValue());
103           else if (state == ATM.PIN)
104           {
105              try
106              {
107                 theATM.selectCustomer((int) pad.getValue());
108              }
109              catch (SQLException exception)
110              {
111                 JOptionPane.showMessageDialog(null,
112                       "Database error");
113              }
114           }
115           else if (state == ATM.ACCOUNT)
116              theATM.selectAccount(ATM.CHECKING);
117           else if (state == ATM.TRANSACT)
118           {
119              try
120              {
121                 theATM.withdraw(pad.getValue());               
122              }
123              catch (SQLException exception)
124              {
125                 JOptionPane.showMessageDialog(null,
126                       "Database error");
127              }
128              theATM.back();
129           }
130           showState();
131        }
132     }
133     
134     class BButtonListener implements ActionListener
135     {  
136        public void actionPerformed(ActionEvent event)
137        {  
138           int state = theATM.getState();
139           if (state == ATM.ACCOUNT)
140              theATM.selectAccount(ATM.SAVINGS);
141           else if (state == ATM.TRANSACT)
142           {
143              try
144              {
145                 theATM.deposit(pad.getValue());
146              }
147              catch (SQLException exception)
148              {
149                 JOptionPane.showMessageDialog(null,
150                       "Database error");
151              }
152              theATM.back();
153           }
154           showState();
155        }
156     }
157  
158     class CButtonListener implements ActionListener
159     {  
160        public void actionPerformed(ActionEvent event)
161        {  
162           int state = theATM.getState();
163           if (state == ATM.ACCOUNT)
164              theATM.reset();
165           else if (state == ATM.TRANSACT)
166              theATM.back();
167           showState();
168        }
169     }
170  }