1

So I am creating a program which has a standard bank account balance of 500. The program asks the user if they would like to Withdraw or Deposit money, then calculate how much they Withdrew or Deposited and update the current balance. Why is it not working and how would I fix it?

public class MyFrame extends JFrame {

    private JPanel panel;
    private JLabel wordsLabel;
    private JLabel balanceLabel;
    private JLabel choiceLabel;
    private JTextField transactionAmount;
    private JButton depositButton;
    private JButton withdrawButton;
    private double balance;

    public MyFrame() {
        final int FIELD_WIDTH = 10;
        balance = 500;
        panel = new JPanel();
        wordsLabel = new JLabel();
        balanceLabel = new JLabel();
        choiceLabel = new JLabel();
        transactionAmount = new JTextField(FIELD_WIDTH);
        JPanel buttonPanel = new JPanel();
        ButtonGroup myGroup = new ButtonGroup();
        //panel.setLayout(new BorderLayout());
        depositButton = new JButton("Deposit");
        withdrawButton = new JButton("Withdraw");
        transactionAmount.setText("0");
        wordsLabel.setText("Welcome to Wes Banco! Your current balance is: ");
        balanceLabel.setText("500");
        choiceLabel.setText("How much would you like to deposit/withdraw?");
        panel.add(wordsLabel);
        panel.add(balanceLabel);
        panel.add(choiceLabel);
        panel.add(transactionAmount);
        myGroup.add(depositButton);
        myGroup.add(withdrawButton);
        buttonPanel.add(depositButton);
        buttonPanel.add(withdrawButton);
        panel.add(depositButton);

        ButtonListener myListener = new ButtonListener();
        depositButton.addActionListener(myListener);
        withdrawButton.addActionListener(myListener);

        panel.add(buttonPanel);
        this.add(panel);
    }

    class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            double amount = Double.parseDouble(transactionAmount.getText());
            if (amount == 0) {
                JOptionPane.showMessageDialog(null, "Enter an amount");
            }
            if (depositButton.isSelected()) {
                balanceLabel.setText("" + 500 + amount);
                JOptionPane.showMessageDialog(null, 
                     "You have deposited: " + amount);
            }
            if (withdrawButton.isSelected()) {
            }
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Moreover, consider using [BigDecimal](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) for currency related manipulations, instead of using `double`. – nIcE cOw Jul 23 '13 at 12:27

2 Answers2

2

You're not using the right "if" in your actionPerformed method. You should use:

if (event.getSource() == depositButton) {
    //recalculate
}

instead of:

if (depositButton.isSelected()) {
        //recalculate
}

isSelected() is not the right method to know if a JButton was clicked. You have to compare the source of the click event with your button.

mael
  • 2,214
  • 1
  • 19
  • 20
2

There are a few things that go wrong.

  1. You use the wrong way to identify which button was clicked (as pointed out by mael).
  2. You don't update the balance variable (so multiple deposits/withdraws won't have effect).
  3. You compute the new balance with strings. "" + 500 + amount uses string concatenation, rather than addition.

You need something like:

public void actionPerformed(ActionEvent event) {
    double amount = Double.parseDouble(transactionAmount.getText());
    if (amount == 0) {
        JOptionPane.showMessageDialog(null, "Enter an amount");
    } else {
        if (event.getSource() == depositButton) {
            JOptionPane.showMessageDialog(null, 
                 "You have deposited: " + amount);
            balance += amount;
        } else if (event.getSource() == withdrawButton) {
            if (balance < amount) {
                JOptionPane.showMessageDialog(null, 
                     "You cannot withdraw more than your balance."); 
            } else {
                JOptionPane.showMessageDialog(null, 
                     "You have withdrawn: " + amount); 
                balance -= amount;
            }
        }
        balanceLabel.setText(String.valueOf(balance));
    }        
}

You probably also want to format the amount properly, have a look here.

Many users here would also argue that you should use BigDecimal rather than double for financial calculations. But since this is a simple application, double would do fine.

Community
  • 1
  • 1
Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
  • I just saw the information, you had provided regarding BigDecimal usage. Already up voted it :-) – nIcE cOw Jul 23 '13 at 12:28
  • It won't allow me to add an if statement for if the balance is < 0. I was going to make it so if the balance ever dropped below 0, then a message would come up saying there is an insufficient amount of money for that transaction, and force them to re-enter. – vVv Sintherius Jul 23 '13 at 14:53
  • @vVvSintherius I updated the code with a message if the balance is negative. Is that what you mean? – Vincent van der Weele Jul 23 '13 at 17:03
  • @Heuster Well I have that part, but I want to make it so when you attempt to withdraw more than what your current balance is, it will not allow you to do so and will reset the balance to the amount it was before you attempted to withdraw from it. – vVv Sintherius Jul 23 '13 at 18:28
  • 1
    @vVvSintherius : You can use something like `balanceLabel.setText(String.format("$ %10.2f", balance));`, here `10` represents the width of the column in which the balance will be displayed, `.2` refers to the precision. More info on [Formatting](http://docs.oracle.com/javase/tutorial/essential/io/formatting.html) :-) – nIcE cOw Jul 24 '13 at 03:28