-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1207 lines (1144 loc) · 39.3 KB
/
main.cpp
File metadata and controls
1207 lines (1144 loc) · 39.3 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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<iostream>
#include<vector>
#include<chrono>
#include<fstream>
#include<istream>
#include<ostream>
#include<string>
#include<stdlib.h>
#include<sstream>
#include<utility>
#include<stdexcept>
#include<cmath>
using namespace std;
vector<vector<string>> data_base;
vector<vector<string>> data_base_dup;
vector<string> new_modify;
//Functions for file handling
//referenced from java2blog.com
void readfile(string fname){
vector<string> row;
string line, word;
fstream file (fname,ios::in);
if(file.is_open()){
while(getline(file, line)){
row.clear();
stringstream str(line);
while(getline(str, word, ',')) row.push_back(word);
data_base.push_back(row);
}
}
else std::cout<<"Unable to open the file\n";
file.close();
}
void writefile(vector<vector<string>> par, string fname){
fstream fout(fname,ios::out);
for(auto x:par){
for(auto y:x){
fout<<y;
if(y!=x.back()) fout<<",";
}
fout<<"\n";
}
fout.close();
}
void writefileappend(vector<string> par, string fname){
fstream fout(fname,ios::out | ios::app);
for(auto x:par){
fout<<x;
if(x!=par.back()) fout<<",";
}
fout<<"\n";
fout.close();
}
void printdata(vector<vector<string>> par){
int c=1;
for(auto x:par){
std::cout<<c<<". ";
for(auto y:x){
std::cout<<y;
if(y!=x.back()) std::cout<<" | ";
}
c++;
std::cout<<"\n";
}
}
void print_notpass(vector<vector<string>> par){
int c=1;
for(auto x:par){
std::cout<<c<<". ";
std::cout<<x[0]<<" | "<<x[1]<<" | "<<x[3]<<'\n';
c++;
}
}
//Starting of objects
class Car {
public:
//Characteristics
string model;
string condition;
string amount;
string n_avail; //0 if the car is not available and 1 if the car is available
//Functions
void add_car(string model);
void update_car(string model);
void delete_car(string model);
void rent_req(string model, string id, int user_goodness_value,string type_user);
void show_ddate(string model);
};
class User{
private:
string password;
public:
//characteristics
string name;
string id;
vector<string> cars_rented;
string fine_due;
string user_type;
// 0 is the type of customer, 1 is the type of employee, 2 is the type of manager
int user_goodness_value=50;
// the user value is the measure of the goodness index of the user and decides the number of cars he can rent.
//Functions
void display_menu();
void registration();
string username();
void login();
void see_all_cars(string id);
void logout();
void rent_car(string model, string name,int user_goodness_value,string user_type);
void return_car(string id, string model);
void check_available(string id,string model);
int calc_fine(string id, string user_type,float amount);
void clear_fine_amount(string id, string user_type);
void User_add(string id);
void User_update(string id);
void User_delete(string id);
void see_rented_cars(string id); //cars rented by the user
};
class Cust : public User {
public:
string fine;
void display_menu_cust(string id);
};
class Employee : public User {
public:
string fine;
void display_menu_em(string id);
};
class Manager : public User {
public:
void display_menu_manager(string id);
void show_all_users(string id);
void show_all_cars(string id);
void add_user(string id);
void update_user(string id);
void delete_user(string id);
void add_car(string model);
void update_car(string model);
void delete_car(string model);
void show_rented_user(string id, string user_id);
void show_rented_car(string model);
// void show_rented_cars_user(string id, string model);
};
//All classes display menus
void Cust::display_menu_cust(string id){
std::cout<< "********************************************************************\n";
std::cout<<"\n";
std::cout<<"Welcome Customer\n";
std::cout<<"Select 1 to view all the cars\n";
std::cout<<"Select 2 to view the cars rented by you\n";
std::cout<<"Select 3 to check if a car is available\n";
std::cout<<"Select 4 to rent a car\n";
std::cout<<"Select 5 to return your rented car\n";
std::cout<<"Select 6 to logout\n";
std::cout<< "********************************************************************\n";
int count;
cin >> count;
string model;
Cust c;
Car t;
switch(count){
case 1:
c.see_all_cars(id);
c.display_menu_cust(id);
break;
case 2:
c.see_rented_cars(id);
c.display_menu_cust(id);
break;
case 3:
std::cout << "Enter the model of the car please\n";
cin.ignore();
getline(cin,model);
c.check_available(id,model);
c.display_menu_cust(id);
break;
case 4:
std::cout << "Enter the model of the car please\n";
cin.ignore();
getline(cin,model);
c.rent_car(model,id,c.user_goodness_value,"0");
c.display_menu_cust(id);
break;
case 5:
std::cout << "Enter the model of the car please\n";
cin.ignore();
getline(cin,model);
c.return_car(id, model);
c.display_menu_cust(id);
break;
case 6:
c.logout();
break;
}
}
void Employee::display_menu_em(string id){
std::cout<< "********************************************************************\n";
std::cout<<"\n";
std::cout<<"Welcome Employee\n";
std::cout<<"Select 1 to view all the cars\n";
std::cout<<"Select 2 to view the cars rented by you\n";
std::cout<<"Select 3 to check if a car is available\n";
std::cout<<"Select 4 to rent a car\n";
std::cout<<"Select 5 to return your rented car\n";
std::cout<<"Select 6 to logout\n";
std::cout<< "********************************************************************\n";
int count;
cin >> count;
string model;
Employee c;
Car t;
switch(count){
case 1:
c.see_all_cars(id);
c.display_menu_em(id);
break;
case 2:
c.see_rented_cars(id);
c.display_menu_em(id);
break;
case 3:
std::cout << "Enter the model of the car please\n";
cin.ignore();
getline(cin,model);
c.check_available(id,model);
c.display_menu_em(id);
break;
case 4:
std::cout << "Enter the model of the car please\n";
cin.ignore();
getline(cin,model);
c.rent_car(model,id,c.user_goodness_value,"1");
c.display_menu_em(id);
break;
case 5:
std::cout << "Enter the model of the car please\n";
cin.ignore();
getline(cin,model);
c.return_car(id, model);
c.display_menu_em(id);
break;
case 6:
c.logout();
break;
}
}
void Manager :: display_menu_manager(string id){
std::cout<< "********************************************************************\n";
std::cout<<"\n";
std::cout<<"Welcome Manager\n";
std::cout<<"Choose 1 to add a user\n";
std::cout<<"Choose 2 to update a user\n";
std::cout<<"Choose 3 to delete a user\n";
std::cout<<"Choose 4 to add a car\n";
std::cout<<"Choose 5 to update a car\n";
std::cout<<"Choose 6 to delete a car\n";
std::cout<<"Choose 7 to see the status of the car\n";
std::cout<<"Choose 8 to show due date of a car\n";
std::cout<<"Choose 9 to see all cars rented to a particular user\n";
std::cout<<"Choose u to view all users\n";
std::cout<<"Choose c to view all cars\n";
std::cout<<"Choose l to logout\n";
std::cout<< "********************************************************************\n";
char c;
cin>>c;
Manager m;
Car b;
User u;
string user_id;
string model;
switch(c){
case '1':
u.User_add(id);
m.display_menu_manager(id);
break;
case '2':
u.User_update(id);
m.display_menu_manager(id);
break;
case '3':
u.User_delete(id);
m.display_menu_manager(id);
break;
case '4':
b.add_car(id);
m.display_menu_manager(id);
break;
case '5':
b.update_car(id);
m.display_menu_manager(id);
break;
case '6':
b.delete_car(id);
m.display_menu_manager(id);
break;
case '7':
std::cout<<"Enter the model of the car : ";
cin.ignore();
getline(cin,model);
m.show_rented_car(model);
m.display_menu_manager(id);
break;
case '8':
std::cout<<"Enter the model of the car : ";
cin>>model;
b.show_ddate(model);
m.display_menu_manager(id);
break;
case '9':
std::cout<<"Enter the id of the user : ";
cin>>user_id;
m.show_rented_user(id,user_id);
m.display_menu_manager(id);
break;
case 'u':
m.show_all_users(id);
m.display_menu_manager(id);
break;
case 'c':
m.show_all_cars(id);
m.display_menu_manager(id);
break;
case 'l':
logout();
m.display_menu_manager(id);
break;
}
}
void User :: display_menu(){
std::cout << "░██╗░░░░░░░██╗███████╗██╗░░░░░░█████╗░░█████╗░███╗░░░███╗███████╗ ████████╗░█████╗░\n";
std::cout << "░██║░░██╗░░██║██╔════╝██║░░░░░██╔══██╗██╔══██╗████╗░████║██╔════╝ ╚══██╔══╝██╔══██╗\n";
std::cout << "░╚██╗████╗██╔╝█████╗░░██║░░░░░██║░░╚═╝██║░░██║██╔████╔██║█████╗░░ ░░░██║░░░██║░░██║\n";
std::cout << "░░████╔═████║░██╔══╝░░██║░░░░░██║░░██╗██║░░██║██║╚██╔╝██║██╔══╝░░ ░░░██║░░░██║░░██║\n";
std::cout << "░░╚██╔╝░╚██╔╝░███████╗███████╗╚█████╔╝╚█████╔╝██║░╚═╝░██║███████╗ ░░░██║░░░╚█████╔╝\n";
std::cout << "░░░╚═╝░░░╚═╝░░╚══════╝╚══════╝░╚════╝░░╚════╝░╚═╝░░░░░╚═╝╚══════╝ ░░░╚═╝░░░░╚════╝░\n";
std::cout << "\n\n";
std::cout << "░█████╗░░█████╗░██████╗░ ░█████╗░ ███╗░░░███╗░█████╗░████████╗██████╗░██╗██╗░░██╗\n";
std::cout << "██╔══██╗██╔══██╗██╔══██╗ ██╔══██╗ ████╗░████║██╔══██╗╚══██╔══╝██╔══██╗██║╚██╗██╔╝\n";
std::cout << "██║░░╚═╝███████║██████╔╝ ███████║ ██╔████╔██║███████║░░░██║░░░██████╔╝██║░╚███╔╝░\n";
std::cout << "██║░░██╗██╔══██║██╔══██╗ ██╔══██║ ██║╚██╔╝██║██╔══██║░░░██║░░░██╔══██╗██║░██╔██╗░\n";
std::cout << "╚█████╔╝██║░░██║██║░░██║ ██║░░██║ ██║░╚═╝░██║██║░░██║░░░██║░░░██║░░██║██║██╔╝╚██╗\n";
std::cout << "░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝ ╚═╝░░╚═╝ ╚═╝░░░░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚═╝░░╚═╝╚═╝╚═╝░░╚═╝\n";
char c;
std::cout<< "********************************************************************\n";
std::cout<<"1. Choose 1 to enter the Car Matrix\n";
std::cout<<"2. Choose 2 to register\n";
std::cout<<"3. Choose 3 to exit\n";
std::cout<< "********************************************************************\n";
cin>>c;
if(c=='1'){ // User enter
User u;
u.login();
}
if(c=='2'){
User u;
u.registration();
}
else{
std::cout<<"Good Bye, hope you enjoyed the matrix !\n";
exit(1);
}
}
string User::username(){
int counter=0;
string user_id;
User u;
std::cout<<"Please enter the user id : ";
cin>> user_id;
readfile("all_users_data.csv");
for(int i=0;i<data_base.size();i++){
if (data_base[i][1]==user_id){
counter++;
}
}
if(counter==0){
std::cout << "User ID is available and is successfully assigned !\n";
}
else{
std::cout<<"User ID is already taken.";
user_id=u.username();
}
return user_id;
}
void User::registration(){
User u;
string c;
string name,user_id,password,user_type;
int counter=0;
data_base.clear();
std::cout<<"Thank you for choosing Car-A-Matrix\n";
std::cout<<"Enter the Matrix as a :- \n";
std::cout<<"0. Choose 0 as a customer\n";
std::cout<<"1. Choose 1 as an employee.\n";
std::cout<<"2. Choose 2 as a manager.\n";
cin>>c;
if(c=="0"){
user_type="0";
std::cout<<"Welcome Customer !\n";
std::cout<<"Please enter your name :";
}
else if(c=="1"){
user_type="1";
std::cout<<"Welcome Employee !\n";
std::cout<<"Please enter your name :";
}
else if(c=="2"){
user_type="2";
std::cout<<"Welcome Manager !\n";
std::cout<<"Please enter your name :";
}
else{
std::cout<<"Don't mess with the Matrix. It may devour you !!\n";
std::cout<<"Please enter valid details.\n";
u.registration();
}
cin.ignore();
getline(cin,name);
user_id=u.username();
std::cout<<"Please enter your password : ";
cin.ignore();
getline(cin,password);
new_modify.clear();
new_modify.push_back(name);
new_modify.push_back(user_id);
new_modify.push_back(password);
new_modify.push_back(user_type);
new_modify.push_back("50");
writefileappend(new_modify,"all_users_data.csv");
if(c=="0"){
std::cout<<"You have been successfully registered as a customer !\n";
writefileappend(new_modify,"all_customers_data.csv");
}
else if(c=="1"){
std::cout<<"You have been successfully registered as an employee !\n";
writefileappend(new_modify,"all_employees_data.csv");
}
else if(c=="2"){
std::cout<<"You have been successfully registered as a Manager !\n";
writefileappend(new_modify,"all_managers_data.csv");
}
u.display_menu();
}
void User :: login(){
string id;
string password;
std::cout<<"Enter your id : ";
cin>>id;
std::cout<<"Enter the password : ";
cin>>password;
vector<string> word_row;
string line_csv,word;
fstream file("all_users_data.csv",ios::in);
int count=0;
if(file.is_open()){
while(getline(file,line_csv)){
word_row.clear();
stringstream str(line_csv);
while(getline(str,word,',')) word_row.push_back(word);
if(word_row[1]==id){
count=1;
if(word_row[2]==password){
count++;
break;
}
else{
while(password!=word_row[2]){
std::cout<<"You entered the wrong password. Choose 1 to reenter the password and 2 to exit\n";
char c;
cin>>c;
if(c=='1'){
std::cout<<"Enter the password : ";
cin>>password;
if(password==word_row[2]) count++;
}
else if(c=='2'){
std::cout<<"Leaving the Matrix...";
return;
}
else{
std::cout<<"Please enter a valid input.\n";
}
}
if(count==2) break;
}
}
if(count==2) break;
}
if(count==0){
std::cout<<"User not found\n";
User u;
u.display_menu();
}
if(count==2){
Cust c;
Employee e;
Manager m;
if(word_row[3]=="0"){
c.display_menu_cust(word_row[1]);
}
else if(word_row[3]=="1"){
e.display_menu_em(word_row[1]);
}
else{
m.display_menu_manager(word_row[1]);
}
}
}
}
void User :: see_all_cars(string id){
data_base.clear();
std::cout<<"Below, 0 says that the car can be rented and 1 says the cars has already been rented.\n";
readfile("all_cars_data.csv");
printdata(data_base);
data_base.clear();
}
//Finish the seg fault
void User :: rent_car(string model, string id,int user_goodness_value, string type_user){
data_base.clear();
int counter=0;
int count=0;
string amount;
data_base.clear();
readfile("rented_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==id){
count++;
}
}
data_base.clear();
if(count>=user_goodness_value/10){
std::cout<<"You cannot rent more than" <<user_goodness_value/10<<" cars !\n\n";
}
else{
readfile("all_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==model && data_base[i][3]=="0"&& stoi(data_base[i][1])>30){
counter=1;
amount=data_base[i][2];
std::cout<<"Car is available to rent !\n";
data_base[i][3]="1";
new_modify.clear();
new_modify.push_back(id);
new_modify.push_back(model);
new_modify.push_back(data_base[i][2]);
new_modify.push_back(data_base[i][3]);
new_modify.push_back(to_string(time(0)));
writefileappend(new_modify,"rented_cars_data.csv");
break;
}
}
writefile(data_base,"all_cars_data.csv");
data_base.clear();
readfile("all_users_data.csv");
string user_type;
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==id){
user_type=data_base[i][3];
}
}
if(counter==1){
if(user_type=="0"){
std::cout<<"Car succesfully rented !\n";
std::cout<<"The rental period of the car is 5 days\n";
std::cout<<"Please pay a rental fee of "<<ceil(stof(amount))<<"Rs. to the manager.\n";
}
if(user_type=="1"){
std::cout<<"Car succesfully rented with a 15 percent discount !\n";
std::cout<<"The rental period of the car is 7 days\n";
std::cout<<"Please pay a rental fee of "<<ceil(stof(amount)*0.85)<<"Rs. to the manager.\n ";
}
}
if(counter==0) std::cout<<"Car not available !\n";
data_base.clear();
}
}
void User :: return_car(string id, string model){
data_base.clear();
int counter=0;
float amount;
string user_type;
readfile("all_users_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==id){
user_type=data_base[i][3];
}
}
data_base.clear();
readfile("rented_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==model && data_base[i][0]==id){
counter=1;
data_base.erase(data_base.begin()+i,data_base.begin()+i+1);
std::cout<<"This car was rented and is now being returned.\n";
writefile(data_base,"rented_cars_data.csv");
data_base.clear();
readfile("all_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==model){
data_base[i][3]="0";
data_base[i][1]=to_string(stoi(data_base[i][1])-10);
amount=stof(data_base[i][2]);
}
}
writefile(data_base,"all_cars_data.csv");
break;
}
}
if(counter==0) std::cout<<"Invalid details!\n";
data_base.clear();
if(counter!=0){
calc_fine(id,user_type,amount);
}
}
void User :: see_rented_cars(string id){
int count=0;
data_base.clear();
readfile("rented_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==id){
count++;
std::cout<<count<<". ";
for(int k=0;k<data_base[i].size()-1;k++)
std::cout<<data_base[i][k]<<" | ";
time_t time = stoi(data_base[i][4]);
tm *dateformat = localtime(&time);
std::cout<< (dateformat->tm_mday)<<"/"<<1 + dateformat->tm_mon<<"/"<<1900 + dateformat->tm_year<<"\n";
}
}
if(count==0) std::cout<<"You have no rented cars !\n\n";
data_base.clear();
}
void User :: check_available(string id,string model){
data_base.clear();
int counter=0;
readfile("all_cars_data.csv");
int count=1;
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==model && data_base[i][3]=="0"){
counter=1;
std::cout<<count<<". ";
count++;
if(stoi(data_base[i][1])>30){
std::cout<<"Car can be rented !\n";
for(auto j:data_base[i])
std::cout<<j<<" | ";
std::cout<<"\n";
break;
}
else{
std::cout<<"The Car connot be rented due to its poor condition.\n";
}
}
}
if(counter==0) std::cout<<"Car cannot be rented or is not available.\n";
data_base.clear();
}
int User :: calc_fine(string id, string user_type, float amount){
int fine=0;
int user_goodness_value_decrement=0;
int curtime;
int isstime;
if(user_type=="0"){
data_base.clear();
readfile("rented_cars_data.csv");
// cout << "doing0" << endl;
// cout << data_base.size() << " " << endl;
for(int i=0;i<data_base.size();i++){
// cout << i << " " << endl;
if(data_base[i][0]==id){
curtime = time(0);
isstime = stoi(data_base[i][4]);
user_goodness_value_decrement=((curtime-isstime)/86400-5);
if(((curtime-isstime)/86400)>5) {
fine+=((curtime-isstime)/86400 - 5)*(amount)/4;
}
// cout << curtime << " " << isstime << " " << fine << " " << endl;
}
}
}
if(user_type=="1"){
data_base.clear();
readfile("rented_cars_data.csv");
// cout << "doing1" << endl
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==id){
int curtime = time(0);
int isstime = stoi(data_base[i][4]);
user_goodness_value_decrement=((curtime-isstime)/86400-7);
if((curtime-isstime)/86400>7) {
fine+=2*((curtime-isstime)/86400 - 7)*(amount)/4;
}
}
}
}
data_base.clear();
readfile("all_users_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==id){
data_base[i][4]=to_string(stoi(data_base[i][4])-user_goodness_value_decrement);
if(stoi(data_base[i][4])>100){
data_base[i][4]="100";
}
if(stoi(data_base[i][4])<10){
data_base[i][4]="10";
}
}
}
if(user_goodness_value_decrement==0){
std::cout<<"The way the matrix thinks of you doesn't change !\n";
}
else if(user_goodness_value_decrement<0){
std::cout<<"The matrix thinks more positively of you !\n";
}
else{
std::cout<<"Your status in the eyes of Matrix falls further !\n";
}
writefile(data_base,"all_users_data.csv");
data_base.clear();
readfile("all_users_data.csv");
data_base_dup.clear();
for(int i=0;i<data_base.size();i++){
if(user_type==data_base[i][3]){
data_base_dup.push_back(data_base[i]);
}
}
if(user_type=="0"){
writefile(data_base_dup,"all_customers_data.csv");
}
if(user_type=="1"){
writefile(data_base_dup,"all_employees_data.csv");
}
if(user_type=="2"){
writefile(data_base_dup,"all_managers_data.csv");
}
std::cout<<"Your fine is : "<<fine<<"\n";
data_base.clear();
return fine;
}
void User :: logout(){
std::cout<<"Leaving the Matrix...\n";
User u;
u.display_menu();
}
//Manager Functions
void User :: User_add(string id){
Manager m;
m.add_user(id);
}
void User :: User_delete(string id){
Manager m;
m.delete_user(id);
}
void User :: User_update(string id){
Manager m;
m.update_user(id);
}
//A normal customer can rent for 5 days whereas an employee can rent for 7 days
void Car :: show_ddate(string model){
data_base.clear();
string user_id;
time_t time;
readfile("rented_cars_data.csv");
int counter=0;
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==model){
counter=1;
user_id = data_base[i][0];
time = stoi(data_base[i][4]);
break;
}
}
data_base.clear();
int days=30;
readfile("all_users_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==user_id){
if(data_base[i][3]=="0")
days=5;
else
days=7;
break;
}
}
time+=days*86400;
tm *due_time = localtime(&time);
if(counter==0) std::cout<<"Car was not rented!\n";
else{
std::cout<<"Due date of the car is : ";
std::cout<< due_time->tm_mday<<"/"<<1 + due_time->tm_mon<<"/"<<1900 + due_time->tm_year<<"\n";
}
}
void Car :: rent_req(string model, string id, int user_goodness_value,string type_user ){
User u;
u.rent_car(model, id, user_goodness_value,type_user);
}
void Car :: add_car(string id){
Manager m;
m.add_car(id);
}
void Car :: delete_car(string id){
Manager m;
m.delete_car(id);
}
void Car :: update_car(string id){
Manager m;
m.update_car(id);
}
void Manager :: show_all_users(string id){
data_base.clear();
readfile("all_users_data.csv");
std::cout<<"The following is the list of all the users.\n\n";
std::cout<<"The data is given in the form of name, id and the type of the user.\n\n";
std::cout<<"0 at the end represents a normal customer, 1 represent an employee and 2 represents a manager.\n";
print_notpass(data_base);
data_base.clear();
}
void Manager :: show_all_cars(string id){
data_base.clear();
readfile("all_cars_data.csv");
std::cout<<"The following is the list of all the cars\n\n";
std::cout<<"The data is given in the format of model, condition, amount and availability of the car where 1 means already rented.\n\n";
std::cout<<"The condition ranges from 0 to 100 and you cannot rent a car with condition below 35.\n\n";
printdata(data_base);
data_base.clear();
}
void Manager :: add_user(string id){
std::cout<<"Enter the details of the new user : \n";
string name,user_id,password,user_type;
int user_goodness_value;
std::cout<<"Enter the name: ";
cin.ignore();
getline(cin,name);
user_id=username();
std::cout<<"Enter the password : ";
cin>>password;
std::cout<<"Enter the type of the user : 0 for customer, 1 for employee, 2 for manager : ";
cin>>user_type;
user_goodness_value=50;
fstream fout("all_users_data.csv",ios::out | ios::app);
fout<<name<<","<<user_id<<","<<password<<","<<user_type<<","<<user_goodness_value<<'\n';
if(user_type=="0"){
fstream fout("all_customers_data.csv",ios::out | ios::app);
fout<<name<<","<<user_id<<","<<password<<","<<user_type<<","<<user_goodness_value<<'\n';
}
else if(user_type=="1"){
fstream fout("all_employees_data.csv",ios::out | ios::app);
fout<<name<<","<<user_id<<","<<password<<","<<user_type<<","<<user_goodness_value<<'\n';
}
else if(user_type=="2"){
fstream fout("all_managers_data.csv",ios::out | ios::app);
fout<<name<<","<<user_id<<","<<password<<","<<user_type<<","<<user_goodness_value<<'\n';
}
std::cout<<"New user has been registered.\n";
}
void Manager :: add_car(string id){
std::cout<<"Please enter the details of the new car : \n";
string model,condition,amount;
std::cout<<"Enter the model of the car: ";
cin.ignore();
getline(cin,model);
std::cout<<"Enter the condition of the car : ";
cin>>condition;
std::cout<<"Enter the rental price of the car : ";
cin>>amount;
fstream fout("all_cars_data.csv",ios::out | ios::app);
fout<<model<<","<<condition<<","<<amount<<",0"<<'\n';
std::cout<<"New car has been added to our database.\n";
}
void Manager :: delete_car(string id){
string model;
std::cout<<"Enter the model of the car : ";
cin>>model;
data_base.clear();
int found=0;
readfile("all_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==model){
found=1;
data_base.erase(data_base.begin()+i,data_base.begin()+i+1);
break;
}
}
writefile(data_base,"all_cars_data.csv");
data_base.clear();
readfile("rented_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==model){
data_base.erase(data_base.begin()+i,data_base.begin()+i+1);
break;
}
}
writefile(data_base,"rented_cars_data.csv");
data_base.clear();
if(found==0) std::cout<<"Car not found in the database.\n";
else{
std::cout<<"The Car has been removed from the database.\n";
}
}
void Manager :: delete_user(string id){
string user_id;
string user_type;
std::cout<<"Enter the user id of the user : ";
cin>>user_id;
data_base.clear();
int found=0;
readfile("all_users_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][1]==user_id){
found=1;
user_type=data_base[i][3];
data_base.erase(data_base.begin()+i,data_base.begin()+i+1);
break;
}
}
writefile(data_base,"all_users_data.csv");
data_base.clear();
vector<string> model;
readfile("rented_cars_data.csv");
for(int i=0;i<data_base.size();i++){
if(data_base[i][0]==user_id){
model.push_back(data_base[i][1]);
data_base.erase(data_base.begin()+i);
break;
}
}
writefile(data_base,"rented_cars_data.csv");
data_base.clear();
readfile("all_cars_data.csv");
for(int i=0;i<data_base.size();i++){
for(int j=0;j<model.size();j++){
if(model[j]==data_base[i][0]){
data_base[i][3]="0";
}
}
}
writefile(data_base,"all_cars_data.csv");
data_base.clear();
readfile("all_users_data.csv");
data_base_dup.clear();
for(int i=0;i<data_base.size();i++){
if(user_type==data_base[i][3]){
data_base_dup.push_back(data_base[i]);
}