Search This Blog

Wednesday, June 2, 2021

Library Management System in Java

 Library Management System

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

class Library{
    Scanner sc=new Scanner(System.in);
    List<String> Books=new ArrayList<String>();
    List<String> IssuedBooks=new ArrayList<String>();

    public void addBook(){
        System.out.print("Enter the name of the book to be added: ");
        Books.add(sc.nextLine());
    }
    
    public void showAvailableBooks(){
        if (Books.size()==0){
            System.out.println("No books in the library");
        }
        else {
            System.out.println("Here is a list of books available in our library: ");
            for (int a = 1; a <= Books.size(); a++) {
                System.out.println(a + " " + Books.get(a - 1));
            }
        }
    }

    public void issueBook(){
        System.out.print("Enter the serial no. of the book you want to issue: ");
        int b=sc.nextInt();
        IssuedBooks.add(Books.get(b-1));
        System.out.println("You have issued: "+Books.get(b-1));
        Books.remove(b-1);
    }

    public void showIssuedBooks(){
        if (IssuedBooks.size()==0){
            System.out.println("No books issued!");
        }
        else {
            System.out.println("Here is a list of books issued: ");
            for (int a=1;a<=IssuedBooks.size();a++){
                System.out.println(a+" "+IssuedBooks.get(a-1));
            }
        }

    }

    public void returnBook(){
        System.out.print("Enter the serial no. of the book to be returned from the issued books: ");
        int b= sc.nextInt();
        Books.add(IssuedBooks.get(b-1));
        System.out.println("You have returned: "+IssuedBooks.get(b-1));
        IssuedBooks.remove(b-1);
    }

}
public class online_library {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        //Implement a library using java class library
        //Method: addBook, issueBook, returnBook, showAvailableBooks,showIssuedBooks,
        //Properties: Array to store the available books, Array to store the issued books
        Library jnrm=new Library();
        int a=0;
        System.out.println("Add book to library->add\nShow available books->showl\nIssue book->issue\nShow issued books->showi\nReturn book->return");
    while (a==0){

        String b= sc.next();
        if (b.equals("add")){
            jnrm.addBook();
        }
        else if (b.equals("showl")){
            jnrm.showAvailableBooks();
        }
        else if (b.equals("issue")){
            jnrm.issueBook();
        }
        else if (b.equals("showi")){
            jnrm.showIssuedBooks();
        }
        else if (b.equals("return")){
            jnrm.returnBook();
        }
        else {
            System.out.println("Something went wrong\nCheck the syntax!!!");
        }
    }
    }
}


Trending Articles