Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 39 ยท Projects and Practice

Project: Library System Model

Build a complete UML model of a library: use cases, domain classes, a borrowing sequence and a book copy state machine, then check it with running code.

In this project you model a small library system from scratch, using the diagrams in the order that works in real projects: goals first, then concepts, then behavior, then lifecycle. At the end you implement the core rules as code and check that the model and the code agree. Work along with a pen or a text editor and write each diagram yourself before reading ours.

The brief

A town library lends books to registered members. Members can search the catalog, borrow up to three books at a time for 14 days, return them and reserve a book that is currently out. Librarians register members, add books and handle overdue fines of 0.50 per day. A book title can have several physical copies.

Modeling steps

Goals Use cases for members and librarians.
Concepts Domain classes and associations.
Scenarios Sequence of borrowing a book.
Lifecycle States of one book copy.

Each diagram answers a different question.

1 / 2

Step 1: use cases

Actors are Member and Librarian. The goals are the use cases.

flowchart LR
    M(["Member"])
    L(["Librarian"])
    subgraph Library["Library system"]
        U1(["Search catalog"])
        U2(["Borrow book"])
        U3(["Return book"])
        U4(["Reserve book"])
        U5(["Pay fine"])
        U6(["Register member"])
        U7(["Add book"])
    end
    M --- U1
    M --- U2
    M --- U3
    M --- U4
    M --- U5
    L --- U6
    L --- U7
    L --- U3

Mermaid has no use case diagram, so ellipses are drawn as stadium nodes. A tool would add include and extend links, for example "Return book" extends to "Pay fine" when it is overdue.

Notice that "Return book" is available to both actors: a librarian may check a book in at the desk. Writing one short description per use case (main flow, alternative flow, precondition) is a useful exercise: for "Borrow book", the precondition is that the member is active, has fewer than three loans and no unpaid fine.

Step 2: the domain class diagram

Look for nouns in the brief: member, book, copy, loan, reservation, fine, librarian. A title and a copy are different things: you reserve a title, but you borrow a copy.

classDiagram
    class Member {
        -String name
        -String cardNumber
        +canBorrow() boolean
    }
    class Book {
        -String isbn
        -String title
        -String author
    }
    class Copy {
        -String barcode
        -CopyStatus status
    }
    class Loan {
        -Date borrowedOn
        -Date dueOn
        -Date returnedOn
        +isOverdue(today) boolean
        +fine(today) double
    }
    class Reservation {
        -Date createdOn
    }
    class CopyStatus {
        <<enumeration>>
        Available
        OnLoan
        Reserved
        Lost
    }
    Book "1" --> "1..*" Copy : has
    Member "1" --> "0..3" Loan : holds
    Loan "*" --> "1" Copy : of
    Member "1" --> "*" Reservation : makes
    Reservation "*" --> "1" Book : for
    Copy ..> CopyStatus

Check the multiplicities against the brief: 0..3 loans per member is the three-book rule. Check composition: a Copy cannot exist without its Book, so you could draw a filled diamond from Book to Copy. A Loan is an association class: it carries data (dates) about the link between member and copy, and that is why it is a class of its own.

Step 3: sequence diagram for "Borrow book"

sequenceDiagram
    actor M as Member
    participant UI as DeskTerminal
    participant L as LoanService
    participant C as Copy
    participant R as Member record
    M->>UI: scan card, scan copy
    UI->>L: borrow(cardNumber, barcode)
    L->>R: canBorrow()
    R-->>L: true
    L->>C: isAvailable()
    C-->>L: true
    L->>C: markOnLoan()
    L-->>UI: Loan(due in 14 days)
    UI-->>M: receipt

Every message must correspond to an operation in the class diagram. This diagram uses canBorrow() on Member, isAvailable() and markOnLoan() on Copy, and borrow() on a new LoanService. If the class diagram lacks them, add them now: this is how sequence diagrams drive the design of operations.

Step 4: state machine for a copy

stateDiagram-v2
    [*] --> Available
    Available --> OnLoan : borrow
    OnLoan --> Available : return [no reservation]
    OnLoan --> Reserved : return [reservation waiting]
    Reserved --> OnLoan : borrow by reserver
    Reserved --> Available : reservation expires
    OnLoan --> Lost : reportLost
    Lost --> [*]

Ask the review questions: can an Available copy be returned? (No: the event is ignored.) What happens to a reserved copy nobody collects? (reservation expires.) The diagram forces these decisions.

Step 5: check the model with code

The following code implements the Loan rules and the copy lifecycle. If the model is consistent, the behaviors described in it run without surprises.

Python Library rules from the model

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 input()), it gets the next line from this box.

Step 6: derive the tests

Each rule on a diagram becomes a test: the multiplicity 0..3, the guard [reservation waiting], the transition borrow from OnLoan (illegal). The next script lists them as a simple checklist, a first step toward automated tests.

Python Test checklist generated from the model

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 input()), it gets the next line from this box.

Extensions to try

  • Add include and extend relationships between use cases and describe "Return book" in a table.
  • Add an Author class (many-to-many with Book) and decide whether it needs an association class.
  • Draw an activity diagram for the nightly "send overdue reminders" job.
  • Add a deployment diagram: a desk terminal, an app server, a database.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why are Book and Copy separate classes?

    They have different identities and lifecycles: one title, many physical copies.
  2. 2. Which model element enforces the rule 'at most three books at a time'?

    Multiplicities are constraints, and the code must implement them.
  3. 3. A sequence diagram sends the message markOnLoan() to Copy, but Copy has no such operation. What do you do?

    Diagrams must agree: the sequence diagram has revealed a missing operation.
  4. 4. Why is Loan an association class?

    Dates and fines belong to the borrowing relationship itself.

Summary

  • Model in order: goals (use cases), concepts (classes), scenarios (sequences), lifecycles (states).
  • Multiplicities and guards are rules that become code and tests.
  • Sequence diagrams discover missing operations; state machines discover missing events.
  • Splitting title and copy is a typical modeling insight that needs domain questions.
  • Implement the rules in a few lines to check that the model is consistent.

Next: Project: Online Shop Model