Advertisement
Guest User

ATM

a guest
Apr 18th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.18 KB | None | 0 0
  1. package Money;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.text.DecimalFormat;
  7. import java.util.*;
  8. import javax.swing.*;
  9.  
  10.  
  11. public class guiAtm extends JFrame {
  12.     a
  13.     //Global variables
  14.     //These variables can be called on at any point of the program
  15.     static double userBal; //Used to store the user's balance
  16.     static DecimalFormat df = new DecimalFormat("####0.00");
  17.    
  18.    
  19.     static JTextField text;
  20.     static JLabel labelMessage;
  21.     private static JButton buttonSubmit;
  22.     static JPanel panel = new JPanel();
  23.     static JTextField ddouble = new JTextField();
  24.    
  25.    
  26.     public guiAtm(){ //This constructor calls the UI method and sets parameters of the program's JFrame
  27.        
  28.          panel.setBackground(Color.PINK);
  29.             getContentPane().add(panel);
  30.            
  31.            
  32.            
  33.             JLabel label = new JLabel("Enter Your UserName: ");
  34.             panel.add(label);
  35.            
  36.             text = new JTextField();
  37.             text.setPreferredSize(new Dimension(150, 30));
  38.             panel.add(text);
  39.        
  40.        
  41.         mainMenu2(userBal);
  42.        
  43.  
  44.         setSize(600, 600);
  45.        
  46.         setLocationRelativeTo(null);
  47.        
  48.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49.        
  50.         setResizable(false);
  51.    
  52.        
  53.        
  54.        
  55.     }
  56.  
  57. public static double mainMenu2(double userBal) { // initialize JComboBox(drop-down menu) and is the super method from which all methods in the program derive
  58.         // Makes it easier to test the design of the GUI before adding values/listeners
  59.        
  60.      
  61.      
  62.        
  63.        
  64.         String[] options = { "Deposit", "Withdraw", "Balance Inquiry", "Exit"   };
  65.        
  66.         JComboBox list = new JComboBox(options);   
  67.        
  68.        
  69.        
  70.         labelMessage = new JLabel("Please enter your name!");
  71.         panel.add(labelMessage);
  72.        
  73.        
  74.         buttonSubmit = new JButton("Submit");
  75.         panel.add(buttonSubmit);
  76.         buttonSubmit.addActionListener(new ActionListener() {
  77.            
  78.             @Override
  79.             public void actionPerformed(ActionEvent e) {
  80.                 String name = text.getText();
  81.                 if (name.isEmpty()) {
  82.                     labelMessage.setText("Please Try Again");
  83.                 }else {
  84.                     labelMessage.setText("UserName is: " + name.toUpperCase());
  85.                    
  86.                     //Current balance and mainMenu buttons will only appear after the user has successfully entered their name
  87.                    
  88.                     JLabel labelUserBal = new JLabel("Your current balance is: $" + (df.format(userBal)));
  89.                     panel.add(labelUserBal);
  90.                    
  91.                     panel.add(list); ///adds drop down menu
  92.                    
  93.            
  94.                     list.addActionListener(new ActionListener() {
  95.                        
  96.                         @Override
  97.                         public void actionPerformed(ActionEvent e) {
  98.                                 JComboBox<String> list = (JComboBox<String>) e.getSource();
  99.                                 String selectedOption = (String) list.getSelectedItem();
  100.                                
  101.                                 //Menu options
  102.                          
  103.                                 if (selectedOption.equals("Deposit")) {
  104.                                    
  105.                                     System.out.println("Good choice!");
  106.                                    
  107.                                    
  108.                                  
  109.                                     depositMethod(userBal);
  110.                                    
  111.  
  112.                                    
  113.                                    
  114.                                    //This was that cause of the recurring Submit Button problem
  115.                                    // mainMenu2(userBal);
  116.                                    
  117.                                    
  118.                                      
  119.                                 } else if (selectedOption.equals("Withdraw")) {
  120.                                    
  121.                                     System.out.println("Nice pick, too!");
  122.                                    
  123.                                     withdrawMethod();
  124.                                    
  125.                                    
  126.                                    
  127.                                 } else if  (selectedOption.equals("Balance Inquiry")) {
  128.                                    
  129.                                    
  130.                                     JOptionPane.showMessageDialog(null, "Here is your current balance: $" + (df.format(userBal))); //Creates a dialog box that displays a printed message
  131.                              
  132.                                    
  133.                                    
  134.                                    
  135.                                    
  136.                                    
  137.                                 } else {
  138.                                    
  139.                                     System.exit(0);
  140.                                    
  141.                                     //Create Dialogue box that will ask if you are sure you want to exit. If Yes, then system.exit(0), if No, then kick user back to mainMenu2
  142.                                    
  143.                                 }
  144.                             }
  145.                         });
  146.                    
  147.                    
  148.                    
  149.                 }
  150.                
  151.             }
  152.         });
  153.        
  154.         return userBal;
  155.        
  156.         /*
  157.          * b1.addActionListener(new ActionListener() { //Utilizing addActionListener
  158.          * constructor to make the button perform an action based on the method within
  159.          *
  160.          * public void actionPerformed(ActionEvent e) { //The method that will determine
  161.          * the action of the button
  162.          *
  163.          * depositMethod(userBal);
  164.          *
  165.          * //JOptionPane.showMessageDialog(null, "FinalBoss is a Savage"); //Creates a
  166.          * dialog box that displays a printed message }
  167.          *
  168.          *
  169.          * });
  170.          */
  171.        
  172.        
  173.        
  174.     }
  175.    
  176.     public static void main(String[] args) { //Entry point of program
  177.        
  178.         SwingUtilities.invokeLater(new Runnable() {
  179.            
  180.             @Override
  181.             public void run() {
  182.                 new guiAtm().setVisible(true);;
  183.             }
  184.         });
  185.                
  186.        
  187.          
  188.                  
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. public static double depositMethod (double userBal) {
  198.  
  199.  
  200.     labelMessage.setText("Proceed"); //Why is this needed in order for the rest of the method to appear on the panel?
  201.    
  202.     JLabel jbl = new JLabel("Enter the amount that you would like to deposit today: ");
  203.     panel.add(jbl);
  204.        
  205.     JTextField ddouble = new JTextField();
  206.     panel.add(ddouble);
  207.    
  208.     ddouble.setPreferredSize(new Dimension(200,24));
  209.    
  210.    
  211.    
  212.    
  213.     JButton buttonDeposit = new JButton("Deposit Money");
  214.     panel.add(buttonDeposit);
  215.     buttonDeposit.addActionListener(new ActionListener() {
  216.        
  217.  
  218.         public void actionPerformed(ActionEvent e) {
  219.            
  220.            
  221.             double depAmt = Double.parseDouble(ddouble.getText());
  222.            
  223.            
  224.             depositMath(depAmt);
  225.            
  226.            
  227.             System.out.println(userBal);
  228.            
  229.            
  230.             JOptionPane.showMessageDialog(null, "Your new balance is: $" + (df.format(userBal)) + ". " + "Would you like to make an additional transaction?");
  231.        
  232.            
  233.         }
  234.    
  235.        
  236.     });
  237.    
  238.    
  239.    
  240.    
  241.    
  242.     return userBal;
  243.      
  244.  
  245. }
  246.  
  247.    
  248.    
  249. public static double depositMath(double depAmt) { //new class to hold userBal variable since you cannot pass it to the ActionListener by normal means
  250.    
  251.  
  252.    
  253.    
  254.    
  255.     //double depAmt = Double.valueOf(ddouble.getText());
  256.    
  257.     System.out.println(userBal);
  258.    
  259.     userBal = userBal + depAmt;
  260.    
  261.     System.out.println(userBal);
  262.    
  263.  
  264.     //JOptionPane.showMessageDialog(null, "Your new balance is: $" + (df.format(userBal)) + ". " + "Would you like to make an additional transaction?");
  265.  
  266.    
  267.     return userBal;
  268.    
  269.    
  270.    
  271.      
  272. }
  273.    
  274.  
  275. public static void withdrawMethod() {
  276.    
  277.    
  278.     labelMessage.setText("Withdrawl Activated");
  279.    
  280.     JLabel jbl = new JLabel("Enter the amount that you would like to withdraw today: ");
  281.     panel.add(jbl);
  282.    
  283.     JTextField wdouble = new JTextField();
  284.     panel.add(wdouble);
  285.    
  286.     wdouble.setPreferredSize(new Dimension(200,24));
  287.    
  288.    
  289.     JButton buttonWithdraw = new JButton("Withdraw Money");
  290.     panel.add(buttonWithdraw);
  291.     buttonWithdraw.addActionListener(new ActionListener() {
  292.        
  293.         @Override
  294.         public void actionPerformed(ActionEvent e) {
  295.            
  296.             double withAmt = Double.parseDouble(wdouble.getText());
  297.            
  298.             userBal = userBal - withAmt;
  299.            
  300.             JOptionPane.showMessageDialog(null, "Your new balance is: $" + (df.format(userBal)) + ". " + "Would you like to make an additional transaction?");
  301.            
  302.             return;
  303.            
  304.         }
  305.     });
  306.    
  307.    
  308.    
  309.    
  310.    
  311. }
  312.  
  313.  
  314.  
  315.  
  316.    
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement