-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
757 lines (733 loc) · 18.8 KB
/
Copy pathmain.cpp
File metadata and controls
757 lines (733 loc) · 18.8 KB
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#include <iostream>
#include <conio.h> //for cin.get() function
#include <stdlib.h> //for system("cls") and exit(0) function
#include <fstream> //for file handling
#include <string> // for manipulating strings
#include <sstream> //for to_string function
#include <cstdlib> //for stoi function
#include <ctime> //for time_t, tm, localtime_s, mktime
using namespace std;
void start(); //function for intro page
void MainMenu(); //function for main menu
void adminMenu();//function for admin menu
void membershipMenu(); //function for membership menu
void adminPassCheck(); //Function to require a password to access admin menu
class book {
string bookName;
string authorName;
string bookID;
string quantity;
fstream file;
public:
void createBook()
{
system("cls");
cout << "\n\n\t\t\t\t\t\t\"NEW BOOK ENTRY\"";
cout << "\n\n\tEnter the name of the book : ";
cin.ignore();
getline(cin, bookName);
cout << "\n\n\tEnter the name of the author : ";
getline(cin, authorName);
cout << "\n\n\tEnter the book ID : ";
getline(cin, bookID);
cout << "\n\n\tEnter the quantity of the book : ";
getline(cin, quantity);
file.open("bookRecords.txt", ios::out | ios::app);
file << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
file.close();
cout << "\n\n\tBook Created...!";
cout << "\n\n\tPress Enter to go back to admin menu";
cin.get();
adminMenu();
}
void showBook() //all books
{
system("cls");
file.open("bookRecords.txt", ios::in);
if (!file)
{
cout << "No records found!";
cout << "\n\n\tPress Enter to go back to admin menu";
cin.ignore();
cin.get();
adminMenu();
return;
}
while (getline(file, bookID, '*'))
{
getline(file, bookName, '*');
getline(file, authorName, '*');
getline(file, quantity, '\n');
cout << "----------------------------------------------";
cout << "\n\n\tBook Name : " << bookName;
cout << "\n\n\tAuthor Name : " << authorName;
cout << "\n\n\tBook ID : " << bookID;
cout << "\n\n\tQuantity : " << quantity;
cout << "\n\n----------------------------------------------";
}
file.close();
cout << "\n\n\tPress Enter to go back to admin menu";
cin.ignore();
cin.get();
adminMenu();
return;
}
void modifyBook()
{
system("cls");
fstream tempFile; //object of fstream
file.open("bookRecords.txt", ios::in);
tempFile.open("temp.txt", ios::out);
bool found = false;
cin.ignore();
cout << "\n\tEnter the id of book you want to modify : ";
string id;
getline(cin, id);
while (getline(file, bookID, '*'))
{
getline(file, bookName, '*');
getline(file, authorName, '*');
getline(file, quantity, '\n');
if (bookID == id) {
found = true;
cout << "\n\n\tEnter the new name of the book : ";
getline(cin, bookName);
cout << "\n\n\tEnter the new name of the author : ";
getline(cin, authorName);
cout << "\n\n\tEnter the new quantity of the book : ";
getline(cin, quantity);
tempFile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
cout << "\n\n\tBook Modified...!";
}
else
{
tempFile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
}
}
file.close();
tempFile.close();
remove("bookRecords.txt");
rename("temp.txt", "bookRecords.txt");
if (!found) {
cout << "\n\n\tBook with this id does not exist in our library";
}
cout << "\n\n\tPress Enter to go back to admin menu";
cin.get();
adminMenu();
}
void deleteBook()
{
system("cls");
cout << "\n\tEnter the id of the book you want to delete: ";
string id;
cin.ignore();
getline(cin, id);
fstream tempFile;
file.open("bookRecords.txt", ios::in);
tempFile.open("temp.txt", ios::out);
bool found = false;
while (getline(file, bookID, '*')) {
getline(file, bookName, '*');
getline(file, authorName, '*');
getline(file, quantity, '\n');
if (bookID == id) {
found = true;
cout << "\n\n\tBook with ID " << id << " has been deleted.";
}
else {
tempFile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
}
}
file.close();
tempFile.close();
remove("bookRecords.txt");
rename("temp.txt", "bookRecords.txt");
if (!found) {
cout << "\n\nBook with this ID does not exist in our library.";
}
cout << "\n\n\tPress Enter to go back to admin menu";
cin.get();
adminMenu();
}
void searchBook()
{
system("cls");
cout << "\n\tEnter the id of the book you want to search: ";
string id;
cin.ignore();
getline(cin, id);
file.open("bookRecords.txt", ios::in);
bool found = false;
while (getline(file, bookID, '*'))
{
getline(file, bookName, '*');
getline(file, authorName, '*');
getline(file, quantity, '\n');
if (bookID == id)
{
cout << "\tBook id : " << bookID;
cout << "\n\tBook name : " << bookName;
cout << "\n\tBook Author : " << authorName;
cout << "\n\tBook Quantity : " << quantity;
found = true;
}
}
if (!found)
{
cout << "\tThis book does not exist in our library";
}
cout << "\n\n\tPress Enter to go back to admin menu";
cin.get();
adminMenu();
}
void issueBook()
{
system("CLS");
file.open("bookRecords.txt", ios::in);
while (getline(file, bookID, '*'))
{
getline(file, bookName, '*');
getline(file, authorName, '*');
getline(file, quantity, '\n');
cout << "----------------------------------------------";
cout << "\n\n\tBook Name : " << bookName;
cout << "\n\n\tAuthor Name : " << authorName;
cout << "\n\n\tBook ID : " << bookID;
cout << "\n\n\tQuantity : " << quantity;
cout << "\n\n----------------------------------------------";
}
file.close();
fstream tempfile;
fstream duedate;
file.open("bookRecords.txt", ios::in);
tempfile.open("temp.txt", ios::out);
duedate.open("dueDates.txt", ios::out | ios::app);
string id;
int newQuantity;
cout << "\n\n\tEnter the ID of book you want to borrow : ";
getline(cin, id);
bool found = false;
while (getline(file, bookID, '*'))
{
getline(file, bookName, '*');
getline(file, authorName, '*');
getline(file, quantity, '\n');
if (bookID == id)
{
newQuantity = stoi(quantity); //built in function present in <cstdlib>
if (newQuantity <= 0)
{
cout << "\n\n\tThis book is currently unavailable";
tempfile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
found = true;
}
else
{
cout << "\n\n\tBook Issued";
newQuantity--;
quantity = to_string(newQuantity); //built in function present in <sstream>
tempfile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
// Add due date
time_t now = time(0);
tm ltm;
localtime_s(<m, &now);
ltm.tm_mday += 14; // 2 weeks due date
mktime(<m);
duedate << bookID << "*" << 1900 + ltm.tm_year << "-" << 1 + ltm.tm_mon << "-" << ltm.tm_mday << endl;
found = true;
}
}
else
{
tempfile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
}
}
file.close();
tempfile.close();
duedate.close();
if (!found)
{
cout << "\n\n\tThere is no book with id : " << id;
}
remove("bookRecords.txt");
rename("temp.txt", "bookRecords.txt");
cout << "\n\n\tPress Enter to go back to main menu ";
cin.get();
MainMenu();
}
void returnBook()
{
system("CLS");
file.open("bookRecords.txt", ios::in);
fstream tempfile;
tempfile.open("temp.txt", ios::out);
string id;
int newQuantity;
cin.ignore();
cout << "\n\n\tEnter the ID of book you want to return : ";
getline(cin, id);
bool found = false;
while (getline(file, bookID, '*'))
{
getline(file, bookName, '*');
getline(file, authorName, '*');
getline(file, quantity, '\n');
if (bookID == id)
{
cout << "\n\n\tBook Collected and made available for next use.";
newQuantity = stoi(quantity);
newQuantity++;
quantity = to_string(newQuantity);
tempfile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
found = true;
}
else
{
tempfile << bookID << "*" << bookName << "*" << authorName << "*" << quantity << endl;
}
}
file.close();
tempfile.close();
remove("bookRecords.txt");
rename("temp.txt", "bookRecords.txt");
if (!found)
{
cout << "\n\n\tThis book is not borrowed from our library ";
}
cout << "\n\n\tPress Enter to go back to main menu ";
cin.get();
MainMenu();
}
void checkOverdueBooks()
{
system("CLS");
fstream duedate;
duedate.open("dueDates.txt", ios::in);
if (!duedate)
{
cout << "\n\n\tNo records found!";
cout << "\n\n\tPress Enter to go back to admin menu";
cin.ignore();
cin.get();
adminMenu();
return;
}
string bookID, dueDate;
bool found = false;
time_t now = time(0);
tm ltm;
localtime_s(<m, &now);
string currentDate = to_string(1900 + ltm.tm_year) + "-" + to_string(1 + ltm.tm_mon) + "-" + to_string(ltm.tm_mday);
while (getline(duedate, bookID, '*'))
{
getline(duedate, dueDate, '\n');
if (dueDate < currentDate)
{
cout << "----------------------------------------------";
cout << "\n\n\tBook ID : " << bookID;
cout << "\n\n\tDue Date : " << dueDate;
cout << "\n\n\tStatus : Overdue";
cout << "\n\n----------------------------------------------";
found = true;
}
}
duedate.close();
if (!found)
{
cout << "\n\n\tNo overdue books found.";
}
cout << "\n\n\tPress Enter to go back to admin menu";
cin.ignore();
cin.get();
adminMenu();
}
}obj;
class member
{
string memberID;
string memberName;
string memberPassword;
fstream file;
public:
void createMember()
{
system("cls");
cout << "\n\n\t\t\t\t\t\t\"NEW MEMBER ENTRY\"";
cout << "\n\n\tEnter the name of member : ";
cin.ignore();
getline(cin, memberName);
cout << "\n\n\tEnter the member's ID: ";
getline(cin, memberID);
cout << "\n\n\tSet password for member : ";
getline(cin, memberPassword);
file.open("memberRecords.txt", ios::out | ios::app);
file << memberID << "*" << memberName << "*" << memberPassword << endl;
file.close();
cout << "\n\n\tMember Created...!";
cout << "\n\n\tPress enter to go back to membership menu";
cin.get();
membershipMenu();
}
void removeMember()
{
system("cls");
string id;
cin.ignore();
cout << "\n\tEnter the id of member you want to remove : ";
getline(cin, id);
fstream tempFile;
file.open("memberRecords.txt", ios::in);
tempFile.open("temp.txt", ios::out);
bool found = false;
while (getline(file, memberID, '*'))
{
getline(file, memberName, '*');
getline(file, memberPassword, '\n');
if (id == memberID)
{
cout << "\n\n\tMember with ID " << id << " Deleted...";
found = true;
}
else
{
tempFile << memberID << "*" << memberName << "*" << memberPassword << endl;
}
}
file.close();
tempFile.close();
remove("memberRecords.txt");
rename("temp.txt", "memberRecords.txt");
if (!found)
{
cout << "\n\n\tThere is no member with ID " << id;
}
cout << "\n\n\tPress enter to go back to membership menu";
cin.get();
membershipMenu();
}
void showMembers()
{
system("cls");
file.open("memberRecords.txt", ios::in);
cin.ignore();
while (getline(file, memberID, '*'))
{
getline(file, memberName, '*');
getline(file, memberPassword, '\n');
cout << "----------------------------------------------";
cout << "\n\n\tMember Name : " << memberName;
cout << "\n\n\tMember ID : " << memberID;
cout << "\n\n----------------------------------------------";
}
file.close();
cout << "\n\n\tPress Enter to go back to membership menu";
cin.get();
membershipMenu();
}
void searchMember()
{
system("CLS");
string id;
cin.ignore();
cout << "\n\tEnter the id of member : ";
getline(cin, id);
file.open("memberRecords.txt", ios::in);
bool found = false;
while (getline(file, memberID, '*'))
{
getline(file, memberName, '*');
getline(file, memberPassword, '\n');
if (memberID == id)
{
cout << "----------------------------------------------";
cout << "\n\n\tMember Name : " << memberName;
cout << "\n\n\tMember ID : " << memberID;
cout << "\n\n----------------------------------------------";
found = true;
}
}
file.close();
if (!found)
{
cout << "\tThe member with ID " << id << " cannot be found";
}
cout << "\n\n\tPress enter to continue";
cin.get();
membershipMenu();
}
void modifyMember()
{
system("cls");
string id;
cin.ignore();
cout << "\n\tEnter the id of member : ";
getline(cin, id);
fstream tempfile;
bool found = false;
tempfile.open("temp.txt", ios::out);
file.open("memberRecords.txt", ios::in);
while (getline(file, memberID, '*'))
{
getline(file, memberName, '*');
getline(file, memberPassword, '\n');
if (memberID == id)
{
cout << "\n\n\tEnter the new name of the member : ";
getline(cin, memberName);
cout << "\n\n\tEnter the new Password for the member : ";
getline(cin, memberPassword);
found = true;
tempfile << memberID << "*" << memberName << "*" << memberPassword << endl;
}
else
{
tempfile << memberID << "*" << memberName << "*" << memberPassword << endl;
}
}
file.close();
tempfile.close();
remove("memberRecords.txt");
rename("temp.txt", "memberRecords.txt");
if (!found)
{
cout << "\n\n\tThere is no member with id " << id;
}
cout << "\n\n\tPress enter to go back to membership menu";
cin.get();
membershipMenu();
}
void memberPassCheck()
{
system("CLS");
string id;
string pass;
int tries = 0;
file.open("memberRecords.txt", ios::in);
cin.ignore();
cout << "\n\n\tEnter Your ID : ";
getline(cin, id);
bool idFound = false;
string foundName, foundPassword;
while (getline(file, memberID, '*'))
{
getline(file, memberName, '*');
getline(file, memberPassword, '\n');
if (id == memberID)
{
idFound = true;
foundName = memberName;
foundPassword = memberPassword;
break;
}
}
file.close();
if (!idFound)
{
cout << "\n\n\tThere is no member with this ID, Contact administration to get yourself registered.";
cout << "\n\n\tPress enter to go back to main menu";
cin.get();
MainMenu();
return;
}
while (tries < 3)
{
cout << "\n\n\tEnter Your Password : ";
getline(cin, pass);
if (pass == foundPassword)
{
cout << "\n\n\tWelcome back..... " << foundName;
cout << "\n\n\tPress enter to continue";
cin.get();
obj.issueBook();
return;
}
else
{
tries++;
cout << "\n\n\tInvalid Password " << 3 - tries << " Tries left";
}
}
cout << "\n\n\tToo many failed attempts press enter to go back to main menu";
cin.get();
MainMenu();
}
}abc;
int main()
{
start();
MainMenu();
}
void start()
{
cout << "\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\tLibrary";
cout << "\n\n\n\t\t\t\t\t\t\tManagement";
cout << "\n\n\n\t\t\t\t\t\t\tSystem\n\n\n";
cout << "\n\nPress Enter key to continue";
cin.get(); //To pause the program until enter is pressed
}
void MainMenu()
{
system("cls"); //Function to clear screen
int choice;
do
{
cout << "\n\n\tPress 1 to issue a book : ";
cout << "\n\n\tPress 2 to return a book : ";
cout << "\n\n\tPress 3 for admistrator menu : ";
cout << "\n\n\tPress 4 to quit the program : ";
cout << "\n\n\tEnter Your Choice : (1-4) : ";
cin >> choice; //stores the value given by user
if (choice == 1)
{
abc.memberPassCheck();
}
else if (choice == 2)
{
obj.returnBook();
}
else if (choice == 3)
{
adminPassCheck();
}
else if (choice == 4)
{
cout << "\n\n\tBye..!";
exit(0);
}
else
{
cin.clear();
cin.ignore(1000, '\n');
cout << "\n\n\tInvalid Choice,Try again.";
}
} while (choice != 4); //loop for multiple iterations.
}
void adminPassCheck()
{
system("CLS");
string adminPass = "adminadmin";
string pass;
int tries = 0;
cin.ignore();
abc: //label
if (tries < 3)
{
cout << "\n\tEnter The password for admin menu : ";
getline(cin, pass);
if (pass == adminPass)
{
cout << "\n\n\tAccess Granted ";
cout << "\n\n\tPress Enter to continue to admin menu ";
cin.get();
adminMenu();
}
else
{
tries++;
cout << "\n\n\tInvalid Password, " << 3 - tries << " Tries left";
goto abc;
}
}
else
{
cout << "\n\n\tToo many invalid attempts Quitting the system, Try again later" << endl << endl << endl;
exit(0);
}
}
void adminMenu()
{
system("cls");
int choice2;
cout << "\n\n\t\t\t\t\t\t\"ADMINISTRATOR MENU\"";
x:
cout << "\n\n\t1.Add a new book";
cout << "\n\n\t2.Remove an existing book";
cout << "\n\n\t3.Display all books";
cout << "\n\n\t4.Search for a specific book";
cout << "\n\n\t5.Modify a book";
cout << "\n\n\t6.Membership Control menu";
cout << "\n\n\t7.Check overdue books";
cout << "\n\n\t8.Back to main menu";
cout << "\n\n\tEnter your choice (1-8) : ";
cin >> choice2;
switch (choice2)
{
case 1:
obj.createBook();
break;
case 2:
obj.deleteBook();
break;
case 3:
cout << "\n\n\t\t\t\t\t\t\"BOOK DETAILS\"";
obj.showBook();
break;
case 4:
obj.searchBook();
break;
case 5:
obj.modifyBook();
break;
case 6:
membershipMenu();
break;
case 7:
obj.checkOverdueBooks();
break;
case 8:
MainMenu();
break;
default:
cin.clear();
cin.ignore(1000, '\n');
cout << "\n\n\tInvalid Choice,Try again.";
goto x;
break;
}
}
void membershipMenu()
{
system("cls");
int choice3;
cout << "\n\n\t\t\t\t\t\t\"MEMBERSHIP MENU\"";
x:
cout << "\n\n\t1.Add a new member";
cout << "\n\n\t2.Remove an existing member";
cout << "\n\n\t3.Display all members";
cout << "\n\n\t4.Search for a specific member";
cout << "\n\n\t5.Modify a member"; //overdue banana ha bhai baad me :) xD lol ngl fr
cout << "\n\n\t6.Back to admin menu";
cout << "\n\n\t7.Back to main menu";
cout << "\n\n\tEnter your choice (1-7) : ";
cin >> choice3;
switch (choice3)
{
case 1:
abc.createMember();
break;
case 2:
abc.removeMember();
break;
case 3:
abc.showMembers();
break;
case 4:
abc.searchMember();
break;
case 5:
abc.modifyMember();
break;
case 6:
adminMenu();
break;
case 7:
MainMenu();
break;
default:
cin.clear();
cin.ignore(1000, '\n');
cout << "\n\n\tInvalid Choice,Try again.";
goto x;
break;
}
}