PeaBox Lab › PeaLang › Learn › Chapter 5 › Lesson 5

Part I — Foundations · Chapter 5 · Lesson 5 of 5 · 8 min read

Chapter 5 Checkpoint: Inventory Analyzer

Checkpoint level: Foundation

Purpose: Integrate core values, collections, indexing, mutation, copying, equality, traversal, transformation, filtering, aggregation, and portable boundary design in one complete inventory-analysis program.

Checkpoint outcomes

By completing this checkpoint, you will demonstrate that you can:

Project scenario

A small warehouse needs a command-line analyzer for source-defined inventory records. The program validates each item, identifies active stock that requires attention, derives a report record, aggregates operational totals, and returns one portable summary map.

The checkpoint remains intentionally local: it does not read files, call a database, access a network, or use process execution. A production host would obtain and validate external data, invoke the analysis, validate the result, and perform authorized actions.

5C.1 Required input shape

Declare an inventory list. Each item is a string-keyed map with these fields:

FieldMeaningValidation
skuStable stock identifierNonempty string
categoryGrouping labelNonempty string
quantityUnits currently on handNumber at least zero
reorder_pointAttention thresholdNumber at least zero
unit_priceValue of one unitNumber at least zero
activeWhether the item participatesBoolean

Use at least eight records in the primary test set. Include several categories, active and inactive items, one exact reorder boundary, one zero-quantity item, and at least two invalid records.

5C.2 Required validation behavior

Use reason codes such as MISSING_FIELD, INVALID_SKU, INVALID_CATEGORY, INVALID_QUANTITY, INVALID_REORDER_POINT, INVALID_PRICE, and INVALID_ACTIVE.

5C.3 Selection rule

Select an item when all of these conditions are true:

The exact boundary is included. An active item with quantity equal to its reorder point must be selected.

5C.4 Required transformation

For every selected item, create a new report map containing:

Do not reuse an original item map as the report record. Construct an independent map so the source collection remains unchanged.

5C.5 Required aggregates

Calculate and report:

Each category entry must contain valid item count, quantity total, inventory value, selected count, shortfall total, and replenishment value.

5C.6 Empty-result policy

Minima and maxima require an explicit empty policy. If no valid item exists, set both associated SKU fields and values to null. Do not invent zero as a minimum or maximum merely because accumulators need initialization.

If valid records exist but none is selected, selected lists remain empty and selected totals remain zero.

5C.7 Required result contract

Build one result map with this conceptual shape:

{
    "valid": true,
    "summary": { ... },
    "selected_items": [ ... ],
    "invalid_items": [ ... ],
    "products": { ... },
    "invariants_ok": true
}

Every value in the result must remain portable: null, boolean, integer, float, string, list, or string-keyed map.

5C.8 Required invariants

Calculate invariants_ok from these relationships rather than setting it to true unconditionally.

5C.9 Pipeline design

5C.10 Reference algorithm

Attempt the checkpoint before reading this high-level reference.

let inspected_count = 0
let valid_count = 0
let invalid_count = 0
let selected_items = []
let invalid_items = []
let categories = {}
let valid_quantity_total = 0
let valid_inventory_value = 0
let selected_shortfall_total = 0
let selected_replenishment_value = 0
let minimum_quantity = null
let minimum_quantity_sku = null
let maximum_stock_value = null
let maximum_stock_value_sku = null
for item in inventory {
    inspected_count += 1
    # Validate required fields and values before calculations.
    # Append an invalid summary and continue when validation fails.
    # Update valid totals, extrema, and category aggregates.
    # When selected, build a new report map and append it.
}

The final implementation must replace each comment with explicit logic. Keep validation decisions visible; do not collapse the whole pipeline into one dense condition.

5C.11 Required test matrix

CaseInput characteristicExpected emphasis
AEmpty inventoryAll counts zero; extrema null
BOne valid active item above reorder pointValid but not selected
COne active item exactly at reorder pointSelected with zero shortfall
DOne active item below reorder pointPositive shortfall and replenishment value
EInactive item below reorder pointValid but not selected
FMissing required keyInvalid; later items still processed
GNegative quantity or priceInvalid range reason
HSeveral categoriesGrouped totals reconcile
IAll records invalidNo valid totals; extrema null
JMixed primary datasetAll outputs and invariants

5C.12 Prediction worksheet

Before execution, record for every case:

For floating-point totals, define an acceptable comparison tolerance and record it with the prediction.

5C.13 Source-preservation test

Keep an independent pre-analysis copy and compare it with the source after the pipeline. The comparison should demonstrate that selected report maps and category summaries were constructed separately.

Also mutate one derived report record in a temporary test. Confirm that the corresponding source item does not change. If it changes, the implementation has retained an unintended shared reference.

5C.14 Diagnostic exercises

Create temporary defective versions demonstrating:

For each defect, record the symptom, broken invariant, root cause, minimal correction, and regression test.

5C.15 Validation workflow

5C.16 Acceptance criteria

5C.17 Submission package

5C.18 Assessment rubric

AreaEvidence of mastery
Data modelingLists and string-keyed maps represent the domain clearly.
ValidationShape, required fields, categories, and ranges are checked safely.
TraversalEvery item is inspected once in source order.
TransformationSelected report maps are independent and complete.
AggregationCounts, totals, extrema, and category summaries are accurate.
InvariantsAccounting relationships are calculated and verified.
TestingEmpty, boundary, malformed, mixed, and preservation cases pass.
PortabilityThe final contract uses only portable values.
SecurityNo unnecessary capability or sensitive data is exposed.

Completion standard: Every acceptance criterion is met, all required tests pass, category and global summaries reconcile, and the learner can explain why the source collection remains unchanged.

5C.19 Reflection questions

Chapter 5 summary

You can now model core values and collections, access and update them deliberately, reason about copying and equality, traverse data, filter and transform records, compute aggregates, and design portable host boundaries. The Inventory Analyzer combines these skills into a validated pipeline whose outputs reconcile through explicit invariants.

Transition to Part II

Part II begins with Chapter 6: Errors, Exceptions, and Diagnostics. The next lesson examines failure categories and useful error messages, building on the validation and diagnostic practices used throughout the foundation projects.

Checkpoint status: Ready for review.

← Previous Next →