Library management system

Library Management System — Zoho Round 3

The challenge of designing a Library Management System presents a unique opportunity to blend technical knowledge with practical problem-solving.

This system not only manages books and members but also enforces real-world rules like borrowing limits and tracking book availability. Here’s how I approached this problem in Zoho Round 3, breaking it down into key components.

The Problem Statement

We were tasked with designing a system where:

  1. Admins can manage book inventory (add, update, remove books).

2. Members can register, borrow, and return books.

3. Track book availability and borrowing history.

4. Include search functionality by title, author, or genre.

5. Restrict borrowing to a maximum of 5 books for up to 30 days.

The Solution

Breaking Down the Components

A successful Library Management System (LMS) must address the needs of two key stakeholders: admins and users (members). Each user has distinct actions, which we divide into separate modules:

  1. Admin Actions:
  • Add, update, and remove books.
  • View all books and members.

2. User Actions:

  • Register as a member.
  • Borrow and return books within defined limits.
  • Search for books by title, author, or genre.

The Implementation

Here’s the complete code for the LMS, explained in plain language with comments.

Book and Member Classes

The Book and Member classes represent the basic entities in our library system.

class Book {
    String title;
    String author;
    String genre;
    boolean isAvailable;

    Book(String title, String author, String genre) {
        this.title = title;
        this.author = author;
        this.genre = genre;
        this.isAvailable = true;  // Initially, all books are available
    }
}

Here, Book includes fields like title, author, and genre, alongside a boolean isAvailable flag to track availability.

The Member class handles user-specific data:

class Member {
    String username;
    List<Book> borrowedBooks;
    Member(String username) {
        this.username = username;
        this.borrowedBooks = new ArrayList<>();
    }

    boolean borrowBook(Book book) {
        if (borrowedBooks.size() < 5 && book.isAvailable) {
            borrowedBooks.add(book);
            book.isAvailable = false;  // Mark the book as borrowed
            return true;
        }
        return false;
    }

    void returnBook(Book book) {
        borrowedBooks.remove(book);
        book.isAvailable = true;  // Mark the book as available
    }
}

Step 2: Core Library Management System

The LibraryManagementSystem class ties everything together. It contains:

  1. Books and Members Lists: These store all books and registered members.
  2. Admin and User Roles: The program begins by asking the user to choose their role.
public class LibraryManagementSystem {
    static Scanner scanner = new Scanner(System.in);
    static List<Book> books = new ArrayList<>();
    static List<Member> members = new ArrayList<>();
    //loggedInMember variable will contain a reference to the Member object and it stores username and borrowed books of user
    static Member loggedInMember = null;
    static final String adminUsername = "admin";
    static final String adminPassword = "password";

    public static void main(String[] args) {
        // Hardcoding two books
        books.add(new Book("Science", "John Doe", "Educational"));
        books.add(new Book("Social Studies", "Jane Smith", "Educational"));

        while (true) {
            System.out.print("Enter role (admin/user): ");
            String role = scanner.nextLine();

            if (role.equals("admin")) {
                loginAsAdmin();
            } else if (role.equals("user")) {
                loginAsUser();
            } else {
                System.out.println("Invalid role. Please try again.");
            }
        }
    }

    private static void loginAsAdmin() {
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        if (username.equals(adminUsername) && password.equals(adminPassword)) {
            System.out.println("Logged in as admin.");
            adminMenu();
        } else {
            System.out.println("Invalid admin credentials.");
        }
    }

    private static void loginAsUser() {
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
//checks for existing username in members , or else create new object        
        loggedInMember = findOrCreateMember(username);
        System.out.println("Logged in as user.");
        userMenu();
    }
    
    private static Member findOrCreateMember(String username) {
        for (Member member : members) {
        //if member already exist, the return memeber
            if (member.username.equals(username)) {
                return member;
            }
        }
        Member newMember = new Member(username);
        members.add(newMember);
        return newMember;
    }

    private static void adminMenu() {
        while (true) {
            System.out.println("\nLibrary Management System");
            System.out.println("1. Add Book");
            System.out.println("2. Update Book");
            System.out.println("3. Remove Book");
            System.out.println("4. Add Member");
            System.out.println("5. Display All Books");
            System.out.println("6. Display All Members");
            System.out.println("7. Exit");

            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();  // consume the newline character

            switch (choice) {
                case 1: addBook(); break;
                case 2: updateBook(); break;
                case 3: removeBook(); break;
                case 4: addMember(); break;
                case 5: displayAllBooks(); break;
                case 6: displayAllMembers(); break;
                case 7: return;
                default: System.out.println("Invalid choice. Please try again.");
            }
        }
    }

    private static void userMenu() {
        while (true) {
            System.out.println("\nLibrary Management System");
            System.out.println("1. Borrow Book");
            System.out.println("2. Return Book");
            System.out.println("3. Display All Books");
            System.out.println("4. Display All Members");
            System.out.println("5. Exit");

            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();  // consume the newline character

            switch (choice) {
                case 1: borrowBook(); break;
                case 2: returnBook(); break;
                case 3: displayAllBooks(); break;
                case 4: displayAllMembers(); break;
                case 5: return;
                default: System.out.println("Invalid choice. Please try again.");
            }
        }
    }

    private static void addBook() {
        System.out.print("Enter book title: ");
        String title = scanner.nextLine();
        System.out.print("Enter book author: ");
        String author = scanner.nextLine();
        System.out.print("Enter book genre: ");
        String genre = scanner.nextLine();
    //Show in screenshot how the books arraylist stores the values
        books.add(new Book(title, author, genre));
        System.out.println("Book added successfully.");
    }

    private static void updateBook() {
        System.out.print("Enter book title to update: ");
        String title = scanner.nextLine();

        Book book = findBookByTitle(title);
        if (book != null) {
            System.out.print("Enter new author: ");
            book.author = scanner.nextLine();
            System.out.print("Enter new genre: ");
            book.genre = scanner.nextLine();
            System.out.println("Book updated successfully.");
        } else {
            System.out.println("Book not found.");
        }
    }

    private static void removeBook() {
        System.out.print("Enter book title to remove: ");
        String title = scanner.nextLine();

        Book book = findBookByTitle(title);
        if (book != null) {
            books.remove(book);
            System.out.println("Book removed successfully.");
        } else {
            System.out.println("Book not found.");
        }
    }
    
    private static Book findBookByTitle(String title) {
        for (Book book : books) {
            if (book.title.equalsIgnoreCase(title)) {
                return book;
            }
        }
        return null;
    }

    private static void addMember() {
        System.out.print("Enter member username: ");
        String username = scanner.nextLine();
        members.add(new Member(username));
        System.out.println("Member added successfully.");
    }

    private static void borrowBook() {
        System.out.print("Enter book title to borrow: ");
        String title = scanner.nextLine();

        Book book = findBookByTitle(title);
        //Explain this with an example senario
    //This checks if the loggedInMember successfully borrows the book.
        if (book != null && loggedInMember.borrowBook(book)) {
            System.out.println("Book borrowed successfully.");
        } else {
            System.out.println("Book is either unavailable or borrow limit reached.");
        }
    }

    private static void returnBook() {
        System.out.print("Enter book title to return: ");
        String title = scanner.nextLine();

        Book book = findBookByTitle(title);
        if (book != null && !book.isAvailable) {
 //This method is in the members class, remove it from borrowedBooks list
 //return true or false accordingly
            loggedInMember.returnBook(book);
            System.out.println("Book returned successfully.");
        } else {
            System.out.println("Book not found or it wasn't borrowed.");
        }
    }

    private static void displayAllBooks() {
        if(books.isEmpty()){
            System.out.println("No books available");
        }else{
            System.out.println("\nList of Books:");
            for (Book book : books) {
                System.out.println("Title: " + book.title + ", Author: " + book.author + ", Genre: " + book.genre + ", Available: " + book.isAvailable);
            }
        }
    }

    private static void displayAllMembers() {
        if(members.isEmpty()){
            System.out.println("No memebers exist");
        }else{
            System.out.println("\nList of Members:");
            for (Member member : members) {
                System.out.println("Username: " + member.username + ", Borrowed Books: " + member.borrowedBooks.size());
            }
        }
    }
}

Conclusion

Building a Library Management System was a thought-provoking task that showcased how programming bridges real-world logic with technical execution.

The clear separation of admin and user roles, combined with the practical borrowing constraints, ensures a well-rounded experience.

If you’d like to try the code, feel free to implement it and enhance it further — add features like late return fines or member suspension for overdue books!

Picture of aravind16101800@gmail.com

aravind16101800@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *