Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payment Not Reset When No Change is Returned #61

Open
MudassarStd opened this issue Dec 22, 2024 · 0 comments
Open

Payment Not Reset When No Change is Returned #61

MudassarStd opened this issue Dec 22, 2024 · 0 comments

Comments

@MudassarStd
Copy link

Affected Code Link

@Override
public void returnChange() {
double change = vendingMachine.getTotalPayment() - vendingMachine.getSelectedProduct().getPrice();
if (change > 0) {
System.out.println("Change returned: $" + change);
vendingMachine.resetPayment();
} else {
System.out.println("No change to return.");
}
vendingMachine.resetSelectedProduct();
vendingMachine.setState(vendingMachine.getIdleState());
}

Description
In the ReturnChangeState class, the returnChange method resets the payment only if there is a change to return. If no change is due, the payment remains unreset. This could lead to payment being carried over and added to subsequent transactions which is a critical issue here.

Expected Behavior
The payment should always be reset after the returnChange method is executed, regardless of whether there is any change to return or not.

Original Code

@Override
    public void returnChange() {
        double change = vendingMachine.getTotalPayment() - vendingMachine.getSelectedProduct().getPrice();
        if (change > 0) {
            System.out.println("Change returned: $" + change);
            vendingMachine.resetPayment();  // payment reset is conditional here
        } else {
            System.out.println("No change to return.");
        }
        vendingMachine.resetSelectedProduct();
        vendingMachine.setState(vendingMachine.getIdleState());
    }

Suggested Fix

@OverRide
public void returnChange() {
double change = vendingMachine.getTotalPayment() - vendingMachine.getSelectedProduct().getPrice();
if (change > 0) {
System.out.println("Change returned: $" + change);
} else {
System.out.println("No change to return.");
}

    vendingMachine.resetPayment();  // payment reset is unconditional
    vendingMachine.resetSelectedProduct();
    vendingMachine.setState(vendingMachine.getIdleState());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant