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
materialIdsandinitialUnitsarrays and starts with no recorded events. - First record. If
eventIdhas not been recorded,recordUsestores itsmaterialIdandunits. - Safe replay. If
eventIdhas already been recorded,recordUseleaves the recorded events unchanged. - Remaining units.
remainingreturns the material's starting units minus the units from every recorded event for that material. - Event lookup.
wasRecordedreturns whether the event ID has been recorded.
API
| Signature | Returns | Behavior |
|---|---|---|
MaterialUsageJournal(materialIds: string[], initialUnits: integer[]) | Not applicable | Creates the configured journal. |
recordUse(eventId: string, materialId: string, units: integer) | void | Records the first use of an event ID and ignores its valid replay. |
remaining(materialId: string) | integer | Returns the remaining units for the configured material. |
wasRecorded(eventId: string) | boolean | Reports whether the event ID is present. |
Examples
| Step | Operation | Result |
|---|---|---|
| 1 | MaterialUsageJournal(["WOOD"], [10]) | Journal created |
| 2 | remaining("WOOD") | 10 |
| 3 | recordUse("CUT-1", "WOOD", 3) | No return value |
| 4 | remaining("WOOD") | 7 |
| 5 | wasRecorded("CUT-1") | true |
Constraints
1 <= materialIds.length <= 10, andmaterialIdsandinitialUnitshave 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.