Library Management System


Introduction


The Library Management System will help students manage books in a library. It will include features to add, view, update, and delete books. This project covers Java fundamentals like classes, methods, lists, and basic file I/O.


Part 1: Requirement Gathering and Feature Discussion


Requirements:

  1. Add Book: Add a new book with details such as ISBN, title, author, and quantity.
  2. View Books: Display all books in the library.
  3. Update Book: Update details of an existing book.
  4. Delete Book: Remove a book from the library.
  5. Save Data: Save the book records to a file.
  6. Load Data: Load book records from a file.


Part 2: Design Discussion


Design Components:

  1. Book Class: Represents a book entity.
  2. Library Class: Manages the collection of books.
  3. LibraryManagementSystem Class: Main class to interact with the user and handle file I/O.


Part 3: Code Implementation


1. Book Class

public class Book {
    private String isbn;
    private String title;
    private String author;
    private int quantity;

    public Book(String isbn, String title, String author, int quantity) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.quantity = quantity;
    }

    // Getters and Setters
    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    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 int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "Book [ISBN=" + isbn + ", Title=" + title + ", Author=" + author + ", Quantity=" + quantity + "]";
    }
}

2. Library Class

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

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

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

    public List<Book> viewBooks() {
        return new ArrayList<>(books);
    }

    public boolean updateBook(String isbn, String title, String author, int quantity) {
        for (Book book : books) {
            if (book.getIsbn().equals(isbn)) {
                book.setTitle(title);
                book.setAuthor(author);
                book.setQuantity(quantity);
                return true;
            }
        }
        return false;
    }

    public boolean deleteBook(String isbn) {
        return books.removeIf(book -> book.getIsbn().equals(isbn));
    }

    public List<Book> searchBooksByTitle(String title) {
        List<Book> result = new ArrayList<>();
        for (Book book : books) {
            if (book.getTitle().equalsIgnoreCase(title)) {
                result.add(book);
            }
        }
        return result;
    }
}

3. LibraryManagementSystem Class

import java.io.*;
import java.util.List;
import java.util.Scanner;

public class LibraryManagementSystem {
    private static Library library = new Library();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        loadData();
        while (true) {
            displayMenu();
            handleUserInput();
        }
    }

    private static void displayMenu() {
        System.out.println("\nLibrary Management System");
        System.out.println("1. Add Book");
        System.out.println("2. View Books");
        System.out.println("3. Update Book");
        System.out.println("4. Delete Book");
        System.out.println("5. Search Books by Title");
        System.out.println("6. Save Data");
        System.out.println("7. Exit");
        System.out.print("Enter your choice: ");
    }

    private static void handleUserInput() {
        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                viewBooks();
                break;
            case 3:
                updateBook();
                break;
            case 4:
                deleteBook();
                break;
            case 5:
                searchBooksByTitle();
                break;
            case 6:
                saveData();
                break;
            case 7:
                saveData();
                System.exit(0);
                break;
            default:
                System.out.println("Invalid choice. Please try again.");
        }
    }

    private static void addBook() {
        System.out.print("Enter ISBN: ");
        String isbn = scanner.nextLine();
        System.out.print("Enter Title: ");
        String title = scanner.nextLine();
        System.out.print("Enter Author: ");
        String author = scanner.nextLine();
        System.out.print("Enter Quantity: ");
        int quantity = scanner.nextInt();

        Book book = new Book(isbn, title, author, quantity);
        library.addBook(book);
        System.out.println("Book added successfully.");
    }

    private static void viewBooks() {
        List<Book> books = library.viewBooks();
        if (books.isEmpty()) {
            System.out.println("No books found.");
        } else {
            for (Book book : books) {
                System.out.println(book);
            }
        }
    }

    private static void updateBook() {
        System.out.print("Enter ISBN to update: ");
        String isbn = scanner.nextLine();
        System.out.print("Enter new Title: ");
        String title = scanner.nextLine();
        System.out.print("Enter new Author: ");
        String author = scanner.nextLine();
        System.out.print("Enter new Quantity: ");
        int quantity = scanner.nextInt();

        boolean isUpdated = library.updateBook(isbn, title, author, quantity);
        if (isUpdated) {
            System.out.println("Book updated successfully.");
        } else {
            System.out.println("Book not found.");
        }
    }

    private static void deleteBook() {
        System.out.print("Enter ISBN to delete: ");
        String isbn = scanner.nextLine();
        boolean isDeleted = library.deleteBook(isbn);
        if (isDeleted) {
            System.out.println("Book deleted successfully.");
        } else {
            System.out.println("Book not found.");
        }
    }

    private static void searchBooksByTitle() {
        System.out.print("Enter Title to search: ");
        String title = scanner.nextLine();
        List<Book> books = library.searchBooksByTitle(title);
        if (books.isEmpty()) {
            System.out.println("No books found.");
        } else {
            for (Book book : books) {
                System.out.println(book);
            }
        }
    }

    private static void saveData() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("library.dat"))) {
            oos.writeObject(library.viewBooks());
            System.out.println("Data saved successfully.");
        } catch (IOException e) {
            System.out.println("Failed to save data.");
        }
    }

    private static void loadData() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("library.dat"))) {
            List<Book> books = (List<Book>) ois.readObject();
            for (Book book : books) {
                library.addBook(book);
            }
            System.out.println("Data loaded successfully.");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("No existing data found. Starting fresh.");
        }
    }
}


Part 4: Testing Plan


  1. Manual Testing:

    • Run the program and manually test each feature (add, view, update, delete, search by title, save data, load data).
    • Verify that the system behaves as expected and handles edge cases.

  2. Unit Testing:

    • Write unit tests for the Library class to ensure each method works correctly.
    • Use a testing framework like JUnit for structured testing.


Example Unit Test


import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class LibraryTest {
    private Library library;

    @BeforeEach
    public void setUp() {
        library = new Library();
    }

    @Test
    public void testAddAndViewBook() {
        Book book = new Book("123", "Java Programming", "Author1", 5);
        library.addBook(book);
        assertEquals(1, library.viewBooks().size());
        assertEquals("Java Programming", library.viewBooks().get(0).getTitle());
    }

    @Test
    public void testUpdateBook() {
        Book book = new Book("123", "Java Programming", "Author1", 5);
        library.addBook(book);
        assertTrue(library.updateBook("123", "Advanced Java", "Author2", 10));
        assertFalse(library.updateBook("456", "Non-existent Book", "Author3", 5));
    }

    @Test
    public void testDeleteBook() {
        Book book = new Book("123", "Java Programming", "Author1", 5);
        library.addBook(book);
        assertTrue(library.deleteBook("123"));
        assertFalse(library.deleteBook("456"));
    }

    @Test
    public void testSearchBooksByTitle() {
        Book book1 = new Book("123", "Java Programming", "Author1", 5);
        Book book2 = new Book("456", "Java Programming", "Author2", 3);
        library.addBook(book1);
        library.addBook(book2);
        assertEquals(2, library.searchBooksByTitle("Java Programming").size());
    }
}


Leave a Reply

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