98.7k views
1 vote
Suppose the CashRegister needs to support a method void undo() that undoes the addition of the preceding item. This enables a cashier to quickly undo a mistake. What instance variables should you add to the CashRegister class to support this modification

User Sergey Sob
by
5.3k points

1 Answer

2 votes

Answer:

previousAddition instance variable

Step-by-step explanation:

In order to accomplish this you would need to add a previousAddition instance variable. In this variable you would need to save the amount that was added at the end of the process. Therefore, if a mistake were to occur you can simply call the previousAddition variable which would have that amount and subtract it from the total. This would quickly reverse the mistake, and can be easily called from the undo() method.

User Franbenz
by
5.4k points