// Java Program to Illustrate book Class that
// takes Input from the books and related information
// Importing required classes
import [Link];
// Class
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([Link]);
// Method
// To add book details
public book()
{
// Display message for taking input later
// taking input via
// nextInt() and nextLine() standard methods
[Link]("Enter Serial No of Book:");
[Link] = [Link]();
[Link]();
[Link]("Enter Book Name:");
[Link] = [Link]();
[Link]("Enter Author Name:");
[Link] = [Link]();
[Link]("Enter Quantity of Books:");
[Link] = [Link]();
bookQtyCopy = [Link];
}
}
// 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
// CLass
class books {
// Class data members
book theBooks[] = new book[50];
public static int count;
Scanner input = new Scanner([Link]);
// Method 1
// To compare books
public int compareBookObjects(book b1, book b2)
{
// If book name matches
if ([Link]([Link])) {
// Printing book exists
[Link](
"Book of this Name Already Exists.");
return 0;
}
// if book serial matches
if ([Link] == [Link]) {
// Print book exists
[Link](
"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 ([Link](b, [Link][i])
== 0)
return;
}
if (count < 50) {
theBooks[count] = b;
count++;
}
else {
[Link](
"No Space to Add More Books.");
}
}
// Method 3
// To search book by serial number
public void searchBySno()
{
// Display message
[Link](
"\t\t\t\tSEARCH BY SERIAL NUMBER\n");
// Class data members
int sNo;
[Link]("Enter Serial No of Book:");
sNo = [Link]();
int flag = 0;
[Link](
"[Link]\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
for (int i = 0; i < count; i++) {
if (sNo == theBooks[i].sNo) {
[Link](
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)
[Link]("No Book for Serial No "
+ sNo + " Found.");
}
// Method 4
// To search author by name
public void searchByAuthorName()
{
// Display message
[Link](
"\t\t\t\tSEARCH BY AUTHOR'S NAME");
[Link]();
[Link]("Enter Author Name:");
String authorName = [Link]();
int flag = 0;
[Link](
"[Link]\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 ([Link](
theBooks[i].authorName)) {
// Print below corresponding credentails
[Link](
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)
[Link]("No Books of " + authorName
+ " Found.");
}
// Method 5
// To display all books
public void showAllBooks()
{
[Link]("\t\t\t\tSHOWING ALL BOOKS\n");
[Link](
"[Link]\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
for (int i = 0; i < count; i++) {
[Link](
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()
{
[Link](
"\t\t\t\tUPGRADE QUANTITY OF A BOOK\n");
[Link]("Enter Serial No of Book");
int sNo = [Link]();
for (int i = 0; i < count; i++) {
if (sNo == theBooks[i].sNo) {
// Display message
[Link](
"Enter No of Books to be Added:");
int addingQty = [Link]();
theBooks[i].bookQty += addingQty;
theBooks[i].bookQtyCopy += addingQty;
return;
}
}
}
// Method 7
// To create menu
public void dispMenu()
{
// Displaying menu
[Link](
"----------------------------------------------------------------------------------
------------------------");
[Link]("Press 1 to Add new Book.");
[Link]("Press 0 to Exit Application.");
[Link](
"Press 2 to Upgrade Quantity of a Book.");
[Link]("Press 3 to Search a Book.");
[Link]("Press 4 to Show All Books.");
[Link]("Press 5 to Register Student.");
[Link](
"Press 6 to Show All Registered Students.");
[Link]("Press 7 to Check Out Book. ");
[Link]("Press 8 to Check In Book");
[Link](
"----------------------------------------------------------------------------------
---------------------");
}
// 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) {
[Link](
"Book is Available.");
return i;
}
[Link]("Book is Unavailable");
return -1;
}
}
[Link]("No Book of Serial Number "
+ " Available in Library.");
return -1;
}
// Method 9
// To remove the book from the library
public book checkOutBook()
{
[Link](
"Enter Serial No of Book to be Checked Out.");
int sNo = [Link]();
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 ([Link](theBooks[i])) {
theBooks[i].bookQtyCopy++;
return;
}
}
}
}
// Java Program to Illustrate Student Class
// to take Input from the student and related Information
// Class
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([Link]);
// Constructor
public student()
{
// Print statement
[Link]("Enter Student Name:");
// This keywords refers to current instance
[Link] = [Link]();
// Print statement
[Link]("Enter Registration Number:");
[Link] = [Link]();
}
}
// Java Program to Illustrate students Class
// To Do all the Operations related to students:
// add Students,check-in books,check out books,ValidStudent
// Class
class students {
// Creating objects of Scanner and students class
Scanner input = new Scanner([Link]);
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 ([Link](
theStudents[i].regNum)) {
// Print statement
[Link](
"Student of Reg Num " + [Link]
+ " 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
[Link]("Student Name\t\tReg Number");
for (int i = 0; i < count; i++) {
[Link](theStudents[i].studentName
+ "\t\t"
+ theStudents[i].regNum);
}
}
// Method 3
// To check the Student
public int isStudent()
{
// Display message only
[Link]("Enter Reg Number:");
String regNum = [Link]();
for (int i = 0; i < count; i++) {
if (theStudents[i].[Link](
regNum)) {
return i;
}
}
// Print statements
[Link]("Student is not Registered.");
[Link]("Get Registered First.");
return -1;
}
// Method 4
// To remove the book
public void checkOutBook(books book)
{
int studentIndex = [Link]();
if (studentIndex != -1) {
[Link]("checking out");
[Link]();
book b = [Link]();
[Link]("checking out");
if (b != null) {
if (theStudents[studentIndex].booksCount
<= 3) {
[Link]("adding book");
theStudents[studentIndex].borrowedBooks
[theStudents[studentIndex]
.booksCount]
= b;
theStudents[studentIndex].booksCount++;
return;
}
else {
[Link](
"Student Can not Borrow more than 3 Books.");
return;
}
}
[Link]("Book is not Available.");
}
}
// Method 5
// To add the book
public void checkInBook(books book)
{
int studentIndex = [Link]();
if (studentIndex != -1) {
// Printing credentials corresponding to student
[Link](
"[Link]\t\t\tBook Name\t\t\tAuthor Name");
student s = theStudents[studentIndex];
for (int i = 0; i < [Link]; i++) {
[Link](
[Link][i].sNo + "\t\t\t"
+ [Link][i].bookName + "\t\t\t"
+ [Link][i].authorName);
}
// Display message only
[Link](
"Enter Serial Number of Book to be Checked In:");
int sNo = [Link]();
for (int i = 0; i < [Link]; i++) {
if (sNo == [Link][i].sNo) {
[Link]([Link][i]);
[Link][i] = null;
return;
}
}
[Link]("Book of Serial No " + sNo
+ "not Found");
}
}
}
// Java Program to Illustrate Application CLass
// To Create The Menu For the Program
// Class
public 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([Link]);
// Displaying menu
[Link](
"********************Welcome to the GFG Library!********************");
[Link](
" Select From The Following Options: ");
[Link](
"**********************************************************************");
// 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 {
[Link]();
choice = [Link]();
// Switch case
switch (choice) {
// Case
case 1:
book b = new book();
[Link](b);
break;
// Case
case 2:
[Link]();
break;
// Case
case 3:
[Link](
" press 1 to Search with Book Serial No.");
[Link](
" Press 2 to Search with Book's Author Name.");
searchChoice = [Link]();
// Nested switch
switch (searchChoice) {
// Case
case 1:
[Link]();
break;
// Case
case 2:
[Link]();
}
break;
// Case
case 4:
[Link]();
break;
// Case
case 5:
student s = new student();
[Link](s);
break;
// Case
case 6:
[Link]();
break;
// Case
case 7:
[Link](ob);
break;
// Case
case 8:
[Link](ob);
break;
// Default case that will execute for sure
// if above cases does not match
default:
// Print statement
[Link]("ENTER BETWEEN 0 TO 8.");
}
// Checking condition at last where we are
// checking case entered value is not zero
while (choice != 0);
}
}