Warehouse With Held Orders
Implement a class Warehouse that tracks stock on a shelf, where units can be held for an order before they ship. Units of an item are available when they are on the shelf and not held for an active order. Write these methods:
Warehouse()starts with nothing on the shelf.restock(item, qty)putsqtymore units ofitemon the shelf. Returns how many units ofitemare available afterwards.hold(order, item, qty)holdsqtyunits ofitemfor the order numberorder. It works only if at leastqtyunits ofitemare available: thenorderbecomes an active order and the method returnstrue. Otherwise nothing changes and it returnsfalse.release(order)cancels the active orderorder, so its units are available again. Returns how many units it was holding, or0iforderis not an active order.ship(order)ships the active orderorder: its units leave the shelf for good and the order is no longer active. Returns how many units were shipped, or0iforderis not an active order.count_available(item)returns how many units ofitemare available (0for an item that was never restocked).
Every call to hold uses a new order number. An order is active only after a hold that returned true, and only until it is released or shipped. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Order 1 holds 3 of the 5 bolts, so only 2 are available and order 2 fails. Order 3 takes the last 2. Shipping order 1 removes its 3 bolts from the shelf, and releasing order 3 frees 2. Order 1 is no longer active, so shipping it again gives 0.
Constraints
itemis1to8lowercase letters.1 <= qty <= 10and1 <= order <= 50- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.