You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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());
}
The text was updated successfully, but these errors were encountered:
Affected Code Link
awesome-low-level-design/solutions/java/src/vendingmachine/ReturnChangeState.java
Lines 30 to 41 in 774631a
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
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.");
}
The text was updated successfully, but these errors were encountered: