-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathwaitingroom.cpp
135 lines (107 loc) · 3.7 KB
/
waitingroom.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
/*This is an implementation of an emergency waiting room in an hospital
where patients are served on first come first serve basis.
Implemented using queues.*/
#include<iostream>
#include<string>
#include<stdio.h>
#include <queue>
using namespace std;
class Patient{ //patient class to store information about patients
public:
int id;
string name;
};
class Waiting_Room{ //wating room class has functions to manage the pateints in the room
public:
static queue <Patient> Patients;
static int id_gen;
void RegisterPatient(string);
void ServePatient();
void CancelAll();
bool CanDoctorGoHome();
void ShowAllPatient();
};
int Waiting_Room:: id_gen=1;
queue<Patient> Waiting_Room::Patients;
void Waiting_Room:: RegisterPatient(string _name){ //creatd patient objects and stores them in the patients queue
Patient newPatient;
newPatient.name=_name;
newPatient.id=id_gen++;
Patients.push(newPatient);
cout<<"Patient "<<newPatient.name<<" with ID: "<<newPatient.id<<" is Registered.\n";
}
void Waiting_Room:: ServePatient(){ //serves patients on the basis first come first serve
Patient temp=Patients.front();
cout<<"Patient "<<temp.name<<" with ID: "<<temp.id<<" is served.\n";
Patients.pop();
}
void Waiting_Room::CancelAll(){ //cancels all appointments
while(!Patients.empty()){
Patients.pop();//pops until empty
}
cout<<"\nAll apointments cancelled\n\n";
}
bool Waiting_Room::CanDoctorGoHome(){ //doctor goes home if all patients served
if(Patients.empty())
{
cout<<"All patients served. Doctor can go home.\n\n";
return true;
}
else{
cout<<"Patients Remaining. Doctor cannot go home.\n\n";
return false;
}
}
void Waiting_Room::ShowAllPatient(){ //sorts patient records in alphabetical order
vector<Patient> tempRecord;
while(!Patients.empty()){
tempRecord.push_back(Patients.front());
Patients.pop();
}
//sort by alphabetical order
for (int i=0;i<tempRecord.size();i++){
for(int j=0;j<tempRecord.size();j++){
if (tempRecord[i].name<tempRecord[j].name){
Patient temp;
temp=tempRecord[j];
tempRecord[j]=tempRecord[i];
tempRecord[i]=temp;
}
}
}
// Display the sorted patient records
cout<<"\nSorted Rcords:\n\n";
for (int i = 0; i < tempRecord.size(); i++) {
cout << "Patient ID: " << tempRecord[i].id << ", Name: " << tempRecord[i].name << endl;
}
cout<<"\n\n";
//sort by ids to go back to original queue
for (int i=0;i<tempRecord.size();i++){
for(int j=0;j<tempRecord.size();j++){
if (tempRecord[i].id<tempRecord[j].id){
Patient temp;
temp=tempRecord[j];
tempRecord[j]=tempRecord[i];
tempRecord[i]=temp;
}
}
}
//pushing it back into the Queue
for (int i = 0; i < tempRecord.size(); i++) {
Patients.push(tempRecord[i]);
}
}
int main(){
Waiting_Room r1;
r1.RegisterPatient("Ahmed");
r1.RegisterPatient("salman");
r1.RegisterPatient("basim");
r1.ShowAllPatient();
r1.ServePatient();
r1.CanDoctorGoHome();
r1.CancelAll();
r1.CanDoctorGoHome();
/* r1.ServePatient();
r1.ServePatient();
*/
}