C++ · Intermediate level

Intermediate level · Module 6: Intermediate C++ Project · Lesson 2 of 3

Implementing Rules and Persistence

Implement the inventory rules and CSV persistence, checking each rule as it is written.

In this lesson you will

  • Implement dispensing and expiry rules with STL containers
  • Load and save the inventory with clear error reporting

Key concepts

first-expiry-first-outrule checks with assertround-trip save/loaderror reporting

The lesson

Implement dispensing by sorting batches by expiry and taking from the earliest unexpired batch first. Reject requests larger than the available unexpired stock with an exception.

Write checks as you go. A simple approach is a test program using assert(condition) for each rule; a framework such as Catch2 or GoogleTest adds clearer reports. Either way, run the checks after every change.

Save and reload the inventory and check that the result equals the original — a round-trip check that catches formatting and parsing mistakes together.

Step by step

  1. Implement adding stock and listing items below the reorder level.
  2. Implement first-expiry-first-out dispensing.
  3. Write checks for each rule.
  4. Implement CSV save and load with a round-trip check.

Practice activity

Implement all features with passing checks.

Expected result: All rules behave as specified and the checks pass in a sanitizer build.

Check your understanding

  1. What does first-expiry-first-out mean?

    Show answer

    Dispense from the batch with the earliest expiry date first.

  2. What does a round-trip check catch?

    Show answer

    Mismatches between how data is saved and how it is loaded.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.