Lesson contents

C++ — Inventory project with boundary tests

This C++ inventory function processes Event values in order and returns a Result containing stock and rejected counts. A shipment can succeed only when enough stock is available.

A simple example

Trace one stock change

  1. Start with 3 in stock
  2. Do this Take 1 away
  3. Result 2
One subtraction updates the starting count. The project below adds checks for larger changes.
lesson.cpp
#include <iostream>

int main() {
    int stock = 3;
    stock -= 1;
    std::cout << stock << '\n';
}
Output
2
Advertisements

Build an inventory event processor

Keep the event-processing function separate from printing and testing. An invalid event changes only the rejection count. The example uses small, bounded integer quantities.

Receive 5, ship 2: 3 in stock. Invalid events: 2 rejected. Receive 4: 7 in stock.
Reconcile a stock ledger

The first delivery brings five units and a shipment takes two. A request to ship nine and an unknown event are both rejected, so stock stays at three. The final delivery adds four and closes the ledger at seven.

Run the example

C++17. Run these commands from the directory containing the example. Use a Unix-style shell.

c++ -std=c++17 lesson.cpp -o lesson
./lesson
lesson.cpp
#include <iostream>
#include <cassert>
#include <string>
#include <vector>

struct Event { std::string kind; int units; };
struct Result { int stock = 0; int rejected = 0; };

Result process(const std::vector<Event>& events) {
    Result result;
    for (const auto& event : events) {
        if (event.kind == "in" && event.units > 0) result.stock += event.units;
        else if (event.kind == "out" && event.units > 0 && event.units <= result.stock)
            result.stock -= event.units;
        else ++result.rejected;
    }
    return result;
}

int main() {
    const std::vector<Event> events{{"in", 5}, {"out", 2}, {"out", 9}, {"?", 1}, {"in", 4}};
    const auto empty = process({});
    const auto drained = process({{"in", 2}, {"out", 2}});
    const auto result = process(events);
    assert(empty.stock == 0 && empty.rejected == 0);
    assert(drained.stock == 0 && drained.rejected == 0);
    assert(result.stock == 7 && result.rejected == 2);
    std::cout << result.stock << ' ' << result.rejected << '\n';
    return 0;
}
Output
7 2

Start with zero units. Receive 5 and ship 2 to leave 3. Reject the shipment of 9 and the unknown event. Finally receive 4. The two output numbers are the ending stock of 7 and the two rejected events.

Buy supplies for the next shift

A breakfast kiosk has 28 cups left, expects 64 orders tomorrow and keeps four spare cups for spills. The supervisor subtracts usable stock from the target and buys sealed sleeves of ten, rounding the order up to a whole sleeve.

lesson.cpp
#include <algorithm>
#include <iostream>

int main() {
    int orders = 64, spare = 4, on_hand = 28;
    int shortage = std::max(0, orders + spare - on_hand);
    std::cout << (shortage + 9) / 10 << " sleeves";
    return 0;
}
Output
4 sleeves

The target is 68 cups. After counting the 28 on hand, the kiosk needs 40 more, exactly four sleeves of ten.

Try it yourself

Append a shipment of 7, followed by another shipment of 1.

Check your answer

The first shipment exactly empties stock. The next one is rejected. Update the expected result to 0 units and 3 rejections. Keep the existing checks for the empty event list and exact depletion.

C++ language and library reference

Related tags

  • C++
  • inventory
  • testing
  • state transitions