001: import java.awt.Container;
002: import java.awt.FlowLayout;
003: import java.awt.GridLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.io.FileInputStream;
007: import java.io.IOException;
008: import java.sql.SQLException;
009: import java.util.Properties;
010: import javax.swing.JButton;
011: import javax.swing.JFrame;
012: import javax.swing.JOptionPane;
013: import javax.swing.JPanel;
014: import javax.swing.JTextArea;
015: 
016: /**
017:    A frame displaying the components of an ATM
018: */
019: class ATM extends JFrame
020: {  
021:    /**
022:       Constructs the user interface of the ATM application.
023:    */
024:    public ATM()
025:    {  
026:       theBank = new Bank();
027:    
028:       // construct components
029: 
030:       pad = new KeyPad();
031: 
032:       display = new JTextArea(4, 20);
033:       
034:       aButton = new JButton("  A  ");
035:       aButton.addActionListener(new AButtonListener());
036: 
037:       bButton = new JButton("  B  ");
038:       bButton.addActionListener(new BButtonListener());
039: 
040:       cButton = new JButton("  C  ");
041:       cButton.addActionListener(new CButtonListener());
042:       
043:       // add components to content pane
044: 
045:       JPanel buttonPanel = new JPanel();
046:       buttonPanel.setLayout(new GridLayout(3, 1));
047:       buttonPanel.add(aButton);
048:       buttonPanel.add(bButton);
049:       buttonPanel.add(cButton);
050:       
051:       Container contentPane = getContentPane();
052:       contentPane.setLayout(new FlowLayout());
053:       contentPane.add(pad);
054:       contentPane.add(display);
055:       contentPane.add(buttonPanel);
056:       try
057:       {
058:          setNextState(START_STATE);      
059:       }
060:       catch (SQLException exception)
061:       {
062:          JOptionPane.showMessageDialog(null, 
063:             exception.getMessage());
064:       }
065:    }
066:    
067:    /** 
068:       Sets the current customer number to the keypad value
069:       and sets state to PIN.
070:    */
071:    public void setCustomerNumber() throws SQLException
072:    {  
073:       customerNumber = (int)pad.getValue();
074:       setNextState(PIN_STATE);
075:    }
076: 
077:    /** 
078:       Gets PIN from keypad, finds customer in bank.
079:       If found sets state to ACCOUNT, else to START.
080:    */
081:    public void selectCustomer() throws SQLException
082:    {  
083:       int pin = (int)pad.getValue();
084:       currentCustomer 
085:          = theBank.find(customerNumber, pin);
086:       if (currentCustomer == null) 
087:          setNextState(START_STATE);
088:       else 
089:          setNextState(ACCOUNT_STATE);
090:    }
091:    
092:    /** 
093:       Sets current account to checking or savings. Sets 
094:       state to TRANSACT
095:       @param account one of CHECKING_ACCOUNT or SAVINGS_ACCOUNT
096:    */
097:    public void selectAccount(int account) throws SQLException
098:    {
099:       if (account == CHECKING_ACCOUNT)
100:          currentAccount
101:             = currentCustomer.getCheckingAccount();
102:       else
103:          currentAccount
104:             = currentCustomer.getSavingsAccount();
105:       setNextState(TRANSACT_STATE);
106:    }
107: 
108:    /** 
109:       Withdraws amount typed in keypad from current account. 
110:       Sets state to ACCOUNT. 
111:    */
112:    public void withdraw() throws SQLException
113:    {  
114:       currentAccount.withdraw(pad.getValue());
115:       setNextState(ACCOUNT_STATE);
116:    }
117: 
118:    /** 
119:       Deposits amount typed in keypad to current account. 
120:       Sets state to ACCOUNT. 
121:    */
122:    public void deposit() throws SQLException
123:    {  
124:       currentAccount.deposit(pad.getValue());
125:       setNextState(ACCOUNT_STATE);
126:    }
127: 
128:    /** 
129:       Sets state and updates display message.
130:       @param state the next state
131:    */
132:    public void setNextState(int newState)
133:       throws SQLException
134:    {  
135:       state = newState;
136:       pad.clear();
137:       if (state == START_STATE)
138:          display.setText("Enter customer number\nA = OK");
139:       else if (state == PIN_STATE)
140:          display.setText("Enter PIN\nA = OK");
141:       else if (state == ACCOUNT_STATE)
142:          display.setText("Select Account\n" 
143:            + "A = Checking\nB = Savings\nC = Exit");
144:       else if (state == TRANSACT_STATE)
145:          display.setText("Balance = " 
146:             + currentAccount.getBalance() 
147:             + "\nEnter amount and select transaction\n"
148:             + "A = Withdraw\nB = Deposit\nC = Cancel");
149:    }
150: 
151:    private class AButtonListener implements ActionListener
152:    {  
153:       public void actionPerformed(ActionEvent event)
154:       {  
155:          try
156:          {
157:             if (state == START_STATE)
158:                setCustomerNumber();
159:             else if (state == PIN_STATE)
160:                selectCustomer();
161:             else if (state == ACCOUNT_STATE)
162:                selectAccount(CHECKING_ACCOUNT);
163:             else if (state == TRANSACT_STATE)
164:                withdraw();
165:          }
166:          catch (SQLException exception)
167:          {
168:             JOptionPane.showMessageDialog(null, 
169:                exception.getMessage());
170:          }
171:       }
172:    }
173:    
174:    private class BButtonListener implements ActionListener
175:    {  
176:       public void actionPerformed(ActionEvent event)
177:       {  
178:          try
179:          {
180:             if (state == ACCOUNT_STATE)
181:                selectAccount(SAVINGS_ACCOUNT);
182:             else if (state == TRANSACT_STATE)
183:                deposit();
184:          }
185:          catch (SQLException exception)
186:          {
187:             JOptionPane.showMessageDialog(null, 
188:                exception.getMessage());
189:          }
190:       }
191:    }
192: 
193:    private class CButtonListener implements ActionListener
194:    {  
195:       public void actionPerformed(ActionEvent event)
196:       {  
197:          try
198:          {
199:             if (state == ACCOUNT_STATE)
200:                setNextState(START_STATE);
201:             else if (state == TRANSACT_STATE)
202:                setNextState(ACCOUNT_STATE);
203:          }
204:          catch (SQLException exception)
205:          {
206:             JOptionPane.showMessageDialog(null, 
207:                exception.getMessage());
208:          }
209:       }
210:    }
211: 
212:    private int state;
213:    private int customerNumber;
214:    private Bank theBank;
215:    private Customer currentCustomer;
216:    private BankAccount currentAccount;
217:    
218:    private JButton aButton;
219:    private JButton bButton;
220:    private JButton cButton;
221:    
222:    private KeyPad pad;
223:    private JTextArea display;
224:    
225:    private static final int START_STATE = 1;
226:    private static final int PIN_STATE = 2;
227:    private static final int ACCOUNT_STATE = 3;
228:    private static final int TRANSACT_STATE = 4;
229: 
230:    private static final int CHECKING_ACCOUNT = 1;
231:    private static final int SAVINGS_ACCOUNT = 2;
232: }