Design an inventory management system
medium 80 minCount 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
| Call | Answers | Refuses with | Cost |
|---|---|---|---|
Inventory(policy, now, on_low) | An empty inventory | O(1) | |
add_line(sku, place, low_at) | Nothing | ValueError if it exists | O(1) |
receive(sku, place, qty, ref) | Nothing | KeyError, ValueError | O(1) |
available(sku) | A number, old at once | Never | O(lines) |
reserve(order, wanted, seconds) | Reservation | OutOfStock, ValueError | O(products × lines) |
ship(id) | Reservation | NotHeld | O(picks) |
release(id) | Nothing | NotHeld | O(picks) |
expire_due() | How many | Never | O(promises) |
Is sold out an exception or an answer?
BreaksA boolean
- How
reservereturns 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)orMissing({sku: how many})- Limit
- Two more small types.
- Choose when
- A shop that shows "only 3 left" for each product.