Library Management System Using Switch Statement in Java
Here we are implementing a simple library Management application using a switch case statement in Java. In this program, I have added four options that are Add Book, Display available books, Borrow a book and Exit. So the program illustrates all the mentioned things.
When the user selects option 1, it prompts for the book title and author, creates a Book object, and adds it to the list of books. Option 2 displays the available books by iterating over the list and printing their details. Option 3 allows the user to borrow a book by entering the book title, and it removes the book from the list if found. Option 4 exits the program.
Example 1
In this example, we have a LibraryManagementSystem class that manages the library operations. The Book class represents a book with a title and an author. The main logic is implemented in the main method, which displays a menu using a switch statement. Based on the user's choice, it performs the corresponding operation using the methods defined in the LibraryManagementSystem class.
FileName: Library.java
public class Library { private Book[] books; // Array to store the books in the library private int bookCount; // Counter for the number of books in the library public Library() { books = new Book[50]; // Initialize the array with a capacity of 50 books bookCount = 0; // Initialize the book count to 0 } public void addBook(Book book) { if (bookCount < 50) { // Check if there is space in the library to add more books books[bookCount] = book; // Add the book to the next available position in the array bookCount++; // Increment the book count System.out.println("Book added successfully."); } else { System.out.println("No space to add more books."); } } public void increaseBookQty(int serialNo, int qty) { for (int i = 0; i < bookCount; i++) { if (books[i].getSerialNo() == serialNo) { // Check if the book with the given serial number exists books[i].increaseQty(qty); // Increase the quantity of the book System.out.println("Book quantity increased successfully."); return; } } System.out.println("Book not found."); } public void searchBookBySerialNo(int serialNo) { for (int i = 0; i < bookCount; i++) { if (books[i].getSerialNo() == serialNo) { // Check if the book with the given serial number exists System.out.println(books[i]); // Print the details of the book return; } } System.out.println("Book not found."); } public void searchBookByAuthorName(String authorName) { boolean found = false; // Flag to indicate if any books are found for the given author for (int i = 0; i < bookCount; i++) { if (books[i].getAuthorName().equalsIgnoreCase(authorName)) { // Check if the book's author name matches the given author name (case-insensitive) System.out.println(books[i]); // Print the details of the book found = true; } } if (!found) { System.out.println("No books found for author: " + authorName); } } public void displayAllBooks() { for (int i = 0; i < bookCount; i++) { System.out.println(books[i]); // Print the details of all the books in the library } } }
FileName: StudentManager.java
public class StudentManager { private Student[] students; // Array to store the students private int studentCount; // Counter for the number of students public StudentManager() { students = new Student[50]; // Initialize the array with a capacity of 50 students studentCount = 0; // Initialize the student count to 0 } public void addStudent(Student student) { if (studentCount < 50) { // Check if there is space to add more students students[studentCount] = student; // Add the student to the next available position in the array studentCount++; // Increment the student count System.out.println("Student added successfully."); } else { System.out.println("No space to add more students."); } } public void displayAllStudents() { for (int i = 0; i < studentCount; i++) { System.out.println(students[i]); // Print the details of all the students } } }
FileName: Student.java
public class Student { private String studentName; private String registrationNumber; public Student(String studentName, String registrationNumber) { this.studentName = studentName; // Assign the provided student name to the instance variable this.registrationNumber = registrationNumber; // Assign the provided registration number to the instance variable } public String getStudentName() { return studentName; // Return the student's name } public String getRegistrationNumber() { return registrationNumber; // Return the student's registration number } @Override public String toString() { return "Student Name: " + studentName + ", Registration Number: " + registrationNumber; // Return a string representation of the student's details in the format: "Student Name: [name], Registration Number: [registration number]" } }
FileName: Book.java
public class Book { private int serialNo; private String bookName; private String authorName; private int availableQty; private int totalQty; public Book(int serialNo, String bookName, String authorName, int totalQty) { this.serialNo = serialNo; // Assign the provided serial number to the instance variable this.bookName = bookName; // Assign the provided book name to the instance variable this.authorName = authorName; // Assign the provided author name to the instance variable this.availableQty = totalQty; // Set the available quantity to the total quantity initially this.totalQty = totalQty; // Assign the provided total quantity to the instance variable } public int getSerialNo() { return serialNo; // Return the book's serial number } public String getBookName() { return bookName; // Return the book's name } public String getAuthorName() { return authorName; // Return the book's author name } public int getAvailableQty() { return availableQty; // Return the available quantity of the book } public int getTotalQty() { return totalQty; // Return the total quantity of the book } public void increaseQty(int qty) { totalQty += qty; // Increase the total quantity by the provided quantity availableQty += qty; // Increase the available quantity by the provided quantity } public void decreaseQty(int qty) { availableQty -= qty; // Decrease the available quantity by the provided quantity } @Override public String toString() { return "Serial No: " + serialNo + ", Book Name: " + bookName + ", Author Name: " + authorName + ", Available Qty: " + availableQty + ", Total Qty: " + totalQty; // Return a string representation of the book's details in the format: // "Serial No: [serial number], Book Name: [book name], Author Name: [author name], Available Qty: [available quantity], Total Qty: [total quantity]" } }
FileName: LibraryManagementSystem.java
import java.util.Scanner; public class LibraryManagementSystem { public static void main(String[] args) { Library library = new Library(); // Create an instance of the Library class StudentManager studentManager = new StudentManager(); // Create an instance of the StudentManager class Scanner scanner = new Scanner(System.in); // Create a Scanner object to read user input int choice; do { System.out.println("**** Library Management System ****"); System.out.println("1. Add Book"); System.out.println("2. Increase Book Quantity"); System.out.println("3. Search Book by Serial Number"); System.out.println("4. Search Book by Author Name"); System.out.println("5. Display All Books"); System.out.println("6. Add Student"); System.out.println("7. Display All Students"); System.out.println("0. Exit"); System.out.print("Enter your choice: "); choice = scanner.nextInt(); // Read the user's choice scanner.nextLine(); // Consume the newline character switch (choice) { case 1: System.out.print("Enter Serial Number: "); int serialNo = scanner.nextInt(); // Read the book's serial number scanner.nextLine(); System.out.print("Enter Book Name: "); String bookName = scanner.nextLine(); // Read the book's name System.out.print("Enter Author Name: "); String authorName = scanner.nextLine(); // Read the book's author name System.out.print("Enter Quantity: "); int quantity = scanner.nextInt(); // Read the book's quantity scanner.nextLine(); Book book = new Book(serialNo, bookName, authorName, quantity); // Create a Book object with the provided details library.addBook(book); // Add the book to the library break; case 2: System.out.print("Enter Serial Number: "); int sn = scanner.nextInt(); // Read the serial number of the book to increase quantity scanner.nextLine(); System.out.print("Enter Quantity to Increase: "); int qty = scanner.nextInt(); // Read the quantity to increase scanner.nextLine(); library.increaseBookQty(sn, qty); // Increase the quantity of the book in the library break; case 3: System.out.print("Enter Serial Number: "); int serNo = scanner.nextInt(); // Read the serial number of the book to search scanner.nextLine(); library.searchBookBySerialNo(serNo); // Search for the book in the library by serial number break; case 4: System.out.print("Enter Author Name: "); String authName = scanner.nextLine(); // Read the author name to search for books library.searchBookByAuthorName(authName); // Search for books in the library by author name break; case 5: library.displayAllBooks(); // Display all books in the library break; case 6: System.out.print("Enter Student Name: "); String studentName = scanner.nextLine(); // Read the student's name System.out.print("Enter Registration Number: "); String regNo = scanner.nextLine(); // Read the student's registration number Student student = new Student(studentName, regNo); // Create a Student object with the provided details studentManager.addStudent(student); // Add the student to the student manager break; case 7: studentManager.displayAllStudents(); // Display all students in the student manager break; case 0: System.out.println("********** Thank You *************"); break; default: System.out.println("Invalid choice. Please try again."); break; } System.out.println(); } while (choice != 0); scanner.close(); // Close the scanner object } }
Output
**** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 1 Enter Serial Number: 101 Enter Book Name: Chankya Niti Enter Author Name: Arya Chankya Enter Quantity: 5 Book added successfully. **** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 2 Enter Serial Number: 101 Enter Quantity to Increase: 5 Book quantity increased successfully. **** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 3 Enter Serial Number: 101 Serial No: 101, Book Name: Chankya Niti, Author Name: Arya Chankya, Available Qty: 10, Total Qty: 10 **** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 4 Enter Author Name: Arya Chankya Serial No: 101, Book Name: Chankya Niti, Author Name: Arya Chankya, Available Qty: 10, Total Qty: 10 **** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 5 Serial No: 101, Book Name: Chankya Niti, Author Name: Arya Chankya, Available Qty: 10, Total Qty: 10 **** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 6 Enter Student Name: Rahul Dikkar Enter Registration Number: 1001 Student added successfully. **** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 7 Student Name: Rahul Dikkar, Registration Number: 1001 **** Library Management System **** 1. Add Book 2. Increase Book Quantity 3. Search Book by Serial Number 4. Search Book by Author Name 5. Display All Books 6. Add Student 7. Display All Students 0. Exit Enter your choice: 0 ********** Thank You *************