⚡0 XP🔥0

Project: Library Management System

Lesson 15 of 15 ⏱ 8 min read ✎ 1 exercises ❓ 3-question quiz ▶ Runs Java

Build a complete object-oriented library system in Java - Book and Member classes, a Library service with collections, custom exceptions, fines, reports with streams, and a command-driven interface.

This project uses everything from the course - classes, encapsulation, enums, collections, custom exceptions, streams and a command loop - to build the kind of system asked for in college mini-projects and fresher interviews.

Requirements#

  • Add books (ISBN, title, author, category) and members (id, name).
  • Issue a book to a member (max 3 books per member; a book can be issued to only one member).
  • Return a book; charge a fine if it is late (Rs 2/day for the first week, Rs 5/day after; loan period 14 days).
  • Search books by title/author.
  • Reports: available books, issued books, and the count per category.

Design#

Book            Member              Library                      LibraryException
- isbn          - id                - Map<String, Book> books    (checked, custom)
- title         - name              - Map<Integer, Member> members
- author        - List<Book> loans  + addBook, addMember
- category      - totalFines        + issue(isbn, memberId, day)
- issuedTo                          + returnBook(isbn, day)
- issueDay                          + search(text), report()
हिंदी में समझें

पहले classes और उनकी ज़िम्मेदारियाँ तय करें: Book अपनी जानकारी और "किसे issue हुई" रखती है, Member अपनी किताबें और fine, और Library सबको संभालती है और नियम लागू करती है। नियम टूटे (जैसे 3 से ज़्यादा किताबें) तो LibraryException फेंकें - main में एक ही जगह उसे पकड़ कर संदेश दिखाएँ।

The complete program#

Days are simple numbers (day 1, day 20...) so the program's output is the same every time you run it. The input box contains a series of librarian commands.

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Issued 'Head First Java' to Asha on day 1, due day 15
Issued 'Data Science from Scratch' to Asha on day 2, due day 16
ERROR: 'Head First Java' is already issued to Asha
Issued 'Let Us C' to Ravi on day 3, due day 17
Issued 'The Lean Startup' to Asha on day 5, due day 19
ERROR: Asha already has 3 books
Asha returned 'Head First Java' 5 days late, fine Rs 10
Asha returned 'Data Science from Scratch' 14 days late, fine Rs 49
Search 'java':
  B101 | Head First Java              | Kathy Sierra       | PROGRAMMING
  B105 | Effective Java               | Joshua Bloch       | PROGRAMMING
--- Available ---
B101 | Head First Java              | Kathy Sierra       | PROGRAMMING
B103 | Data Science from Scratch    | Joel Grus          | DATA
B105 | Effective Java               | Joshua Bloch       | PROGRAMMING
--- Issued ---
Let Us C -> Ravi
The Lean Startup -> Asha
--- Books per category --- {PROGRAMMING=3, DATA=1, MANAGEMENT=1}
Fines: Asha Rs 59

Walk-through#

  1. Asha borrows three books; the fourth request (B105) fails because of MAX_LOANS.
  2. Ravi cannot borrow B101 while Asha has it - a LibraryException is thrown and printed.
  3. B101 was issued on day 1, due day 15, returned on day 20: 5 days late → Rs 10.
  4. B103 was issued on day 2, due day 16, returned on day 30: 14 days late → 7 × 2 + 7 × 5 = Rs 49.
  5. The report uses streams to list available and issued books and to count books per category.

Ideas to extend it#

  1. Save books and members to CSV files with BufferedWriter/BufferedReader (try-with-resources).
  2. Use java.time.LocalDate for real dates instead of day numbers.
  3. Add a reservation queue per book (Queue<Member>).
  4. Make Book and Member implement Comparable and print sorted lists.
  5. Turn it into a Spring Boot REST API or a JavaFX desktop app as a portfolio project.

Summary#

You combined classes with encapsulated state, an enum, a custom checked exception, Map/List collections, streams with groupingBy, a modern switch and a command loop into a working system - a solid example to talk through in a fresher interview.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Overdue fine calculation

The library charges Rs 2 per day for the first 7 days late and Rs 5 per day after that. Write static int fine(int daysLate) (0 if not late). The given main prints:

0 days late: Rs 0
5 days late: Rs 10
12 days late: Rs 39
Expected output:
0 days late: Rs 0
5 days late: Rs 10
12 days late: Rs 39

Quick Quiz

Q1.

Why does Library keep books in a Map<String, Book> keyed by ISBN?

Q2.

Why is LibraryException a custom exception?

Q3.

Which OOP principle does Book.issueTo() changing its own private fields show?