Untitled

 avatar
unknown
plain_text
a month ago
4.8 kB
4
Indexable
import java.util.ArrayList;
import java.util.Scanner;

class Book {
    int id;
    String name;
    String author;
    boolean issued;

    Book(int id, String name, String author) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.issued = false;
    }
}

public class LibraryManagementSystem {

    static ArrayList<Book> books = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        int choice;

        do {
            System.out.println("\n===== LIBRARY MANAGEMENT SYSTEM =====");
            System.out.println("1. Add Book");
            System.out.println("2. View Books");
            System.out.println("3. Search Book");
            System.out.println("4. Issue Book");
            System.out.println("5. Return Book");
            System.out.println("6. Delete Book");
            System.out.println("7. Exit");
            System.out.print("Enter your choice: ");

            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    addBook();
                    break;

                case 2:
                    viewBooks();
                    break;

                case 3:
                    searchBook();
                    break;

                case 4:
                    issueBook();
                    break;

                case 5:
                    returnBook();
                    break;

                case 6:
                    deleteBook();
                    break;

                case 7:
                    System.out.println("Thank You!");
                    break;

                default:
                    System.out.println("Invalid Choice!");
            }

        } while (choice != 7);
    }

    static void addBook() {
        System.out.print("Enter Book ID: ");
        int id = sc.nextInt();
        sc.nextLine();

        System.out.print("Enter Book Name: ");
        String name = sc.nextLine();

        System.out.print("Enter Author Name: ");
        String author = sc.nextLine();

        books.add(new Book(id, name, author));

        System.out.println("Book Added Successfully!");
    }

    static void viewBooks() {

        if (books.isEmpty()) {
            System.out.println("No Books Available!");
            return;
        }

        System.out.println("\n--- Book List ---");

        for (Book b : books) {
            System.out.println("ID: " + b.id);
            System.out.println("Name: " + b.name);
            System.out.println("Author: " + b.author);
            System.out.println("Issued: " + (b.issued ? "Yes" : "No"));
            System.out.println("---------------------");
        }
    }

    static void searchBook() {

        System.out.print("Enter Book ID to Search: ");
        int id = sc.nextInt();

        for (Book b : books) {
            if (b.id == id) {
                System.out.println("Book Found!");
                System.out.println("Name: " + b.name);
                System.out.println("Author: " + b.author);
                return;
            }
        }

        System.out.println("Book Not Found!");
    }

    static void issueBook() {

        System.out.print("Enter Book ID to Issue: ");
        int id = sc.nextInt();

        for (Book b : books) {
            if (b.id == id) {

                if (!b.issued) {
                    b.issued = true;
                    System.out.println("Book Issued Successfully!");
                } else {
                    System.out.println("Book Already Issued!");
                }

                return;
            }
        }

        System.out.println("Book Not Found!");
    }

    static void returnBook() {

        System.out.print("Enter Book ID to Return: ");
        int id = sc.nextInt();

        for (Book b : books) {

            if (b.id == id) {

                if (b.issued) {
                    b.issued = false;
                    System.out.println("Book Returned Successfully!");
                } else {
                    System.out.println("Book Was Not Issued!");
                }

                return;
            }
        }

        System.out.println("Book Not Found!");
    }

    static void deleteBook() {

        System.out.print("Enter Book ID to Delete: ");
        int id = sc.nextInt();

        for (Book b : books) {

            if (b.id == id) {
                books.remove(b);
                System.out.println("Book Deleted Successfully!");
                return;
            }
        }

        System.out.println("Book Not Found!");
    }
}
Editor is loading...
Leave a Comment