//Jeremy Socia //5/6/02 //Final Project //ATM Machine //class AtmTester import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class AtmTester extends JFrame{ //aggregation relatioship of account manager AccMan manager; //declare all J components private JLabel nameL, pinL, instructL; private JTextField nameF, instructF, quitF; private JButton enterButton, quitButton; private JPasswordField pinF; private JRadioButton deposit, withdrawal; private JComboBox transType; private JList moneyList; private JTextArea outputArea; private Container container; private JPanel inputPanel, tranPanel, statusPanel; private FlowLayout tranLayout, statusLayout; private JScrollPane scroller; private ButtonGroup radiogrp; // arrays for the combobox and list private String types[] = {"Savings", "Checking", "Loan"}; private String listVals[] = {"10.00", "20.00", "30.00", "40.00"}; //all other variables i need private String comboBox; private String list; private double listNum; private boolean dep; private int type; private boolean go; public AtmTester() { super( "Welcome to the Last National Bank"); comboBox = ""; list = ""; listNum = 0; dep = true; type = 1; go = true; //declare new account manager and handlers manager = new AccMan(); ActionEventHandler handler = new ActionEventHandler(); RHandler rHandler = new RHandler(); ListSelectHandler listSel = new ListSelectHandler(); //get border layout for container container = getContentPane(); container.setLayout(new BorderLayout()); // set panels up inputPanel = new JPanel(); inputPanel.setLayout(new GridLayout(3,2)); tranPanel = new JPanel(); tranLayout = new FlowLayout(); tranLayout.setAlignment(FlowLayout.CENTER); tranPanel.setLayout(tranLayout); statusPanel = new JPanel(); statusLayout = new FlowLayout(); statusLayout.setAlignment(FlowLayout.CENTER); statusPanel.setLayout(new FlowLayout()); //set name setup nameL = new JLabel( "Enter Name" ); nameF = new JTextField(); inputPanel.add(nameL); inputPanel.add(nameF); //set pin setup pinL = new JLabel( "Enter Pin Number" ); pinF = new JPasswordField(); inputPanel.add(pinL); pinF.addActionListener(handler); inputPanel.add(pinF); //set instructions setup instructL = new JLabel( "Instructions:" ); instructF = new JTextField(); inputPanel.add(instructL); inputPanel.add(instructF); //set combobox setup transType = new JComboBox( types ); transType.setMaximumRowCount(3); transType.addItemListener(rHandler); tranPanel.add(transType); //set list setup moneyList = new JList( listVals ); moneyList.setVisibleRowCount(4); moneyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); moneyList.addListSelectionListener(listSel); tranPanel.add(moneyList); //set radio buttons deposit = new JRadioButton("deposit",true); deposit.addItemListener(rHandler); tranPanel.add(deposit); withdrawal = new JRadioButton("withdrawal", false); withdrawal.addItemListener(rHandler); tranPanel.add(withdrawal); //make button grp so only one can be selected at once radiogrp = new ButtonGroup(); radiogrp.add(deposit); radiogrp.add(withdrawal); // enter button setup enterButton = new JButton( "Enter" ); enterButton.addActionListener( handler ); tranPanel.add(enterButton); // quit button setup quitF = new JTextField(30); statusPanel.add(quitF); quitButton = new JButton( "Quit" ); quitButton.addActionListener( handler ); statusPanel.add(quitButton); //put all panels in container container.add(inputPanel, BorderLayout.NORTH); container.add(tranPanel, BorderLayout.CENTER); container.add(statusPanel, BorderLayout.SOUTH); //outputArea object outputArea = new JTextArea(30, 10); scroller = new JScrollPane(outputArea); }// end constructor private class RHandler implements ItemListener{ public void itemStateChanged(ItemEvent event)//method to take handling { if(event.getSource() == transType) { if(event.getStateChange() == ItemEvent.SELECTED); { comboBox = types[transType.getSelectedIndex()]; if(comboBox.equals("Savings")) type = 1; else if(comboBox.equals("Checking")) type = 2; else if(comboBox.equals("Loan")) type = 3; } } else if(event.getSource() == deposit) dep = true; else if(event.getSource() == withdrawal) dep = false; } } private class ListSelectHandler implements ListSelectionListener{ public void valueChanged(ListSelectionEvent event) { list = listVals[moneyList.getSelectedIndex()]; if(list.equals("10.00")) listNum = 10; else if(list.equals("20.00")) listNum = 20; else if(list.equals("30.00")) listNum = 30; else if(list.equals("40.00")) listNum = 40; } } private class ActionEventHandler implements ActionListener{ public void actionPerformed(ActionEvent event)//method to take handling { String name = ""; int pin = 0; String pinS; instructF.setText(""); if(event.getSource() == pinF) { name = nameF.getText();//takes name pinS = new String(pinF.getPassword());//gets password to string pin = Integer.parseInt(pinS);//makes string an integer if(manager.findAcc(name,pin))//if name is on database, go { instructF.setText("Input transaction."); go = true; } else { instructF.setText("Invalid pin!"); go = false; } nameF.setText(""); pinF.setText(""); quitF.setText(""); } else if(event.getSource() == enterButton && go)//if found name, then update account { String output = ""; output = manager.updateAcc(dep, listNum, type); quitF.setText(output);//shows thank you message } else if(event.getSource() == quitButton)//if quit, then print out array { outputArea.setText(manager.outputAcc());//send out joptionpane to see acount info JOptionPane.showMessageDialog(null, scroller, "Account Information", JOptionPane.INFORMATION_MESSAGE); } } } public static void main(String args[]) { AtmTester test = new AtmTester(); //use window adapter to close window with "x" button test.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent event) { System.exit(0); } } ); test.setSize(500, 300); test.setVisible(true); } }