Skip to content
C++ project #09Intermediate

Library Management System

Catalogue books, register members, lend and return copies with due dates, late fines, borrowing limits and a waiting list.

Estimated time
~5 hours to build
Files
5 files · 430 lines
Technologies
C++17, <map>, <vector>, <algorithm>

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

Write a command-driven library system. It must catalogue books (adding copies to existing titles), register members, lend books for 14 days with a limit of three loans per member, refuse loans to members with unpaid fines, charge 25 cents per late day on return, keep a first-come waiting list per title that is served automatically when a copy comes back, search by title or author, list overdue loans and print a member report.

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.

TextArchitecture diagram
+-----------+
command ->| Library |
+-----------+
| | |
books_ | | | members_ loans_ (vector<Loan>)
map<isbn,Book> | map<id,Member> +--> {isbn, member, due_day}
|
waitlist_: map<isbn, vector<member id>>

Folder structure

TextProject tree
library-system/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
Book, Loan and Member types, the Library class with all lending rules, and the command parser (book lines use title | author).
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Library System === (loan period 14 days, fine $0.25/day)
> Catalogued "The C++ Programming Language" (1/1 available)
> Catalogued "A Tour of C++" (2/2 available)
> Catalogued "Effective Modern C++" (1/1 available)
> Added copies of "A Tour of C++" (3/3 available)
> Asha joined as member #1
> Ravi joined as member #2
> Meera joined as member #3
> Asha borrowed "The C++ Programming Language", due on day 15
> No copies left; Ravi is number 1 on the waiting list
> No copies left; Meera is number 2 on the waiting list
> Ravi borrowed "Effective Modern C++", due on day 15
> No book with ISBN 978-0000000000
> 978-0134997834 A Tour of C++ by Bjarne Stroustrup [3/3]
978-0321563842 The C++ Programming Language by Bjarne Stroustrup [0/1]
> It is now day 17
> Asha: "The C++ Programming Language" 2 day(s) overdue
Ravi: "Effective Modern C++" 2 day(s) overdue
> Asha returned "The C++ Programming Language", 2 day(s) late: fine $0.50
-> reserved copy goes to Ravi
Ravi borrowed "The C++ Programming Language", due on day 31
> Asha owes $0.50 and must pay before borrowing
> Asha paid $0.50
> Asha borrowed "A Tour of C++", due on day 31
> It is now day 20
> Ravi returned "Effective Modern C++", 5 day(s) late: fine $1.25
> Day 20: 3 titles, 2 on loan
#1 Asha 1 book(s)
#2 Ravi 1 book(s), owes $1.25
#3 Meera 0 book(s)
>
Input
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.txt and compare with tests/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 1 the 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

  1. 1Replace the loans vector with a std::multimap keyed by member id.
  2. 2Send a 'due tomorrow' reminder when the clock advances.
  3. 3Support searching by ISBN prefix.

Built and tested it yourself? Mark it to update your dashboard.