Design Driver Dispatch for Food Orders
Model how ready food orders get offered to drivers, accepted or declined, picked up and delivered, so no order is lost and no driver carries two orders at once.
A food ordering app needs the part that hands orders to delivery drivers. Design the classes and contracts for that dispatch logic, then implement its core.
For this exercise, assume restaurants mark an order ready for pickup, and drivers go online or offline and work in one zone of the city. When an order is ready, the system offers it to one available driver in the same zone: the driver at the front of that zone's driver queue.
Drivers join the back of the driver queue when they come online, finish a delivery, decline an offer or let an offer time out, so the front is the driver who has waited longest. The driver has 60 seconds to accept. A decline or no answer moves the offer to the next driver in the driver queue.
An order is offered to one driver at a time and never to a driver who already declined it. A driver carries at most one order at a time. If no driver is free, the order waits in the zone's order queue, which is handled oldest first when a driver becomes free.
After accepting, the driver marks the order picked up and then delivered. A restaurant can cancel an order until it is picked up.
Also assume the code runs in one process, in-memory dictionaries may stand in for the database, and time comes from an injected clock. Maps and real distances, payments, customer ordering and driver pay are out of scope.