Prototype: implement change_due(price, paid). Both arguments must be nonnegative integers within the declared C++ numeric type. Return paid - price when payment is sufficient; otherwise raise std::invalid_argument. Invalid values also raise std::invalid_argument.

Final: implement VendingMachine(products), where products is a list of (code, price, stock) tuples. Codes must be unique nonempty strings, price a positive integer, and stock a nonnegative integer; numeric inputs use Int. Empty inventory is valid. Copy the supplied inventory. Invalid inventory raises std::invalid_argument.

insert(amount) accepts a positive integer, adds it to credit, and returns the new credit. balance() returns current credit. select(code) returns the fields declared in the value record below on success, decreases that item's stock by one, and sets credit to zero. Unknown codes raise std::out_of_range. A known item with zero stock or insufficient credit raises std::invalid_argument. Failures leave all state unchanged. refund() returns current credit and resets it to zero; repeated refunds return zero. inventory() returns a fresh value record mapping each code to a fresh the fields declared in the value record below value record. There is no restock operation, and instances must be independent.

The public C++ API below is authoritative for field types and return values. erase is used where another language might use a method named delete, because delete is a C++ keyword. Returned records are value snapshots with the documented named fields; mutating a record or its nested vectors cannot affect the service.

cpp
struct Product {Int price,stock;bool operator==(const Product&)const=default;};
struct Sale {std::string code;Int change;bool operator==(const Sale&)const=default;};
inline Int change_due(Int price,Int paid);
class VendingMachine { public:
 explicit VendingMachine(const Vec<std::tuple<std::string,Int,Int>>& products);
 Int insert(Int amount);
 Int balance() const;
 Sale select(const std::string& code);
 Int refund();
 std::map<std::string,Product> inventory() const;
};

Compile and run from this directory:

Shell
c++ -std=c++20 -O2 -Wall -Wextra -pedantic -pthread main.cpp -o practice
./practice --stage prototype
./practice --stage final

Edit exercise.hpp. Both stages start unfinished and must report failures until implemented. Keep main.cpp as the executable contract. The runner returns nonzero for a failed check, an unexpected exception, an invalid stage, or an empty suite. C++20 and only the standard library are required.

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
No results yet.

Complete the starter code, then run the checks.
Results and errors will appear here.

C++ runtime powered by Wasmer.