Introduction
A Library Management System (LMS) is a software application used to manage and organize library books, members, and transactions. This system allows library staff to manage book details, track borrowed books, and maintain a record of library members. It provides functionalities such as adding new books, displaying book details, registering new members, and managing book borrowing and returning operations.
Objective
The objective of this Library Management System is to build a simple Java application to manage books and members of a library. It provides a user-friendly interface for adding, viewing, and updating books, as well as managing member registrations and transactions such as borrowing and returning books.
Java Code for Library Management System
import java.util.*; class Book { private String title; private String author; private boolean isAvailable; public Book(String title, String author) { this.title = title; this.author = author; this.isAvailable = true; } public String getTitle() { return title; } public String getAuthor() { return author; } public boolean isAvailable() { return isAvailable; } public void borrowBook() { isAvailable = false; } public void returnBook() { isAvailable = true; } } class Member { private String name; private String id; public Member(String name, String id) { this.name = name; this.id = id; } public String getName() { return name; } public String getId() { return id; } } class Library { private Map<String, Book> books = new HashMap<>(); private Map<String, Member> members = new HashMap<>(); public void addBook(String title, String author) { Book book = new Book(title, author); books.put(title, book); System.out.println("Book added: " + title); } public void registerMember(String name, String id) { Member member = new Member(name, id); members.put(id, member); System.out.println("Member registered: " + name); } public void borrowBook(String title, String memberId) { if (books.containsKey(title) && members.containsKey(memberId)) { Book book = books.get(title); if (book.isAvailable()) { book.borrowBook(); System.out.println("Book borrowed: " + title); } else { System.out.println("Book is not available."); } } else { System.out.println("Invalid book or member ID."); } } public void returnBook(String title) { if (books.containsKey(title)) { Book book = books.get(title); book.returnBook(); System.out.println("Book returned: " + title); } else { System.out.println("Invalid book."); } } public void displayBooks() { System.out.println("Books in Library:"); for (Book book : books.values()) { System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor() + ", Available: " + book.isAvailable()); } } } public class LibraryManagementSystem { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Library library = new Library(); // Menu while (true) { System.out.println("\nLibrary Management System"); System.out.println("1. Add Book"); System.out.println("2. Register Member"); System.out.println("3. Borrow Book"); System.out.println("4. Return Book"); System.out.println("5. Display Books"); System.out.println("6. Exit"); System.out.print("Choose an option: "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume newline switch (choice) { case 1: System.out.print("Enter book title: "); String title = scanner.nextLine(); System.out.print("Enter book author: "); String author = scanner.nextLine(); library.addBook(title, author); break; case 2: System.out.print("Enter member name: "); String name = scanner.nextLine(); System.out.print("Enter member ID: "); String id = scanner.nextLine(); library.registerMember(name, id); break; case 3: System.out.print("Enter book title to borrow: "); String borrowTitle = scanner.nextLine(); System.out.print("Enter member ID: "); String memberId = scanner.nextLine(); library.borrowBook(borrowTitle, memberId); break; case 4: System.out.print("Enter book title to return: "); String returnTitle = scanner.nextLine(); library.returnBook(returnTitle); break; case 5: library.displayBooks(); break; case 6: System.out.println("Exiting program..."); scanner.close(); return; default: System.out.println("Invalid option. Please try again."); } } } }
Program Explanation
This Library Management System is implemented in Java, using the following classes:
- Book: Represents a book with title, author, and availability status.
- Member: Represents a library member with a name and ID.
- Library: Manages the books and members, providing methods to add books, register members, borrow and return books, and display all books.
- LibraryManagementSystem: The main class that contains the user interface for interacting with the library system.
The program follows a simple menu-driven approach where users can choose operations like adding books, registering members, borrowing, and returning books, or displaying all books available in the library.
The program uses a HashMap
to store books and members, where the book title or member ID acts as the key. It uses a Scanner
to interact with the user, taking input for different operations.
How to Run the Program
- Copy the code into a Java file, e.g.,
LibraryManagementSystem.java
. - Compile the program using the Java compiler:
javac LibraryManagementSystem.java
- Run the program:
java LibraryManagementSystem
- The program will display a menu. You can perform various operations as required.