Simple Library Menu in Java with Source Code

Simple Library Menu in Java with Source Code

Simple Library Menu in Java

Managing a library, whether large or small, requires a structured system to keep track of books, borrowers, and their transactions. Many libraries today use automated systems for efficient operations, but these systems can often be complex and expensive. Luckily, with programming languages like Java, you can create your own Simple Library Menu to manage these tasks. In this blog post, we’ll walk you through how to build a simple library system in Java and provide the source code so you can try it yourself.

Features

A Simple Library Menu in Java offers the basic functionality that any library would need to manage books and borrowers:

  1. View Available Books: Display a list of all the books in the library.
  2. Borrow a Book: Allow users to borrow books and update the library records.
  3. Return a Book: Users can return borrowed books and make them available for others.
  4. Donate a Book: Users can add new books to the library collection.
  5. Exit the Program: The user can close the application when finished.

Now, let’s break down how to implement this in Java step by step.

Download New Real Time Projects :-Click here

See also  Blockchain Security

How to Build

1. Setting Up the Program Structure

First, we need to define the basic structure of our program. We will use an ArrayList to store books and a console-based menu to interact with the user. Let’s create the necessary classes and methods to handle each operation.

2. Defining the Book Class

The Book class represents a book in the library. It will include attributes like the book title and a status indicating whether the book is available for borrowing.

https://updategadh.com/category/php-project

class Book {
    private String title;
    private boolean isAvailable;

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

    public String getTitle() {
        return title;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void borrow() {
        isAvailable = false;
    }

    public void returnBook() {
        isAvailable = true;
    }
}

3. Creating the Library Class

Next, we create the Library class that will store a list of books and manage all the library operations like viewing books, borrowing, returning, and donating.

import java.util.ArrayList;
import java.util.Scanner;

class Library {
    private ArrayList<Book> books;

    public Library() {
        books = new ArrayList<>();
        // Adding some initial books to the library
        books.add(new Book("The Great Gatsby"));
        books.add(new Book("1984"));
        books.add(new Book("The Catcher in the Rye"));
        books.add(new Book("To Kill a Mockingbird"));
    }

    public void viewBooks() {
        System.out.println("\nAvailable Books:");
        for (Book book : books) {
            if (book.isAvailable()) {
                System.out.println("- " + book.getTitle());
            }
        }
    }

    public void borrowBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equalsIgnoreCase(title) && book.isAvailable()) {
                book.borrow();
                System.out.println("You have borrowed: " + title);
                return;
            }
        }
        System.out.println("Sorry, the book is either not available or does not exist.");
    }

    public void returnBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equalsIgnoreCase(title) && !book.isAvailable()) {
                book.returnBook();
                System.out.println("You have returned: " + title);
                return;
            }
        }
        System.out.println("Sorry, this book was not borrowed or does not exist.");
    }

    public void donateBook(String title) {
        books.add(new Book(title));
        System.out.println("Thank you for donating: " + title);
    }
}

4. Creating the Menu System

Now that we have the Library and Book classes, let’s create the menu system that allows users to interact with the library. We’ll display options to the user, process their input, and call the appropriate methods.

public class LibraryMenu {
    public static void main(String[] args) {
        Library library = new Library();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\nWelcome to the Library");
            System.out.println("1. View available books");
            System.out.println("2. Borrow a book");
            System.out.println("3. Return a book");
            System.out.println("4. Donate a book");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    library.viewBooks();
                    break;
                case 2:
                    System.out.print("Enter the name of the book you want to borrow: ");
                    String borrowTitle = scanner.nextLine();
                    library.borrowBook(borrowTitle);
                    break;
                case 3:
                    System.out.print("Enter the name of the book you want to return: ");
                    String returnTitle = scanner.nextLine();
                    library.returnBook(returnTitle);
                    break;
                case 4:
                    System.out.print("Enter the name of the book you want to donate: ");
                    String donateTitle = scanner.nextLine();
                    library.donateBook(donateTitle);
                    break;
                case 5:
                    System.out.println("Exiting the library system. Goodbye!");
                    scanner.close();
                    System.exit(0);
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        }
    }
}

5. Running the Program

When you run the program, you’ll be presented with a menu. Here’s an example of what the output might look like:

Welcome to the Library
1. View available books
2. Borrow a book
3. Return a book
4. Donate a book
5. Exit
Enter your choice: 1

Available Books:
- The Great Gatsby
- 1984
- The Catcher in the Rye
- To Kill a Mockingbird

Simple Library Menu in Java
Simple Library Menu in Java
Simple Library Menu in Java
Simple Library Menu in Java
Simple Library Menu in Java
Simple Library Menu in Java

Expanding the Library Menu

This simple Java program serves as a foundation for a more sophisticated library management system. Here are a few ways you can expand the program:

  • User Accounts: Implement user management to track which user has borrowed a book.
  • Persistent Data: Use file handling or connect the program to a database so that book data is saved even after the program is closed.
  • Advanced Search: Add more complex search functionality to allow users to find books by author, genre, or year.
  • Graphical User Interface (GUI): Use JavaFX or Swing to create a more user-friendly GUI for the library system.
  • Simple Library Menu in Java with Source Code
  • Simple Library Menu in Java
See also  DES Encryption Program Code Using Java JFrame

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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