Java case
study
A.Hrushikesh varma
2023002077
The Library Management System is a simple,
yet effective Java-based application designed to
manage books and members in a library. It
allows librarians to add books, register
members, track book borrowing, and
display records while ensuring data integrity
through exception handling.
CODE:
import java.util.Scanner;
class LibraryItem {
  protected String title;
  protected int itemId;
  public LibraryItem(String title, int itemId) {
      this.title = title;
      this.itemId = itemId;
  }
  public void displayDetails() {
        System.out.println("Title: " + title + ", Item ID: " + itemId);
    }
}
interface Borrowable {
    boolean borrowItem(int quantity);
}
class Book extends LibraryItem implements Borrowable {
    private String author;
    private int stock;
   public Book(String title, int itemId, String author, int stock)
throws StockException {
        super(title, itemId);
        if (stock < 0) {
            throw new StockException("Stock cannot be negative.");
        }
        this.author = author;
        this.stock = stock;
    }
    @Override
    public void displayDetails() {
        super.displayDetails();
     System.out.println("Author: " + author + ", Stock: " +
stock);
    }
    @Override
    public boolean borrowItem(int quantity) {
        if (quantity <= stock) {
            stock -= quantity;
            return true;
        }
        return false;
    }
}
class StockException extends Exception {
    public StockException(String message) {
        super(message);
    }
}
class Member {
    private int memberId;
    private String name;
    private int age;
   public Member(int memberId, String name, int age) throws
InvalidMemberException {
        if (!name.matches("[a-zA-Z ]+") || age < 16 || age > 100)
{
        throw new InvalidMemberException("Invalid member
details: Name should only contain letters, and age should be
between 16 and 100.");
        }
        this.memberId = memberId;
        this.name = name;
        this.age = age;
    }
    public void printMemberInfo() {
     System.out.println("Member ID: " + memberId + ", Name:
" + name + ", Age: " + age);
    }
}
class InvalidMemberException extends Exception {
    public InvalidMemberException(String message) {
        super(message);
    }
}
public class LibraryManagement {
    private static Book[] books = new Book[5];
    private static Member[] members = new Member[3];
    private static int bookCount = 0;
    private static int memberCount = 0;
    public static Book searchBookByTitle(String title) {
    for (Book book : books) {
        if (book != null && book.title.equalsIgnoreCase(title)) {
            return book;
        }
    }
    return null;
}
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int choice;
    do {
        System.out.println("\nLibrary Management System");
        System.out.println("1. Add a book");
        System.out.println("2. Register a member");
        System.out.println("3. Borrow a book");
        System.out.println("4. Display all books and members");
        System.out.println("5. Exit");
        System.out.print("Enter your choice: ");
        choice = scanner.nextInt();
        scanner.nextLine();
        switch (choice) {
            case 1:
              if (bookCount < books.length) {
                System.out.print("Enter book title: ");
               String title = scanner.nextLine();
               System.out.print("Enter item ID: ");
               int itemId = scanner.nextInt();
               scanner.nextLine();
               System.out.print("Enter author: ");
               String author = scanner.nextLine();
               System.out.print("Enter stock: ");
               int stock = scanner.nextInt();
               try {
                  books[bookCount++] = new Book(title,
itemId, author, stock);
               } catch (StockException e) {
                   System.out.println(e.getMessage());
               }
           } else {
               System.out.println("Book storage full!");
           }
           break;
         case 2:
           if (memberCount < members.length) {
               System.out.print("Enter member ID: ");
               int memberId = scanner.nextInt();
               scanner.nextLine();
               System.out.print("Enter name: ");
               String name = scanner.nextLine();
               System.out.print("Enter age: ");
               int age = scanner.nextInt();
                try {
              members[memberCount++] = new
Member(memberId, name, age);
                } catch (InvalidMemberException e) {
                    System.out.println(e.getMessage());
                }
            } else {
                System.out.println("Member storage full!");
            }
            break;
          case 3:
            System.out.print("Enter book title: ");
            String title = scanner.nextLine();
            System.out.print("Enter quantity: ");
            int quantity = scanner.nextInt();
            Book book = searchBookByTitle(title);
            if (book != null && book.borrowItem(quantity)) {
                System.out.println("Book borrowed
successfully!");
            } else {
                System.out.println("Book not available or
insufficient stock.");
            }
            break;
          case 4:
            System.out.println("\nBooks:");
            for (Book b : books) {
                if (b != null) b.displayDetails();
                }
                System.out.println("\nMembers:");
                for (Member m : members) {
                    if (m != null) m.printMemberInfo();
                }
                break;
              case 5:
                System.out.println("Exiting...");
                break;
              default:
                System.out.println("Invalid choice!");
          }
        } while (choice != 5);
        scanner.close();
    }
}
OUTPUT: