Design a library management system
Design a library system where members search for books, borrow copies and return them.
Good first question for: Fresher
Requirements to confirm first
- Search the catalogue by title, author or ISBN
- A book can have several copies, and a member borrows one specific copy
- A limit on how many books a member holds, and a due date for every loan
- Return a copy and charge a fine when it is late
- Reserve a book when every copy is out
- Ask: what are the fine rules? Are there member types? Are renewals allowed?
Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.
Show the answer key (write yours first)
Classes and what each one owns
- Book
- The title itself: ISBN, title, authors
- BookCopy
- One physical copy with its own id and status
- Member
- The person who borrows, with a membership type that sets the limits
- Loan
- One copy and one member, with borrowed, due and returned dates
- Reservation
- A member waiting, in order, for any copy of a book
- Catalogue
- Search over books
- FinePolicy
- Interface that turns a late loan into an amount
How they connect
- Book has many BookCopies; loans are on copies, reservations are on books
- Loan links a Member and a BookCopy; history is the list of past loans, not a field on the copy
- LibraryService uses FinePolicy and the Catalogue; Member never computes its own fine
Design patterns that fit, and why
- Strategy
- Fine and loan-period rules differ by membership type and change over time.
- Observer
- The first member in the reservation queue is told when a copy comes back, without the return code knowing about messages.
- Repository
- Catalogue search behind an interface, so an in-memory index can become a database later.
Code sketch in Java
The core types and one real method. It is a sketch to talk through, not a full program.
enum CopyStatus { AVAILABLE, ON_LOAN, RESERVED, LOST }
class Loan {
final BookCopy copy; final Member member;
final LocalDate borrowedOn, dueOn;
LocalDate returnedOn; // null while the copy is out
}
interface FinePolicy { long fineInPaise(Loan loan, LocalDate today); }
class LibraryService {
Loan borrow(Member member, String isbn) { /* check the limit, pick an available copy, create the loan */ }
long giveBack(Loan loan, LocalDate today) { /* close the loan, compute the fine, serve the reservation queue */ }
}Edge cases to raise yourself
- Two members borrow the last copy at the same moment
- A copy is returned while someone has reserved the book: it must not go back to the open shelf
- A member at the limit, or with unpaid fines
- A lost or damaged copy
Where answers usually go wrong
- One Book class with a 'borrowed' flag. A library holds several copies of one title; without BookCopy you cannot lend two of them.
- Putting the borrower and the due date on the copy. The history is lost, and a Loan object is what fines and reports need.
- Subclasses for Student and Faculty that differ only in two numbers. Use a membership type with limits, or a policy.
Follow-up questions
- Add renewals with a maximum count
- Several branches that share one catalogue
- Recommend books from loan history: where does that code live?