Gui Atm
/* This is a GUI atm program Pin is 111 * */ import javax.swing.*; import java.awt.event.*; import java.awt.*; // Needed for the layout class public class GuiAtm extends JFrame { JPanel numpanel; JPanel textpanel; JPanel mainpanel; JButton one; JButton two; JButton zero; JButton three; JButton four; JButton five; JButton six; JButton seven; JButton eight; JButton nine; JButton enter; JButton clear; JTextArea textarea; boolean value = true; public GuiAtm() { // Sets the title setTitle("Enter Pin Number"); // Sets the size of the window setSize(300, 200); // Sets the location of the frame to middle setLocationRelativeTo(null); // Sets the action when the window is close setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creates a text area textarea = new JTextArea("", 1, 15); // Create the number buttons zero = new JButton("0"); one = new JButton("1"); two = new JButton("2"); three = new JButton("3"); four = new JButton("4"); five = new JButton("5"); six = new JButton("6"); seven = new JButton("7"); eight = new JButton("8"); nine = new JButton("9"); enter = new JButton("Enter"); clear = new JButton("Clear"); // Create three panels mainpanel = new JPanel(); numpanel = new JPanel(); textpanel = new JPanel(); // Add the text area to textpanel textpanel.add(textarea); // Sets the layout for the main panel mainpanel.setLayout(new BorderLayout()); // Sets the layout for the number panel numpanel.setLayout(new FlowLayout(FlowLayout.CENTER)); // Add the buttons to number panel numpanel.add(zero); numpanel.add(one); numpanel.add(two); numpanel.add(three); numpanel.add(four); numpanel.add(five); numpanel.add(six); numpanel.add(seven); numpanel.add(eight); numpanel.add(nine); numpanel.add(clear); numpanel.add(enter); // Adds textpanel and number panel to the main panel mainpanel.add(textpanel, BorderLayout.NORTH); mainpanel.add(numpanel, BorderLayout.CENTER); // Adds the main panel to the content pane add(mainpanel); // Create an event listener for the buttons zero.addActionListener(new ButtonListener()); one.addActionListener(new ButtonListener()); two.addActionListener(new ButtonListener()); three.addActionListener(new ButtonListener()); four.addActionListener(new ButtonListener()); five.addActionListener(new ButtonListener()); six.addActionListener(new ButtonListener()); seven.addActionListener(new ButtonListener()); eight.addActionListener(new ButtonListener()); nine.addActionListener(new ButtonListener()); enter.addActionListener(new ButtonListener()); clear.addActionListener(new ButtonListener()); // Set the visibility of the window setVisible(value); } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { // Append texts to text area if a number button is clicked if(e.getSource( )== zero) { textarea.append("0"); } if(e.getSource( )== one) { textarea.append("1"); } if(e.getSource() == two) { textarea.append("2"); } if(e.getSource() == three) { textarea.append("3"); } if(e.getSource() == four) { textarea.append("4"); } if(e.getSource() == five) { textarea.append("5"); } if(e.getSource() == six) { textarea.append("6"); } if(e.getSource() == seven) { textarea.append("7"); } if(e.getSource() == eight) { textarea.append("8"); } if(e.getSource() == nine) { textarea.append("9"); } // Clears everything on the text area if(e.getSource() == clear) { textarea.setText(""); } if(e.getSource()==enter) { // Convert the Strings into integer int num = Integer.parseInt(textarea.getText()); /* If pin matches call the main atm class Then set the value to be false So the window would disappear With only the main atm class window appear * */ if(num==111) { value = false; setVisible(value); MainAtm matm = new MainAtm(); } // If pin doesn't match ask the user to re-enter pin else { JOptionPane.showMessageDialog(null, "Pin does not match\n Please re-enter"); } } } catch(NumberFormatException ae) { } } } public static void main(String[] args) { new GuiAtm(); } } /* This is the main atm program The user does all the transaction in here Start money is 1000.00 * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainAtm extends JFrame { JPanel panel; static double amount = 1000.00; JButton dep; JButton wi; JButton cb; JButton x; public MainAtm() { // Sets the title of the atm setTitle("Welcome to the ATM"); // Sets the size of the atm setSize(300, 200); // Sets the action when the window is close setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets the location of the window to the middle of the screen setLocationRelativeTo(null); // Create 4 buttons dep = new JButton("Deposit"); wi = new JButton("WithDraw"); cb = new JButton("Check Balance"); x = new JButton("Exit"); // Create a panel and add the buttons to the panel panel = new JPanel(); panel.add(dep); panel.add(wi); panel.add(cb); panel.add(x); // Add the panel to the content pane add(panel); // Create event listener for the buttons dep.addActionListener(new ButtonListener()); wi.addActionListener(new ButtonListener()); cb.addActionListener(new ButtonListener()); x.addActionListener(new ButtonListener()); // Sets the visibility of the window setVisible(true); } // Add the deposit money to the amount public static void setDeposit(double newamount) { amount += newamount; } // Subtract the withdraw money from the amount public static void setWithDraw(double newamount) { amount -= newamount; } // Returns the amount of the money public static double getAmount() { return amount; } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Checks for the balance if(e.getSource() == cb) { JOptionPane.showMessageDialog(null, "Your current balance is " + getAmount()); } // Ask the user how much they want to deposit if(e.getSource() == dep) { String getamount = JOptionPane.showInputDialog("How much do you want to deposit"); double depamount = Double.parseDouble(getamount); setDeposit(depamount); } // Ask the user how much they want to withdraw if(e.getSource() == wi) { String takeamount = JOptionPane.showInputDialog("How much do you want to withdraw"); double wiamount = Double.parseDouble(takeamount); setWithDraw(wiamount); } // Exit the program if(e.getSource() == x) { JOptionPane.showMessageDialog(null, "Thank you for using the program"); System.exit(0); } } } }
ClickHere to download GuiAtm.java
ClickHere to download MainAtm.java
Need to download both java and store in the same directory in order for the program to work...Compile both java then run the GuiAtm.java
< Java > < GUI > < Home >