Loading...
Loading...
A shopping app keeps versions of a list of n items. SET i item changes item i of the current list, SAVE stores the current list as the next version, RESTORE v makes the current list a copy of version v, and SHOW prints the current list. Saved versions must never change afterwards.
SET i item, SAVE, RESTORE v or SHOW.SAVE prints Saved version <k>. RESTORE prints Restored version <v>, or No version <v> if it does not exist. SHOW prints the items separated by / .Example
Input
3 Milk Bread Eggs 5 SAVE SET 2 Rusk SHOW RESTORE 1 SHOW
Output
Saved version 1 Milk / Rusk / Eggs Restored version 1 Milk / Bread / Eggs
Version 1 still has Bread, because SAVE stored a copy.
Try the problem first. Open one hint at a time.
Keep versions in an array of ShoppingList objects with a counter, as there are at most 50.
Both directions need a copy: versions[count] = new ShoppingList(current) and current = new ShoppingList(versions[v - 1]).
The copy constructor must clone the items array.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor