Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Build a Library System with Books Members and Borrow Records

Model a small library using composition: Books, Members, and a Library that manages borrow/return records and availability.

Python practice28 minComposition & Real-World ModelingAdvancedLast updated April 11, 2026

Problem statement

Design a small library system using composition (not inheritance). Create Book and Member classes to hold data, and a Library class that composes them. The Library should: add books (with multiple copies), register members, allow members to borrow and return books, and track availability. Borrowing should fail if the book doesn't exist, the member doesn't exist, no copies are available, or the member already has that book. Returning should fail if the member didn't borrow that book. Provide helper functions to build sample libraries and run small scenarios.

Task

Implement a Library class that manages Book and Member objects, supports borrowing and returning books, and enforces availability and borrowing rules using composition.

Examples

Basic borrow

Input

l = sample_library(); l.borrow('m1', 'ISBN-001')

Output

True

Member m1 successfully borrows ISBN-001 because copies are available.

Input format

You will interact with the Library API via function calls in the tests, e.g., l.borrow(member_id, isbn).

Output format

Functions return boolean values for borrow/return operations (True/False) and lists for availability queries. The test harness compares stringified return values.

Constraints

Use composition: Library should hold Book and Member instances. Do not use external libraries. Ensure operations are idempotent and consistent for edge cases (nonexistent book/member, zero copies, double-borrow prevention).

Samples

Sample 1

Input

l = sample_library(); l.borrow('m1','ISBN-002'); l.return_book('m1','ISBN-002'); l.borrow('m2','ISBN-002')

Output

True

m1 borrows the single copy of ISBN-002, returns it, then m2 can borrow it: final borrow succeeds and returns True.