Prototype: implement available_copies(copies, title). Input copies is a list of (copy_id, title, available) tuples with unique copy identifiers and boolean availability. Assume valid inputs. Return a fresh lexicographically sorted list of available identifiers whose title equals the supplied case-sensitive title. Do not modify input; an unknown title returns [].
Final: implement Library(copies), where copies is a list of (copy_id, title) pairs. Copy identifiers must be unique nonempty strings; titles and member identifiers are nonempty strings. Invalid constructor or method identifiers raise std::invalid_argument before mutation. Empty catalog is valid. Title strings are the title identity for this exercise; matching is exact and case-sensitive.
borrow(member_id, title) returns the smallest available copy identifier, or nullopt if the title is unknown or all its copies are loaned. If that member already holds this title, raise std::invalid_argument without changing any loan. return_copy(copy_id, member_id) returns true after releasing the member's loan; return false for an unknown or unloaned copy. If a different member holds it, raise std::invalid_argument without changing state. available(title) returns a fresh sorted list of free copy identifiers. loans() returns a fresh list of fresh value records with keys copy_id, title, and member_id, sorted by copy identifier. Copy constructor input and keep instances independent. Do not add due dates or catalog editing.
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.
struct Loan {std::string copy_id,title,member_id;bool operator==(const Loan&)const=default;};
inline Strings available_copies(const Vec<std::tuple<std::string,std::string,bool>>& copies,const std::string& title);
class Library { public:
explicit Library(const Vec<std::pair<std::string,std::string>>& copies);
Opt<std::string> borrow(const std::string& member,const std::string& title);
bool return_copy(const std::string& id,const std::string& member);
Strings available(const std::string& title) const;
Vec<Loan> loans() const;
};Compile and run from this directory:
c++ -std=c++20 -O2 -Wall -Wextra -pedantic -pthread main.cpp -o practice
./practice --stage prototype
./practice --stage finalEdit 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.
Complete the starter code, then run the checks.
Results and errors will appear here.
C++ runtime powered by Wasmer.