-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
2052 lines (1773 loc) · 65.9 KB
/
main.cpp
File metadata and controls
2052 lines (1773 loc) · 65.9 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 <fstream>
#include <string>
#include <conio.h>
#include <iomanip>
#include <windows.h>
#include <sstream>
#include <stack>
#include"header.h"
using namespace std;
struct Patient {
int id;
string name;
string cnic;
int age;
string gender;
string contact;
string address;
string admissionDate;
string doctorAssigned;
string medicalHistory;
string currentIllness;
string treatmentPlan;
int roomNumber;
double billingAmount;
string priority;
bool emergency;
string condition;
Patient* next;
};
class HospitalManagement {
private:
Patient* head;
public:
HospitalManagement() : head(nullptr) {
loadPatients(); // Load patients from file on initialization
}
void addPatient(int id, string name, string cnic, int age, string gender, string contact, string address,
string admissionDate, string doctorAssigned, string medicalHistory, string currentIllness,
string treatmentPlan, int roomNumber, double billingAmount, string priority,
bool emergency, string condition) {
// Check if the room is already allocated
if (isRoomAllocated(roomNumber)) {
cout << "Room " << roomNumber << " is already booked. Please select an alternate room.\n";
displayAvailableRooms();
cout << "Enter alternate Room Number (1-35): ";
cin >> roomNumber;
}
Patient* newPatient = new Patient{id, name, cnic, age, gender, contact, address, admissionDate,
doctorAssigned, medicalHistory, currentIllness, treatmentPlan,
roomNumber, billingAmount, priority, emergency, condition, nullptr};
newPatient->next = head;
head = newPatient;
cout << "Patient added successfully.\n";
savePatientToFile(newPatient); // Save the new patient to the file
}
bool isRoomAllocated(int roomNumber) {
Patient* temp = head;
while (temp != nullptr) {
if (temp->roomNumber == roomNumber) {
return true; // Room is already allocated
}
temp = temp->next;
}
return false; // Room is free
}
void displayAvailableRooms() {
cout << "Available rooms:\n";
for (int i = 1; i <= 35; ++i) {
if (!isRoomAllocated(i)) {
cout << "Room " << i << " is available.\n";
}
}
}
void displayPatientByID(int id) {
Patient* temp = head;
while (temp != nullptr) {
if (temp->id == id) {
cout << "\n--- Patient Details ---\n";
cout << "ID: " << temp->id << "\n";
cout << "Name: " << temp->name << "\n";
cout << "CNIC: " << temp->cnic << "\n";
cout << "Age: " << temp->age << "\n";
cout << "Gender: " << temp->gender << "\n";
cout << "Contact: " << temp->contact << "\n";
cout << "Address: " << temp->address << "\n";
cout << "Admission Date: " << temp->admissionDate << "\n";
cout << "Doctor Assigned: " << temp->doctorAssigned << "\n";
cout << "Medical History: " << temp->medicalHistory << "\n";
cout << "Current Illness: " << temp->currentIllness << "\n";
cout << "Treatment Plan: " << temp->treatmentPlan << "\n";
cout << "Room Number: " << temp->roomNumber << "\n";
cout << "Billing Amount: " << temp->billingAmount << "\n";
cout << "Priority: " << temp->priority << "\n";
cout << "Emergency: " << (temp->emergency ? "Yes" : "No") << "\n";
cout << "Condition: " << temp->condition << "\n";
return;
}
temp = temp->next;
}
cout << "Patient with ID " << id << " not found.\n";
}
void displayBill(int id) {
Patient* temp = head;
while (temp != nullptr) {
if (temp->id == id) {
cout << "\n--- Billing Details ---\n";
cout << "Patient ID: " << temp->id << "\n";
cout << "Patient Name: " << temp->name << "\n";
cout << "Room Number: " << temp->roomNumber << "\n";
cout << "Billing Amount: " << temp->billingAmount << "\n";
return;
}
temp = temp->next;
}
cout << "Patient with ID " << id << " not found.\n";
}
void findPatientByCNIC(string cnic) {
Patient* temp = head;
while (temp != nullptr) {
if (temp->cnic == cnic) {
displayPatientByID(temp->id); // Reuse the display function
return;
}
temp = temp->next;
}
cout << "No patient found with CNIC " << cnic << ".\n";
}
void deletePatientByID(int id) {
Patient* temp = head;
Patient* prev = nullptr;
while (temp != nullptr && temp->id != id) {
prev = temp;
temp = temp->next;
}
if (temp == nullptr) {
cout << "Patient with ID " << id << " not found.\n";
return;
}
if (prev == nullptr) {
head = temp->next; // Deleting the head
} else {
prev->next = temp->next; // Bypass the node to delete it
}
delete temp;
cout << "Patient with ID " << id << " deleted successfully.\n";
// Optionally, you could implement a function to rewrite the file after deletion
}
void displayPatients() {
Patient* temp = head;
cout << "\n--- All Patients ---\n";
if (temp == nullptr) {
cout << "No patients found.\n";
return;
}
while (temp != nullptr) {
cout << "ID: " << temp->id << ", Name: " << temp->name << ", Room: " << temp->roomNumber << "\n";
temp = temp->next;
}
}
void savePatientToFile(Patient* patient) {
ofstream outFile("patients.txt", ios::app);
if (outFile) {
outFile << patient->id << ","
<< patient->name << ","
<< patient->cnic << ","
<< patient->age << ","
<< patient->gender << ","
<< patient->contact << ","
<< patient->address << ","
<< patient->admissionDate << ","
<< patient->doctorAssigned << ","
<< patient->medicalHistory << ","
<< patient->currentIllness << ","
<< patient->treatmentPlan << ","
<< patient->roomNumber << ","
<< patient->billingAmount << ","
<< patient->priority << ","
<< patient->emergency << ","
<< patient->condition << "\n";
outFile.close();
} else {
cout << "Error opening file for writing.\n";
}
}
void loadPatients() {
ifstream inFile("patients.txt");
if (inFile) {
string line;
while (getline(inFile, line)) {
Patient* newPatient = new Patient;
stringstream ss(line);
string token;
getline(ss, token, ','); newPatient->id = stoi(token);
getline(ss, newPatient->name, ',');
getline(ss, newPatient->cnic, ',');
getline(ss, token, ','); newPatient->age = stoi(token);
getline(ss, newPatient->gender, ',');
getline(ss, newPatient->contact, ',');
getline(ss, newPatient->address, ',');
getline(ss, newPatient->admissionDate, ',');
getline(ss, newPatient->doctorAssigned, ',');
getline(ss, newPatient->medicalHistory, ',');
getline(ss, newPatient->currentIllness, ',');
getline(ss, newPatient->treatmentPlan, ',');
getline(ss, token, ','); newPatient->roomNumber = stoi(token);
getline(ss, token, ','); newPatient->billingAmount = stod(token);
getline(ss, newPatient->priority, ',');
getline(ss, token, ','); newPatient->emergency = (token == "1");
getline(ss, newPatient->condition, ',');
newPatient->next = head;
head = newPatient;
}
inFile.close();
} else {
cout << "Error opening file for reading.\n";
}
}
~HospitalManagement() {
while (head) {
Patient* temp = head;
head = head->next;
delete temp;
}
}
};
struct Doctor {
string name;
string specialization;
int yearsOfExperience;
string qualifications;
string contactInfo;
};
// Function to add a new doctor profile
void addDoctorProfile() {
Doctor doctor;
cout << "Enter Doctor's Name: ";
cin.ignore();
getline(cin, doctor.name);
cout << "Enter Specialization: ";
getline(cin, doctor.specialization);
cout << "Enter Years of Experience: ";
cin >> doctor.yearsOfExperience;
cin.ignore();
cout << "Enter Qualifications: ";
getline(cin, doctor.qualifications);
cout << "Enter Contact Info: ";
getline(cin, doctor.contactInfo);
ofstream file("doctors.txt", ios::app); // Open in append mode
if (file.is_open()) {
file << doctor.name << "|" << doctor.specialization << "|"
<< doctor.yearsOfExperience << "|" << doctor.qualifications
<< "|" << doctor.contactInfo << "\n";
file.close();
cout << "Doctor profile added successfully.\n";
} else {
cout << "Error opening file.\n";
}
}
// Function to view all doctor profiles
void viewDoctorProfiles() {
ifstream file("doctors.txt");
if (file.is_open()) {
string line;
cout << "\n--- Doctor Profiles ---\n";
while (getline(file, line)) {
// Split each line by '|'
string name, specialization, years, qualifications, contact;
size_t pos = 0;
pos = line.find('|');
name = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find('|');
specialization = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find('|');
years = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find('|');
qualifications = line.substr(0, pos);
line.erase(0, pos + 1);
contact = line;
// Display doctor profile
cout << "Name: " << name << "\nSpecialization: " << specialization
<< "\nYears of Experience: " << years
<< "\nQualifications: " << qualifications
<< "\nContact Info: " << contact << "\n\n";
}
file.close();
} else {
cout << "Error opening file.\n";
}
}
// Function to update an existing doctor's profile
void updateDoctorProfile() {
string searchName;
cout << "Enter the name of the doctor to update: ";
cin.ignore();
getline(cin, searchName);
ifstream file("doctors.txt");
ofstream tempFile("temp.txt"); // Temporary file to store updated data
bool found = false;
if (file.is_open() && tempFile.is_open()) {
string line;
while (getline(file, line)) {
Doctor doctor;
size_t pos = 0;
pos = line.find('|');
doctor.name = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find('|');
doctor.specialization = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find('|');
doctor.yearsOfExperience = stoi(line.substr(0, pos));
line.erase(0, pos + 1);
pos = line.find('|');
doctor.qualifications = line.substr(0, pos);
line.erase(0, pos + 1);
doctor.contactInfo = line;
if (doctor.name == searchName) {
found = true;
cout << "\nUpdating profile for " << doctor.name << ":\n";
cout << "Enter New Specialization: ";
getline(cin, doctor.specialization);
cout << "Enter New Years of Experience: ";
cin >> doctor.yearsOfExperience;
cin.ignore();
cout << "Enter New Qualifications: ";
getline(cin, doctor.qualifications);
cout << "Enter New Contact Info: ";
getline(cin, doctor.contactInfo);
}
tempFile << doctor.name << "|" << doctor.specialization << "|"
<< doctor.yearsOfExperience << "|" << doctor.qualifications
<< "|" << doctor.contactInfo << "\n";
}
file.close();
tempFile.close();
// Replace original file with updated temp file
remove("doctors.txt");
rename("temp.txt", "doctors.txt");
if (found) {
cout << "Doctor profile updated successfully.\n";
} else {
cout << "Doctor not found.\n";
}
} else {
cout << "Error opening file.\n";
}
}
// Main function to run the Doctor Profile Management module
// Display main menu for high-level users
void displayMenuH(int selectedOption) {
system("cls");
setTextColor(10);
cout << "\n\n\t\t\tWelcome to Medical Management System\n\n";
if (selectedOption == 0) {
cout << "\t\t\t\t> 1. High-Level User\n";
cout << "\t\t\t\t2. Local User\n";
} else {
cout << "\t\t\t\t1. High-Level User\n";
cout << "\t\t\t\t> 2. Local User\n";
}
cout << "\n\tUse arrow keys to navigate and press Enter to select.";
}
// Countdown timer function
void countdown() {
cout << setw(40) << setfill(' ') << "This tab will Open in 10 Seconds...\n\n";
for (int i = 1; i <= 10; i++) {
cout << i << "\t";
Sleep(300);
}
system("cls");
}
// User class for managing individual users
class User {
private:
string username;
string password;
public:
User() {}
User(string& username, string& password) : username(username), password(password) {}
void saveToFile(const string& filename) {
ofstream file(filename, ios::out);
if (file.is_open()) {
file << username << "\n" << password << "\n";
file.close();
}
}
bool loadFromFile(const string& filename) {
ifstream file(filename, ios::in);
if (file.is_open()) {
getline(file, username);
getline(file, password);
file.close();
return true;
}
return false;
}
void createID() {
setTextColor(7);
cout << "\n\t*Create your user id*" << endl;
cout << "\tEnter User Name: ";
cin >> username;
char ch;
password.clear();
cout << "Enter password: ";
while ((ch = _getch()) != '\r') {
if (ch == '\b' && !password.empty()) {
password.pop_back();
cout << "\b \b";
} else {
password.push_back(ch);
cout << '*';
}
}
cout << endl;
system("cls");
cout << "\n\tYour ID is creating..........." << endl;
for (int i = 0; i < 5; ++i) {
cout << ".";
Sleep(500);
}
system("cls");
}
bool validate(const string& inputUsername, const string& inputPassword) {
return (username == inputUsername && password == inputPassword);
}
void setPassword(const string& newPassword) {
password = newPassword;
}
};
HospitalManagement hm;
struct Staff {
string name;
string occupation;
int age;
string contact;
Staff* next; // Pointer to the next staff profile
};
// Initialize the circular linked list
Staff* head = nullptr;
// Function to save staff data to a file
void saveToFile() {
ofstream file("staff_profiles.txt");
if (!file) {
cout << "Error opening file for writing.\n";
return;
}
Staff* temp = head;
if (temp) {
do {
file << temp->name << "\n" << temp->occupation << "\n" << temp->age << "\n" << temp->contact << "\n";
temp = temp->next;
} while (temp != head);
}
file.close();
cout << "Staff profiles saved to file successfully.\n";
}
// Function to load staff data from a file
void loadFromFile() {
ifstream file("staff_profiles.txt");
if (!file) {
cout << "Error opening file for reading.\n";
return;
}
string name, occupation, contact;
int age;
while (getline(file, name) && getline(file, occupation) && file >> age && file.ignore() && getline(file, contact)) {
Staff* newStaff = new Staff{name, occupation, age, contact, nullptr};
if (!head) {
head = newStaff;
newStaff->next = head;
} else {
Staff* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newStaff;
newStaff->next = head;
}
}
file.close();
cout << "Staff profiles loaded from file successfully.\n";
}
// Function to add a new staff profile
void addStaffProfile() {
Staff* newStaff = new Staff();
cout << "Enter name: ";
cin.ignore();
getline(cin, newStaff->name);
cout << "Enter occupation (Nurse, Receptionist, Lab Technician, Cleaner, Security): ";
getline(cin, newStaff->occupation);
cout << "Enter age: ";
cin >> newStaff->age;
cout << "Enter contact number: ";
cin.ignore();
getline(cin, newStaff->contact);
if (!head) { // If list is empty
head = newStaff;
newStaff->next = head;
} else {
Staff* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newStaff;
newStaff->next = head;
}
cout << "Staff profile added successfully!\n";
saveToFile(); // Save changes to file
}
// Function to view all staff profiles
void viewStaffProfiles() {
if (!head) {
cout << "No staff profiles available.\n";
return;
}
Staff* temp = head;
cout << "\n--- Hospital Staff Profiles ---\n";
do {
cout << "Name: " << temp->name << ", Occupation: " << temp->occupation
<< ", Age: " << temp->age << ", Contact: " << temp->contact << endl;
temp = temp->next;
} while (temp != head);
}
// Function to update a staff profile
void updateStaffProfile() {
if (!head) {
cout << "No staff profiles to update.\n";
return;
}
string name;
cout << "Enter the name of the staff to update: ";
cin.ignore();
getline(cin, name);
Staff* temp = head;
do {
if (temp->name == name) {
cout << "Enter new age: ";
cin >> temp->age;
cout << "Enter new contact number: ";
cin.ignore();
getline(cin, temp->contact);
cout << "Staff profile updated successfully!\n";
saveToFile(); // Save changes to file
return;
}
temp = temp->next;
} while (temp != head);
cout << "Staff profile not found.\n";
}
// Function to delete a staff profile
void deleteStaffProfile() {
if (!head) {
cout << "No staff profiles to delete.\n";
return;
}
string name;
cout << "Enter the name of the staff to delete: ";
cin.ignore();
getline(cin, name);
Staff* temp = head;
Staff* prev = nullptr;
// Check if the node to delete is the head node
if (head->name == name) {
if (head->next == head) { // Only one node in the list
delete head;
head = nullptr;
} else {
while (temp->next != head) { // Find the last node
temp = temp->next;
}
temp->next = head->next;
delete head;
head = temp->next;
}
cout << "Staff profile deleted successfully!\n";
saveToFile(); // Save changes to file
return;
}
// For non-head nodes
do {
prev = temp;
temp = temp->next;
if (temp->name == name) {
prev->next = temp->next;
delete temp;
cout << "Staff profile deleted successfully!\n";
saveToFile(); // Save changes to file
return;
}
} while (temp != head);
cout << "Staff profile not found.\n";
}
// Main menu for managing staff profiles
void manageStaffModule() {
loadFromFile(); // Load data from file at the start
int choice;
do {
cout << "\n--- Hospital Low-Level Staff Management ---\n";
cout << "1. Add Staff Profile\n";
cout << "2. View Staff Profiles\n";
cout << "3. Update Staff Profile\n";
cout << "4. Delete Staff Profile\n";
cout << "5. Exit to Main Menu\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
addStaffProfile();
} else if (choice == 2) {
viewStaffProfiles();
} else if (choice == 3) {
updateStaffProfile();
} else if (choice == 4) {
deleteStaffProfile();
} else if (choice == 5) {
cout << "Exiting to Main Menu...\n";
break;
} else {
cout << "Invalid choice. Please try again.\n";
}
} while (5);
}
// Structure to represent a patient
struct Patients{
string name;
int age;
string issue;
int priority; // Priority for emergency cases (higher = more urgent)
Patients* next; // Pointer to the next patient node
};
// Regular queue class for regular patients
class RegularQueue {
private:
Patients* front; // Front of the queue
Patients* rear; // Rear of the queue
public:
RegularQueue() : front(nullptr), rear(nullptr) {}
// Add a patient to the regular queue
void enqueue(const string& name, int age, const string& issue) {
Patients* newPatient = new Patients{name, age, issue, 0, nullptr};
// If queue is empty, front and rear are the new patient
if (rear == nullptr) {
front = rear = newPatient;
} else {
rear->next = newPatient;
rear = newPatient;
}
// Write patient data to file
ofstream file("que.txt", ios::app);
file << "Regular," << name << "," << age << "," << issue << "\n";
file.close();
cout << "Regular appointment added for " << name << ".\n";
}
// Remove and return the patient at the front of the queue
Patients* dequeue() {
if (front == nullptr) return nullptr; // Queue is empty
Patients* patientToServe = front;
front = front->next;
// If front becomes null, set rear to null as well
if (front == nullptr) rear = nullptr;
return patientToServe;
}
// Check if the regular queue is empty
bool isEmpty() const {
return front == nullptr;
}
// Getter for front to display regular patients
Patients* getFront() const {
return front;
}
};
// Priority queue class for emergency patients
class EmergencyQueue {
private:
Patients* head; // Head of the list
public:
EmergencyQueue() : head(nullptr) {}
// Function to access the head for displaying patients
Patients* getHead() const {
return head;
}
// Insert patient based on priority (higher priority patients come first)
void enqueue(const string& name, int age, const string& issue, int priority) {
Patients* newPatient = new Patients{name, age, issue, priority, nullptr};
if (head == nullptr || head->priority < priority) {
// Insert at the beginning if list is empty or new patient has highest priority
newPatient->next = head;
head = newPatient;
} else {
// Insert based on priority
Patients* temp = head;
while (temp->next != nullptr && temp->next->priority >= priority) {
temp = temp->next;
}
newPatient->next = temp->next;
temp->next = newPatient;
}
// Write patient data to file
ofstream file("que.txt", ios::app);
file << "Emergency," << name << "," << age << "," << issue << "," << priority << "\n";
file.close();
cout << "Emergency appointment added for " << name << " with priority " << priority << ".\n";
}
// Remove and return the patient with the highest priority
Patients* dequeue() {
if (head == nullptr) return nullptr; // Queue is empty
Patients* patientToServe = head;
head = head->next;
return patientToServe;
}
// Check if the emergency queue is empty
bool isEmpty() const {
return head == nullptr;
}
};
// Function to serve the next patient, prioritizing emergency cases
void serveNextPatient(EmergencyQueue& emergencyQueue, RegularQueue& regularQueue) {
Patients* patientToServe;
if (!emergencyQueue.isEmpty()) {
// Serve the emergency patient with the highest priority
patientToServe = emergencyQueue.dequeue();
cout << "Serving emergency patient: " << patientToServe->name
<< " (Age: " << patientToServe->age
<< ", Issue: " << patientToServe->issue
<< ", Priority: " << patientToServe->priority << ")\n";
} else if (!regularQueue.isEmpty()) {
// Serve the next regular patient if no emergency patients
patientToServe = regularQueue.dequeue();
cout << "Serving regular patient: " << patientToServe->name
<< " (Age: " << patientToServe->age
<< ", Issue: " << patientToServe->issue << ")\n";
} else {
cout << "No patients in the queue.\n";
return;
}
delete patientToServe; // Free memory after serving the patient
}
// Function to display all patients in both queues
void displayAllPatients(EmergencyQueue& emergencyQueue, RegularQueue& regularQueue) {
cout << "\n--- Emergency Patients ---\n";
Patients* temp = emergencyQueue.getHead(); // Use getHead() to access the head of the emergency queue
while (temp != nullptr) {
cout << "Name: " << temp->name << ", Age: " << temp->age << ", Issue: " << temp->issue
<< ", Priority: " << temp->priority << endl;
temp = temp->next;
}
cout << "\n--- Regular Patients ---\n";
temp = regularQueue.getFront();
while (temp != nullptr) {
cout << "Name: " << temp->name << ", Age: " << temp->age << ", Issue: " << temp->issue << endl;
temp = temp->next;
}
}
// Load patients from file at startup
void loadPatientsFromFile(EmergencyQueue& emergencyQueue, RegularQueue& regularQueue) {
ifstream file("que.txt");
if (!file) return; // File doesn't exist, no data to load
string line, type, name, issue;
int age, priority;
while (getline(file, line)) {
istringstream iss(line);
getline(iss, type, ',');
getline(iss, name, ',');
iss >> age;
iss.ignore();
getline(iss, issue, ',');
if (type == "Emergency") {
iss >> priority;
emergencyQueue.enqueue(name, age, issue, priority);
} else {
regularQueue.enqueue(name, age, issue);
}
}
file.close();
}
// Main menu function to handle user input
void appointmentSystem() {
RegularQueue regularQueue;
EmergencyQueue emergencyQueue;
// Load existing patients from file
loadPatientsFromFile(emergencyQueue, regularQueue);
int choice;
do {
cout << "\n--- Hospital Appointment System ---\n";
cout << "1. Add Regular Patient\n";
cout << "2. Add Emergency Patient\n";
cout << "3. Serve Next Patient\n";
cout << "4. Display All Patients\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
string name, issue;
int age;
cout << "Enter patient's name: ";
cin.ignore();
getline(cin, name);
cout << "Enter patient's age: ";
cin >> age;
cout << "Enter patient's issue: ";
cin.ignore();
getline(cin, issue);
// Add patient to the regular queue
regularQueue.enqueue(name, age, issue);
} else if (choice == 2) {
string name, issue;
int age, priority;
cout << "Enter emergency patient's name: ";
cin.ignore();
getline(cin, name);
cout << "Enter patient's age: ";
cin >> age;
cout << "Enter patient's issue: ";
cin.ignore();
getline(cin, issue);
cout << "Enter emergency priority level (1-10, 10 = highest): ";
cin >> priority;
// Add patient to the emergency queue
emergencyQueue.enqueue(name, age, issue, priority);
} else if (choice == 3) {
// Serve the next patient based on priority
serveNextPatient(emergencyQueue, regularQueue);
} else if (choice == 4) {
// Display all patients in both queues
displayAllPatients(emergencyQueue, regularQueue);
} else if (choice == 5) {
cout << "Exiting the system.\n";
} else {
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
}
// Structure to represent an alert
struct Alert {
string timestamp;
string alertType;
string message;
};
// Class to manage the alert system using a stack
class AlertSystem {
private:
stack<Alert> alertStack; // Stack to store alerts
// Function to save a new alert to file
void saveAlertToFile(const Alert& alert) {
ofstream file("stack.txt", ios::app);
if (file.is_open()) {
file << alert.timestamp << "," << alert.alertType << "," << alert.message << "\n";
file.close();
} else {
cout << "Error opening file for writing alerts.\n";
}
}
// Function to load alerts from file during startup
void loadAlertsFromFile() {
ifstream file("stack.txt");
if (!file.is_open()) return;
string line, timestamp, alertType, message;
while (getline(file, line)) {
istringstream iss(line);
getline(iss, timestamp, ',');
getline(iss, alertType, ',');
getline(iss, message, ',');
Alert alert{timestamp, alertType, message};
alertStack.push(alert);