AlgoMaster Logo
AlgoMasterDesign Order Fulfillment Sagahard

Design Order Fulfillment Saga

hard

Fulfilling an order touches three systems in turn: stock is reserved, the customer is charged, and a shipment is created. Any step can refuse, and when one does, everything that already happened has to be put back. Each step is a command that knows how to run and how to compensate, and the whole order is a command made of those steps.

Inventory, Payments, Shipping and OrderProcessor are provided in the starter code. Implement only the command types they use: OrderStep, ReserveStockCommand, ChargeCommand, ShipCommand and FulfillOrderCommand (IOrderStep in C#).

The provided receivers work like this:

  • Inventory.reserve(sku, quantity) takes units out of stock and returns true, or returns false without changing anything when there are not enough. release(sku, quantity) puts units back.
  • Payments.charge(customer, amount) takes credit and returns true, or returns false without changing anything when the customer cannot cover it. refund(customer, amount) gives credit back.
  • Shipping.ship(customer, sku, quantity) always succeeds and returns a new shipment id. cancel(id) removes that shipment.

The provided OrderProcessor behaves as follows:

  • OrderProcessor() starts with empty inventory, no credit, no shipments and no fulfilled orders.
  • boolean addStock(String sku, int quantity) adds units and returns true. An empty sku or a quantity below 1 changes nothing and returns false.
  • boolean addCredit(String customer, int amount) adds credit and returns true. An empty customer or an amount below 1 changes nothing and returns false.
  • boolean placeOrder(String customer, String sku, int quantity, int unitPrice) builds one FulfillOrderCommand from a reserve step, a charge step for quantity * unitPrice, and a ship step, in that order. It executes the order and returns its result, recording it only when it succeeded. Empty names or values below 1 return false without building anything.
  • boolean undoLast() reverses the most recently fulfilled order and returns true, or returns false when there is none.
  • int stock(String sku) and int credit(String customer) report the receivers.
  • int shipmentCount() returns how many shipments are active.
  • int fulfilledCount() returns how many fulfilled orders are available to undo.

When a step fails, the steps before it have already changed the receivers. The composite must undo those, newest first, and then return false. A step that failed changed nothing and must not be undone.

The tests call the provided OrderProcessor; your work should be confined to the step contract and the four concrete command classes.

Example 1:

Input:

Output:

Explanation: All three steps succeed: two units are reserved, sixty is charged, and one shipment is created.

Example 2:

Input:

Output:

Explanation: The reservation succeeds but the charge of sixty exceeds Bob's credit. The composite releases the three reserved units before reporting failure, so nothing changed.

Constraints

  • 0 <= sku.length <= 10 and 0 <= customer.length <= 10
  • -5 <= quantity <= 100, -5 <= unitPrice <= 1000 and -5 <= amount <= 10000
  • At most 100 calls in total are made across all methods.

Starter Code

Implement the OrderStep contract and the four concrete commands, including the composite FulfillOrderCommand. The three receivers and OrderProcessor are complete and must not be modified.

How the design is graded

needs 7/10 to pass
  • One contract for steps and the order

    Full marks when `OrderStep` defines a boolean `execute` and a void `undo`, the three concrete steps implement it against their receivers, and `FulfillOrderCommand` implements the same contract while holding a list of steps. Lose points when the processor has to know which step failed or how to compensate.

  • Compensation on failure

    Full marks when a failing step causes the composite to undo every step that already succeeded, in reverse order, and return `false`, so stock, credit and shipments are exactly as they were before the order. Lose points heavily when a failed charge leaves stock reserved, or when the composite undoes the step that failed.

  • Undo of a fulfilled order

    Full marks when undoing a fulfilled order releases the stock, refunds the charge and cancels the specific shipment the ship step created. Lose points when the ship step does not keep its shipment id, or for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new OrderProcessor()null
addStock("sku1", 10)true
addCredit("ann", 100)true
placeOrder("ann", "sku1", 2, 30)true
stock("sku1")8
credit("ann")40
shipmentCount()1

All three steps succeed: two units are reserved, sixty is charged, and one shipment is created.

Run checks these cases. Submit also runs a larger hidden set.