-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhospital_management.cpp
More file actions
189 lines (168 loc) · 4.05 KB
/
hospital_management.cpp
File metadata and controls
189 lines (168 loc) · 4.05 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
#include <bits/stdc++.h>
using namespace std;
class Patient
{
public:
int patient_no;
int age;
string name;
string address;
int sevearity;
Patient(int patient_no = 0, int age = 0, string name = "", string address = "", int sevearity = 0)
{
this->patient_no = patient_no;
this->age = age;
this->name = name;
this->address = address;
this->sevearity = sevearity;
}
void display() const
{
cout << "patient_no : " << patient_no
<< " | Age : " << age
<< " | Name : " << name
<< " | Address : " << address
<< " | Sevearity : " << sevearity << endl;
}
};
struct CompareSeverity
{
bool operator()(const Patient &a, const Patient &b) const
{
return a.sevearity < b.sevearity;
}
};
class Hospital
{
map<int, Patient> patients;
priority_queue<Patient, vector<Patient>, CompareSeverity> pq;
string filename = "patientinfo.txt";
public:
Hospital()
{
loadfromfile();
}
void add_patient()
{
int id;
cout << "Enter Patient ID : ";
cin >> id;
cin.ignore();
int age, sevearity;
string name, address;
cout << "Enter Patient Name : ";
getline(cin, name);
cout << "Enter Patient Age : ";
cin >> age;
cin.ignore();
cout << "Enter Address : ";
getline(cin, address);
cout << "Enter Sevearity : ";
cin >> sevearity;
if (patients.find(id) != patients.end())
{
cout << "Patient Already exists!\n";
}
else
{
cout << "Patient added!\n";
patients[id] = Patient(id, age, name, address, sevearity);
pq.push(patients[id]);
savetofile();
}
}
void serve_patient()
{
if (pq.empty())
{
cout << "No patients!\n";
return;
}
Patient p = pq.top();
pq.pop();
cout << "Serving Patient: " << p.name << " (Severity: " << p.sevearity << ")\n";
// Remove from map too
patients.erase(p.patient_no);
savetofile();
}
void display()
{
if (patients.empty())
{
cout << "No Patients Available!\n";
return;
}
for (auto &b : patients)
{
b.second.display();
}
}
private:
void loadfromfile()
{
ifstream fin(filename);
if (!fin.is_open())
return;
patients.clear();
while (true)
{
int id, age, sevearity;
string name, address;
fin >> id >> age >> sevearity;
if (fin.fail())
break;
fin.ignore();
getline(fin, name);
getline(fin, address);
Patient p(id, age, name, address, sevearity);
patients[id] = p;
pq.push(p);
}
fin.close();
}
void savetofile()
{
ofstream fout(filename);
for (auto &entry : patients)
{
Patient p = entry.second;
fout << p.patient_no << " " << p.age << " " << p.sevearity << "\n";
fout << p.name << "\n";
fout << p.address << "\n";
}
fout.close();
}
};
int main()
{
Hospital p;
int choice;
do
{
cout << "\n------ Welcome To Hospital -------" << endl;
cout << "1. Add Patient" << endl;
cout << "2. Serve Patient" << endl;
cout << "3. Patient Details" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
p.add_patient();
break;
case 2:
p.serve_patient();
break;
case 3:
p.display();
break;
case 0:
cout << "Exiting...\n";
break;
default:
cout << "Invalid Choice\n";
}
} while (choice != 0);
return 0;
}