Skip to content
All projects

Systems programming course, 2024

Food Warehouse Simulator

A step-by-step simulation in C++ of a volunteer-run food warehouse. Orders move from collectors to drivers, the whole state can be backed up and restored, and every object is owned by hand with a Rule of 5 that Valgrind checks for leaks.

A terminal session with the warehouse next to its test suite. Three orders are placed, a volunteer is shown mid-task, an order is delivered, the warehouse is backed up and restored, and close lists every order. All 19 tests pass, including the Valgrind check.
  • Orders flow through collectors and drivers one unit of time at a time, and the oldest order always gets the next free volunteer.
  • The warehouse owns every customer, volunteer, order and action through raw pointers, with deep copies via virtual clone() and moves that leave nothing behind.
  • 19 end-to-end tests compare the full output of real sessions, plus a Valgrind run that must free every heap block.

What it is

A command-line simulation of a food warehouse run by volunteers. Customers place orders, collectors pack them, drivers deliver them, and the user moves time forward with step. Along the way you can ask for the status of any order, customer or volunteer, read the log of everything that happened, and back up the whole warehouse to restore it later.

I built it with a partner in 2024 for the Systems Programming course at Ben-Gurion University. The course gave us header files with the public method signatures, and the point of the assignment was everything behind them: an object model with real inheritance, and memory that the program manages entirely by itself. Later I reworked the internals, and added a test suite and CI.

How a step works

An order goes Pending, Collecting, Delivering, Completed. There are four kinds of volunteer: collectors, who take a fixed number of steps per order, drivers, who cover a set distance per step and won’t take an order beyond their range, and a limited version of each that leaves after a set number of orders.

Each step runs four stages in a fixed order:

  1. Assign. Waiting orders are offered to volunteers, oldest first. A new order can only go to a collector, and a packed one only to a free driver in range.
  2. Work. Every busy collector gets one step closer, and every busy driver covers its distance per step, never going below zero.
  3. Hand off. A packed order goes back to the waiting list, still marked Collecting until a driver takes it. A delivered order is completed.
  4. Retire. A limited volunteer who has used up their orders and finished the last one is removed.

Volunteers decide for themselves. The warehouse never asks what kind of volunteer it has. Each one answers canTakeOrder, which checks that it is free, that the order is at its stage, and for a driver that the customer is in range. A limited volunteer extends the regular one and adds only its order counter.

Fair by construction. The waiting list is kept sorted by order ID. An order that comes back from a slow collector slots in by age, so it reaches a driver before a newer order that happened to be packed first. A test sets up exactly that race.

Owning everything by hand

Customers, volunteers, orders and logged actions all live on the heap, behind raw pointers held by the warehouse. That makes the warehouse responsible for all five special members:

  • Destructor: deletes everything it points to.
  • Copy constructor and copy assignment: deep copies. Volunteers, customers and actions are copied through a virtual clone(), so a limited driver stays a limited driver.
  • Move constructor and move assignment: take the other warehouse’s pointers and leave it empty, so its destructor has nothing to free twice.

backup and restore are just those copy operations. A backup is a full copy of the warehouse, and restoring assigns it back, which also rolls back the action log and the ID counters. A second backup assigns into the existing snapshot rather than allocating a new one.

Details that matter

Distances never go negative. If a driver has 4 left and covers 6 per step, the delivery ends this step with 0 left, not -2.

Lookups never return garbage. Every getOrder, getCustomer and getVolunteer throws on an unknown ID instead of returning a reference to nothing. The actions check first and print the error message the spec defines.

Input is parsed strictly. step 0, order abc and customer Bob pirate 3 1 print the command’s usage and never reach the log. Bad lines in the config file are reported with their line number and skipped, and a missing config file exits with an error.

End of input is a clean close. If the input runs out, the warehouse closes just as if you typed close, printing every order and freeing everything.

Testing

The tests drive the real binary. Each one writes a config file, feeds a list of commands, and compares the entire output with what it should be, so a wrong status, a missing line or an extra space all fail.

  • the action log matches the example in the assignment
  • an order passes through all four stages, and drivers respect their range and take one order at a time
  • the older order reaches a driver first, even when it was packed later
  • limited volunteers count down and leave after their last order
  • restore brings back orders, customers, the log and the ID counters
  • malformed commands and config lines are rejected

The last check runs a session full of backups, restores and retiring volunteers under Valgrind with --leak-check=full --show-reachable=yes, and passes only if every heap block was freed. GitHub Actions builds with warnings as errors on every push and runs the whole suite.