Skip to content
Katabench
Try free

Undo What Actually Happened

Medium About 35 min Free, no account needed

A stock room describes every adjustment twice, once in an Apply switch and again in an Undo switch. Turn each adjustment into a command that undoes itself behind a history that knows no stock, so a mis-keyed shipment can be reversed on its own without rolling back the deliveries that came after it.

The brief

Inventory.StockRoom tracks the on-hand quantity of one SKU. A clerk can receive a delivery, ship an order, or replace the figure after a cycle count, and can undo recent adjustments, newest first. The behavior is right. The design makes every new adjustment expensive: each adjustment is written twice, once in Apply's switch and again in Undo's switch, and the two must be kept in step by hand. The next two adjustments on the backlog are write-offs and transfers between bins, and each one means editing both switches.

There is a second request on the backlog. Clerks sometimes mis-key a shipment and only notice after more deliveries have landed. They want to reverse that one adjustment on its own, without rolling back the deliveries that came after it. The stock room's undo cannot do that today.

The Command pattern turns each adjustment into an object that can execute itself and undo itself. A history records the commands it ran and undoes them newest first, without knowing what any of them does. The knowledge needed to reverse an adjustment lives next to the code that performed it.

Your job

Introduce IUndoableCommand with string Execute() and string Undo(), and three commands built from the stock room and a quantity: ReceiveStock(StockRoom room, int quantity), ShipStock(StockRoom room, int quantity), and RecountStock(StockRoom room, int counted). A shipment never ships more than is on the shelf. Keep the reply text exactly as it is today:

Action Execute reply Undo reply
receive received:<n> undid-receive:<taken back>
ship shipped:<actually shipped> undid-ship:<actually shipped>
recount recounted:<before>-><counted> undid-recount:<counted>-><before>

Undoing a delivery or a shipment reverses only the quantity that moved, even when other adjustments have happened since it ran, and the reply reports what the undo actually changed: undoing a delivery takes back at most what is still on the shelf. A recount replaced the figure outright, so undoing it puts back the figure it replaced.

Add a sealed CommandHistory with string Execute(IUndoableCommand command), which runs the command once and remembers it, and string Undo(), which undoes the newest remaining command or returns nothing-to-undo. Reduce StockRoom to the operations the commands need, with no undo log of its own, and keep its parameterless constructor. Solution maps each clerk action to a command and hands it to the history.

How it's graded

Your submission must (1) preserve every reply and the final on-hand quantity, including short shipments, recounts, and undo on an empty history, and (2) satisfy every design rule shown in the results: the command contract exists and all three adjustments implement it, and CommandHistory records the contract, names no command type and not StockRoom, keeps readonly fields, and is sealed.

Two trusted callers then check the behavior no rule can see. The first drives your CommandHistory with commands that have nothing to do with stock and journals every call. Each command must run once when executed, undo must reverse the newest remaining command first and touch it once, and an empty history must answer nothing-to-undo.

The second uses your three commands without a history. It builds all of them around one fresh StockRoom before any of them runs and calls Execute and Undo on them directly. It undoes a shipment after a later delivery has landed, and a delivery after a later shipment has emptied most of the shelf, then reads back the final on-hand figure. Every reply and that figure must follow the rules above.

Undo is not the opposite of the request. It is the opposite of what the request did, and only the command was there to see it.

How it is graded

Executable structural checks grade the shape of your types as well as their behavior.

SOLID principles exercises →

What it practices

Command and memento patterns
Capture actions and opaque snapshots so undo and history stay correct.

Related katas