Java Project - Library Management System

A library management system is an essential piece of software for educational institutions, public libraries, and any organization that needs to manage a large number of books. A robust library management system can save hours of manual work, track books more efficiently, and enhance the user experience for both staff and readers. 

In this blog post, we'll provide an overview of a simple library management system project written in Java. We'll touch on some core functionalities and provide source code snippets for illustration. 

Features of the Library Management System: 

Book Management: Add, update, delete, or search for books. 

Issue & Return Books: Users can borrow and return books while the system keeps track of due dates and late fees. 

View Book History: Users can view the books they've borrowed in the past.

Library Management System Project in Java 

1. Book Class: 

This class represents a book with attributes like bookId, title, author, and isAvailable.

   class Book {
        private int bookId;
        private String title;
        private String author;
        private boolean isAvailable;

        public Book(int bookId, String title, String author) {
            this.bookId = bookId;
            this.title = title;
            this.author = author;
            this.isAvailable = true;
        }

        public int getBookId() { return bookId; }
        public String getTitle() { return title; }
        public String getAuthor() { return author; }
        public boolean isAvailable() { return isAvailable; }

        public void setAvailable(boolean isAvailable) {
            this.isAvailable = isAvailable;
        }
    }

2. User Class: 

This class represents a user with attributes like userId, username, password, and borrowedBooks.

 class User {
        private int userId;
        private String username;
        private String password;
        private List<Book> borrowedBooks = new ArrayList<>();

        public User(int userId, String username, String password) {
            this.userId = userId;
            this.username = username;
            this.password = password;
        }

        public String getUsername(){
            return this.username;
        }
        public List<Book> getBorrowedBooks() { return borrowedBooks; }
        public void borrowBook(Book book) { borrowedBooks.add(book); }
        public void returnBook(Book book) { borrowedBooks.remove(book); }
    }

3. Library Class: 

This class contains methods for book management and user operations.

     class Library {
        private List<Book> books = new ArrayList<>();
        private List<User> users = new ArrayList<>();

        public void addBook(Book book) {
            books.add(book);
        }

        public void removeBook(int bookId) {
            books.removeIf(b -> b.getBookId() == bookId);
        }

        public Book searchBook(String title) {
            for (Book book : books) {
                if (book.getTitle().equalsIgnoreCase(title) && book.isAvailable()) {
                    return book;
                }
            }
            return null;
        }

        public void registerUser(User user) {
            users.add(user);
        }

        public User findUser(String username) {
            for (User user : users) {
                if (user.getUsername().equals(username)) {
                    return user;
                }
            }
            return null;
        }

        public void issueBook(User user, Book book) {
            if (book != null && book.isAvailable()) {
                book.setAvailable(false);
                user.borrowBook(book);
            }
        }

        public void returnBook(User user, Book book) {
            if (book != null && !book.isAvailable()) {
                book.setAvailable(true);
                user.returnBook(book);
            }
        }
    }

4. Main Application: 

A simple command-line application to test our library system.

public class LibraryManagementSystem {

    public static void main(String[] args) {
        Library library = new Library();

        // Add some initial data
        library.addBook(new Book(1, "Harry Potter", "J.K. Rowling"));
        library.registerUser(new User(1, "alice", "password123"));

        // Simple testing: Issue a book to a user and then return it.
        User alice = library.findUser("alice");
        Book hp = library.searchBook("Harry Potter");
        library.issueBook(alice, hp);
        System.out.println("After issuing: " + hp.isAvailable());

        library.returnBook(alice, hp);
        System.out.println("After returning: " + hp.isAvailable());
    }
}

Complete Source Code with Output

import java.util.ArrayList;
import java.util.List;

class Book {
        private int bookId;
        private String title;
        private String author;
        private boolean isAvailable;

        public Book(int bookId, String title, String author) {
            this.bookId = bookId;
            this.title = title;
            this.author = author;
            this.isAvailable = true;
        }

        public int getBookId() { return bookId; }
        public String getTitle() { return title; }
        public String getAuthor() { return author; }
        public boolean isAvailable() { return isAvailable; }

        public void setAvailable(boolean isAvailable) {
            this.isAvailable = isAvailable;
        }
    }
    
     class User {
        private int userId;
        private String username;
        private String password;
        private List<Book> borrowedBooks = new ArrayList<>();

        public User(int userId, String username, String password) {
            this.userId = userId;
            this.username = username;
            this.password = password;
        }

        public String getUsername(){
            return this.username;
        }
        public List<Book> getBorrowedBooks() { return borrowedBooks; }
        public void borrowBook(Book book) { borrowedBooks.add(book); }
        public void returnBook(Book book) { borrowedBooks.remove(book); }
    }
    
     class Library {
        private List<Book> books = new ArrayList<>();
        private List<User> users = new ArrayList<>();

        public void addBook(Book book) {
            books.add(book);
        }

        public void removeBook(int bookId) {
            books.removeIf(b -> b.getBookId() == bookId);
        }

        public Book searchBook(String title) {
            for (Book book : books) {
                if (book.getTitle().equalsIgnoreCase(title) && book.isAvailable()) {
                    return book;
                }
            }
            return null;
        }

        public void registerUser(User user) {
            users.add(user);
        }

        public User findUser(String username) {
            for (User user : users) {
                if (user.getUsername().equals(username)) {
                    return user;
                }
            }
            return null;
        }

        public void issueBook(User user, Book book) {
            if (book != null && book.isAvailable()) {
                book.setAvailable(false);
                user.borrowBook(book);
            }
        }

        public void returnBook(User user, Book book) {
            if (book != null && !book.isAvailable()) {
                book.setAvailable(true);
                user.returnBook(book);
            }
        }
    }
    
public class LibraryManagementSystem {

    public static void main(String[] args) {
        Library library = new Library();

        // Add some initial data
        library.addBook(new Book(1, "Harry Potter", "J.K. Rowling"));
        library.registerUser(new User(1, "alice", "password123"));

        // Simple testing: Issue a book to a user and then return it.
        User alice = library.findUser("alice");
        Book hp = library.searchBook("Harry Potter");
        library.issueBook(alice, hp);
        System.out.println("After issuing: " + hp.isAvailable());

        library.returnBook(alice, hp);
        System.out.println("After returning: " + hp.isAvailable());
    }
}

Output:

After issuing: false
After returning: true

Conclusion

Building a full-fledged library management system requires careful planning, design, and testing. This project can be an excellent starting point for beginners looking to apply their Java skills in a real-world scenario or for institutions looking for a customizable in-house solution. Feel free to take this source code as a base, expand upon it, and make it your own!


Comments