Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 50 ยท Projects

Project: Design a Library System

A guided design project: write requirements and use cases for a library, draw class and sequence diagrams, then code and test the borrowing rules.

In this project you will take a small system from a paragraph of description all the way to tested code, using the techniques from the earlier chapters: requirements, use cases, class diagrams, sequence diagrams and testing. The system is a small public library: members borrow copies of books, return them, renew loans, reserve titles that are out, and pay fines.

The diagrams are drawn with Mermaid and are read-only. The code is a JavaScript stand-in: a plain in-memory model of the rules, without a database, screens or a real calendar. It is the kind of "domain model" you would write first in a real project, before any user interface exists. Dates are plain day numbers so that every run gives identical results.

Step 1: the problem statement

Start with the words a librarian would use:

Members borrow copies of books for 14 days. A member can have at most 3 books at once. Late returns cost a fine of 25 cents per day. A member whose unpaid fines reach 5.00 cannot borrow until they pay. If every copy of a title is out, a member can reserve it, and the next copy returned is held for the first person in the queue. A loan can be renewed once for another 14 days, unless the loan is overdue or someone is waiting for the title.

Already there are decisions hiding in these sentences. What does "reach 5.00" mean exactly, at or above? Is the day a book is due still on time? Can a member reserve a title they are already borrowing? A good designer lists the questions and records the answers. For this project we decide:

  • A loan is on time through its due day; fines start the day after.
  • Borrowing is blocked when fines are at or above 5.00 (500 cents). Money is stored in whole cents to avoid rounding errors.
  • Reserving is allowed only when no copy can be borrowed right now.
  • A member may reserve a title only once at a time.

Step 2: requirements and use cases

Turn the statement into numbered, testable requirements. Numbering matters because tests and design elements can later point back to them (see the traceability tool project).

ID Requirement
R1 A loan is due 14 days after the borrowing day.
R2 A member may have at most 3 active loans.
R3 A copy that is on loan cannot be borrowed by anyone else.
R4 A late return costs 25 cents for each day after the due day.
R5 A member with unpaid fines of 500 cents or more cannot borrow.
R6 A member can reserve a title when no copy is available; the next returned copy is held for the first member in the queue.
R7 A loan can be renewed once, unless it is overdue or the title is reserved.

The actors are the member and the librarian (who works the desk). The main use cases are Borrow a copy, Return a copy, Renew a loan, Reserve a title and Pay a fine. Here is the first one written out in the structured form from the use cases lesson:

TEXT
Use case: Borrow a copy
Primary actor: Member (served by the librarian)
Precondition: The member has a library card.
Main success scenario:
  1. Librarian identifies the member.
  2. Librarian scans the copy.
  3. System checks fines, loan limit and that the copy is available.
  4. System records the loan and shows the due date.
Extensions:
  3a. Fines at or above 5.00: system refuses and shows the amount owed.
  3b. Member already has 3 loans: system refuses.
  3c. Copy is held for another member: system refuses.
Postcondition: The copy is on loan to the member until the due day.

Notice that each extension maps to a requirement (R5, R2, R6). That is the point of writing them: error paths become requirements instead of surprises.

From words to working code

Describe Write the rules in plain words.
Number Turn rules into testable requirements.
Model Use cases, classes, sequences.
Test Code each rule and prove it.

Every step feeds the next; a vague sentence early becomes a bug later.

1 / 2

Step 3: the class diagram

Find the nouns in the problem statement: member, book, copy, loan, fine, reservation, library. Then decide which are classes and which are just attributes. A fine is only an amount owed, so it becomes an attribute of the member. A title and a copy are different things: you reserve a title, but you borrow a particular copy. Mixing them up is the classic mistake in library models, and it breaks reservations.

classDiagram
    class Library {
        +borrow(memberId, isbn) Loan
        +giveBack(copyId) int
        +renew(memberId, isbn) int
        +reserve(memberId, isbn) int
        +payFine(memberId, cents)
    }
    class Member {
        +id
        +name
        +fines
    }
    class Title {
        +isbn
        +title
    }
    class Copy {
        +id
        +heldFor
    }
    class Loan {
        +dueDay
        +returnedDay
        +renewed
    }
    Library "1" o-- "*" Member
    Library "1" o-- "*" Title
    Title "1" *-- "1..*" Copy
    Member "1" --> "0..3" Loan : has active
    Loan "*" --> "1" Copy

Titles own their copies (composition); a member has at most three active loans; every loan refers to exactly one copy.

The multiplicities carry rules: 0..3 is requirement R2 drawn as a picture. The Library class acts as a facade: callers use its few methods and never edit loans directly, which keeps the rules in one place (see design principles).

Step 4: a sequence diagram

A sequence diagram checks that the classes can actually collaborate to deliver a use case. Here is "Borrow a copy", including one refusal.

sequenceDiagram
    actor L as Librarian
    participant Lib as Library
    participant M as Member
    participant C as Copy
    L->>Lib: borrow(memberId, isbn)
    Lib->>M: check fines
    alt fines at or above 500
        Lib-->>L: error "unpaid fines too high"
    else fines are fine
        Lib->>M: count active loans
        Lib->>C: find an available copy
        Lib-->>L: Loan with due day
    end

Borrowing checks the cheapest rejections first and returns either an error or a loan.

Drawing it exposes a design decision: the order of the checks. If a member has both too many loans and too many fines, which message do they see? We choose fines first, then the loan limit, then availability. Write choices like this into the tests so a later change cannot flip them by accident.

Step 5: the model and its tests

Now code the model. The code below is complete: the Library class holds all the state and rules, and a tiny test runner at the bottom checks the requirements. Each test name starts from a requirement, and each uses a fresh library from setup() so the tests do not depend on each other. Error cases are tested by checking the exact error message, because the messages are part of the behavior the librarian sees.

JS The library model with tests

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

No output.

All ten tests pass. Try breaking a rule on purpose: change FINE_LIMIT to 501 and the R5 test fails, because a fine of exactly 500 would no longer block; change >= to > in borrow and the same thing happens. That is the point of a boundary test: it pins down "reach 5.00" so the ambiguity in the original sentence cannot return. Also note what the model leaves out on purpose: no database, no user interface, no real dates, no librarian log-in. A first design should include only what the rules need.

Step 6: test the policy as a table

The borrow method makes its decision from three facts: the member's fines, how many loans they hold and whether a copy is free. With three values per number and two for availability, you can list every combination and check the decision against what the requirements say. This is a decision table, one of the test design techniques, and it works best on a pure function with no state. The second stand-in extracts the policy and prints the full table.

JS A decision table for borrowing

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

No output.

The values were chosen at the boundaries: 499 and 500 for fines, 2 and 3 for loans. Eighteen combinations produce four allow rows and fourteen refusals. Read the table against the requirements: a member with 500 in fines is refused whatever else is true (the priority we chose in the sequence diagram), and "no copy" only matters when the other two checks pass. A librarian or product owner can read this table and confirm it, which is far easier than reading code.

Step 7: extend and review

You now have requirements, a use case, two diagrams, a model and tests. Use these exercises to deepen the design; each one is a small change request and should start with a requirement and a failing test.

  1. Reservation expiry: a held copy is released if the member does not collect it within 3 days. Which class owns the timer? What changes in the class diagram?
  2. Lost copies: a member reports a copy lost. Define the charge, remove the copy from circulation and decide what happens to the queue.
  3. Different rules per member type: children get 5 loans and no fines. Where do you put the limits so borrow does not fill up with if statements? (Hint: a MembershipPlan with the numbers as data.)
  4. Persistence: the model must survive a restart. Which parts belong in a database, and does the Library facade need to change?
  5. Review the design: are there any rules in the code that are not in the requirements table? Add them or delete them.

Finally, run the design through the checklist of software qualities: is the code easy to change (maintainability), do the rules give the same answer every time (correctness) and what happens with a thousand members (performance)? Several operations here scan all loans; that is fine for a branch library and a good example of a decision you would revisit when the numbers grow.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the model separate a title from a copy?

    Reservations apply to a title (any copy will do), while loans and holds apply to one physical copy. Merging them breaks the reservation rules.
  2. 2. Why is money stored as whole cents in the model?

    Whole numbers of cents are exact. Floating point amounts like 0.1 + 0.2 do not add up exactly, which causes subtle bugs in comparisons such as 'fines at or above 5.00'.
  3. 3. What does the boundary test 'a fine of exactly 500 blocks borrowing' protect against?

    Boundary values are where off-by-one errors live. The test pins down what 'reach 5.00' means.
  4. 4. In the decision table, why do 'refuse: no copy' rows only appear when fines are below 500 and loans are below 3?

    The code checks fines, then the loan limit, then availability, so a member who fails an earlier check is refused for that reason first.

Summary

  • A design project starts by turning a plain description into numbered, testable requirements and by recording every ambiguity and the decision made about it.
  • Nouns suggest classes: here titles, copies, members and loans, with the Library class as the single place where the rules live.
  • A class diagram carries rules in its multiplicities, and a sequence diagram checks that the classes collaborate to deliver a use case, including the order of the checks.
  • Tests named after requirements, with boundary values and exact error messages, turn the rules into something you can run; a decision table checks every combination of the inputs to a policy.
  • Real systems add storage, screens, security and scale, but the domain model and its tests are the stable core.

Next: Project: An Order Workflow State Machine.