-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlegacy code.cpp
738 lines (674 loc) · 21.7 KB
/
legacy code.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
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
#include<iostream>
#include<fstream>
#include<string>
#include <vector>
using namespace std;
class password {
private:
string x, y;
string decrypt(string password) {
for (int i = 0; i < password.size(); i++) {
password[i] += password.size() + i;
}
return password;
}
string encrypt(string password) {
for (int i = 0; i < password.size(); i++) {
password[i] -= password.size() + i;
}
return password;
}
public:
password(void) {
fstream file;
file.open("password.txt", ios::in);
if (file.is_open()) {
getline(file, y);
y = decrypt(y);
file.close();
}
}
void set_password(string p) {
x = p;
}
bool check_password() {
if (x == y) return false;
return true;
}
bool check_legitpassword(string password) {
bool haslower = true, hasupper = true, hasdigit = true;
if (password.length() < 8) {
cout << "your password must have at least <8> digits.\n";
return false;
}
for (int i = 0; i < password.length(); i++) {
if (islower(password[i]))
haslower = false;
if (isupper(password[i]))
hasupper = false;
if (isdigit(password[i]))
hasdigit = false;
}
if (haslower)
cout << "your password must have at least <1> lowercase character.\n";
else if (hasupper)
cout << "your password must have at least <1> uppercase character.\n";
else if (hasdigit)
cout << "your password must have at least <1> digit character.\n";
if (haslower || hasupper || hasdigit)
return false;
else
return true;
}
void set_newpassword(void) {
cout << "enter the old password: ";
do {
cin >> x;
if (!check_password()) {
cout << "please enter a new password or < 0 > to return to menu : ";
while (true) {
cin >> x;
if (x != "0" && check_legitpassword(x)) {
cout << "password has changed successfully\n";
fstream file;
file.open("password.txt", ios::out);
if (file.is_open()) {
file << encrypt(x);
file.close();
}
y = x;
break;
}
else if (x == "0") break;
else cout << "please enter a new password with match constraints or < 0 > to return to menu: ";
}
}
else if (x != "0" && check_password()) cout << "please enter a correct password or < 0 > to return to menu: ";
} while (check_password() && x != "0");
}
};
class Patient {
private:
string name, BloodType, mobile, age, id;
public:
// constructors
Patient() {}
Patient(string name, string bloodType, string age, string mobile, string id);
// setters
void add_new(vector<Patient>& obj1, int* p) {
string x1;
cout << "\nPatient #" << *p + 1 << "\n\n";
cout << "name: ";
cin.ignore();
getline(cin, x1);
obj1[*p].setName(x1);
while (true) {
cout << "Blood type: ";
cin >> x1;
if (check_BloodType(x1))
{
obj1[*p].setBloodType(x1);
break;
}
else cout << "\nInvalid Blood type\n\n";
}
while (true) {
cout << "mobile: ";
cin >> x1;
if (check_mobile(x1))
{
obj1[*p].setMobile(x1);
break;
}
else cout << "\nInvalid phone number\n\n";
}
while (true) {
cout << "id: ";
cin >> x1;
if (check_ID(x1))
{
obj1[*p].setID(x1);
break;
}
else cout << "\nInvalid ID\n\n";
}
while (true)
{
cout << "Enter age: ";
cin >> x1;
if (check_age(x1))
{
obj1[*p].setAge(x1);
break;
}
else
cout << "\nage is Invalid\n\n";
}
(*p)++;
app_data(obj1, *(p));
cout << "\nPatient #" << *p << " has been added successfully\n";
cout << "\nthe current number of patients: " << *p << "\n\n";
}
void edit_data(vector<Patient>& obj1, int* p) {
string x;
cout << "Enter the patient's id: ";
cin >> x;
for (int i = 0; i < *p; i++)
{
if (x == obj1[i].getID())
{
string y;
char choice;
cout << "Editing data of patient #" << i + 1 << endl;
while (true) {
cout << "1 edit name\n";
cout << "2 edit blood type\n";
cout << "3 edit mobile\n";
cout << "4 edit ID\n";
cout << "5 edit age\n";
cout << "6 return to main menu\n";
cout << "Please enter your choice: ";
cin >> choice;
switch (choice)
{
case '1':
cout << "Enter the new name: ";
cin.ignore();
getline(cin, y);
obj1[i].setName(y);
break;
case '2':
while (true) {
cout << "Enter the new blood type: ";
cin >> y;
if (obj1[i].check_BloodType(y))
{
obj1[i].setBloodType(y);
break;
}
else cout << "\nInvalid Blood type\n\n";
}
break;
case '3':
while (true) {
cout << "Enter the new mobile: ";
cin >> y;
if (obj1[i].check_mobile(y))
{
obj1[i].setMobile(y);
break;
}
else cout << "\nInvalid phone number\n\n";
}
break;
case '4':
while (true) {
cout << "Enter the new id: ";
cin >> y;
if (obj1[i].check_ID(y))
{
obj1[i].setID(y);
break;
}
else cout << "\nInvalid ID\n\n";
}
break;
case '5':
while (true) {
cout << "Enter the new age: ";
cin >> y;
if (obj1[i].check_age(y))
{
obj1[i].setAge(y);
break;
}
else cout << "\nInvalid Age\n\n";
}
break;
case '6':
return;
default:
break;
}
}
}
}
cout << "\nid not found\n\n";
}
void refrish(int* p) {
fstream file;
file.open("data.txt", ios::in);
if (file.is_open()) {
string t;
while (!file.eof()) {
getline(file, t);
if (t.size())
(*p)++;
file >> ws;
}
file.close();
}
}
void update(vector<Patient>& obj, int num) {
fstream file;
file.open("data.txt", ios::in);
if (file.is_open()) {
string temp;
for (int i = 0; i < num; i++) {
file >> ws;
getline(file, temp);
obj[i].setName(temp.substr(0, temp.find(",")));
obj[i].setBloodType(temp.substr(temp.find(",") + 1, temp.find(";") - temp.find(",") - 1));
obj[i].setMobile(temp.substr(temp.find(";") + 1, temp.find("/") - temp.find(";") - 1));
obj[i].setID(temp.substr(temp.find("/") + 1, temp.find(":") - temp.find("/") - 1));
obj[i].setAge(temp.substr(temp.find(":") + 1, temp.find(".") - temp.find(":") - 1));
file >> ws;
}
file.close();
}
}
void overwrite_data(vector<Patient>& obj, int num) {
fstream file;
file.open("data.txt", ios::out);
if (file.is_open()) {
for (int i = 0; i < num; i++)
{
file << obj[i].getName() << ",";
file << obj[i].getBloodType() << ";";
file << obj[i].getMobile() << "/";
file << obj[i].getID() << ":";
file << obj[i].getAge() << ".\n";
}
}
file.close();
}
void app_data(vector<Patient>& obj, int num) {
fstream file;
num--;
file.open("data.txt", ios::app);
if (file.is_open()) {
file << obj[num].getName() << ",";
file << obj[num].getBloodType() << ";";
file << obj[num].getMobile() << "/";
file << obj[num].getID() << ":";
file << obj[num].getAge() << ".\n";
file.close();
}
}
int delete_element(vector<Patient>& obj1, int* p) {
string x;
char y;
cout << "Enter id of patient: ";
cin >> x;
cout << "\nare you sure about this deletion process? Enter <Y> or <N> ";
cin >> y;
if (y == 'y' || y == 'Y')
{
for (int i = 0; i < *p; i++)
{
if (x == obj1[i].getID())
{
obj1.erase(obj1.begin() + i);
(*p)--;
overwrite_data(obj1, *p);
cout << "\npatient's data has been deleted successfully\n";
cout << "\nthe current number of patients: " << *p << "\n\n";
return 0;
}
}
cout << "\nid not found\n\n";
return 0;
}
if (y == 'n' || y == 'N')
return 0;
}
void search(vector<Patient>obj, int num)
{
string x;
int i;
char choice;
while (true) {
cout << "1 search by id\n";
cout << "2 search by number\n";
cout << "3 search for donors\n";
cout << "4 search by Blood Type\n";
cout << "5 return to main menu\n";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case '1':
cout << "Enter the patient's id: ";
cin >> x;
for (int i = 0; i < num; i++)
{
if (x == obj[i].getID()) {
printData(obj, i);
t = false;
}
}
if(t)cout << "\nID was not found\n\n";
break;
case '2':
cout << "the no. of patient : ";
cin >> i;
Search_no(obj, num, i);
break;
case '3':
while (true)
{
cout << "Enter recipient blood type: ";
cin >> x;
if (check_BloodType(x))
{
search_recip(obj, num, x);
break;
}
else
cout << "\nInvalid Blood Type\n\n";
}
break;
case '4':
Search_BloodType(obj, num);
break;
case '5':
return;
default:
break;
}
}
}
void search_recip(vector<Patient>obj, int num, string x)
{
bool found = false;
if (x == "AB+")
{
for (int i = 0; i < num; i++)
printData(obj, i);
}
else if (x == "AB-")
{
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == "O-" || obj[i].getBloodType() == "B-" || obj[i].getBloodType() == "A-" || obj[i].getBloodType() == "AB-")
{
found = true;
printData(obj, i);
}
}
if (!found)
cout << "No donors have been found\n";
}
else if (x == "A+")
{
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == "O-" || obj[i].getBloodType() == "O+" || obj[i].getBloodType() == "A-" || obj[i].getBloodType() == "A+")
{
found = true;
printData(obj, i);
}
}
if (!found)
cout << "No donors have been found\n";
}
else if (x == "A-")
{
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == "O-" || obj[i].getBloodType() == "A-")
{
found = true;
printData(obj, i);
}
}
if (!found)
cout << "No donors have been found\n";
}
else if (x == "B+")
{
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == "O-" || obj[i].getBloodType() == "O+" || obj[i].getBloodType() == "B-" || obj[i].getBloodType() == "B+")
{
found = true;
printData(obj, i);
}
}
if (!found)
cout << "No donors have been found\n";
}
else if (x == "B-")
{
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == "O-" || obj[i].getBloodType() == "B-")
{
found = true;
printData(obj, i);
}
}
if (!found)
cout << "No donors have been found\n";
}
else if (x == "O+")
{
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == "O-" || obj[i].getBloodType() == "O+")
{
found = true;
printData(obj, i);
}
}
if (!found)
cout << "No donors have been found\n";
}
else if (x == "O-")
{
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == "O-")
{
found = true;
printData(obj, i);
}
}
if (!found)
cout << "No donors have been found\n";
}
}
void Search_BloodType(vector<Patient>obj, int num) {
{
string patientbloodtype;
while (true)
{
cout << "Enter blood type: ";
cin >> patientbloodtype;
if (check_BloodType(patientbloodtype))
break;
else
cout << "\nInvalid Blood Type\n\n";
}
system("CLS");
cout << "============================================================================================" << endl;
cout << "==================================| Data that was found |===================================" << endl;
cout << "============================================================================================" << endl;
cout << " NO : | BloodType: | ID " << endl;
cout << "____________________________________________________________________________________________" << endl;
int counter = 0;
for (int i = 0; i < num; i++)
{
if (obj[i].getBloodType() == patientbloodtype)
{
counter++;
cout << " " << counter << " " << obj[i].getBloodType() << " " << obj[i].getID() << endl;
}
}
cout << "=============================================================================================" << endl;
if (counter == 0)
{
cout << "\nNo Records Found !\n\n";
}
}
}
void Search_no(vector<Patient>obj, int num, int i) {
if (i > 0 && i <= num) {
i--;
printData(obj, i);
}
else cout << "it is not found\n";
}
bool check_BloodType(string x) {
if (x == "A+" || x == "A-")
return true;
else if (x == "B+" || x == "B-")
return true;
else if (x == "O+" || x == "O-")
return true;
else if (x == "AB+" || x == "AB-")
return true;
else
return false;
}
bool check_ID(string x) {
if (x.find_first_not_of("0123456789") == string::npos && x.length() == 14)
return true;
else
return false;
}
bool check_mobile(string x) {
if (x.find_first_not_of("0123456789") == string::npos && x.length() == 11)
return true;
else
return false;
}
bool check_age(string x) {
if (x.find_first_not_of("0123456789") == string::npos)
return true;
else
return false;
}
void setName(string name);
void setBloodType(string BloodType);
void setAge(string age);
void setMobile(string mobile);
void setID(string id);
// getters
string getName() { return name; }
string getBloodType() { return BloodType; }
string getMobile() { return mobile; }
string getAge() { return age; }
string getID() { return id; }
// other functions
void readData();
void printData(vector<Patient>obj, int i);
};
Patient::Patient(string name, string bloodType, string age, string mobile, string id) {
this->name = name;
this->BloodType = bloodType;
this->age = age;
this->mobile = mobile;
this->id = id;
}
void Patient::setName(string name) {
this->name = name;
}
void Patient::setBloodType(string BloodType) {
this->BloodType = BloodType;
}
void Patient::setMobile(string mobile) {
this->mobile = mobile;
}
void Patient::setAge(string age) {
this->age = age;
}
void Patient::setID(string id) {
this->id = id;
}
void Patient::readData() {
string name, bloodType, mobile, age, id;
cout << "Please enter the patient's data: \n";
cout << "Name: ";
cin.ignore();
getline(cin, name); setName(name);
cout << "Blood Type: ";
cin >> BloodType; setBloodType(bloodType);
cout << "Mobile: ";
cin >> mobile; setMobile(mobile);
cout << "Age: ";
cin >> age; setAge(age);
cout << "ID: ";
cin >> id; setID(id);
cout << endl;
}
void Patient::printData(vector<Patient>obj, int i) {
cout << "*********************\n";
cout << "Name: " << obj[i].getName() << endl;
cout << "Blood Type: " << obj[i].getBloodType() << endl;
cout << "Mobile: " << obj[i].getMobile() << endl;
cout << "ID: " << obj[i].getID() << endl;
cout << "Age: " << obj[i].getAge() << endl;
cout << "*********************\n";
}
int main() {
string my_password;
vector<Patient>obj(100);
password owner;
do {
cout << "please enter a correct password or < 0 > to quit: ";
cin >> my_password;
owner.set_password(my_password);
if (my_password == "0") {
cout << "see you soon\n";
return 0;
}
} while (owner.check_password());
int num = 0;
obj[0].refrish(&num);
obj[0].update(obj, num);
cout << "\nthe number of patients in the system : " << num << endl;
char tester;
while (true) {
cout << "\nto change your password ,| enter <C> |\n";
cout << "to Search ,| enter <S> |\n";
cout << "to Add data ,| enter <A> |\n";
cout << "to delete data ,| enter <D> |\n";
cout << "to Edit data ,| enter <E> |\n";
cout << "to clear screen ,| enter <X> |\n";
cout << "to Quit ,| enter <Q> |\n";
cout << "Enter your option: ";
cin >> tester;
switch (tester){
case 'C':
case 'c':
owner.set_newpassword();
break;
case 'S':
case 's':
obj[0].search(obj, num);
break;
case 'A':
case 'a':
obj[0].add_new(obj, &num);
break;
case 'E':
case 'e':
obj[0].edit_data(obj, &num);
obj[0].overwrite_data(obj, num);
break;
case 'D':
case 'd':
obj[0].delete_element(obj, &num);
break;
case 'Q':
case 'q':
cout << "See you son.\n";
return 0;
break;
default:
system("cls");
break;
}
}
}