-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary_Management.cpp
More file actions
705 lines (657 loc) · 14.1 KB
/
Library_Management.cpp
File metadata and controls
705 lines (657 loc) · 14.1 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
#include<bits/stdc++.h>
using namespace std;
int days_in_month_arr[12]={31,28,31,30,31,30,31,31,30,31,30,31};
class User;
void space(int n)
{
for (int i=0;i<n;i++)
{
cout<<" ";
}
cout<<"|";
}
struct Time {
int d, m, y;
};
int days_in_month(int month,int year)
{
if(month==1)
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
{
return 29;
}
else
return 28;
}
else
return 29;
}
}
return days_in_month_arr[month];
}
Time* add_days(int days,Time date)
{
Time* due_date=(Time*)malloc(sizeof(Time));
int d,m,y;
due_date->d=date.d+days;
due_date->m=date.m;
due_date->y=date.y;
while(due_date->d>days_in_month(date.m-1,date.y))
{
due_date->d=due_date->d-days_in_month(date.m-1,date.y);
due_date->m=date.m+1;
if(due_date->m>12)
{
due_date->m=1;
due_date->y++;
}
}
return due_date;
}
#include"book.h"
#include"user.h"
int User::give_due_time()
{
if(Professor * u = dynamic_cast<Professor *>(this))
return 60;
else if (Student * u = dynamic_cast<Student*>(this))
return 30;
}
void Book::book_request(User* user)
{
int a;
if(Professor * u = dynamic_cast<Professor *>(user))
u->list_books.push_back(this);
else if (Student * u = dynamic_cast<Student*>(user))
{
if (u->list_books.size()>=5)
{
cout<<"You Already have Borrowed 5 Books :("<<endl;
return;
}
else
u->list_books.push_back(this);
}
this->issued_by=user->id;
time_t now=time(0);
tm *ltm = localtime(&now);
this->issue_time=mktime(ltm);
Time t={ltm->tm_mday,ltm->tm_mon+1,ltm->tm_year+1900};
this->issue_date=t;
return;
}
User_databse users;
Book_database books;
User * cur_user;
void Professor::clear_fine_amount()
{
this->fine_amt=0;
for(auto itr :this->list_books)
{
time_t now=time(0);
// time_t cur=(time_t)(10);
int difference= now-itr->issue_time;
if (difference/(24*60*60)>60)
{
itr->return_book();
}
}
}
void Student::clear_fine_amount()
{
this->fine_amt=0;
for(auto itr :this->list_books)
{
time_t now=time(0);
// time_t cur=(time_t)(10);
int difference= now-itr->issue_time;
if (difference/(24*60*60)>60)
{
itr->return_book();
}
}
}
void Book::return_book()
{
User* user=users.search(this->issued_by);
int i=0;
if (user->post=="Professor")
{
Professor* p=dynamic_cast<Professor*>(user);
for(auto itr : p->list_books)
{
if (itr->isbn==this->isbn)
{
p->list_books.erase(p->list_books.begin()+i);
break;
}
i++;
}
}
else
{
user=users.search(this->issued_by);
Student* s=dynamic_cast<Student*>(user);
for(auto itr : s->list_books)
{
if (itr->isbn==this->isbn)
{
s->list_books.erase(s->list_books.begin()+i);
break;
}
i++;
}
}
Time t={1,1,1};
this->issue_date=t;
this->issued_by="-1";
cout<<"Book "<<this->isbn<< " Returned"<<endl;
return;
}
Time Book::show_due_date()
{
User * user = users.search(this->issued_by);
int days=user->give_due_time();
Time *due_date= add_days(days,this->issue_date);
return *(due_date);
}
string get_login()
{
string password;
string id;
cout<<"Enter Your ID: ";
cin>>id;
cout<<"Enter your Password: ";
cin>>password;
cur_user=users.search(id);
if (cur_user==NULL)
return "logged out";
else
return cur_user->post;
}
void display_stud_menu(User* cur)
{
cout<<"WElCOME "<<cur->name<<endl;
cout<<"1: See all Books."<<endl;
cout<<"2: List issued Books."<<endl;
cout<<"3: Check availibilty of books."<<endl;
cout<<"4: Return Book."<<endl;
cout<<"5: Check dues"<<endl;
cout<<"6: Logout\n";
cout<<"Enter choice : ";
}
void display_prof_menu(User* cur)
{
cout<<"WElCOME "<<cur->name<<endl;
cout<<"1: See all Books."<<endl;
cout<<"2: List issued Books."<<endl;
cout<<"3: Check availibilty of books."<<endl;
cout<<"4: Return Book."<<endl;
cout<<"5: Check dues"<<endl;
cout<<"6: Logout\n";
cout<<"Enter choice : ";
}
void display_lib_menu(User* cur)
{
cout<<"WElCOME "<<cur->name<<endl;
cout<<"1: Display All Books in Library."<<endl;
cout<<"2: List issued Books by User."<<endl;
cout<<"3: Modify Databases"<<endl;
cout<<"4: Display Database"<<endl;
cout<<"5: Clear Dues for User"<<endl;
cout<<"6: Show Due Date for a Book"<<endl;
cout<<"7: Logout\n";
cout<<"Enter choice : ";
}
void modify_books(User* user)
{
system("cls");
cout<<"Welcome "<<user->name<<endl;
cout<<"1. Add Books."<<endl;
cout<<"2. Update Book Details."<<endl;
cout<<"3. Delete Book."<<endl;
cout<<"Enter choice: ";
char choice;
cin>>choice;
if (choice=='1')
{
string title,author,publication,isbn;
cout<<"Enter Book name: ";
getline(cin>>ws,title);
cout<<"Enter Author name: ";
getline(cin>>ws,author);
cout<<"Enter Name of Publication: ";
getline(cin>>ws,publication);
cout<<"Enter 13-digit ISBN code: ";
cin>>isbn;
books.add(title,author,publication,isbn,"-1",(time_t)(0));
}
else if (choice=='2')
{
string isbn;
cout<<"Enter 13-digit ISBN code: ";
cin>>isbn;
Book *b;
b=books.search(isbn);
string title,author,publication;
cout<<"Enter Book name: ";
getline(cin>>ws,title);
cout<<"Enter Author name: ";
getline(cin>>ws,author);
cout<<"Enter Name of Publication: ";
getline(cin>>ws,publication);
books.update(title,author,publication,isbn);
}
else if (choice=='3')
{
cout<<"Enter ISBN of the book to be deleted"<<endl;
string isbn;
cin>>isbn;
books.Delete(books.search(isbn));
}
else
modify_books(user);
return;
}
void modify_users(User * user)
{
system("cls");
cout<<"Welcome "<<user->name<<endl;
cout<<"1. Add Users."<<endl;
cout<<"2. Update User Details."<<endl;
cout<<"3. Delete Users."<<endl;
cout<<"4. Go Back to Previous Menu"<<endl;
cout<<"Enter choice: ";
char choice;
cin>>choice;
system("cls");
switch(choice)
{
case '1':
{
string name,password,post;
string id;
cout<<"Enter User name: ";
getline(cin>>ws,name);
cout<<"Enter Designation of User: ";
getline(cin>>ws,post);
cout<<"Enter password: ";
getline(cin>>ws,password);
id=1000001+users.list_users.size()+1;
for (long int i=1000001;i<1000001+users.list_users.size()+1;i++)
{
if (users.search(to_string(i))==NULL)
id=to_string(i);
}
users.add(name,password,post,id);
break;
}
case '2':
{
string name,password,post;
string id;
cout<<"Enter Id: ";
cin>>id;
User * user;
user=users.search(id);
if(user!=NULL)
{
cout<<"Enter User name: ";
getline(cin>>ws,name);
cout<<"Enter new password: ";
getline(cin>>ws,password);
cout<<"Enter Designation of the user: ";
getline(cin>>ws,post);
users.update(name,password,post,id);
}
else
{
cout<<"User not found";
modify_users(user);
}
break;
}
case '3':
{
string id;
cout<<"Enter Id of user to be deleted: ";
cin>>id;
users.Delete(id,cur_user);
break;
}
case '4':
{
return;
}
default : modify_users(user);
}
}
void modify_databases(User * user)
{
cout<<"Welcome "<<user->name<<endl;
cout<<"1. Modify User Database."<<endl;
cout<<"2. Modify Book Database."<<endl;
cout<<"3. Go Back to Main Menu."<<endl;
cout<<"Enter choice: ";
char choice;
cin>>choice;
if(choice=='1')
modify_users(user);
else if(choice=='2')
modify_books(user);
else if(choice=='3')
return;
else
modify_databases(user);
return;
}
void check_availibility(User * user)
{
books.display(0);
cout<<"Welcome "<<user->name<<" Enter the id of the Book You wanna check availibility: "<<endl;
string isbn;
cin>>isbn;
Book* b;
b=books.search(isbn);
if (b->issued_by=="-1")
{
char choice;
cout<<"Book Available to Issue , Proceed to Issue Book? (y/n): ";
cin>>choice;
if(choice=='Y' || choice=='y')
{
b->book_request(user);
return;
}
else if (choice=='N' || choice=='n')
{
system("cls");
check_availibility(user);
return;
}
}
}
void read_data()
{
vector<string> row;
string line,word;
fstream file("users.csv", ios::in);
if(file.is_open())
{
while(getline(file, line))
{
row.clear();
stringstream str(line);
while(getline(str, word, ','))
row.push_back(word);
users.add(row[0],row[1],row[2],row[3]);
}
}
file.close();
fstream file2("books.csv", ios::in);
if(file2.is_open())
{
while(getline(file2, line))
{
row.clear();
stringstream str(line);
while(getline(str, word, ','))
row.push_back(word);
books.add(row[0],row[1],row[2],row[3],row[4],(time_t)stoi(row[5]));
}
}
file2.close();
for(auto itr :books.list_books)
{
if (itr->issued_by!="-1")
{
if(users.search(itr->issued_by)->post=="Professor")
{
Professor* prof= dynamic_cast<Professor* >(users.search(itr->issued_by));
prof->list_books.push_back(itr);
}
else
{
Student* stud = dynamic_cast<Student*>(users.search(itr->issued_by));
stud->list_books.push_back(itr);
}
}
}
return;
}
void write_data()
{
ofstream file1;
file1.open("users.csv", ofstream::out | ofstream::trunc);
for(auto itr: users.list_users)
{
file1<<itr->name<<',';
file1<<itr->password<<',';
file1<<itr->post<<',';
file1<<itr->id;
if (itr->id!=users.list_users[users.list_users.size()-1]->id)
file1<<endl;
}
file1.close();
file1.open("books.csv", ofstream::out | ofstream::trunc);
for(auto itr: books.list_books)
{
file1<<itr->title<<',';
file1<<itr->author<<',';
file1<<itr->publication<<',';
file1<<itr->isbn<<',';
file1<<itr->issued_by<<',';
file1<<itr->issue_time;
if (itr->isbn!=books.list_books[books.list_books.size()-1]->isbn)
file1<<endl;
}
file1.close();
}
int main()
{
read_data();
int a;
while(1)
{
cout<<"Welcome User, to Your Library! Here login to get started.\n\n";
string login_status="logged out";
login_status=get_login();
while(1)
{
write_data();
if(login_status!="logged out" && login_status!="logged-out")
{
system("cls");
if(login_status=="Professor")
display_prof_menu(cur_user);
else if (login_status=="Librarian")
display_lib_menu(cur_user);
else
display_stud_menu(cur_user);
int choice;
cin>>choice;
system("cls");
switch(choice)
{
cout<<choice;
case 1: {
books.display();
char c;
cout<<"Enter a character to continue";
cin>>c;
break;
}
case 2: {
if (login_status=="Librarian")
{
string id;
cout<<"Enter id of User: ";
cin>>id;
if (Professor* prof=dynamic_cast<Professor*>(users.search(id)))
{
if (prof->list_books.size()==0)
{
string a;
cout<<"No Books Issued"<<endl;
cout<<"Enter a character to continue.";
cin>>a;
break;
}
prof->display_issued();
}
else
{
Student* stud=dynamic_cast<Student*>(users.search(id));
if (stud->list_books.size()==0)
{
string a;
cout<<"No Books Issued"<<endl;
cout<<"Enter a character to continue.";
cin>>a;
break;
}
stud->display_issued();
}
}
else
{
cur_user->display_issued();
}
char c;
cout<<"Enter a character to continue";
cin>>c;
break;
}
case 3: {
if (login_status=="Librarian")
{
modify_databases(cur_user);
}
else
{
check_availibility(cur_user);
}
char c;
cout<<"Enter a character to continue";
cin>>c;
break;
}
case 4: {
if(login_status=="Librarian")
{
users.display();
books.display(0);
char c;
cin>>c;
}
else
{
string isbn;
if (cur_user->post=="Student")
{
Student* user= dynamic_cast<Student*>(cur_user);
user->display_issued();
}
else
{
Professor * user= dynamic_cast<Professor *>(cur_user);
user->display_issued();
}
cout<<"Enter ISBN of the book:";
cin>>isbn;
Book* book = books.search(isbn);
if (book!=NULL)
books.search(isbn)->return_book();
else
cout<<"Book Not Found\n";
char c;
cout<<"Enter a character to continue";
cin>>c;
break;
}
break;
}
case 5: {
if (cur_user->post!="Librarian")
{
if (cur_user->post=="Student")
{
Student* user= dynamic_cast<Student*>(cur_user);
user->calculate_fine();
cout<<"The Fine amount in your name is : Rs"<<user->fine_amt<<" .";
}
else
{
Professor * user= dynamic_cast<Professor *>(cur_user);
user->calculate_fine();
cout<<"The Fine amount in your name is : Rs"<<user->fine_amt<<" .";
}
char c;
cout<<"Enter a character to continue";
cin>>c;
break;
}
else
{
string id;
cout<<"Enter id of the user to clear fine of :"<<endl;
cin>>id;
User* user=users.search(id);
if (cur_user->post=="Student")
{
Student* stud= dynamic_cast<Student*>(user);
stud->clear_fine_amount();
}
else
{
Professor* prof =dynamic_cast<Professor*>(user);
prof->clear_fine_amount();
}
char c;
cout<<"Enter a character to continue";
cin>>c;
}
break;
}
case 6: {
if (cur_user->post=="Librarian")
{
string id;
cout<<"Enter the ISBN of the Book: ";
cin>>id;
Book* book= books.search(id);
if(book==NULL)
{
cout<<"Book not Found";
break;
}
Time t =book->show_due_date();
if(t.y==1)
{
cout<<"Book Not Issued\n";
}
cout<< t.d<<"/"<<t.m<<"/"<<t.y<<endl;;
char c;
cout<<"Enter a character to continue";
cin>>c;
break;
}
login_status="logged-out";
break;
}
case 7: {
login_status="logged-out";
break;
}
}
}
else
break;
}
}
}