Problemstored value wallet ledger

Session: Sign in to solve

Solution.txt

Material Usage Journal

Problem

A workshop records each material-use event with a unique event ID. Design MaterialUsageJournal to ignore a replay of an already recorded event and calculate the remaining quantity of each configured material.

Requirements

  • Initial materials. Construction creates the starting quantity map from the parallel materialIds and initialUnits arrays and starts with no recorded events.
  • First record. If eventId has not been recorded, recordUse stores its materialId and units.
  • Safe replay. If eventId has already been recorded, recordUse leaves the recorded events unchanged.
  • Remaining units. remaining returns the material's starting units minus the units from every recorded event for that material.
  • Event lookup. wasRecorded returns whether the event ID has been recorded.

API

SignatureReturnsBehavior
MaterialUsageJournal(materialIds: string[], initialUnits: integer[])Not applicableCreates the configured journal.
recordUse(eventId: string, materialId: string, units: integer)voidRecords the first use of an event ID and ignores its valid replay.
remaining(materialId: string)integerReturns the remaining units for the configured material.
wasRecorded(eventId: string)booleanReports whether the event ID is present.

Examples

StepOperationResult
1MaterialUsageJournal(["WOOD"], [10])Journal created
2remaining("WOOD")10
3recordUse("CUT-1", "WOOD", 3)No return value
4remaining("WOOD")7
5wasRecorded("CUT-1")true

Constraints

  • 1 <= materialIds.length <= 10, and materialIds and initialUnits have the same length.
  • Material IDs are unique, and all material and event IDs match [A-Za-z0-9_-]{1,24}.
  • 0 <= initialUnits[i] <= 100.
  • 1 <= units <= 20.
  • At most 20 distinct event IDs and 30 public method calls are used.
  • Every material argument is configured, and a first record never makes its material's remaining units negative.
  • A replayed event ID uses the same material ID and units as its first record.
  • Arguments are not null.

Notes

Calls are sequential.

PRIVATE WORKSPACE

Checking your session…

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