Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 57 ยท Low-Level Design Projects

Project: Library Management System

Design a library system: members, books, copies, loans, reservations, fine strategies, an observer for notifications and a repository layer, with runnable Node.js code.

A library is a gentle but realistic design problem. It has familiar entities, rules that really do change (loan length, fines, limits), a waiting line for popular books and a clear need to tell people things ("your book is ready"). It also contains a trap that catches many first designs: the difference between a book (a title) and a copy (one physical item you can hold). In this project you work through the process from How to Approach a Low-Level Design Problem and finish with a model you can run and extend.

Entities

Book A title in the catalog, identified by ISBN.
Copy One physical item that can be on loan.
Member Borrows, reserves and owes fines.
Loan Links a member to a copy, with dates.
1 / 2

Step 1: requirements

  • The catalog has books (title, ISBN). Each book has one or more copies.
  • A member can borrow up to 3 books at a time. A loan lasts 14 days.
  • If all copies are on loan, a member can reserve the book and joins a waiting line.
  • When a copy of a reserved book is returned, it is held for the first member in the line. Nobody else can borrow it, and that member is notified.
  • Returning a book late creates a fine for each late day. The fine rule may change (flat rate, capped, free grace days).
  • Out of scope: search by author, renewals, payment of fines, librarian accounts, databases.

Step 2: entities and relationships

The key insight is the split between Book and Copy. A reservation is about a title ("I want any copy of this book"), while a loan is about a specific copy (the one in your bag). If you model only Book, you cannot say that two of three copies are out.

classDiagram
    class Library {
      +borrow(memberId, isbn) Loan
      +reserve(memberId, isbn) number
      +returnCopy(copyId) result
      +on(event, listener)
    }
    class Book {
      +isbn
      +title
    }
    class Copy {
      +id
      +isbn
      +heldFor
    }
    class Member {
      +id
      +name
      +fines
    }
    class Loan {
      +copyId
      +memberId
      +dueOn
      +returnedOn
    }
    class FinePolicy {
      <<interface>>
      +fine(daysLate)
    }
    Book "1" --> "many" Copy
    Member "1" --> "many" Loan
    Copy "1" --> "many" Loan
    Library --> FinePolicy
    FinePolicy <|.. PerDayFine
    FinePolicy <|.. CappedFine

A loan connects a member and a copy. The library coordinates and consults a fine policy.

Notice that Loan is an entity of its own. It carries the dates and the state (returned or not) that belong to neither the member nor the copy, which is the kind of hidden entity described in How to Approach a Low-Level Design Problem.

Step 3: the public API

The Library class is the single entry point (a small facade, see Facade):

  • addBook(isbn, title, copyCount) and addMember(id, name) set up data.
  • borrow(memberId, isbn) returns a Loan or throws a clear error.
  • reserve(memberId, isbn) returns the position in line.
  • returnCopy(copyId) returns how many days late the book was and the fine.
  • on(event, listener) lets other code react to borrowed, returned, reservation-ready and fine-charged.

Step 4: patterns, and why

Repository. Library never touches arrays or a database directly. It talks to repositories with add, get and find. Today they keep objects in a Map. Tomorrow one can talk to SQL without the library changing, because it only knows the small interface. In tests an in-memory repository is a perfect fake. See Repository and Unit of Work.

Strategy. The fine rule is a FinePolicy with one method. PerDayFine multiplies, and CappedFine wraps another policy to limit the total. Adding "three free days" means one new class, not an edit to returnCopy. This is Strategy, plus a small decorator-like wrapper.

Observer. Sending an email is not the library's job. When a reserved book becomes ready, the library emits reservation-ready. A notifier (email today, SMS later) subscribes. The library has no idea who listens. See Observer.

Injected clock. Days are numbers from a clock object, so a test can say "twenty days later" without waiting. Dependencies (repositories, fine policy, clock) are passed in through the constructor, as in Dependency Inversion and Dependency Injection.

Step 5: the code

Read the model from the top: repository, entities, fine policies, the Library service, and a notifier that is just one observer. The demo then walks one realistic story: Ana borrows the only copy of a book, Ben is refused and reserves it, Ana returns it five days late, and Ben is told it is ready. Ana's own attempt to borrow it again fails because the copy is now held for Ben.

Node.js The library model and a late return

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check the order of events in the output. The two email lines appear while returnCopy runs, before the "returning" line is printed, because observers are called synchronously inside the method. Ana's fine is 10, because 5 days at a rate of 2 is 10. Ben's due date is day 34, because he borrowed on day 20 and a loan lasts 14 days.

Step 6: rules and edge cases

Most bugs in systems like this live in the edges: the loan limit, unknown ids, double reservations, a held copy, a fine that should be capped. The next example runs the same model with a different fine policy (capped at 10) and uses assert.throws to prove each rule. Note that nothing in the Library class changed to get the capped fine. Only the object passed to createLibrary is different.

Node.js Checking the rules with assertions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Design notes and trade-offs

  • Where rules live. Limits and loan length are constants on Library. If librarians need to change them at run time, they become a LoanPolicy object, another strategy.
  • Held copies. heldFor is a small state on the copy. If the member never shows up, you need an expiry rule (a "hold expires after 3 days" job). That is an extension, and it shows why a copy may deserve a full state machine later (see State).
  • Queue per title. The waiting line belongs to a book (ISBN), not to a copy, because any returned copy will do.
  • Errors. Every failure throws an error with a readable message. A real API would use error types or codes so callers can react.
  • Concurrency. Two members borrowing the last copy at once is the same check-then-act race you saw in Project: Parking Lot. Make "find an available copy and create the loan" one atomic step, with a lock in memory or a transaction with a unique constraint in a database.
  • Extensions. Renewals add a method on Loan. Different member types (student, staff) change the limits: a strategy again. E-books have no physical limit, so they are a different kind of copy, which is where an interface or a subclass starts to earn its place.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the design have both Book and Copy?

    Reservations are about a title, loans are about a specific copy. One class cannot represent both facts cleanly.
  2. 2. What is the main benefit of the repository classes in this model?

    The repository is a seam between the domain logic and storage. It also makes in-memory fakes trivial in tests.
  3. 3. Why is sending the notification email done by an observer and not inside returnCopy?

    Notification is a separate concern. Observer keeps it out of the core logic and open to extension.
  4. 4. How does the model change the fine rule from per-day to capped, without editing Library?

    The policy is injected, so swapping behavior is just passing another object that has the same fine method.

Summary

  • Separate the title (Book) from the physical item (Copy); reservations belong to the title and loans to a copy, and Loan is an entity of its own.
  • A repository hides storage, a fine policy (Strategy) holds the changeable rule, and observers deliver notifications without the library knowing who listens.
  • Reserved copies are held for the first member in line, and rules such as the loan limit are enforced in one service.
  • Inject the clock, the policy and the repositories; then tests and new rules are cheap.
  • In a concurrent system, finding an available copy and creating the loan must be one atomic step.

Next lesson: Project: Ride Sharing and Vending Machine.