Introduction
A library is a good model for a system where several kinds of objects cooperate and rules apply across them: a member can't borrow while owing fines, a book can't be lent when no copies remain, and a returned copy should go to the first person waiting. Time is simulated with an advance command, so you can test overdue logic without waiting two weeks.
Problem statement
Objectives
- Divide responsibilities between Book, Member, Loan and Library
- Use std::map::try_emplace to add-or-update
- Implement cross-object business rules in one place
- Model simulated time to make time-based rules testable
Features
- Multiple copies per title
- Borrowing limit, fines and payment
- Automatic hand-over to the waiting list
- Case-insensitive search across titles and authors; overdue report
Concepts demonstrated
- Class design
- std::map
- Lambdas with find_if/copy_if
- Queues (waiting list)
- Simulated time
System architecture
Library is the façade that owns everything: books_ (ISBN → Book with total and available copies), members_ (id → Member with a fine balance), a vector of Loan records and a waiting list per ISBN. borrow checks the rules in a fixed order (member exists, book exists, no fines, under the limit, not a duplicate, copies available) and puts the member on the waiting list when no copy is free. give_back computes lateness from the simulated today_, adds the fine, frees the copy and then serves the waiting list, skipping anyone who is no longer allowed to borrow.
Folder structure
Complete source code
Makefile
What each file does
- src/main.cpp
- Book, Loan and Member types, the
Libraryclass with all lending rules, and the command parser (book lines usetitle | author). - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
book 978-0321563842 1 The C++ Programming Language | Bjarne Stroustrup book 978-0134997834 2 A Tour of C++ | Bjarne Stroustrup book 978-1491903995 1 Effective Modern C++ | Scott Meyers book 978-0134997834 1 A Tour of C++ | Bjarne Stroustrup join Asha join Ravi join Meera borrow 1 978-0321563842 borrow 2 978-0321563842 borrow 3 978-0321563842 borrow 2 978-1491903995 borrow 1 978-0000000000 search stroustrup advance 16 overdue return 1 978-0321563842 borrow 1 978-0134997834 pay 1 borrow 1 978-0134997834 advance 3 return 2 978-1491903995 report quit
Testing
- Run
./app < tests/sample-input.txtand compare withtests/expected-output.txt. - Asha returns her book 2 days late (fine $0.50); the copy goes straight to Ravi, who was first on the waiting list.
- Asha cannot borrow until she pays; after
pay 1the loan succeeds. - Ravi's second book comes back 5 days late, leaving a $1.25 fine in the report.
Common errors
Waiting member gets the book even though they owe fines
Check the borrowing rules again when serving the waiting list, as give_back does by calling borrow.
Book titles with spaces are cut short
Read the title and author with std::getline after the fixed fields, and split on the | separator.
Possible improvements
- Persist the catalogue, members and loans in files
- Allow renewing a loan once if nobody is waiting
- Add reservations that expire after three days
Additional challenges
- 1Replace the loans vector with a std::multimap keyed by member id.
- 2Send a 'due tomorrow' reminder when the clock advances.
- 3Support searching by ISBN prefix.
Built and tested it yourself? Mark it to update your dashboard.