AlgoViz

Design an inventory management system

medium 80 min

Count goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.

Watch lesson4:28

API and data

The contract first, then the data that serves it.

Class API

The public surface

CallAnswersRefuses withCost
Inventory(policy, now, on_low)An empty inventoryO(1)
add_line(sku, place, low_at)NothingValueError if it existsO(1)
receive(sku, place, qty, ref)NothingKeyError, ValueErrorO(1)
available(sku)A number, old at onceNeverO(lines)
reserve(order, wanted, seconds)ReservationOutOfStock, ValueErrorO(products × lines)
ship(id)ReservationNotHeldO(picks)
release(id)NothingNotHeldO(picks)
expire_due()How manyNeverO(promises)
With an index by product and a heap, lines becomes places of one product, and promises becomes promises that are due.

Is sold out an exception or an answer?

BreaksA boolean
How
reserve returns false
Limit
Which product? And a caller can ignore it.
Choose when
Never, for several products.
WorksAn exception with the product
How
OutOfStock(sku)
Limit
Selling out is not rare, and exceptions are for rare things.
Choose when
When callers mostly expect success, as here.
StrongA result with all that is missing
How
Held(reservation) or Missing({sku: how many})
Limit
Two more small types.
Choose when
A shop that shows "only 3 left" for each product.