Problemstateful refill kiosk

Session: Sign in to solve

Solution.txt

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 service mode with the supplied initial stock.
  • Mode change. Setting the mode changes only the mode and leaves stock unchanged.
  • Refill. Adding units in refill mode increases stock by the supplied amount.
  • Dispense. Dispensing units in service mode decreases stock by the supplied amount.
  • Snapshot. A snapshot returns [mode, decimalString(stock)] as one composite observation.

API

SignatureReturnsBehavior
CanisterKiosk(initialUnits: integer)A new CanisterKioskCreates a kiosk in service mode with the supplied stock.
setMode(mode: string)voidChanges only the mode.
addUnits(units: integer)voidAdds units to stock in refill mode.
dispense(units: integer)voidRemoves units from stock in service mode.
snapshot()string[]Returns the current mode and decimal stock string.

Examples

Given CanisterKiosk(5):

CallResult
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

  • initialUnits is between 0 and 20, inclusive.
  • mode is either service or refill.
  • units is 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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.