import java.util.Scanner;
public class LibraryManagementSystem {
// Classes for Book, Student, and Library Management
static class Book {
private int sNo;
private String bookName;
private String authorName;
private int bookQty;
private int bookQtyCopy;
public Book() {
Scanner input
= new Scanner
(System.in
); System.out
.print("Enter Serial No of Book: "); this.sNo = input.nextInt();
input.nextLine(); // Clear buffer
System.out
.print("Enter Book Name: "); this.bookName = input.nextLine();
System.out
.print("Enter Author Name: "); this.authorName = input.nextLine();
System.out
.print("Enter Quantity of Books: "); this.bookQty = input.nextInt();
this.bookQtyCopy = this.bookQty;
}
public int getsNo() { return sNo; }
public String getBookName() { return bookName; }
public String getAuthorName() { return authorName; }
public int getBookQty() { return bookQty; }
public int getBookQtyCopy() { return bookQtyCopy; }
public void addBookQty(int qty) {
this.bookQty += qty;
this.bookQtyCopy += qty;
}
}
static class Student {
private String studentName;
private String regNum;
private Book[] borrowedBooks = new Book[3];
public int booksCount = 0;
public Student() {
Scanner input
= new Scanner
(System.in
); System.out
.print("Enter Student Name: "); this.studentName = input.nextLine();
System.out
.print("Enter Registration Number: "); this.regNum = input.nextLine();
}
public String getRegNum() { return regNum; }
public String getStudentName() { return studentName; }
}
static class Library {
private Book[] books = new Book[50];
private Student[] students = new Student[50];
private static int bookCount = 0;
private static int studentCount = 0;
public void addBook(Book book) {
for (int i = 0; i < bookCount; i++) {
if (book.getsNo() == books[i].getsNo() || book.getBookName().equalsIgnoreCase(books[i].getBookName())) {
System.out
.println
("Book already exists."); return;
}
}
if (bookCount < 50) {
books[bookCount++] = book;
System.out
.println
("Book added successfully."); } else {
System.out
.println
("No space to add more books."); }
}
public void showAllBooks() {
System.out
.println
("S.No\tName\tAuthor\tAvailable Qty\tTotal Qty"); for (int i = 0; i < bookCount; i++) {
books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
books[i].getBookQtyCopy(), books[i].getBookQty());
}
}
public void searchBySerialNumber(int sNo) {
for (int i = 0; i < bookCount; i++) {
if (books[i].getsNo() == sNo) {
books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
books[i].getBookQtyCopy(), books[i].getBookQty());
return;
}
}
System.out
.println
("No Book found with Serial No " + sNo
); }
public void addStudent(Student student) {
for (int i = 0; i < studentCount; i++) {
if (student.getRegNum().equalsIgnoreCase(students[i].getRegNum())) {
System.out
.println
("Student is already registered."); return;
}
}
if (studentCount < 50) {
students[studentCount++] = student;
System.out
.println
("Student registered successfully."); } else {
System.out
.println
("No space to add more students."); }
}
public void showAllStudents() {
System.out
.println
("Student Name\tReg Number"); for (int i = 0; i < studentCount; i++) {
System.out
.printf("%s\t%s%n", students
[i
].getStudentName
(), students
[i
].getRegNum
()); }
}
}
public static void main(String[] args) {
Scanner input
= new Scanner
(System.in
); Library library = new Library();
int choice;
do {
displayMenu();
choice = input.nextInt();
switch (choice) {
case 1:
library.addBook(new Book());
break;
case 2:
library.showAllBooks();
break;
case 3:
System.out
.print("Enter Serial No of Book to search: "); int sNo = input.nextInt();
library.searchBySerialNumber(sNo);
break;
case 4:
library.addStudent(new Student());
break;
case 5:
library.showAllStudents();
break;
case 0:
System.out
.println
("Exiting the application."); break;
default:
System.out
.println
("Invalid choice. Please select between 0 to 5."); }
} while (choice != 0);
input.close();
}
private static void displayMenu() {
System.out
.println
("******************** Welcome to the Library! ********************"); System.out
.println
("1. Add new Book"); System.out
.println
("2. Show All Books"); System.out
.println
("3. Search a Book by Serial No"); System.out
.println
("4. Register Student"); System.out
.println
("5. Show All Registered Students"); System.out
.println
("0. Exit"); System.out
.println
("********************************************************************"); }
}
import java.util.Scanner;
public class LibraryManagementSystem {
// Classes for Book, Student, and Library Management
static class Book {
private int sNo;
private String bookName;
private String authorName;
private int bookQty;
private int bookQtyCopy;
public Book() {
Scanner input
= new Scanner
(System.in
); System.out
.print("Enter Serial No of Book: "); this.sNo = input.nextInt();
input.nextLine(); // Clear buffer
System.out
.print("Enter Book Name: "); this.bookName = input.nextLine();
System.out
.print("Enter Author Name: "); this.authorName = input.nextLine();
System.out
.print("Enter Quantity of Books: "); this.bookQty = input.nextInt();
this.bookQtyCopy = this.bookQty;
}
public int getsNo() { return sNo; }
public String getBookName() { return bookName; }
public String getAuthorName() { return authorName; }
public int getBookQty() { return bookQty; }
public int getBookQtyCopy() { return bookQtyCopy; }
public void addBookQty(int qty) {
this.bookQty += qty;
this.bookQtyCopy += qty;
}
}
static class Student {
private String studentName;
private String regNum;
private Book[] borrowedBooks = new Book[3];
public int booksCount = 0;
public Student() {
Scanner input
= new Scanner
(System.in
); System.out
.print("Enter Student Name: "); this.studentName = input.nextLine();
System.out
.print("Enter Registration Number: "); this.regNum = input.nextLine();
}
public String getRegNum() { return regNum; }
public String getStudentName() { return studentName; }
}
static class Library {
private Book[] books = new Book[50];
private Student[] students = new Student[50];
private static int bookCount = 0;
private static int studentCount = 0;
public void addBook(Book book) {
for (int i = 0; i < bookCount; i++) {
if (book.getsNo() == books[i].getsNo() || book.getBookName().equalsIgnoreCase(books[i].getBookName())) {
System.out
.println
("Book already exists."); return;
}
}
if (bookCount < 50) {
books[bookCount++] = book;
System.out
.println
("Book added successfully."); } else {
System.out
.println
("No space to add more books."); }
}
public void showAllBooks() {
System.out
.println
("S.No\tName\tAuthor\tAvailable Qty\tTotal Qty"); for (int i = 0; i < bookCount; i++) {
books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
books[i].getBookQtyCopy(), books[i].getBookQty());
}
}
public void searchBySerialNumber(int sNo) {
for (int i = 0; i < bookCount; i++) {
if (books[i].getsNo() == sNo) {
books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
books[i].getBookQtyCopy(), books[i].getBookQty());
return;
}
}
System.out
.println
("No Book found with Serial No " + sNo
); }
public void addStudent(Student student) {
for (int i = 0; i < studentCount; i++) {
if (student.getRegNum().equalsIgnoreCase(students[i].getRegNum())) {
System.out
.println
("Student is already registered."); return;
}
}
if (studentCount < 50) {
students[studentCount++] = student;
System.out
.println
("Student registered successfully."); } else {
System.out
.println
("No space to add more students."); }
}
public void showAllStudents() {
System.out
.println
("Student Name\tReg Number"); for (int i = 0; i < studentCount; i++) {
System.out
.printf("%s\t%s%n", students
[i
].getStudentName
(), students
[i
].getRegNum
()); }
}
}
public static void main(String[] args) {
Scanner input
= new Scanner
(System.in
); Library library = new Library();
int choice;
do {
displayMenu();
choice = input.nextInt();
switch (choice) {
case 1:
library.addBook(new Book());
break;
case 2:
library.showAllBooks();
break;
case 3:
System.out
.print("Enter Serial No of Book to search: "); int sNo = input.nextInt();
library.searchBySerialNumber(sNo);
break;
case 4:
library.addStudent(new Student());
break;
case 5:
library.showAllStudents();
break;
case 0:
System.out
.println
("Exiting the application."); break;
default:
System.out
.println
("Invalid choice. Please select between 0 to 5."); }
} while (choice != 0);
input.close();
}
private static void displayMenu() {
System.out
.println
("******************** Welcome to the Library! ********************"); System.out
.println
("1. Add new Book"); System.out
.println
("2. Show All Books"); System.out
.println
("3. Search a Book by Serial No"); System.out
.println
("4. Register Student"); System.out
.println
("5. Show All Registered Students"); System.out
.println
("0. Exit"); System.out
.println
("********************************************************************"); }
}