Design an Online Shopping Order Lifecycle
Model order placement with all-or-nothing stock reservation, payment outcomes, and a guarded order state machine.
For this exercise, assume one online store sells products that each have an available stock count. A customer places an order for one or more products with quantities. Placing the order reserves stock for every line at once, or fails without changing any stock.
The order then waits for payment through an injected payment gateway: a successful payment marks it paid, and a declined payment marks it payment failed and releases its reserved stock. A paid order can be shipped and then delivered.
Only an order that is pending payment or paid can be cancelled; cancelling a paid order also requests a refund, and reserved stock is released exactly once. Payment failed, delivered, and cancelled orders are final, and any other transition is rejected without changing anything.
Design the objects, responsibilities, interfaces, and interactions, then implement order placement and the order state transitions in memory. A final follow-up covers payment results that arrive twice or late.
Carts, pricing and discounts, tax, shipping logistics, multiple warehouses, customer accounts, persistence, and a distributed design are outside scope.