-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary Management System.cpp
569 lines (518 loc) · 15.1 KB
/
Library Management System.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
class Book {
long bookNumber;
char bookName[100];
char author[50];
public:
void add_new_book() {
cout << "\n\n\t\t\t** ADD NEW BOOK **\n\n";
cout << "\nBOOK NUMBER: ";
cin >> bookNumber;
fflush(stdin);
cout << "\nNAME OF THE BOOK: ";
gets(bookName);
fflush(stdin);
cout << "\nNAME OF THE AUTHOR: ";
gets(author);
fflush(stdin);
cout << "\n** BOOK SUCCESSFULLY ADDED! **\n";
}
void view_book() {
cout << "\nBOOK NUMBER: " << bookNumber;
cout << "\n\nNAME OF THE BOOK: " << bookName;
cout << "\n\nNAME OF THE AUTHOR: " << author;
}
void update_book() {
cout << "\n\n ** ENTER NEW DETAILS!**\n\n";
cout << "\nBOOK NAME: ";
gets(bookName);
cout << "\nNAME OF THE AUTHOR: ";
gets(author);
}
long get_book_number() {
return bookNumber;
}
void report() {
cout << endl;
cout << setw(10);
cout << bookNumber;
cout << setw(40);
cout << bookName;
cout << setw(30);
cout << author;
}
};
class Student {
long admissionNumber;
char studentName[50];
long issuedBookNumber;
int booksIssued;
public:
void add_new_student() {
cout << "\n\n\t\t\t** ADD NEW STUDENT **\n\n";
cout << "\nADMISSION NUMBER: ";
cin >> admissionNumber;
fflush(stdin);
cout << "\nNAME OF THE STUDENT: ";
gets(studentName);
fflush(stdin);
booksIssued = 0;
cout << "\n\n** STUDENT SUCCESSFULLY ADDED! **\n\n";
}
void view_student() {
cout << "\nADMISSION NUMBER: " << admissionNumber;
cout << "\nNAME OF THE STUDENT: " << studentName;
cout << "\nNUMBER OF BOOKS ISSUED: " << booksIssued;
if (!booksIssued) {
cout << "\n\n** THE STUDENT HAS ISSUED NO BOOKS!**\n\n";
}
}
void update_student() {
cout << "\n\n ** ENTER NEW DETAILS!**\n\n";
cout << "\nNAME OF THE STUDENT: " << studentName;
}
long get_admission_number() {
return admissionNumber;
}
long get_issued_books() {
return issuedBookNumber;
}
int get_issued_number() {
return booksIssued;
}
void add_issued_books() {
booksIssued++;
}
void return_book() {
booksIssued--;
}
void set_book_number(long book_number) {
issuedBookNumber = book_number;
}
void report() {
cout << endl;
cout << setw(10);
cout << admissionNumber;
cout << setw(30);
cout << studentName;
cout << setw(10);
cout << booksIssued;
}
};
fstream f;
Book book;
Student student;
void new_book() {
char choice;
f.open("books.dat", ios::app);
do {
system("cls");
book.add_new_book();
f.write((char *) &book, sizeof(Book));
cout << "\n\nDO YOU WANT TO ADD MORE RECORDS? [Y/n] ";
cin >> choice;
fflush(stdin);
} while (tolower(choice) != 'n');
f.close();
}
void new_student() {
char choice;
f.open("students.dat", ios::app);
do {
system("cls");
student.add_new_student();
f.write((char *) &student, sizeof(Student));
cout << "\n\nDO YOU WANT TO ADD MORE RECORDS? [Y/n] ";
cin >> choice;
fflush(stdin);
} while (tolower(choice) != 'n');
f.close();
}
void display_book(long bookNumber) {
system("cls");
cout << "\n\n\t\t\t** BOOK DETAILS ** \n\n";
int flag = 1;
f.open("books.dat", ios::in);
while (f.read((char *) &book, sizeof(Book))) {
if (bookNumber == book.get_book_number()) {
book.view_book();
flag = 0;
}
}
f.close();
if (flag) {
cout << "\n\n** BOOK DOES NOT EXIST! **\n\n";
}
else {
cout << "\n\n";
}
system("pause");
}
void display_student(long admissionNumber) {
system("cls");
cout << "\n\n\t\t\t** STUDENT DETAILS ** \n";
int flag = 1;
f.open("students.dat", ios::in);
while (f.read((char *) &student, sizeof(Student))) {
if (admissionNumber == student.get_admission_number()) {
student.view_student();
flag = 0;
}
}
f.close();
if (flag) {
cout << "\n\n** STUDENT DOES NOT EXIST! **\n\n";
}
else {
cout << "\n\n";
}
system("pause");
}
void update_book_record() {
long bookNumber;
int found = 0;
cout << "\n\n\t\t\t** MODIFY BOOK RECORD **\n\n";
cout << "\nBOOK NUMBER OF THE BOOK TO BE UPDATED: ";
cin >> bookNumber;
fflush(stdin);
f.open("books.dat", ios::in | ios::out);
while (f.read((char *) &book, sizeof(Book))) {
if (book.get_book_number() == bookNumber) {
book.view_book();
book.update_book();
int pos = -1 * sizeof(Book);
f.seekp(pos, ios::cur);
f.write((char *) &book, sizeof(Book));
cout << "\n\n** BOOK SUCCESSFULLY UPDATED! **\n\n";
found = 1;
}
}
f.close();
if (!found) {
cout << "\n\n** BOOK NOT FOUND!! **\n\n";
}
system("pause");
}
void update_student_record() {
long admissionNumber;
int found = 0;
cout << "\n\n\t\t\t** MODIFY STUDENT RECORD **\n\n";
cout << "\nADMISSION NUMBER OF THE STUDENT TO BE UPDATED: ";
cin >> admissionNumber;
fflush(stdin);
f.open("student.dat", ios::in | ios::out);
while (f.read((char *) &student, sizeof(Student))) {
if (student.get_admission_number() == admissionNumber) {
student.view_student();
student.update_student();
int pos = -1 * sizeof(Student);
f.seekp(pos, ios::cur);
f.write((char *) &student, sizeof(Student));
cout << "\n\n** STUDENT SUCCESSFULLY UPDATED! **\n\n";
found = 1;
}
}
f.close();
if (!found) {
cout << "\n\n** STUDENT NOT FOUND!! **\n\n";
}
system("pause");
}
void remove_student() {
long admissionNumber;
int flag = 0;
cout << "\n\n\t\t\t** DELETE STUDENT **\n\n";
cout << "\nADMISSION NUMBER OF THE STUDENT TO BE DELETED: ";
cin >> admissionNumber;
fflush(stdin);
f.open("students.dat", ios::in | ios::out);
fstream fil;
fil.open("temp.dat", ios::out);
f.seekg(0, ios::beg);
while (f.read((char *) &student, sizeof(Student))) {
if (student.get_admission_number() != admissionNumber) {
fil.write((char *) &student, sizeof(Student));
}
else {
flag = 1;
}
}
fil.close();
f.close();
remove("students.dat");
rename("temp.dat", "students.dat");
if (flag) {
cout << "\n\n** STUDENT-RECORD DELETED!! **\n\n";
}
else {
cout << "\n\n** STUDENT NOT FOUND!! **\n\n";
}
system("pause");
}
void remove_book() {
long bookNumber;
int flag = 0;
cout << "\n\n\t\t\t** DELETE BOOK **\n\n";
cout << "\nBOOK NUMBER OF THE BOOK TO BE DELETED: ";
cin >> bookNumber;
fflush(stdin);
f.open("books.dat", ios::in | ios::out);
fstream fil;
fil.open("temp.dat", ios::out);
f.seekg(0, ios::beg);
while (f.read((char *) &book, sizeof(Book))) {
if (book.get_book_number() != bookNumber) {
fil.write((char *) &book, sizeof(Book));
}
else {
flag = 1;
}
}
fil.close();
f.close();
remove("books.dat");
rename("temp.dat", "books.dat");
if (flag) {
cout << "\n\n** BOOK-RECORD DELETED!! **\n\n";
}
else {
cout << "\n\n** BOOK NOT FOUND!! **\n\n";
}
system("pause");
}
void student_list() {
f.open("students.dat", ios::in);
if (!f) {
cout << "\n\n** THERE ARE NO STUDENTS IN THE SYSTEM! **\n\n";
cout << "\n\n";
system("pause");
return;
}
cout << "\n\n\t ** LIST OF THE STUDENTS IN THE SYSTEM **\n\n";
cout << "==========================================================\n";
cout << "ADMISSION NUMBER" << " " << "NAME OF THE STUDENT" << " " << "BOOKS ISSUED\n";
cout << "==========================================================\n";
while (f.read((char *) &student, sizeof(Student))) {
student.report();
}
f.close();
cout << "\n\n";
system("pause");
}
void book_list() {
f.open("books.dat", ios::in);
if (!f) {
cout << "\n\n** THERE ARE NO BOOKS IN THE SYSTEM! **\n\n";
cout << "\n\n";
system("pause");
return;
}
cout << "\n\n\t\t\t** LIST OF THE BOOKS IN THE SYSTEM **\n\n";
cout << "========================================================================================\n";
cout << "-- BOOK NUMBER --" << " " << "-- NAME OF THE BOOK --" << " " << "-- NAME OF THE AUTHOR --\n";
cout << "========================================================================================\n";
while (f.read((char *) &book, sizeof(Book))) {
book.report();
}
f.close();
cout << "\n\n";
system("pause");
}
void issue_book() {
long admissionNumber, bookNumber;
int found = 0, flag = 0;
fstream fil;
cout << "\n\n\t\t** BOOK ISSUE **\n\n";
cout << "\n\nADMISSION NUMBER OF THE STUDENT: ";
cin >> admissionNumber;
fflush(stdin);
f.open("students.dat", ios::in | ios::out);
fil.open("books.dat", ios::in | ios::out);
while (f.read((char *) &student, sizeof(Student))) {
if (student.get_admission_number() == admissionNumber) {
found = 1;
if (student.get_issued_number() < 1) {
cout << "\n\nBOOK NUMBER OF THE BOOK TO BE ISSUED: ";
cin >> bookNumber;
while (fil.read((char *) &book, sizeof(Book))) {
if (book.get_book_number() == bookNumber) {
book.view_book();
flag = 1;
student.add_issued_books();
student.set_book_number(bookNumber);
int pos = -1 * sizeof(Student);
f.seekp(pos, ios::cur);
f.write((char *) &student, sizeof(Student));
cout << "\n\n** BOOK ISSUED SUCCESSFULLY! **\n\n";
}
}
if (!flag) {
cout << "\n\n** BOOK NUMBER DOES NOT EXIST! **\n\n";
}
}
else {
cout << "\n\n** YOU CAN ONLY ISSUE ONE BOOK AT A TIME! **\n\n";
}
}
}
if (!found) {
cout << "\n\n** STUDENT DOESN'T EXIST! **\n\n";
}
system("pause");
f.close();
fil.close();
}
void return_book() {
long bookNumber, admissionNumber;
int found = 0, flag = 0;
fstream fil;
system("cls");
cout << "\n\n\t\t\t** BOOK DEPOSIT **\n\n";
cout << "\n\nENTER ADMISSION NUMBER: ";
cin >> admissionNumber;
fflush(stdin);
f.open("students.dat", ios::in | ios::out);
fil.open("books.dat", ios::in | ios::out);
while (f.read((char *) &student, sizeof(Student))) {
if (student.get_admission_number() == admissionNumber) {
found = 1;
if (student.get_issued_number() == 1) {
while (fil.read((char *) &book, sizeof(Book))) {
if (student.get_issued_books() == book.get_book_number()) {
book.view_book();
flag = 1;
student.return_book();
int pos = -1 * sizeof(student);
f.seekp(pos, ios::cur);
f.write((char *) &student, sizeof(Student));
cout << "\n\n** BOOK DEPOSITED SUCCESSFULLY! **\n\n";
}
}
if (!flag) {
cout << "\n\n** BOOK NUMBER DOES NOT EXIST! **\n\n";
}
}
else {
cout << "\n\n** NO BOOKS ISSUED. PLEASE CHECK! **\n\n";
}
}
}
if (!found) {
cout << "\n\n** STUDENT DOESN'T EXIST! **\n\n";
}
system("pause");
f.close();
fil.close();
}
void administrator() {
int choice;
cout << "\n\n\n\t\t\t** ADMINISTRATOR MENU **";
cout << "\n\n\t1- ADD STUDENT";
cout << "\n\n\t2- DISPLAY ALL STUDENTS";
cout << "\n\n\t3- DISPLAY SPECIFIC STUDENT";
cout << "\n\n\t4- UPDATE STUDENT RECORD";
cout << "\n\n\t5- DELETE STUDENT";
cout << "\n\n\t6- ADD BOOK";
cout << "\n\n\t7- DISPLAY ALL BOOKS";
cout << "\n\n\t8- DISPLAY SPECIFIC BOOK";
cout << "\n\n\t9- UPDATE A BOOK";
cout << "\n\n\t10- DELETE BOOK";
cout << "\n\n\t11- BACK TO MAIN MENU";
cout << "\n\n\tENTER YOUR CHOICE: ";
cin >> choice;
switch (choice) {
case 1:
system("cls");
new_student();
break;
case 2:
system("cls");
student_list();
break;
case 3:
long admissionNumber;
system("cls");
cout << "\n\n\t\t** STUDENT-DETAILS ** \n\n";
cout << "\n\nADMISSION NUMBER: ";
cin >> admissionNumber;
fflush(stdin);
display_student(admissionNumber);
break;
case 4:
system("cls");
update_student_record();
break;
case 5:
system("cls");
remove_student();
break;
case 6:
system("cls");
new_book();
break;
case 7:
system("cls");
book_list();
break;
case 8:
long bookNumber;
system("cls");
cout << "\n\n\t\t** BOOK-DETAILS ** \n\n";
cout << "\n\nBOOK NUMBER: ";
cin >> bookNumber;
fflush(stdin);
display_book(bookNumber);
break;
case 9:
system("cls");
update_book_record();
break;
case 10:
remove_book();
break;
case 11:
return;
default:
cout << "\a";
}
system("cls");
administrator();
}
int main() {
char ch;
do {
system("cls");
cout << "\n\n\t\t\t** LIBRARY MANAGEMENT SYSTEM **\n\n";
cout << "\n\n** MAIN MENU **";
cout << "\n\n\t1- ISSUE BOOK";
cout << "\n\n\t2- RETURN A BOOK";
cout << "\n\n\t3- ADMINISTRATOR\'S MENU";
cout << "\n\n\t4- EXIT";
cout << "\n\n\tCHOICE: ";
cin >> ch;
fflush(stdin);
switch (ch) {
case '1':
system("cls");
issue_book();
break;
case '2':
system("cls");
return_book();
break;
case '3':
system("cls");
administrator();
break;
case '4':
exit(0);
default :
cout << "\a";
}
} while (ch != '4');
return 0;
}