Skip to content

Commit b10de7b

Browse files
Create LibraryManagement.java
This program simulates a basic library management system. It allows users to perform operations like adding students and books, searching for books by serial number or author, viewing the book catalog, increasing book quantities, checking out books to students (with a limit of 3 books per student), and checking in books. Users can also see a list of all registered students. The program presents a menu-driven interface and runs until the user chooses to exit.
1 parent 94f1cac commit b10de7b

File tree

1 file changed

+343
-0
lines changed

1 file changed

+343
-0
lines changed

LibraryManagement.java

Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
import java.util.Scanner;
2+
3+
class Book {
4+
public int sNo;
5+
public String bookName;
6+
public String authorName;
7+
public int bookQty;
8+
public int bookQtyCopy;
9+
10+
Scanner input = new Scanner(System.in);
11+
12+
public Book() {
13+
System.out.println("Enter Serial No of Book:");
14+
this.sNo = input.nextInt();
15+
input.nextLine();
16+
17+
System.out.println("Enter Book Name:");
18+
this.bookName = input.nextLine();
19+
20+
System.out.println("Enter Author Name:");
21+
this.authorName = input.nextLine();
22+
23+
System.out.println("Enter Quantity of Books:");
24+
this.bookQty = input.nextInt();
25+
bookQtyCopy = this.bookQty;
26+
}
27+
}
28+
29+
class Student {
30+
String studentName;
31+
String regNum;
32+
33+
Book borrowedBooks[] = new Book[3];
34+
public int booksCount = 0;
35+
36+
Scanner input = new Scanner(System.in);
37+
38+
public Student() {
39+
System.out.println("Enter Student Name:");
40+
this.studentName = input.nextLine();
41+
42+
System.out.println("Enter Registration Number:");
43+
this.regNum = input.nextLine();
44+
}
45+
}
46+
47+
class Books {
48+
private Book[] theBooks = new Book[50];
49+
private int count;
50+
private Scanner input = new Scanner(System.in);
51+
52+
public int compareBookObjects(Book b1, Book b2) {
53+
if (b1.bookName.equalsIgnoreCase(b2.bookName)) {
54+
System.out.println("Book of this Name Already Exists.");
55+
return 0;
56+
}
57+
if (b1.sNo == b2.sNo) {
58+
System.out.println("Book of this Serial No Already Exists.");
59+
return 0;
60+
}
61+
return 1;
62+
}
63+
64+
public void addBook(Book b) {
65+
for (int i = 0; i < count; i++) {
66+
if (compareBookObjects(b, theBooks[i]) == 0)
67+
return;
68+
}
69+
if (count < 50) {
70+
theBooks[count] = b;
71+
count++;
72+
} else {
73+
System.out.println("No Space to Add More Books.");
74+
}
75+
}
76+
77+
public void searchBySno() {
78+
System.out.println("\t\t\t\tSEARCH BY SERIAL NUMBER\n");
79+
System.out.println("Enter Serial No of Book:");
80+
int sNo = input.nextInt();
81+
int flag = 0;
82+
System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
83+
for (int i = 0; i < count; i++) {
84+
if (sNo == theBooks[i].sNo) {
85+
System.out.println(
86+
theBooks[i].sNo + "\t\t"
87+
+ theBooks[i].bookName + "\t\t"
88+
+ theBooks[i].authorName + "\t\t"
89+
+ theBooks[i].bookQtyCopy + "\t\t"
90+
+ theBooks[i].bookQty);
91+
flag++;
92+
return;
93+
}
94+
}
95+
if (flag == 0)
96+
System.out.println("No Book for Serial No " + sNo + " Found.");
97+
}
98+
99+
public void searchByAuthorName() {
100+
System.out.println("\t\t\t\tSEARCH BY AUTHOR'S NAME");
101+
input.nextLine();
102+
System.out.println("Enter Author Name:");
103+
String authorName = input.nextLine();
104+
int flag = 0;
105+
System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
106+
for (int i = 0; i < count; i++) {
107+
if (authorName.equalsIgnoreCase(theBooks[i].authorName)) {
108+
System.out.println(
109+
theBooks[i].sNo + "\t\t"
110+
+ theBooks[i].bookName + "\t\t"
111+
+ theBooks[i].authorName + "\t\t"
112+
+ theBooks[i].bookQtyCopy + "\t\t"
113+
+ theBooks[i].bookQty);
114+
flag++;
115+
}
116+
}
117+
if (flag == 0)
118+
System.out.println("No Books of " + authorName + " Found.");
119+
}
120+
121+
public void showAllBooks() {
122+
System.out.println("\t\t\t\tSHOWING ALL BOOKS\n");
123+
System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
124+
for (int i = 0; i < count; i++) {
125+
System.out.println(
126+
theBooks[i].sNo + "\t\t"
127+
+ theBooks[i].bookName + "\t\t"
128+
+ theBooks[i].authorName + "\t\t"
129+
+ theBooks[i].bookQtyCopy + "\t\t"
130+
+ theBooks[i].bookQty);
131+
}
132+
}
133+
134+
public void upgradeBookQty() {
135+
System.out.println("\t\t\t\tUPGRADE QUANTITY OF A BOOK\n");
136+
System.out.println("Enter Serial No of Book");
137+
int sNo = input.nextInt();
138+
for (int i = 0; i < count; i++) {
139+
if (sNo == theBooks[i].sNo) {
140+
System.out.println("Enter No of Books to be Added:");
141+
int addingQty = input.nextInt();
142+
theBooks[i].bookQty += addingQty;
143+
theBooks[i].bookQtyCopy += addingQty;
144+
return;
145+
}
146+
}
147+
}
148+
149+
public int isAvailable(int sNo) {
150+
for (int i = 0; i < count; i++) {
151+
if (sNo == theBooks[i].sNo) {
152+
if (theBooks[i].bookQtyCopy > 0) {
153+
System.out.println("Book is Available.");
154+
return i;
155+
}
156+
System.out.println("Book is Unavailable");
157+
return -1;
158+
}
159+
}
160+
System.out.println("No Book of Serial Number Available in Library.");
161+
return -1;
162+
}
163+
164+
public Book checkOutBook() {
165+
System.out.println("Enter Serial No of Book to be Checked Out.");
166+
int sNo = input.nextInt();
167+
int bookIndex = isAvailable(sNo);
168+
if (bookIndex != -1) {
169+
theBooks[bookIndex].bookQtyCopy--;
170+
return theBooks[bookIndex];
171+
}
172+
return null;
173+
}
174+
175+
public void checkInBook(Book b) {
176+
for (int i = 0; i < count; i++) {
177+
if (b.equals(theBooks[i])) {
178+
theBooks[i].bookQtyCopy++;
179+
return;
180+
}
181+
}
182+
}
183+
}
184+
185+
class Students {
186+
Scanner input = new Scanner(System.in);
187+
student theStudents[] = new student[50];
188+
public static int count = 0;
189+
190+
public void addStudent(student s) {
191+
for (int i = 0; i < count; i++) {
192+
if (s.regNum.equalsIgnoreCase(theStudents[i].regNum)) {
193+
System.out.println("Student of Reg Num " + s.regNum + " is Already Registered.");
194+
return;
195+
}
196+
}
197+
198+
if (count <= 50) {
199+
theStudents[count] = s;
200+
count++;
201+
}
202+
}
203+
204+
public void showAllStudents() {
205+
System.out.println("Student Name\t\tReg Number");
206+
for (int i = 0; i < count; i++) {
207+
System.out.println(theStudents[i].studentName + "\t\t" + theStudents[i].regNum);
208+
}
209+
}
210+
211+
public int isStudent() {
212+
System.out.println("Enter Reg Number:");
213+
String regNum = input.nextLine();
214+
215+
for (int i = 0; i < count; i++) {
216+
if (theStudents[i].regNum.equalsIgnoreCase(regNum)) {
217+
return i;
218+
}
219+
}
220+
221+
System.out.println("Student is not Registered.");
222+
System.out.println("Get Registered First.");
223+
return -1;
224+
}
225+
226+
public void checkOutBook(Books book) {
227+
int studentIndex = this.isStudent();
228+
229+
if (studentIndex != -1) {
230+
System.out.println("checking out");
231+
book.showAllBooks();
232+
book b = book.checkOutBook();
233+
System.out.println("checking out");
234+
235+
if (b != null) {
236+
if (theStudents[studentIndex].booksCount <= 3) {
237+
System.out.println("adding book");
238+
theStudents[studentIndex].borrowedBooks[theStudents[studentIndex].booksCount] = b;
239+
theStudents[studentIndex].booksCount++;
240+
return;
241+
} else {
242+
System.out.println("Student Can not Borrow more than 3 Books.");
243+
return;
244+
}
245+
}
246+
System.out.println("Book is not Available.");
247+
}
248+
}
249+
250+
public void checkInBook(Books book) {
251+
int studentIndex = this.isStudent();
252+
if (studentIndex != -1) {
253+
System.out.println("S.No\t\t\tBook Name\t\t\tAuthor Name");
254+
student s = theStudents[studentIndex];
255+
256+
for (int i = 0; i < s.booksCount; i++) {
257+
System.out.println(
258+
s.borrowedBooks[i].sNo + "\t\t\t"
259+
+ s.borrowedBooks[i].bookName + "\t\t\t"
260+
+ s.borrowedBooks[i].authorName);
261+
}
262+
263+
System.out.println("Enter Serial Number of Book to be Checked In:");
264+
int sNo = input.nextInt();
265+
266+
for (int i = 0; i < s.booksCount; i++) {
267+
if (sNo == s.borrowedBooks[i].sNo) {
268+
book.checkInBook(s.borrowedBooks[i]);
269+
s.borrowedBooks[i] = null;
270+
return;
271+
}
272+
}
273+
274+
System.out.println("Book of Serial No " + sNo + " not Found");
275+
}
276+
}
277+
}
278+
279+
public class LibrarySystem {
280+
public static void main(String[] args) {
281+
Scanner input = new Scanner(System.in);
282+
Books books = new Books();
283+
Students students = new Students();
284+
285+
int choice;
286+
287+
do {
288+
System.out.println("Library System Menu:");
289+
System.out.println("1. Add Student");
290+
System.out.println("2. Add Book");
291+
System.out.println("3. Search Book by Serial Number");
292+
System.out.println("4. Search Book by Author Name");
293+
System.out.println("5. Show All Books");
294+
System.out.println("6. Upgrade Quantity of a Book");
295+
System.out.println("7. Check Out Book");
296+
System.out.println("8. Check In Book");
297+
System.out.println("9. Show All Students");
298+
System.out.println("0. Exit");
299+
300+
System.out.print("Enter your choice: ");
301+
choice = input.nextInt();
302+
input.nextLine(); // Consume the newline
303+
304+
switch (choice) {
305+
case 1:
306+
student newStudent = new student();
307+
students.addStudent(newStudent);
308+
break;
309+
case 2:
310+
Book newBook = new Book();
311+
books.addBook(newBook);
312+
break;
313+
case 3:
314+
books.searchBySno();
315+
break;
316+
case 4:
317+
books.searchByAuthorName();
318+
break;
319+
case 5:
320+
books.showAllBooks();
321+
break;
322+
case 6:
323+
books.upgradeBookQty();
324+
break;
325+
case 7:
326+
students.checkOutBook(books);
327+
break;
328+
case 8:
329+
students.checkInBook(books);
330+
break;
331+
case 9:
332+
students.showAllStudents();
333+
break;
334+
case 0:
335+
System.out.println("Exiting Library System.");
336+
break;
337+
default:
338+
System.out.println("Invalid choice. Please try again.");
339+
break;
340+
}
341+
} while (choice != 0);
342+
}
343+
}

0 commit comments

Comments
 (0)