Canister Refill Kiosk
Problem
A service station uses one kiosk to refill empty canisters and dispense full ones. Design CanisterKiosk to track whether staff are refilling it or customers are using it, together with the number of canisters in stock.
Requirements
- Initial state. A new kiosk starts in
servicemode with the supplied initial stock. - Mode change. Setting the mode changes only the mode and leaves stock unchanged.
- Refill. Adding units in
refillmode increases stock by the supplied amount. - Dispense. Dispensing units in
servicemode decreases stock by the supplied amount. - Snapshot. A snapshot returns
[mode, decimalString(stock)]as one composite observation.
API
| Signature | Returns | Behavior |
|---|---|---|
CanisterKiosk(initialUnits: integer) | A new CanisterKiosk | Creates a kiosk in service mode with the supplied stock. |
setMode(mode: string) | void | Changes only the mode. |
addUnits(units: integer) | void | Adds units to stock in refill mode. |
dispense(units: integer) | void | Removes units from stock in service mode. |
snapshot() | string[] | Returns the current mode and decimal stock string. |
Examples
Given CanisterKiosk(5):
| Call | Result |
|---|---|
snapshot() | ["service", "5"] |
setMode("refill") | No return value |
addUnits(4) | No return value |
snapshot() | ["refill", "9"] |
setMode("service") | No return value |
dispense(3) | No return value |
snapshot() | ["service", "6"] |
Constraints
initialUnitsis between 0 and 20, inclusive.modeis eitherserviceorrefill.unitsis between 1 and 10, inclusive.- Stock always remains between 0 and 50, inclusive.
- A testcase contains at most 40 method calls.
Notes
All calls are valid and sequential. addUnits is called only in refill mode without raising stock above 50. dispense is called only in service mode with enough stock. Invalid-call behavior and concurrency are outside the contract.