-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloodBank.cpp
More file actions
616 lines (494 loc) · 15.3 KB
/
Copy pathBloodBank.cpp
File metadata and controls
616 lines (494 loc) · 15.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
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include <sstream>
char YourOption; //global variable for moving to main menu
using namespace std;
//----------------donor section functions
void DonorSection();
void AddDonor();
void UpdateDonor();
void ShowAllDonors();
void DeleteDonor();
void ExitTOMainMenu();
//---------------Blood Section functions
void AddUnits();
void BloodSection();
void AvailableUnits();
void DonateUnits();
//========exit to main menu function
void ExitFrmMainPrgrm();
int main()
{
//menu options
int USoption;
//wellcome message
cout<<"\t----------------------------------------\n";
cout<<"\tWELLCOME TO BLOOD BANK MANAGEMENT SYSTEM";
cout<<"\n\t----------------------------------------";
cout<<"\n\n\t--------------------\n";
cout<<"\t\tMENU";
cout<<"\n\t--------------------";
cout<<"\n\nChoose Number To select The option\n";
cout<<"\n1- Donor Section: ";
cout<<"\n2- Blood Section: ";
cout<<"\n3- Exit From Program: ";
cout<<"\nEnter Your Option: ";
cin>>USoption;
//using switch statements
switch (USoption)
{
case 1:
DonorSection();
break;
case 2:
BloodSection();
break;
case 3:
ExitFrmMainPrgrm();
break;
default:
cout<<"Invalid Choice! Please Select from the given options.";
break;
}
}
//------donor section functions defining
void DonorSection()
{
int DonorSectChoice ;
cout<<"\t----------------------------------------\n";
cout<<"\tWELLCOME TO Donor Section";
cout<<"\n\t----------------------------------------";
cout<<"\n1-Add Donor";
cout<<"\n2-Update Donor";
cout<<"\n3-Delete Donor";
cout<<"\n4-Show All Donors";
cout<<"\n5-Exit To Main Menu";
cout<<"\n\nEnter Your Choice: ";
cin>>DonorSectChoice;
switch (DonorSectChoice)
{
case 1:
AddDonor();
break;
case 2:
UpdateDonor();
break;
case 3:
DeleteDonor();
break;
case 4:
ShowAllDonors();
break;
case 5:
ExitTOMainMenu();
break;
default:
cout<<"Invalid Choice!";
break;
}
}
////ADDING DONOR
// Define a struct to hold donor information
struct Donor {
int id;
string firstName;
string lastName;
string bloodGroup;
string contact;
};
void AddDonor()
{
ofstream AddDonor_List; //making object for storing donor's data table data
Donor newDonor; // donor struct variable
AddDonor_List.open("AddDonorDetails.txt", ios::app);
if (AddDonor_List.tellp() == 0) // check if file is empty
{
// formatting the database with table headers
AddDonor_List << "Donor ID\t\t\t" << "Donors Full Name\t\t" << "Blood Group\t\t" << "Contact Number\t\t\n";
}
do {
bool found; // flag to indicate if ID is found in the file
cout << "/////ADD DONOR/////\n";
// Ask for a new ID and check if it already exists in the file
do {
found = false; // reset flag before checking for new ID
cout << "\nDonor ID: ";
cin >> newDonor.id;
found = false; // assume ID is not found
ifstream donorFile("AddDonorDetails.txt");
string line;
while (getline(donorFile, line)) {
if (line.find(to_string(newDonor.id)) != string::npos) {
found = true;
cout << "ID already exists. Please enter a different ID.\n";
break;
}
}
donorFile.close();
} while (found);
cout << "Donor First Name: ";
cin >> newDonor.firstName;
cout << "Donor Last Name: ";
cin >> newDonor.lastName;
cout << "\nType Your Blood Group As Given\n\nOption\tBlood Group\n\n1\tA+\n2\tO+\n3\tB+\n4\tAB+\n5\tA-\n6\tO-\n7\tB-\n8\tAB-";
cout << "\nBlood Group: ";
cin >> newDonor.bloodGroup;
cout << "\nContact Number: ";
cin >> newDonor.contact;
// Check if the donor already exists in the file
found = false;
ifstream donorFile("AddDonorDetails.txt");
string line;
while (getline(donorFile, line)) {
if (line.find(to_string(newDonor.id)) != string::npos) {
found = true;
cout << "ID already exists. The donor information will not be added.\n";
break;
}
}
donorFile.close();
// Write the donor information to the file if it does not already exist
if (!found) {
AddDonor_List << newDonor.id << "\t" << newDonor.firstName << " " << newDonor.lastName << "\t"
<< newDonor.bloodGroup << "\t" << newDonor.contact << "\n";
cout << "\nRecord Saved Successfully!";
}
cout << "\n\nWant to add another entry?\nPress ' Y ' To another entry or press ' N ' to Jump in Main Menu: ";
cin >> YourOption;
} while (YourOption == 'Y' || YourOption == 'y');
AddDonor_List.close();
if (YourOption) //asking user to another donor
{
main();
}
}
////UPDATE DONOR
void UpdateDonor()
{
do
{
int id;
cout << "Enter ID of donor to update: ";
cin >> id;
ifstream donorFile("AddDonorDetails.txt");
ofstream tempFile("temp.txt");
string line;
bool found = false;
while (getline(donorFile, line)) {
stringstream ss(line);
int donorId;
string firstName, lastName, bloodGroup, contact;
ss >> donorId >> firstName >> lastName >> bloodGroup >> contact;
if (donorId == id) {
found = true;
cout << "Donor Found\n";
cout << "ID: " << donorId << "\n";
cout << "First Name: " << firstName << "\n";
cout << "Last Name: " << lastName << "\n";
cout << "Blood Group: " << bloodGroup << "\n";
cout << "Contact: " << contact << "\n";
cout << "\nWhich information would you like to update?\n";
cout << "1. First Name\n";
cout << "2. Last Name\n";
cout << "3. Blood Group\n";
cout << "4. Contact\n";
int option;
cin >> option;
switch (option) {
case 1:
cout << "Enter new first name: ";
cin >> firstName;
break;
case 2:
cout << "Enter new last name: ";
cin >> lastName;
break;
case 3:
cout << "Enter new blood group: ";
cin >> bloodGroup;
break;
case 4:
cout << "Enter new contact number: ";
cin >> contact;
break;
default:
cout << "Invalid option.\n";
return;
}
// Update the donor information
tempFile << donorId << " " << firstName << " " << lastName << " " << bloodGroup << " " << contact << "\n";
cout << "Record updated successfully!\n";
}
else {
// Copy unmodified record to temp file
tempFile << line << "\n";
}
}
if (!found) {
cout << "Donor not found!\n";
}
donorFile.close();
tempFile.close();
// Remove original file and rename temp file
remove("AddDonorDetails.txt");
rename("temp.txt", "AddDonorDetails.txt");
cout<<"To exit to main menu Press Y\nPress N to continue.";
cin>>YourOption;
} while (YourOption == 'N' || YourOption == 'n');
if (YourOption) //asking user to another donor
{
main();
}
}
////DELETE DONOR
void DeleteDonor(){
do
{
cout<<"/////Delete DONOR DATA /////\n";
ifstream donorFile("AddDonorDetails.txt");
ofstream tempFile("temp.txt");
int deleteId;
cout << "Enter ID of donor to delete: ";
cin >> deleteId;
Donor deletedDonor;
bool found = false;
string line;
while (getline(donorFile, line)) {
stringstream ss(line);
ss >> deletedDonor.id >> deletedDonor.firstName >> deletedDonor.lastName
>> deletedDonor.bloodGroup >> deletedDonor.contact;
if (deletedDonor.id == deleteId) {
found = true;
cout << "Donor Found\n";
cout << "Donor ID: " << deletedDonor.id << endl;
cout << "Name: " << deletedDonor.firstName << " " << deletedDonor.lastName << endl;
cout << "Blood Group: " << deletedDonor.bloodGroup << endl;
cout << "Contact: " << deletedDonor.contact << endl;
char confirmDelete;
cout << "Are you sure you want to delete this donor? (y/n) ";
cin >> confirmDelete;
if (confirmDelete == 'y' || confirmDelete == 'Y') {
cout << "Donor deleted\n";
} else {
cout << "Deletion aborted\n";
tempFile << line << endl;
}
} else {
tempFile << line << endl;
}
}
donorFile.close();
tempFile.close();
remove("AddDonorDetails.txt");
rename("temp.txt", "AddDonorDetails.txt");
if (!found) {
cout << "Donor with ID " << deleteId << " not found\n";
}
cout<<"To exit to main menu Press Y\nPress N to continue.";
cin>>YourOption;
} while (YourOption == 'N' || YourOption == 'n');
if (YourOption) //asking user to another donor
{
main();
}
}
////SHOW ALL DONORS
void ShowAllDonors()
{
do
{
ifstream donorFile("AddDonorDetails.txt");
if (!donorFile) {
cout << "Error opening file.\n";
return;
}
cout << "All Donors:\n";
cout << "Donor ID\t\t\t" << "Donors Full Name\t\t" << "Blood Group\t\t" << "Contact Number\t\t\n";
string line;
while (getline(donorFile, line)) {
cout << line << endl;
}
donorFile.close();
cout<<"To exit to main menu Press Y\nPress N to continue.";
cin>>YourOption;
} while (YourOption == 'N' || YourOption == 'n');
if (YourOption) //asking user to another donor
{
main();
}
}
////EXIT TO MAIN MENU FUNCTION
////WHENEVER IT IS CALLED THE CONTROL MOVE TO main() FUNCTION
void ExitTOMainMenu(){
main();
}
///for donor section
//---------------Blood Section functions
void BloodSection()
{
cout<<"\t----------------------------------------\n";
cout<<"\tWELLCOME TO Blood Section";
cout<<"\n\t----------------------------------------";
int BloodSectionChoice ;
cout<<"\n1- Add Units";
cout<<"\n2- Available Units";
cout<<"\n3- Donate Unit";
cout<<"\n4- Exit To Main Menu";
cout<<"\nEnter Your Choice: ";
cin>>BloodSectionChoice ;
switch (BloodSectionChoice)
{
case 1:
AddUnits();
break;
case 2:
AvailableUnits();
break;
case 3:
DonateUnits();
break;
case 4:
ExitTOMainMenu();
break;
default:
cout<<"Enter A valid Choice";
break;
}
}
////ADD UNITS
void AddUnits()
{
ofstream AddUnit_Head; //making object for storing unit's data table head
ofstream AddUnit_List; //making object for storing unit's data table data
char YourOption;
AddUnit_Head.open("AddUnitDetails.txt");
// formatting the database with table headers
AddUnit_Head << "Blood Group\t\t" << "Units\t\n";
AddUnit_Head.close();
AddUnit_List.open("AddUnitDetails.txt", ios::app);
do {
string bloodGroup;
int units;
cout << "/////ADD UNITS/////\n";
cout << "Enter Blood Group: ";
cin >> bloodGroup;
cout << "Enter Number of Units: ";
cin >> units;
// Write the unit information to the file
AddUnit_List << bloodGroup << "\t\t" << units << "\n";
cout << "\nRecord Saved Successfully!";
cout << "\n\nWant to add another entry?\nPress ' Y ' To add another entry or press ' N ' to jump to Main Menu: ";
cin >> YourOption;
} while (YourOption == 'Y' || YourOption == 'y');
AddUnit_List.close();
if (YourOption)
{
main();
}
}
////AVAILABLE UNITS
void AvailableUnits()
{
do
{
ifstream inFile("AddUnitDetails.txt");
if (!inFile) {
cout << "Unable to open file blood_bank.txt\n";
return;
}
cout<<"/////AVAILABLE UNITS/////\n";
string bloodType;
cout << "Enter the blood type to check available units: ";
cin >> bloodType;
string line;
bool found = false;
while (getline(inFile, line)) {
stringstream ss(line);
string bloodGroup;
int units;
ss >> bloodGroup >> units;
if (bloodGroup == bloodType) {
found = true;
cout << "Blood Group: " << bloodGroup << " Units: " << units << endl;
}
}
if (!found) {
cout << "No units available for blood type " << bloodType << endl;
}
inFile.close();
cout << "\n\nWant to Check another blood group unit?\nPress ' Y ' To Continue or press ' N ' to jump to Main Menu: ";
cin >> YourOption;
} while (YourOption == 'Y' || YourOption == 'y');
if (YourOption)
{
main();
}
}
////DONATE UNITS
void DonateUnits()
{
do
{
ifstream unitFile("AddUnitDetails.txt", ios::in);
ofstream tempFile("temp.txt", ios::out);
string bloodGroup;
int unitsToDonate;
bool found = false;
cout << "Enter the blood group to donate: ";
cin >> bloodGroup;
// Check if the blood group exists in the file
string line;
while (getline(unitFile, line)) {
stringstream ss(line);
string group;
int units;
ss >> group >> units;
if (group == bloodGroup) {
cout << "Available units: " << units << endl;
cout << "Enter the number of units to donate: ";
cin >> unitsToDonate;
if (unitsToDonate > units) {
cout << "Not enough units available!" << endl;
break;
}
units -= unitsToDonate;
found = true;
}
tempFile << group << " " << units << endl;
}
unitFile.close();
tempFile.close();
// If blood group is found and units are deducted from the file
if (found) {
// Delete the old file and rename the temp file to the original file name
remove("AddUnitDetails.txt");
rename("temp.txt", "AddUnitDetails.txt");
cout << "Units successfully donated!" << endl;
} else {
cout << "Blood group not found!" << endl;
remove("temp.txt");
}
// Ask if the user wants to donate more units or go back to main menu
cout << "\n\nDonate another blood group unit?\nPress ' Y ' To Continue or press ' N ' to jump to Main Menu: ";
cin >> YourOption;
} while (YourOption == 'Y' || YourOption == 'y');
if (YourOption)
{
main();
}
}
////EXIT FROM THE WHOLE PROGRAM FUNCTION
void ExitFrmMainPrgrm(){
char chr ;
cout<<"Do You Want To Exit from the program? ";
cout<<"\nPress ' Y ' to continue or press any key to exit from the program: ";
cin>>chr;
if (chr == 'y' || chr == 'Y')
{
main();
}
else{
cout<<"Bye!";
}
}