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        {
 76           display.setText("Enter customer number\nA = OK");
 77        }
 78        else if (state == ATM.PIN)
 79        {
 80           display.setText("Enter PIN\nA = OK");
 81        }
 82        else if (state == ATM.ACCOUNT)
 83        {
 84           display.setText("Select Account\n" 
 85                 + "A = Checking\nB = Savings\nC = Exit");
 86        }
 87        else if (state == ATM.TRANSACT)
 88        {
 89           try
 90           {
 91              display.setText("Balance = " 
 92                    + theATM.getBalance() 
 93                    + "\nEnter amount and select transaction\n"
 94                    + "A = Withdraw\nB = Deposit\nC = Cancel");
 95           }
 96           catch (SQLException exception)
 97           {
 98              JOptionPane.showMessageDialog(null,
 99                    "Database error");
100           }
101        }
102     }
103     
104     class AButtonListener implements ActionListener
105     {  
106        public void actionPerformed(ActionEvent event)
107        {  
108           int state = theATM.getState();
109           if (state == ATM.START)
110           {
111              theATM.setCustomerNumber((int) pad.getValue());
112           }
113           else if (state == ATM.PIN)
114           {
115              try
116              {
117                 theATM.selectCustomer((int) pad.getValue());
118              }
119              catch (SQLException exception)
120              {
121                 JOptionPane.showMessageDialog(null,
122                       "Database error");
123              }
124           }
125           else if (state == ATM.ACCOUNT)
126           {
127              theATM.selectAccount(ATM.CHECKING);
128           }
129           else if (state == ATM.TRANSACT)
130           {
131              try
132              {
133                 theATM.withdraw(pad.getValue());               
134              }
135              catch (SQLException exception)
136              {
137                 JOptionPane.showMessageDialog(null,
138                       "Database error");
139              }
140              theATM.back();
141           }
142           showState();
143        }
144     }
145     
146     class BButtonListener implements ActionListener
147     {  
148        public void actionPerformed(ActionEvent event)
149        {  
150           int state = theATM.getState();
151           if (state == ATM.ACCOUNT)
152           {
153              theATM.selectAccount(ATM.SAVINGS);
154           }
155           else if (state == ATM.TRANSACT)
156           {
157              try
158              {
159                 theATM.deposit(pad.getValue());
160              }
161              catch (SQLException exception)
162              {
163                 JOptionPane.showMessageDialog(null,
164                       "Database error");
165              }
166              theATM.back();
167           }
168           showState();
169        }
170     }
171  
172     class CButtonListener implements ActionListener
173     {  
174        public void actionPerformed(ActionEvent event)
175        {  
176           int state = theATM.getState();
177           if (state == ATM.ACCOUNT)
178           {
179              theATM.reset();
180           }
181           else if (state == ATM.TRANSACT)
182           {
183              theATM.back();
184           }
185           showState();
186        }
187     }
188  }