001: import java.awt.FlowLayout;
002: import java.awt.GridLayout;
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.sql.SQLException;
006: import javax.swing.JButton;
007: import javax.swing.JFrame;
008: import javax.swing.JOptionPane;
009: import javax.swing.JPanel;
010: import javax.swing.JTextArea;
011: 
012: /**
013:    A frame displaying the components of an ATM.
014: */
015: public class ATMFrame extends JFrame
016: {  
017:    /**
018:       Constructs the user interface of the ATM frame.
019:    */
020:    public ATMFrame(ATM anATM)
021:    {  
022:       theATM = anATM;
023: 
024:       // Construct components
025: 
026:       pad = new KeyPad();
027: 
028:       display = new JTextArea(4, 20);
029:       
030:       aButton = new JButton("  A  ");
031:       aButton.addActionListener(new AButtonListener());
032: 
033:       bButton = new JButton("  B  ");
034:       bButton.addActionListener(new BButtonListener());
035: 
036:       cButton = new JButton("  C  ");
037:       cButton.addActionListener(new CButtonListener());
038:       
039:       // Add components
040: 
041:       JPanel buttonPanel = new JPanel();
042:       buttonPanel.setLayout(new GridLayout(3, 1));
043:       buttonPanel.add(aButton);
044:       buttonPanel.add(bButton);
045:       buttonPanel.add(cButton);
046:       
047:       setLayout(new FlowLayout());
048:       add(pad);
049:       add(display);
050:       add(buttonPanel);
051:       showState();
052: 
053:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
054:    }
055:    
056:    /** 
057:       Updates display message.
058:    */
059:    public void showState()
060:    {  
061:       int state = theATM.getState();
062:       pad.clear();
063:       if (state == ATM.START)
064:          display.setText("Enter customer number\nA = OK");
065:       else if (state == ATM.PIN)
066:          display.setText("Enter PIN\nA = OK");
067:       else if (state == ATM.ACCOUNT)
068:          display.setText("Select Account\n" 
069:                + "A = Checking\nB = Savings\nC = Exit");
070:       else if (state == ATM.TRANSACT)
071:          try
072:          {
073:             display.setText("Balance = " 
074:                   + theATM.getBalance() 
075:                   + "\nEnter amount and select transaction\n"
076:                   + "A = Withdraw\nB = Deposit\nC = Cancel");
077:          }
078:          catch (SQLException exception)
079:          {
080:             JOptionPane.showMessageDialog(null,
081:                   "Database error");
082:          }
083:    }
084:    
085:    private class AButtonListener implements ActionListener
086:    {  
087:       public void actionPerformed(ActionEvent event)
088:       {  
089:          int state = theATM.getState();
090:          if (state == ATM.START)
091:             theATM.setCustomerNumber((int) pad.getValue());
092:          else if (state == ATM.PIN)
093:          {
094:             try
095:             {
096:                theATM.selectCustomer((int) pad.getValue());
097:             }
098:             catch (SQLException exception)
099:             {
100:                JOptionPane.showMessageDialog(null,
101:                      "Database error");
102:             }
103:          }
104:          else if (state == ATM.ACCOUNT)
105:             theATM.selectAccount(ATM.CHECKING);
106:          else if (state == ATM.TRANSACT)
107:          {
108:             try
109:             {
110:                theATM.withdraw(pad.getValue());               
111:             }
112:             catch (SQLException exception)
113:             {
114:                JOptionPane.showMessageDialog(null,
115:                      "Database error");
116:             }
117:             theATM.back();
118:          }
119:          showState();
120:       }
121:    }
122:    
123:    private class BButtonListener implements ActionListener
124:    {  
125:       public void actionPerformed(ActionEvent event)
126:       {  
127:          int state = theATM.getState();
128:          if (state == ATM.ACCOUNT)
129:             theATM.selectAccount(ATM.SAVINGS);
130:          else if (state == ATM.TRANSACT)
131:          {
132:             try
133:             {
134:                theATM.deposit(pad.getValue());
135:             }
136:             catch (SQLException exception)
137:             {
138:                JOptionPane.showMessageDialog(null,
139:                      "Database error");
140:             }
141:             theATM.back();
142:          }
143:          showState();
144:       }
145:    }
146: 
147:    private class CButtonListener implements ActionListener
148:    {  
149:       public void actionPerformed(ActionEvent event)
150:       {  
151:          int state = theATM.getState();
152:          if (state == ATM.ACCOUNT)
153:             theATM.reset();
154:          else if (state == ATM.TRANSACT)
155:             theATM.back();
156:          showState();
157:       }
158:    }
159: 
160:    private JButton aButton;
161:    private JButton bButton;
162:    private JButton cButton;
163:    
164:    private KeyPad pad;
165:    private JTextArea display;
166: 
167:    private ATM theATM;
168: 
169:    private static final int FRAME_WIDTH = 300;
170:    private static final int FRAME_HEIGHT = 400;
171: }