0

I have 3 TextFields. One is a totalTF the other is a tenderedTF and the last is a changeTF. I am wondering how to go about taking the total price in the totalTF and allowing the user to enter in the amount they give to the cashier into the tenderedTF, then it should work out the change once the pay button is chosen and display in the changeTF. Here is my code so far. Im trying to do the math then set the changeTF. Any help would be greatly appreciated thanks.

JButton payButton = new JButton("Pay");
        payButton.setBounds(970, 569, 209, 51);
        contentPane.add(payButton);

        // Calculate Change
        changeTF.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                if(e.getSource() == payButton)
                {
                    double change = Double.valueOf(totalTF.getText()) - Double.valueOf(tenderedTF.getText());
                    changeTF.setText(String.valueOf(change));
                }
            }
        });

        tenderedTF.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                if(e.getSource() == payButton)
                {
                    double change = Double.valueOf(totalTF.getText()) - Double.valueOf(tenderedTF.getText());
                    changeTF.setText(String.valueOf(change));
                }
            }
        });
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
Movillian
  • 23
  • 7
  • 1
    `changeTF` and `tenderedTF` will never receive action events fired by `payButton`, they only receive the action events fired by themselves as your code is. Try adding an `ActionListener` to the button. – Arnaud Apr 11 '17 at 14:50
  • 1
    You want the calculation to happen when the pay button is clicked, correct? Then you need an action listener on the button, not on the fields. – lucasvw Apr 11 '17 at 14:52
  • Brilliant!! its working, only it doesn't set it in the changeTF to the correct decimal places. How should i overcome this. Thank You – Movillian Apr 11 '17 at 14:54
  • thankyou @ lucasvw. I have changed it and it is now working. silly me i should have known. – Movillian Apr 11 '17 at 14:55
  • Please take a look at [Representing Monetary Values in Java](http://stackoverflow.com/questions/285680/representing-monetary-values-in-java) – G. Fiedler Apr 11 '17 at 15:03

1 Answers1

0

You want to execute code when user clicks on payButton, so I think you should add the listener on the payButton:

payButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        double change = Double.valueOf(totalTF.getText()) - Double.valueOf(tenderedTF.getText());
        changeTF.setText(String.valueOf(change));
    }
});

I think the listeners you added to textfields can't work because e.getSource() returns the textfield that originated the event, it can't be the payButton.

Edit: New revision to include the question posted in comment:

payButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        double change = Double.valueOf(tenderedTF.getText()) - Double.valueOf(totalTF.getText());
        if(change<0){
            changeTF.setText(String.valueOf(-change) + " missing");
        } else {
            changeTF.setText(String.valueOf(change));
        }
    }
});

Honestly I did not compile and test this code but I hope it gives you the idea: check 'change' and provide different messages according to the positive or negative value.

This is a very basic approach, you might want to think of something more sophisticated.

Please note I chenged the computation for the value of change because this new one sounds more in line with the description you gave according to field names. Please double check.

Hope it helps. Good luck.

Dario
  • 548
  • 1
  • 7
  • 14
  • @Movillian, don't forget to "accept" the answer so people know your problem has been solved. – camickr Apr 11 '17 at 15:22
  • I know I have accepted the answer but I was wondering how to check to ensure that if an order is finalized and it totals at €10.00 for example, and a customer only gives the cashier €9.00. How can i flag that the customer has not tendered enough money. – Movillian Apr 11 '17 at 16:36