Stop 07 · Object-Oriented ProgrammingProjects · 1/1

Library Management System

Part of the Python Roadmap

In short

This project is where OOP stops being theory. You'll design classes that talk to each other, enforce real rules like borrowing limits, and raise clear errors, just like a backend developer does.

A small object-oriented system with Book, Member and Library classes that lets members borrow and return books, tracks due dates and stops borrowing when limits are reached. It practises class design, methods and custom exceptions.

The jaadu sits at a library issue desk stamping a due date inside a book while a student waits with two more books in hand.

Think of it like this

Your college library desk

The librarian keeps a register of books, a list of members with their cards, and rules: three books at a time, two weeks to return. Each book knows if it's on the shelf, each member knows what they've borrowed, and the desk makes sure nobody breaks the rules. Those are your three classes.

Watch it happen

Borrowing a book

Member
Library
Book

borrow('B12')

1/5Sana asks the library for book B12.

The big ideas

What you need to know

Design classes first

Book holds title and status, Member holds name and borrowed books, Library coordinates everything.

Rules as exceptions

LimitReachedError and BookUnavailableError make broken rules explicit and easy to handle.

Dates matter

Store a due date with datetime and timedelta, and compute late fees from it.

Lookups by id

Library keeps dicts of books and members by id, so finding one is instant.

How it works

Build order

  1. 1

    Book and Member

    Two dataclasses with ids, names and status fields.

  2. 2

    Exceptions

    A LibraryError base plus specific errors.

  3. 3

    Library class

    add_book, add_member, borrow and give_back with all checks.

  4. 4

    Late fees

    Compute ₹2 per day late when a book is returned.

  5. 5

    Menu or tests

    Drive it from a small CLI or a few test calls.

See it in code

Walk through the code

python
1from dataclasses import dataclass, field2from datetime import date, timedelta3 4class LibraryError(Exception): ...5class BookUnavailableError(LibraryError): ...6class LimitReachedError(LibraryError): ...7 8@dataclass9class Book:10    book_id: str11    title: str12    due: date | None = None          # None means on the shelf13 14@dataclass15class Member:16    name: str17    books: list[Book] = field(default_factory=list)18 19class Library:20    MAX_BOOKS = 321    FINE_PER_DAY = 222 23    def __init__(self) -> None:24        self.catalogue: dict[str, Book] = {}25 26    def add_book(self, book: Book) -> None:27        self.catalogue[book.book_id] = book28 29    def borrow(self, member: Member, book_id: str) -> Book:30        if len(member.books) >= self.MAX_BOOKS:31            raise LimitReachedError(f"{member.name} already has {self.MAX_BOOKS} books")32        book = self.catalogue[book_id]33        if book.due is not None:34            raise BookUnavailableError(f"'{book.title}' is issued until {book.due}")35        book.due = date.today() + timedelta(days=14)36        member.books.append(book)37        return book38 39    def give_back(self, member: Member, book: Book, on: date) -> int:40        assert book.due is not None41        fine = max(0, (on - book.due).days) * self.FINE_PER_DAY42        member.books.remove(book)43        book.due = None44        return fine45 46lib = Library()47lib.add_book(Book("B12", "Wings of Fire"))48sana = Member("Sana")49b = lib.borrow(sana, "B12")50print(lib.give_back(sana, b, b.due + timedelta(days=5)))   # 1051try:52    lib.borrow(Member("Om"), "B12")53    lib.borrow(Member("Jai"), "B12")54except LibraryError as err:55    print("Sorry:", err)

1/9A base LibraryError and two specific errors, one line each.

The heart of the system: two dataclasses, a family of exceptions and a Library that enforces the rules.

Save yourself the pain

Common mistakes

Oops

Putting all the logic in one giant Library class with books stored as plain lists of strings.

Do this instead

Give each real-world thing its own class. Book and Member carry their own state, Library coordinates.

Oops

Printing 'Error!' inside borrow() and returning None.

Do this instead

Raise a specific exception so the caller, whether a CLI or a web API later, decides how to show it.

Your mission

Extend your library

~60 min

Quick check

Why does the except clause catch LibraryError instead of BookUnavailableError?

Go deeper

  1. 1Sketch your classes and their methods before writing code.
  2. 2Raise custom exceptions for unavailable books or borrowing limits.
  3. 3Save and load the library's data from a JSON file.

Also part of these roadmaps

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge