|
| 1 | +Excellent choice 📚 — a **Library Management System** is a classic OOP case study that demonstrates inheritance, abstraction, and polymorphism in a clean and meaningful way. |
| 2 | + |
| 3 | +Below you’ll get: |
| 4 | +✅ **GitHub-ready README.md** |
| 5 | +✅ **Complete Java program (single file)** |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 🧾 README.md (for GitHub) |
| 10 | + |
| 11 | +```markdown |
| 12 | +# 📚 Library Management System (Java OOP Project) |
| 13 | + |
| 14 | +A real-world **Object-Oriented Programming (OOP)** implementation of a **Library Management System** in Java. |
| 15 | +This project simulates the process of managing books, members, borrowing/returning books, calculating fines, and maintaining library records. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## 🏢 Use Case |
| 20 | + |
| 21 | +The library has: |
| 22 | +- Books of different categories (Fiction, Non-Fiction, Reference) |
| 23 | +- Members (Students, Teachers) |
| 24 | +- A system to issue and return books |
| 25 | +- Fine calculation for late returns |
| 26 | +- Edge-case handling for invalid book IDs, unavailable books, and duplicate issues |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## 🧩 OOP Concepts Applied |
| 31 | + |
| 32 | +| **Concept** | **Example in this System** | |
| 33 | +|--------------|-----------------------------| |
| 34 | +| **Encapsulation** | Private fields in `Book`, `Member`, etc., with getters/setters | |
| 35 | +| **Inheritance** | `Book` → `FictionBook`, `ReferenceBook` | |
| 36 | +| **Polymorphism** | `calculateFine()` behaves differently for `Student` and `Teacher` | |
| 37 | +| **Abstraction** | Abstract `Book` and `Member` classes define core behaviors | |
| 38 | +| **Composition** | `Library` has `List<Book>`, `List<Member>`, and `List<Transaction>` | |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## 🧠 Class Diagram (Simplified) |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | +Book (abstract) |
| 47 | +│ |
| 48 | +├── FictionBook |
| 49 | +├── NonFictionBook |
| 50 | +└── ReferenceBook |
| 51 | + |
| 52 | +Member (abstract) |
| 53 | +│ |
| 54 | +├── Student |
| 55 | +└── Teacher |
| 56 | + |
| 57 | +Transaction → Library |
| 58 | + |
| 59 | +``` |
| 60 | +
|
| 61 | +--- |
| 62 | +
|
| 63 | +## ⚙️ Features |
| 64 | +
|
| 65 | +✅ Add and remove books/members |
| 66 | +✅ Issue and return books |
| 67 | +✅ Prevent duplicate issues |
| 68 | +✅ Fine calculation based on member type |
| 69 | +✅ Display all active transactions |
| 70 | +✅ Handle unavailable or invalid book IDs gracefully |
| 71 | +
|
| 72 | +--- |
| 73 | +
|
| 74 | +## 💻 Example Output |
| 75 | +
|
| 76 | +``` |
| 77 | + |
| 78 | +✅ Book '1984' issued to Student Ravi |
| 79 | +✅ Book 'Physics Fundamentals' issued to Teacher Meena |
| 80 | +⚠️ Book '1984' is already issued! |
| 81 | +✅ Book '1984' returned by Student Ravi |
| 82 | +💰 Fine for late return: ₹20.0 |
| 83 | +❌ Book ID not found in library! |
| 84 | + |
| 85 | +```` |
| 86 | +
|
| 87 | +--- |
| 88 | +
|
| 89 | +## 🧪 Edge Cases Covered |
| 90 | +
|
| 91 | +| **Scenario** | **Behavior** | |
| 92 | +|---------------|--------------| |
| 93 | +| Duplicate issue attempt | Shows "⚠️ Book already issued!" | |
| 94 | +| Invalid book ID | Displays "❌ Book not found!" | |
| 95 | +| Late return | Fine applied based on member type | |
| 96 | +| Reference books | Cannot be borrowed | |
| 97 | +| Empty return list | Graceful handling | |
| 98 | +
|
| 99 | +--- |
| 100 | +
|
| 101 | +## 🖥️ How to Run |
| 102 | +
|
| 103 | +1. Clone this repository: |
| 104 | + ```bash |
| 105 | + git clone https://github.com/<your-username>/LibraryManagementSystem.git |
| 106 | +```` |
| 107 | + |
| 108 | +2. Navigate into the project directory: |
| 109 | + |
| 110 | + ```bash |
| 111 | + cd LibraryManagementSystem |
| 112 | + ``` |
| 113 | +3. Compile the Java file: |
| 114 | + |
| 115 | + ```bash |
| 116 | + javac LibraryManagementSystem.java |
| 117 | + ``` |
| 118 | +4. Run the program: |
| 119 | + |
| 120 | + ```bash |
| 121 | + java LibraryManagementSystem |
| 122 | + ``` |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## 🧑💻 Author |
| 127 | + |
| 128 | +**Ankit Majee** |
| 129 | +📧 [Email](mailto:your-email@example.com) | 💼 [LinkedIn](https://linkedin.com/in/ankitmajee) |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +## 🏁 License |
| 134 | + |
| 135 | +Licensed under the **MIT License** — free to use for learning and open-source development. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +> 📖 *A robust Java OOP implementation of a Library Management System featuring encapsulation, inheritance, and polymorphism.* |
| 140 | +
|
| 141 | +```` |
| 142 | +
|
| 143 | +--- |
| 144 | +
|
| 145 | +## 💻 Java Code — `LibraryManagementSystem.java` |
| 146 | +
|
| 147 | +```java |
| 148 | +import java.util.*; |
| 149 | +
|
| 150 | +// ===== ABSTRACT BOOK CLASS ===== |
| 151 | +abstract class Book { |
| 152 | + private String id; |
| 153 | + private String title; |
| 154 | + private String author; |
| 155 | + private boolean isIssued; |
| 156 | +
|
| 157 | + public Book(String id, String title, String author) { |
| 158 | + this.id = id; |
| 159 | + this.title = title; |
| 160 | + this.author = author; |
| 161 | + this.isIssued = false; |
| 162 | + } |
| 163 | +
|
| 164 | + public String getId() { return id; } |
| 165 | + public String getTitle() { return title; } |
| 166 | + public boolean isIssued() { return isIssued; } |
| 167 | + public void setIssued(boolean issued) { isIssued = issued; } |
| 168 | +
|
| 169 | + public abstract boolean isBorrowable(); |
| 170 | +
|
| 171 | + @Override |
| 172 | + public String toString() { |
| 173 | + return title + " by " + author + (isIssued ? " (Issued)" : " (Available)"); |
| 174 | + } |
| 175 | +} |
| 176 | +
|
| 177 | +// ===== SUBCLASSES OF BOOK ===== |
| 178 | +class FictionBook extends Book { |
| 179 | + public FictionBook(String id, String title, String author) { |
| 180 | + super(id, title, author); |
| 181 | + } |
| 182 | + public boolean isBorrowable() { return true; } |
| 183 | +} |
| 184 | +
|
| 185 | +class NonFictionBook extends Book { |
| 186 | + public NonFictionBook(String id, String title, String author) { |
| 187 | + super(id, title, author); |
| 188 | + } |
| 189 | + public boolean isBorrowable() { return true; } |
| 190 | +} |
| 191 | +
|
| 192 | +class ReferenceBook extends Book { |
| 193 | + public ReferenceBook(String id, String title, String author) { |
| 194 | + super(id, title, author); |
| 195 | + } |
| 196 | + public boolean isBorrowable() { return false; } |
| 197 | +} |
| 198 | +
|
| 199 | +// ===== ABSTRACT MEMBER CLASS ===== |
| 200 | +abstract class Member { |
| 201 | + private String id; |
| 202 | + private String name; |
| 203 | +
|
| 204 | + public Member(String id, String name) { |
| 205 | + this.id = id; |
| 206 | + this.name = name; |
| 207 | + } |
| 208 | +
|
| 209 | + public String getId() { return id; } |
| 210 | + public String getName() { return name; } |
| 211 | +
|
| 212 | + public abstract double calculateFine(int daysLate); |
| 213 | +} |
| 214 | +
|
| 215 | +class Student extends Member { |
| 216 | + public Student(String id, String name) { |
| 217 | + super(id, name); |
| 218 | + } |
| 219 | +
|
| 220 | + public double calculateFine(int daysLate) { |
| 221 | + return daysLate > 0 ? daysLate * 2.0 : 0.0; |
| 222 | + } |
| 223 | +} |
| 224 | +
|
| 225 | +class Teacher extends Member { |
| 226 | + public Teacher(String id, String name) { |
| 227 | + super(id, name); |
| 228 | + } |
| 229 | +
|
| 230 | + public double calculateFine(int daysLate) { |
| 231 | + return daysLate > 0 ? daysLate * 1.0 : 0.0; |
| 232 | + } |
| 233 | +} |
| 234 | +
|
| 235 | +// ===== TRANSACTION CLASS ===== |
| 236 | +class Transaction { |
| 237 | + private Book book; |
| 238 | + private Member member; |
| 239 | + private Date issueDate; |
| 240 | + private Date returnDate; |
| 241 | +
|
| 242 | + public Transaction(Book book, Member member) { |
| 243 | + this.book = book; |
| 244 | + this.member = member; |
| 245 | + this.issueDate = new Date(); |
| 246 | + this.returnDate = null; |
| 247 | + } |
| 248 | +
|
| 249 | + public Book getBook() { return book; } |
| 250 | + public Member getMember() { return member; } |
| 251 | +
|
| 252 | + public void returnBook(int daysLate) { |
| 253 | + this.returnDate = new Date(); |
| 254 | + double fine = member.calculateFine(daysLate); |
| 255 | + book.setIssued(false); |
| 256 | + System.out.println("✅ Book '" + book.getTitle() + "' returned by " + member.getName()); |
| 257 | + if (fine > 0) |
| 258 | + System.out.println("💰 Fine for late return: ₹" + fine); |
| 259 | + } |
| 260 | +} |
| 261 | +
|
| 262 | +// ===== LIBRARY CLASS ===== |
| 263 | +class Library { |
| 264 | + private Map<String, Book> books; |
| 265 | + private List<Member> members; |
| 266 | + private List<Transaction> transactions; |
| 267 | +
|
| 268 | + public Library() { |
| 269 | + this.books = new HashMap<>(); |
| 270 | + this.members = new ArrayList<>(); |
| 271 | + this.transactions = new ArrayList<>(); |
| 272 | + } |
| 273 | +
|
| 274 | + public void addBook(Book book) { |
| 275 | + books.put(book.getId(), book); |
| 276 | + } |
| 277 | +
|
| 278 | + public void addMember(Member member) { |
| 279 | + members.add(member); |
| 280 | + } |
| 281 | +
|
| 282 | + public void issueBook(String bookId, Member member) { |
| 283 | + Book book = books.get(bookId); |
| 284 | +
|
| 285 | + if (book == null) { |
| 286 | + System.out.println("❌ Book ID not found in library!"); |
| 287 | + return; |
| 288 | + } |
| 289 | +
|
| 290 | + if (!book.isBorrowable()) { |
| 291 | + System.out.println("⚠️ '" + book.getTitle() + "' is a reference book and cannot be issued."); |
| 292 | + return; |
| 293 | + } |
| 294 | +
|
| 295 | + if (book.isIssued()) { |
| 296 | + System.out.println("⚠️ Book '" + book.getTitle() + "' is already issued!"); |
| 297 | + return; |
| 298 | + } |
| 299 | +
|
| 300 | + book.setIssued(true); |
| 301 | + Transaction t = new Transaction(book, member); |
| 302 | + transactions.add(t); |
| 303 | + System.out.println("✅ Book '" + book.getTitle() + "' issued to " + member.getName()); |
| 304 | + } |
| 305 | +
|
| 306 | + public void returnBook(String bookId, Member member, int daysLate) { |
| 307 | + Optional<Transaction> trans = transactions.stream() |
| 308 | + .filter(t -> t.getBook().getId().equals(bookId) && t.getMember().equals(member)) |
| 309 | + .findFirst(); |
| 310 | +
|
| 311 | + if (trans.isEmpty()) { |
| 312 | + System.out.println("❌ No active transaction found for this book and member!"); |
| 313 | + return; |
| 314 | + } |
| 315 | +
|
| 316 | + trans.get().returnBook(daysLate); |
| 317 | + transactions.remove(trans.get()); |
| 318 | + } |
| 319 | +
|
| 320 | + public void displayAvailableBooks() { |
| 321 | + System.out.println("\n📘 Available Books:"); |
| 322 | + books.values().stream() |
| 323 | + .filter(book -> !book.isIssued()) |
| 324 | + .forEach(book -> System.out.println("- " + book)); |
| 325 | + } |
| 326 | +} |
| 327 | +
|
| 328 | +// ===== MAIN CLASS ===== |
| 329 | +public class LibraryManagementSystem { |
| 330 | + public static void main(String[] args) { |
| 331 | + Library library = new Library(); |
| 332 | +
|
| 333 | + // Adding books |
| 334 | + library.addBook(new FictionBook("B1", "1984", "George Orwell")); |
| 335 | + library.addBook(new NonFictionBook("B2", "Physics Fundamentals", "HC Verma")); |
| 336 | + library.addBook(new ReferenceBook("B3", "Oxford Dictionary", "Oxford Press")); |
| 337 | +
|
| 338 | + // Adding members |
| 339 | + Member s1 = new Student("S1", "Ravi"); |
| 340 | + Member t1 = new Teacher("T1", "Meena"); |
| 341 | +
|
| 342 | + library.addMember(s1); |
| 343 | + library.addMember(t1); |
| 344 | +
|
| 345 | + // Transactions |
| 346 | + library.issueBook("B1", s1); |
| 347 | + library.issueBook("B2", t1); |
| 348 | + library.issueBook("B1", t1); // Duplicate issue |
| 349 | + library.issueBook("B3", s1); // Reference book |
| 350 | +
|
| 351 | + library.displayAvailableBooks(); |
| 352 | +
|
| 353 | + library.returnBook("B1", s1, 10); // Late return |
| 354 | + library.returnBook("B4", s1, 0); // Invalid book ID |
| 355 | + } |
| 356 | +} |
| 357 | +```` |
| 358 | + |
| 359 | +--- |
| 360 | + |
| 361 | +## ✅ Example Output |
| 362 | + |
| 363 | +``` |
| 364 | +✅ Book '1984' issued to Ravi |
| 365 | +✅ Book 'Physics Fundamentals' issued to Meena |
| 366 | +⚠️ Book '1984' is already issued! |
| 367 | +⚠️ 'Oxford Dictionary' is a reference book and cannot be issued. |
| 368 | +
|
| 369 | +📘 Available Books: |
| 370 | +- Oxford Dictionary by Oxford Press (Available) |
| 371 | +
|
| 372 | +✅ Book '1984' returned by Ravi |
| 373 | +💰 Fine for late return: ₹20.0 |
| 374 | +❌ Book ID not found in library! |
| 375 | +``` |
| 376 | + |
| 377 | +--- |
0 commit comments