0

Please I got a change algorithm calculation which adds up instead of subtracting the amount paid by the user . i want the program to be able to deduct the amount paid from the textbox using a set of currency buttons and when the amount is over paid, a balance should be returned back to the user. it instead adds up each time i click on any of the currency buttons using the message box. Thanks

These are the currency buttons:

      private void tenPoundButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
    amountToPay -= 1000;
    calculate(amountToPay);
}                                              

private void twentyPoundButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    // TODO add your handling code here:
    amountToPay -= 2000;
    calculate(amountToPay);
}                                                 

private void fiftyPoundButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    // TODO add your handling code here:
    amountToPay -= 5000;
    calculate(amountToPay);
}         

this is the change algorithm :

    public void calculate(double amount) {

    if (amount > 0) {
        amountPaidTextBox.setText(pounds.format(amount/100));
    }
    else {
        amountPaidTextBox.setText("");

        if (amount < 0){
            double change;
            change = -amount;

            javax.swing.JOptionPane.showMessageDialog(this, "Your change is " + pounds.format(change / 100)

                    + coins (change), 
                    "change", JOptionPane.INFORMATION_MESSAGE);
        }
        else {
            javax.swing.JOptionPane.showMessageDialog(this, "Thank you, Exact Amount");
        }
    }
}



      String coins(double change) {
    String answer = ":";

    if (change >= 4000) // give two £20 note change
    {
        answer += "\nTwo £20 note";
        change -= 4000;
    }
    if (change >= 2000) // give a £20 note change
    {
        answer += "\nOne £20 note";
        change -= 2000;
    }
    if (change >= 1500) // give £15 note change, a £10 note and a £5 note.
    {
        answer += "\nOne £10 note,\nOne £5 note ";
        change -= 1500;
    }
    if (change >= 1000) // give a £10 note change
    {
        answer += "\nOne £10 note";
        change -= 1000;
    }
    if (change >= 500) // give a £5 note change
    {
        answer += "\nOne £5 note";
        change -= 500;
    }
4

1 回答 1

0

我认为这将解决您的问题。

javax.swing.JOptionPane.showMessageDialog(this, "Your change is " + pounds.format(change / 100)+ coins (change), "change", JOptionPane.INFORMATION_MESSAGE);

打电话时coins(double change)您时传递的是负参数。

假设如果你想扣除 2000 那么你正在通过count=-2000

-2000/100 = -20

因此在执行上述语句后,您将获得负值。如果您需要更多解释,请告诉我。

于 2013-01-03T10:54:54.723 回答