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;
}