Library Management System Project in Java with Source Code

Creating a Library Management System in Java is a great way to understand object-oriented programming concepts. This step-by-step tutorial will guide you through building a simple Library Management System Project in Java, focusing on adding, updating, deleting, listing, searching for books, and managing their checkout status.

Step 1: Setup Your Project

Create a Java Project: In your IDE, create a new Java project named LibraryManagementSystem.

Create Packages and Classes: Inside the project, create a package net.javaguides.lms. Within this package, create three classes: LibraryManager, Book, and Main.

Step 2: Implement the Book Class

Start with the Book class in Book.java. This class represents a book in the library with attributes like id, title, author, and isBorrowed. Implement getters, setters, and a toString method for printing book details.
package net.javaguides.lms;

public class Book {
    private int id;
    private String title;
    private String author;
    private boolean isBorrowed;

    public Book(int id, String title, String author) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.isBorrowed = false;
    }

    // Getters and Setters
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public String getAuthor() { return author; }
    public void setAuthor(String author) { this.author = author; }
    public boolean isBorrowed() { return isBorrowed; }
    public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; }

    @Override
    public String toString() {
        return "Book{" +
               "id=" + id +
               ", title='" + title + '\'' +
               ", author='" + author + '\'' +
               ", isBorrowed=" + isBorrowed +
               '}';
    }
}

Step 3: Develop the LibraryManager Class 

In LibraryManager.java, implement the LibraryManager class, which manages the books in the library. Here's a breakdown of its methods:
package net.javaguides.lms;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class LibraryManager {
    private List<Book> books = new ArrayList<>();

    public void addBook(Book book) {
        books.add(book);
        System.out.println("Book added successfully!");
    }

    public void updateBook(int id, String title, String author) {
        books.stream()
             .filter(book -> book.getId() == id)
             .findFirst()
             .ifPresent(book -> {
                 book.setTitle(title);
                 book.setAuthor(author);
                 System.out.println("Book updated successfully!");
             });
    }

    public void deleteBook(int id) {
        if (books.removeIf(book -> book.getId() == id)) {
            System.out.println("Book deleted successfully!");
        } else {
            System.out.println("Book not found!");
        }
    }

    public void listBooks() {
        books.forEach(System.out::println);
    }

    public void searchBooks(String query) {
        List<Book> foundBooks = books.stream()
                                     .filter(book -> book.getTitle().toLowerCase().contains(query.toLowerCase()) ||
                                                     book.getAuthor().toLowerCase().contains(query.toLowerCase()))
                                     .collect(Collectors.toList());
        if (foundBooks.isEmpty()) {
            System.out.println("No books found matching the query.");
        } else {
            foundBooks.forEach(System.out::println);
        }
    }

    public void checkOutBook(int id) {
        books.stream()
             .filter(book -> book.getId() == id && !book.isBorrowed())
             .findFirst()
             .ifPresentOrElse(book -> {
                 book.setBorrowed(true);
                 System.out.println("Book checked out successfully!");
             }, () -> System.out.println("Book is not available."));
    }

    public void checkInBook(int id) {
        books.stream()
             .filter(book -> book.getId() == id && book.isBorrowed())
             .findFirst()
             .ifPresentOrElse(book -> {
                 book.setBorrowed(false);
                 System.out.println("Book checked in successfully!");
             }, () -> System.out.println("Book was not checked out."));
    }

    // Method to input book details
    private Book inputBookDetails(Scanner scanner) {
        System.out.print("Enter Book ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // consume newline
        System.out.print("Enter Book Title: ");
        String title = scanner.nextLine();
        System.out.print("Enter Book Author: ");
        String author = scanner.nextLine();

        return new Book(id, title, author);
    }

    // Method to start the library management system
    public void start() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\nLibrary Management System");
            System.out.println("1. Add Book");
            System.out.println("2. Update Book");
            System.out.println("3. Delete Book");
            System.out.println("4. List All Books");
            System.out.println("5. Search Books");
            System.out.println("6. Check Out Book");
            System.out.println("7. Check In Book");
            System.out.println("8. Exit");
            System.out.print("Enter your choice: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Fix for Scanner bug
            switch (choice) {
                case 1:
                    addBook(inputBookDetails(scanner));
                    break;
                case 2:
                    System.out.print("Enter Book ID to update: ");
                    int id = scanner.nextInt();
                    scanner.nextLine(); // Consume newline
                    Book book = inputBookDetails(scanner);
                    updateBook(id, book.getTitle(), book.getAuthor());
                    break;
                case 3:
                    System.out.print("Enter Book ID to delete: ");
                    id = scanner.nextInt();
                    deleteBook(id);
                    break;
                case 4:
                    listBooks();
                    break;
                case 5:
                    System.out.print("Enter search query (title or author): ");
                    String query = scanner.nextLine();
                    searchBooks(query);
                    break;
                case 6:
                    System.out.print("Enter Book ID to check out: ");
                    id = scanner.nextInt();
                    checkOutBook(id);
                    break;
                case 7:
                    System.out.print("Enter Book ID to check in: ");
                    id = scanner.nextInt();
                    checkInBook(id);
                    break;
                case 8:
                    System.out.println("Exiting...");
                    return;
                default:
                    System.out.println("Invalid choice, please enter a number between 1 and 8.");
                    break;
            }
        }
    }
}
addBook(Book book): Adds a new book to the library. 

updateBook(int id, String title, String author): Updates the title and author of a book identified by id.

deleteBook(int id): Removes a book from the library using its id. 

listBooks(): Prints details of all books in the library. 

searchBooks(String query): Searches for books by title or author. 

checkOutBook(int id): Marks a book as borrowed. 

checkInBook(int id): Marks a book as returned. 

inputBookDetails(Scanner scanner): Utility method to read book details from the user. 

start(): Runs the library management system, showing a menu to the user and processing commands. 

Step 4: Build the Main Class 

The Main class in Main.java serves as the application's entry point. It creates an instance of LibraryManager and calls its start method to begin the library management process.
package net.javaguides.lms;

public class Main {
    public static void main(String[] args) {
        LibraryManager manager = new LibraryManager();
        manager.start();
    }
}

Step 5: Running the Library Management System 

Compile and Run: Compile your Java files and run the Main class. The application will display a menu of actions to manage your library.

Here is the output:
Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 1
Enter Book ID: 101
Enter Book Title: Effective Java
Enter Book Author: Joshua Bloch
Book added successfully!

Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 1
Enter Book ID: 102
Enter Book Title: Java Concurrency in Practice
Enter Book Author: Brian Goetz
Book added successfully!

Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 4
Book{id=101, title='Effective Java', author='Joshua Bloch', isBorrowed=false}
Book{id=102, title='Java Concurrency in Practice', author='Brian Goetz', isBorrowed=false}

Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 5
Enter search query (title or author): java
Book{id=101, title='Effective Java', author='Joshua Bloch', isBorrowed=false}
Book{id=102, title='Java Concurrency in Practice', author='Brian Goetz', isBorrowed=false}

Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 6
Enter Book ID to check out: 101
Book checked out successfully!

Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 7
Enter Book ID to check in: 101
Book checked in successfully!

Library Management System
1. Add Book
2. Update Book
3. Delete Book
4. List All Books
5. Search Books
6. Check Out Book
7. Check In Book
8. Exit
Enter your choice: 8
Exiting...

Process finished with exit code 0

Conclusion 

You've now built a basic Library Management System in Java! This system allows you to manage a collection of books, including adding, updating, and deleting entries, as well as checking books in and out. To enhance your project, consider adding features such as saving and loading the library data from a file, implementing user accounts, or even a GUI for easier interaction.

Comments