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.
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 59Walk-through#
- Asha borrows three books; the fourth request (
B105) fails because ofMAX_LOANS. - Ravi cannot borrow
B101while Asha has it - aLibraryExceptionis thrown and printed. B101was issued on day 1, due day 15, returned on day 20: 5 days late → Rs 10.B103was issued on day 2, due day 16, returned on day 30: 14 days late → 7 × 2 + 7 × 5 = Rs 49.- The report uses streams to list available and issued books and to count books per category.
Ideas to extend it#
- Save books and members to CSV files with
BufferedWriter/BufferedReader(try-with-resources). - Use
java.time.LocalDatefor real dates instead of day numbers. - Add a reservation queue per book (
Queue<Member>). - Make
BookandMemberimplementComparableand print sorted lists. - 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.