Inventory Commands with Undo
Hard · Modelling
Write processCommands(commands) that runs typed inventory commands on an empty stock and returns { stock, errors }.
- add: qty must be greater than 0, otherwise the error is "invalid quantity". Adds to the item.
- remove: qty must be greater than 0 ("invalid quantity"). If the stock has less than qty, the error is "not enough <item>". An item that reaches 0 is deleted from stock.
- rename: moves the whole quantity of from to to (adding to to if it already exists). If from is not in stock, the error is "unknown <from>". from and to are always different.
- undo: restores the stock to how it was before the last successful command that has not been undone yet (undo can be repeated). If there is none, the error is "nothing to undo".
A failed command changes nothing. Each error is prefixed with the 1-based command number, e.g. "#3: not enough pear".