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