Library Management System Using Switch Statement in Java

Here we are supposed to design and implement a simple library management system using a switch statement in Java, where have operated the following operations Add a new book, Check Out a book, display specific book status, search specific book, and display book details using different classes.
Menu for Library Management
Here is the list set of options that we will be creating that are as follows:Â
| Exit Application |
|---|
| Add a New Book |
| Upgrade Quantity of a Book |
| Search a Book |
| Show All Books |
| Register Student |
| Show All Registered Students |
| Check Out Book |
| Check-In Book |
Implementation:
We will be creating 5 files including our application(main) class responsible for the execution of our mini-project listed as follows:Â
- Library
- StudentsÂ
- StudentÂ
- booksÂ
- book
Note: As per naming conventions in java, it is good practice to have class initial with uppercase letter still here we are adhering with lowercase for better understanding of the code.
A. File: book.java
Java
// Java Program to Illustrate book Class that// takes Input from the books and related informationÂ
package College;Â
// Importing required classesimport java.util.Scanner;Â
// Classpublic class book {Â
    // Class data members    public int sNo;    public String bookName;    public String authorName;    public int bookQty;    public int bookQtyCopy;Â
    // Creating object of Scanner class to    // read input from users    Scanner input = new Scanner(System.in);Â
    // Method    // To add book details    public book()    {        // Display message for taking input later        // taking input via        // nextInt() and nextLine() standard methods        System.out.println("Enter Serial No of Book:");        this.sNo = input.nextInt();        input.nextLine();Â
        System.out.println("Enter Book Name:");        this.bookName = input.nextLine();Â
        System.out.println("Enter Author Name:");        this.authorName = input.nextLine();Â
        System.out.println("Enter Quantity of Books:");        this.bookQty = input.nextInt();        bookQtyCopy = this.bookQty;    }} |
B. File: books.java
Java
// Java Program to Illustrate books class// To Do all the Operations related to Books such as// add, check-in, check-out,Valid books,Update booksÂ
// Importing required classesimport java.util.Scanner;Â
// CLasspublic class books {Â
    // Class data members    book theBooks[] = new book[50];    public static int count;Â
    Scanner input = new Scanner(System.in);Â
    // Method 1    // To compare books    public int compareBookObjects(book b1, book b2)    {Â
        // If book name matches        if (b1.bookName.equalsIgnoreCase(b2.bookName)) {Â
            // Printing book exists            System.out.println(                "Book of this Name Already Exists.");            return 0;        }Â
        // if book serial matches        if (b1.sNo == b2.sNo) {Â
            // Print book exists            System.out.println(                "Book of this Serial No Already Exists.");Â
            return 0;        }        return 1;    }Â
    // Method 2    // To add book    public void addBook(book b)    {Â
        for (int i = 0; i < count; i++) {Â
            if (this.compareBookObjects(b, this.theBooks[i])                == 0)                return;        }Â
        if (count < 50) {Â
            theBooks[count] = b;            count++;        }        else {Â
            System.out.println(                "No Space to Add More Books.");        }    }Â
    // Method 3    // To search book by serial number    public void searchBySno()    {Â
        // Display message        System.out.println(            "\t\t\t\tSEARCH BY SERIAL NUMBER\n");Â
        // Class data members        int sNo;        System.out.println("Enter Serial No of Book:");        sNo = input.nextInt();Â
        int flag = 0;        System.out.println(            "S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");Â
        for (int i = 0; i < count; i++) {            if (sNo == theBooks[i].sNo) {                System.out.println(                    theBooks[i].sNo + "\t\t"                    + theBooks[i].bookName + "\t\t"                    + theBooks[i].authorName + "\t\t"                    + theBooks[i].bookQtyCopy + "\t\t"                    + theBooks[i].bookQty);                flag++;                return;            }        }        if (flag == 0)            System.out.println("No Book for Serial No "                               + sNo + " Found.");    }Â
    // Method 4    // To search author by name    public void searchByAuthorName()    {Â
        // Display message        System.out.println(            "\t\t\t\tSEARCH BY AUTHOR'S NAME");Â
        input.nextLine();Â
        System.out.println("Enter Author Name:");        String authorName = input.nextLine();Â
        int flag = 0;Â
        System.out.println(            "S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");Â
        for (int i = 0; i < count; i++) {Â
            // if author matches any of its book            if (authorName.equalsIgnoreCase(                    theBooks[i].authorName)) {Â
                // Print below corresponding credentials                System.out.println(                    theBooks[i].sNo + "\t\t"                    + theBooks[i].bookName + "\t\t"                    + theBooks[i].authorName + "\t\t"                    + theBooks[i].bookQtyCopy + "\t\t"                    + theBooks[i].bookQty);                flag++;            }        }Â
        // Else no book matches for author        if (flag == 0)            System.out.println("No Books of " + authorName                               + " Found.");    }Â
    // Method 5    // To display all books    public void showAllBooks()    {Â
        System.out.println("\t\t\t\tSHOWING ALL BOOKS\n");        System.out.println(            "S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");Â
        for (int i = 0; i < count; i++) {Â
            System.out.println(                theBooks[i].sNo + "\t\t"                + theBooks[i].bookName + "\t\t"                + theBooks[i].authorName + "\t\t"                + theBooks[i].bookQtyCopy + "\t\t"                + theBooks[i].bookQty);        }    }Â
    // Method 6    // To edit the book    public void upgradeBookQty()    {Â
        System.out.println(            "\t\t\t\tUPGRADE QUANTITY OF A BOOK\n");        System.out.println("Enter Serial No of Book");Â
        int sNo = input.nextInt();Â
        for (int i = 0; i < count; i++) {Â
            if (sNo == theBooks[i].sNo) {Â
                // Display message                System.out.println(                    "Enter No of Books to be Added:");Â
                int addingQty = input.nextInt();                theBooks[i].bookQty += addingQty;                theBooks[i].bookQtyCopy += addingQty;Â
                return;            }        }    }Â
    // Method 7    // To create menu    public void dispMenu()    {Â
        // Displaying menu        System.out.println(            "----------------------------------------------------------------------------------------------------------");        System.out.println("Press 1 to Add new Book.");        System.out.println("Press 0 to Exit Application.");        System.out.println(            "Press 2 to Upgrade Quantity of a Book.");        System.out.println("Press 3 to Search a Book.");        System.out.println("Press 4 to Show All Books.");        System.out.println("Press 5 to Register Student.");        System.out.println(            "Press 6 to Show All Registered Students.");        System.out.println("Press 7 to Check Out Book. ");        System.out.println("Press 8 to Check In Book");        System.out.println(            "-------------------------------------------------------------------------------------------------------");    }Â
    // Method 8    // To search the library    public int isAvailable(int sNo)    {Â
        for (int i = 0; i < count; i++) {            if (sNo == theBooks[i].sNo) {                if (theBooks[i].bookQtyCopy > 0) {Â
                    System.out.println(                        "Book is Available.");                    return i;                }                System.out.println("Book is Unavailable");                return -1;            }        }Â
        System.out.println("No Book of Serial Number "                           + " Available in Library.");        return -1;    }Â
    // Method 9    // To remove the book from the library    public book checkOutBook()    {Â
        System.out.println(            "Enter Serial No of Book to be Checked Out.");        int sNo = input.nextInt();Â
        int bookIndex = isAvailable(sNo);Â
        if (bookIndex != -1) {            theBooks[bookIndex].bookQtyCopy--;            return theBooks[bookIndex];        }        return null;    }Â
    // Method 10    // To add the Book to the Library    public void checkInBook(book b)    {        for (int i = 0; i < count; i++) {            if (b.equals(theBooks[i])) {                theBooks[i].bookQtyCopy++;                return;            }        }    }} |
C. File: Student.java
Java
// Java Program to Illustrate Student Class// to take Input from the student and related InformationÂ
// Importing required classesimport java.util.Scanner;Â
// Classpublic class student {Â
    // Class member variables    String studentName;    String regNum;Â
    book borrowedBooks[] = new book[3];    public int booksCount = 0;Â
    // Creating object of Scanner class to    // take input from user    Scanner input = new Scanner(System.in);Â
    // Constructor    public student()    {        // Print statement        System.out.println("Enter Student Name:");Â
        // This keywords refers to current instance        this.studentName = input.nextLine();Â
        // Print statement        System.out.println("Enter Registration Number:");        this.regNum = input.nextLine();    }} |
D. File: students.java
Java
// Java Program to Illustrate students Class// To Do all the Operations related to students:// add Students,check-in books,check out books,ValidStudentÂ
// Importing required classesimport java.util.Scanner;Â
// Classpublic class students {Â
    // Creating objects of Scanner and students class    Scanner input = new Scanner(System.in);    student theStudents[] = new student[50];Â
    public static int count = 0;Â
    // Method 1    // To add books    public void addStudent(student s)    {        for (int i = 0; i < count; i++) {Â
            if (s.regNum.equalsIgnoreCase(                    theStudents[i].regNum)) {Â
                // Print statement                System.out.println(                    "Student of Reg Num " + s.regNum                    + " is Already Registered.");Â
                return;            }        }Â
        if (count <= 50) {            theStudents[count] = s;            count++;        }    }Â
    // Method 2    // Displaying all students    public void showAllStudents()    {        // Printing student name and        // corresponding registered number        System.out.println("Student Name\t\tReg Number");        for (int i = 0; i < count; i++) {Â
            System.out.println(theStudents[i].studentName                               + "\t\t"                               + theStudents[i].regNum);        }    }Â
    // Method 3    // To check the Student    public int isStudent()    {        // Display message only        System.out.println("Enter Reg Number:");Â
        String regNum = input.nextLine();Â
        for (int i = 0; i < count; i++) {Â
            if (theStudents[i].regNum.equalsIgnoreCase(                    regNum)) {                return i;            }        }Â
        // Print statements        System.out.println("Student is not Registered.");        System.out.println("Get Registered First.");Â
        return -1;    }Â
    // Method 4    // To remove the book    public void checkOutBook(books book)    {        int studentIndex = this.isStudent();Â
        if (studentIndex != -1) {            System.out.println("checking out");Â
            book.showAllBooks();            book b = book.checkOutBook();Â
            System.out.println("checking out");            if (b != null) {Â
                if (theStudents[studentIndex].booksCount                    <= 3) {Â
                    System.out.println("adding book");                    theStudents[studentIndex].borrowedBooks                        [theStudents[studentIndex]                             .booksCount]                        = b;                    theStudents[studentIndex].booksCount++;Â
                    return;                }                else {Â
                    System.out.println(                        "Student Can not Borrow more than 3 Books.");                    return;                }            }            System.out.println("Book is not Available.");        }    }Â
    // Method 5    // To add the book    public void checkInBook(books book)    {        int studentIndex = this.isStudent();        if (studentIndex != -1) {Â
            // Printing credentials corresponding to student            System.out.println(                "S.No\t\t\tBook Name\t\t\tAuthor Name");Â
            student s = theStudents[studentIndex];Â
            for (int i = 0; i < s.booksCount; i++) {Â
                System.out.println(                    s.borrowedBooks[i].sNo + "\t\t\t"                    + s.borrowedBooks[i].bookName + "\t\t\t"                    + s.borrowedBooks[i].authorName);            }Â
            // Display message only            System.out.println(                "Enter Serial Number of Book to be Checked In:");Â
            int sNo = input.nextInt();Â
            for (int i = 0; i < s.booksCount; i++) {                if (sNo == s.borrowedBooks[i].sNo) {                    book.checkInBook(s.borrowedBooks[i]);                    s.borrowedBooks[i] = null;Â
                    return;                }            }Â
            System.out.println("Book of Serial No " + sNo                               + "not Found");        }    }} |
E. File: Library.java
Java
// Java Program to Illustrate Application CLass// To Create The Menu For the ProgramÂ
// Importing required classesimport java.util.Scanner;Â
// Classpublic class Library {Â
    // Main driver method    public static void main(String[] args)    {        // Creating object of Scanner class        // to take input from user        Scanner input = new Scanner(System.in);Â
        // Displaying menu        System.out.println(            "********************Welcome to the GFG Library!********************");        System.out.println(            "                 Select From The Following Options:              ");        System.out.println(            "**********************************************************************");Â
        // Creating object of book class        books ob = new books();        // Creating object of students class        students obStudent = new students();Â
        int choice;        int searchChoice;Â
        // Creating menu        // using do-while loop        do {Â
            ob.dispMenu();            choice = input.nextInt();Â
            // Switch case            switch (choice) {Â
                // Case            case 1:                book b = new book();                ob.addBook(b);                break;Â
                // Case            case 2:                ob.upgradeBookQty();                break;Â
            // Case            case 3:Â
                System.out.println(                    " press 1 to Search with Book Serial No.");                System.out.println(                    " Press 2 to Search with Book's Author Name.");                searchChoice = input.nextInt();Â
                // Nested switch                switch (searchChoice) {Â
                    // Case                case 1:                    ob.searchBySno();                    break;Â
                    // Case                case 2:                    ob.searchByAuthorName();                }                break;Â
                // Case            case 4:                ob.showAllBooks();                break;Â
                // Case            case 5:                student s = new student();                obStudent.addStudent(s);                break;Â
                // Case            case 6:                obStudent.showAllStudents();                break;Â
                // Case            case 7:                obStudent.checkOutBook(ob);                break;Â
                // Case            case 8:                obStudent.checkInBook(ob);                break;Â
                // Default case that will execute for sure                // if above cases does not match            default:Â
                // Print statement                System.out.println("ENTER BETWEEN 0 TO 8.");            }Â
        }Â
        // Checking condition at last where we are        // checking case entered value is not zero        while (choice != 0);    }} |
Output:
Add Book
Â
Register Student
Â
Rent Book /Check Out Book
Â
Show All Book
Â
Return The Book/Check-In Book
Â
Update The Quantity Of the Book
Â
Search The Book
Â
Show All The Registered Student
Â


