CS 112 › Lesson 10 of 10

Capstone: OOP Library System

Lesson 10 · OKSTEM College · Associate of Science in Computer Science

Capstone: OOP Library System

You will design and implement a small library management system using every OOP concept from this course. This is an open-ended project — implement it in your local Python environment and test with pytest.

Requirements

Start small: get Book and Member working first, then add Library, then the factory and tests.

Starter Skeleton

from abc import ABC, abstractmethod from typing import Optional class LibraryItem(ABC): @property @abstractmethod def title(self) -> str: ... @property @abstractmethod def item_id(self) -> str: ... @abstractmethod def checkout_duration_days(self) -> int: ... class Book(LibraryItem): def __init__(self, title: str, author: str, isbn: str): self._title = title self.author = author self.isbn = isbn # TODO: implement abstract members + __repr__, __eq__, __lt__ class Member: def __init__(self, name: str, email: str): self.name = name self.email = email self.checked_out: list = [] # TODO: add @property with validation for name and email class Library: item_count = 0 # class variable def __init__(self): self.inventory: list = [] # TODO: add_item, checkout, return_item, create_item factory classmethod

Checklist

Finished early? Add a Reservation system using the Observer pattern — Member subscribes to an item and is notified when it becomes available.

Knowledge Check

Which OOP concept prevents instantiating LibraryItem directly?

Encapsulation is about hiding state, not preventing instantiation.
Correct — unimplemented abstract methods cause TypeError on instantiation.
Name-mangling doesn't prevent instantiation.
Singleton limits to one instance; ABC prevents any instance.
Recap: class LibraryItem(ABC) with @abstractmethod stubs forces every concrete subclass to implement those methods.

Book.__eq__ based on ISBN means

Only books with the same ISBN are equal.
Correct — ISBN uniquely identifies a book; two copies have the same ISBN.
Sorting uses __lt__, not __eq__.
Defining __eq__ sets __hash__ to None; you'd also need __hash__ for set use.
Recap: define equality semantics deliberately. For a Book, same ISBN = same book (value equality), even if they're different Python objects.

sorted(books) works on a list of Book objects because

Without __lt__, sort raises TypeError.
Correct — sorted() uses __lt__ to order elements.
LibraryItem doesn't define __lt__ in the starter skeleton.
sorted() doesn't use __repr__; it uses comparison operators.
Recap: implement __lt__ (and ideally use @functools.total_ordering) to make your objects sortable with sorted() and .sort().

Library.create_item() is an example of

Observer is for event notification.
Correct — the factory decides which class to instantiate based on input.
Singleton controls instance count, not object type.
Factory is a design pattern; abstract method is a language feature.
Recap: Library.create_item('dvd', ...) returns a DVD — the caller doesn't import DVD directly. Easy to extend with new item types.

A property that validates email should raise ValueError when

Length wasn't listed as a requirement; the key check is the @ symbol.
Correct — minimum validation: an email must contain @.
Dots are valid in email addresses.
Lowercase is perfectly valid for email.
Recap: property setters are ideal validation points. Raise ValueError with a descriptive message so the caller knows exactly what was wrong.

← Previous