JAVA Problem How to click the withdraw button 3 times and m

JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program already runs without issues.

GUI.java

package atmMachine;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GUI implements ActionListener {

   // Creating objects
   final private JFrame frame;
   final private JButton withdraw;
   final private JButton deposit;
   final private JButton transfer;
   final private JButton balance;
   final private JButton exitProgram;
   private JRadioButton checking;
   private JRadioButton savings;
   private static JTextField output;
   final private JPanel panel;
   private static Account checkingAccount;
   private static Account savingsAccount;
   private static Account currentAccount;
   private static boolean exitLoop = false;
   private int clicks;
  
  
  
   public static void main(String args[]) {
       GUI someObj = new GUI();
   }
  
  
   public GUI() {
      
       // Placing a title on the ATM\'s GUI
       frame = new JFrame(\"ATM Machine\");
      
       // Creating the buttons on the panel
       deposit = new JButton(\"Deposit\");
       transfer = new JButton(\"Transfer to\");
       withdraw = new JButton(\"Withdraw\");
       balance = new JButton(\"Balance\");
       exitProgram = new JButton(\"Exit\");
      
       // Creating the output text field
       output = new JTextField();
      
       // Creating the panel for which to hold the buttons.
       panel = new JPanel();
      
       // Setting the amounts in the checing and savings accounts,
       // and also setting the currently selected account\'s value.
       checkingAccount = new Account(1000);
       savingsAccount = new Account(2000);
       currentAccount = new Account(0);

      
       // Add event listener
       exitProgram.addActionListener(this);
       transfer.addActionListener(this);
       deposit.addActionListener(this);
       balance.addActionListener(this);
       withdraw.addActionListener(this);
      
      
       // Creates Radio Button Option for Checking and Savings
       checking = new JRadioButton(\"Checking\");
       savings = new JRadioButton(\"Savings\");
      
      
       // This method determines if the user selects the Checking Account Radio Button.
       checking.addActionListener(new ActionListener() {
      
       public void actionPerformed(ActionEvent e) {
              
               if (checking.isSelected()) {
                   currentAccount = checkingAccount;
                   savings.setSelected(false);
                   exitLoop = true;
               }
           }
       }
       ); // END Checking Action Listener.
      
      
       // This method determines if the user selects the Savings Account Radio Button.
       savings.addActionListener(new ActionListener() {
          
           public void actionPerformed(ActionEvent e) {
              
               if (savings.isSelected()) {
                   currentAccount = savingsAccount;
                   checking.setSelected(false);
                   exitLoop = true;
                  
               }
           }
       }
       );// END Savings Action Listener.
      
      
       // Adds buttons to the panel
      
      
       //panel.setLayout(new GridLayout(4, 4));
       panel.add(checking);
       panel.add(savings);
       panel.add(deposit);
       panel.add(transfer);
       panel.add(balance);
       panel.add(withdraw);
       panel.add(exitProgram);
       panel.add(output, BorderLayout.CENTER);
       panel.setLayout(new GridLayout(4, 4));
      
      
      
       // Storing the panel in a parent object, the panel cannot exist alone. Stored within
       // ATM\'s GUI(JFrame)
       frame.add(panel);
       frame.pack();
       frame.setSize(800, 400);
       // Centers the ATM\'s GUI(the JFrame) on a screen with any resolution, the pack(),
       // and size() methods must come first for it to be centered.
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
      
   }

  
   // Decides with a switch which button the user clicked.
   public void actionPerformed(ActionEvent e) {
      
       int i;
       int count = 1;
       if(exitLoop == true) {
       for(i=0; i        count =+ i;
       switch (e.getActionCommand()) {
      
       case \"Withdraw\": {
          
           String amount = JOptionPane.showInputDialog(\"Please enter the amount to withdraw. \");
          
           try {
               currentAccount.withdraw(Double.parseDouble(amount));
               output.setText(\"$\" + amount + \" The withdrawal was successful! Take our cash.\");
           }
          
           catch (NumberFormatException e1) {
               output.setText(\"Input must be Integer!\");
           }
          
           catch (InsufficentFunds e1) {
               output.setText(e1.getMessage());
           }
           break;
       }
      
       case \"Deposit\": {
          
           String amount = JOptionPane
                   .showInputDialog(\"Enter deposit amount \");
          
           try {
               currentAccount.deposit(Double.parseDouble(amount));
               output.setText(\"$\" + amount + \" Deposited\");
           }
          
           catch (NumberFormatException e1) {
               output.setText(\"You must enter an integer.\");
           }
           break;
       }
      
       case \"Transfer to\": {
          
           String amount = JOptionPane
                   .showInputDialog(\"Please enter the transfer amount.\");
          
           if (currentAccount.equals(savingsAccount)) {
              
               try {
                   currentAccount.deposit(Double.parseDouble(amount));
                   checkingAccount.withdraw(Double.parseDouble(amount));
                   output.setText(\"$\" + amount
                           + \" transferred to saving account\");
               }
              
               catch (NumberFormatException e1) {
                   output.setText(\"Input must be Integer!\");
               }
              
               catch (InsufficentFunds e1) {
                   output.setText(e1.getMessage());
               }

           }
          
           else {
              
               try {
                   currentAccount.deposit(Double.parseDouble(amount));
                   savingsAccount.withdraw(Double.parseDouble(amount));
                   output.setText(\"$\" + amount
                           + \" transferred to checking account\");
               }
              
               catch (NumberFormatException e1) {
                   output.setText(\"Input must be Integer!\");
               }
              
               catch (InsufficentFunds e1) {
                   output.setText(e1.getMessage());
               }
           }
           break;
       }
      
       // Gives the user the balance of the currently selected account.
       case \"Balance\":
          
           NumberFormat formatter = new DecimalFormat(\"#0.00\");
           JFrame frame = new JFrame(\"JOptionPane showMessageDialog example\");
           JOptionPane.showMessageDialog(frame, \"The current balance is $\"
                   + formatter.format(currentAccount.getBalance()));
           break;
          
       default:
           break;
       }
       }
       }
      
       // This switch exits the program when the user clicks the exit button.
       switch(e.getActionCommand()) {
       case \"Exit\":
           System.exit(0);
       default:
           break;
          
       }
      }
  
}//END FILE

Account.java

package atmMachine;

public class Account {
  
   // Creating and initializing variables for transaction free and account balance.
   private static final double transactionFee = 1.5;
   private double acctBal = 0;
   int j = 1;
  
   public Account(double acctBal) {
       this.acctBal = acctBal;
   }
  
   // Setters
   public void setBalance(double acctBal) {
       this.acctBal = acctBal;
   }
  
   // Getters
   public double getBalance() {
       return this.acctBal;
   }
  
   // Deposit method
   public void deposit(double amount) {
       acctBal += amount;
   }
  
  
   // This method is one for handling the withdrawals, it also throws exceptions for insufficent funds
   // and determines and subtracts the transaction fee.
   public void withdraw(double amount) throws InsufficentFunds {
              
               if(amount%20 !=0) {
                   throw new InsufficentFunds(\"The amount withdrawn may only be in denominations of 20, thank you.\");
               }
      
               else if(this.acctBal > acctBal) {
                   throw new InsufficentFunds(\"Insufficent Funds! Please try again.\");
               }
              
          
               else if(this.acctBal >= amount + transactionFee) {
                  
                   this.acctBal = this.acctBal - amount;
      
                   if(j%4 == 0)
                       this.acctBal = this.acctBal - transactionFee;
                  
                   else
                       j++;
               }
              
               else
                   System.out.println(\"Insufficent Funds!\");
   }
  
}// END FILE

InsufficentFunds.java

package atmMachine;

public class InsufficentFunds extends Exception{
  
  
   private static final long serialVersionUID = 1L;

   public InsufficentFunds(String e2) {
       super(e2);
   }

}// END FILE

Solution

package atmMachine;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GUI implements ActionListener {

   // Creating objects
   final private JFrame frame;
   final private JButton withdraw;
   final private JButton deposit;
   final private JButton transfer;
   final private JButton balance;
   final private JButton exitProgram;
   private JRadioButton checking;
   private JRadioButton savings;
   private static JTextField output;
   final private JPanel panel;
   private static Account checkingAccount;
   private static Account savingsAccount;
   private static Account currentAccount;
   private static boolean exitLoop = false;
   private int clicks;
  
  
  
   public static void main(String args[]) {
       GUI someObj = new GUI();
   }
  
  
   public GUI() {
      
       // Placing a title on the ATM\'s GUI
       frame = new JFrame(\"ATM Machine\");
      
       // Creating the buttons on the panel
       deposit = new JButton(\"Deposit\");
       transfer = new JButton(\"Transfer to\");
       withdraw = new JButton(\"Withdraw\");
       balance = new JButton(\"Balance\");
       exitProgram = new JButton(\"Exit\");
      
       // Creating the output text field
       output = new JTextField();
      
       // Creating the panel for which to hold the buttons.
       panel = new JPanel();
      
       // Setting the amounts in the checing and savings accounts,
       // and also setting the currently selected account\'s value.
       checkingAccount = new Account(1000);
       savingsAccount = new Account(2000);
       currentAccount = new Account(0);

      
       // Add event listener
       exitProgram.addActionListener(this);
       transfer.addActionListener(this);
       deposit.addActionListener(this);
       balance.addActionListener(this);
       withdraw.addActionListener(this);
      
      
       // Creates Radio Button Option for Checking and Savings
       checking = new JRadioButton(\"Checking\");
       savings = new JRadioButton(\"Savings\");
      
      
       // This method determines if the user selects the Checking Account Radio Button.
       checking.addActionListener(new ActionListener() {
      
       public void actionPerformed(ActionEvent e) {
              
               if (checking.isSelected()) {
                   currentAccount = checkingAccount;
                   savings.setSelected(false);
                   exitLoop = true;
               }
           }
       }
       ); // END Checking Action Listener.
      
      
       // This method determines if the user selects the Savings Account Radio Button.
       savings.addActionListener(new ActionListener() {
          
           public void actionPerformed(ActionEvent e) {
              
               if (savings.isSelected()) {
                   currentAccount = savingsAccount;
                   checking.setSelected(false);
                   exitLoop = true;
                  
               }
           }
       }
       );// END Savings Action Listener.
      
      
       // Adds buttons to the panel
      
      
       //panel.setLayout(new GridLayout(4, 4));
       panel.add(checking);
       panel.add(savings);
       panel.add(deposit);
       panel.add(transfer);
       panel.add(balance);
       panel.add(withdraw);
       panel.add(exitProgram);
       panel.add(output, BorderLayout.CENTER);
       panel.setLayout(new GridLayout(4, 4));
      
      
      
       // Storing the panel in a parent object, the panel cannot exist alone. Stored within
       // ATM\'s GUI(JFrame)
       frame.add(panel);
       frame.pack();
       frame.setSize(800, 400);
       // Centers the ATM\'s GUI(the JFrame) on a screen with any resolution, the pack(),
       // and size() methods must come first for it to be centered.
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
      
   }

  
   // Decides with a switch which button the user clicked.
   public void actionPerformed(ActionEvent e) {
      
       int i;
       int count = 1;
       if(exitLoop == true) {
       for(i=0; i        count =+ i;
       switch (e.getActionCommand()) {
      
       case \"Withdraw\": {
          
           String amount = JOptionPane.showInputDialog(\"Please enter the amount to withdraw. \");
          
           try {
               currentAccount.withdraw(Double.parseDouble(amount));
               output.setText(\"$\" + amount + \" The withdrawal was successful! Take our cash.\");
           }
          
           catch (NumberFormatException e1) {
               output.setText(\"Input must be Integer!\");
           }
          
           catch (InsufficentFunds e1) {
               output.setText(e1.getMessage());
           }
           break;
       }
      
       case \"Deposit\": {
          
           String amount = JOptionPane
                   .showInputDialog(\"Enter deposit amount \");
          
           try {
               currentAccount.deposit(Double.parseDouble(amount));
               output.setText(\"$\" + amount + \" Deposited\");
           }
          
           catch (NumberFormatException e1) {
               output.setText(\"You must enter an integer.\");
           }
           break;
       }
      
       case \"Transfer to\": {
          
           String amount = JOptionPane
                   .showInputDialog(\"Please enter the transfer amount.\");
          
           if (currentAccount.equals(savingsAccount)) {
              
               try {
                   currentAccount.deposit(Double.parseDouble(amount));
                   checkingAccount.withdraw(Double.parseDouble(amount));
                   output.setText(\"$\" + amount
                           + \" transferred to saving account\");
               }
              
               catch (NumberFormatException e1) {
                   output.setText(\"Input must be Integer!\");
               }
              
               catch (InsufficentFunds e1) {
                   output.setText(e1.getMessage());
               }

           }
          
           else {
              
               try {
                   currentAccount.deposit(Double.parseDouble(amount));
                   savingsAccount.withdraw(Double.parseDouble(amount));
                   output.setText(\"$\" + amount
                           + \" transferred to checking account\");
               }
              
               catch (NumberFormatException e1) {
                   output.setText(\"Input must be Integer!\");
               }
              
               catch (InsufficentFunds e1) {
                   output.setText(e1.getMessage());
               }
           }
           break;
       }
      
       // Gives the user the balance of the currently selected account.
       case \"Balance\":
          
           NumberFormat formatter = new DecimalFormat(\"#0.00\");
           JFrame frame = new JFrame(\"JOptionPane showMessageDialog example\");
           JOptionPane.showMessageDialog(frame, \"The current balance is $\"
                   + formatter.format(currentAccount.getBalance()));
           break;
          
       default:
           break;
       }
       }
       }
      
       // This switch exits the program when the user clicks the exit button.
       switch(e.getActionCommand()) {
       case \"Exit\":
           System.exit(0);
       default:
           break;
          
       }
      }
  
}//END FILE

Account.java

package atmMachine;

public class Account {
  
   // Creating and initializing variables for transaction free and account balance.
   private static final double transactionFee = 1.5;
   private double acctBal = 0;
   int j = 1;
  
   public Account(double acctBal) {
       this.acctBal = acctBal;
   }
  
   // Setters
   public void setBalance(double acctBal) {
       this.acctBal = acctBal;
   }
  
   // Getters
   public double getBalance() {
       return this.acctBal;
   }
  
   // Deposit method
   public void deposit(double amount) {
       acctBal += amount;
   }
  
  
   // This method is one for handling the withdrawals, it also throws exceptions for insufficent funds
   // and determines and subtracts the transaction fee.
   public void withdraw(double amount) throws InsufficentFunds {
              
               if(amount%20 !=0) {
                   throw new InsufficentFunds(\"The amount withdrawn may only be in denominations of 20, thank you.\");
               }
      
               else if(this.acctBal > acctBal) {
                   throw new InsufficentFunds(\"Insufficent Funds! Please try again.\");
               }
              
          
               else if(this.acctBal >= amount + transactionFee) {
                  
                   this.acctBal = this.acctBal - amount;
      
                   if(j%4 == 0)
                       this.acctBal = this.acctBal - transactionFee;
                  
                   else
                       j++;
               }
              
               else
                   System.out.println(\"Insufficent Funds!\");
   }
  
}// END FILE

InsufficentFunds.java

package atmMachine;

public class InsufficentFunds extends Exception{
  
  
   private static final long serialVersionUID = 1L;

   public InsufficentFunds(String e2) {
       super(e2);
   }

}

// END FILE

JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a
JAVA - Problem: How to click the withdraw button 3 times, and make it work on the third click? I cannot figure it out and cannot find an example. This program a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site