-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhospital.py
307 lines (253 loc) · 9.23 KB
/
hospital.py
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
#########################################
############### Python ##################
######### Hospital Management ###########
#########################################
import csv
import os
from datetime import date
def getInt(msg):
data = input(msg)
try:
return int(data)
except ValueError:
print('Error: Input must be an integer!')
return getInt(msg)
def getFloat(msg):
data = input(msg)
try:
return float(data)
except ValueError:
print('Error: Input must be an floating point value!')
return getFloat(msg)
def uniqueID():
file = open('Patient.csv', 'r', newline='\r\n')
read = csv.reader(file)
id = getInt('Enter Patient ID: ')
exist = 0
for record in read:
if int(record[0]) == id:
exist += 1
if exist != 0:
print('Error: Patient with ID', id, 'already exists!')
uniqueID()
file.close()
return id
def newPatient():
print("Add a new Patient Record")
print("=========================")
file = open('Patient.csv', 'a', newline='\r\n')
write = csv.writer(file)
id = uniqueID()
name = input('Enter Patient Name: ')
illness = input('Enter Illness: ')
fee = getFloat('Enter Fee: ')
doctor = input('Enter name of Doctor: ')
print("----------------------------------------------------")
record = [id, date.today(), name, illness, fee, doctor]
write.writerow(record)
file.close()
print("Info: Patient Record Saved")
def editPatient():
print("Modify a Patient Record")
print("=========================")
file_r = open('Patient.csv', 'r', newline='\r\n')
file_w = open('temp.csv', 'a', newline='\r\n')
read = csv.reader(file_r)
write = csv.writer(file_w)
id = getInt('Enter ID of Patient whose record is to be modified: ')
exist = 0
for record in read:
if int(record[0]) == id:
exist += 1
date = record[1]
name = record[2]
illness = record[3]
fee = record[4]
doctor = record[5]
print("-------------------------------")
print("(1) Patient Name:", name)
print("(2) Illness:", illness)
print("(3) Fee:", fee)
print("(4) Name of Doctor:", doctor)
print("-------------------------------")
choice = getInt("Enter your Choice: ")
if choice == 1:
name = input('Enter new Patient Name: ')
elif choice == 2:
illness = input('Enter Illness: ')
elif choice == 3:
fee = getFloat('Enter Fee: ')
elif choice == 4:
doctor = input('Enter name of Doctor: ')
else:
print('Info: Patient Record is unchanged!')
record = [id, date, name, illness, fee, doctor]
write.writerow(record)
if 1 <= choice <= 4:
print("Info: Patient Record Modified!")
else:
write.writerow(record)
if exist == 0:
print('Error: Patient with ID', id, 'does not exist!')
file_r.close()
file_w.close()
os.remove("Patient.csv")
os.rename("temp.csv", "Patient.csv")
def delPatient():
print("Delete a Patient Record")
print("=========================")
file_r = open('Patient.csv', 'r', newline='\r\n')
file_w = open('temp.csv', 'a', newline='\r\n')
read = csv.reader(file_r)
write = csv.writer(file_w)
id = getInt('Enter ID of Patient whose record is to be deleted: ')
exist = 0
for record in read:
if int(record[0]) == id:
exist += 1
print("-------------------------------")
print("Day of Registration:", record[1])
print("Patient Name:", record[2])
print("Illness:", record[3])
print("Fee:", record[4])
print("Name of Doctor:", record[5])
print("-------------------------------")
choice = input("Do you want to delete the above record (y/n): ")
if choice == 'y' or choice == 'Y':
pass
print("Info: Patient Record Deleted!")
else:
print('Info: Patient Record is unchanged!')
write.writerow(record)
else:
write.writerow(record)
if exist == 0:
print('Error: Patient with ID', id, 'does not exist!')
file_r.close()
file_w.close()
os.remove("Patient.csv")
os.rename("temp.csv", "Patient.csv")
def searchPatient():
print("Search a Patient Record")
print("=======================")
file = open('Patient.csv', 'r', newline='\r\n')
read = csv.reader(file)
id = getInt('Enter ID to search for Patient: ')
exist = 0
for record in read:
if int(record[0]) == id:
exist += 1
print("-------------------------------")
print("Day of Registration:", record[1])
print("Patient Name:", record[2])
print("Illness:", record[3])
print("Fee:", record[4])
print("Name of Doctor", record[5])
print("-------------------------------")
if exist == 0:
print('Error: Patient with ID', id, 'does not exist!')
file.close()
def listPatients():
choice = 0
while choice != 3:
print("(1) List All Patients")
print("(2) List Patients by Day")
print("(3) Return to Main Menu")
print("--------------------------------")
choice = getInt('Enter your Choice: ')
print("--------------------------------", end="\n")
file = open('Patient.csv', 'r', newline='\r\n')
read = csv.reader(file)
if choice == 1:
print("====================================================================================================")
print(" List of All Patients")
print("====================================================================================================")
count = 0
print("ID\t\tDATE\t\t\tNAME\t\tILLNESS\t\tFEE\t\tDOCTOR")
for record in read:
print(record[0], end="\t\t")
print(record[1], end="\t\t")
print(record[2], end="\t\t")
print(record[3], end="\t\t")
print(record[4], end="\t\t")
print(record[5])
count += 1
print("----------------------------------------------------------------------------------------------------")
print('Total: ', count, end="\n")
break
elif choice == 2:
day = input('Enter Day of Registration (YYYY-MM-DD): ')
count = 0
for record in read:
if record[1] == day:
count += 1
if count == 0:
print('Info: No Patients were registered on', day)
break
file.seek(0)
print("=======================================================================")
print(" List of Patients registered on", day)
print("=======================================================================")
print("ID\t\tNAME\t\tILLNESS\t\tFEE\t\tDOCTOR")
for record in read:
if record[1] == day:
print(record[0], end="\t\t")
print(record[2], end="\t\t")
print(record[3], end="\t\t")
print(record[4], end="\t\t")
print(record[5])
print(
"-----------------------------------------------------------------------")
print('Total: ', count, end="\n")
break
elif choice == 3:
break
else:
print("Error: Invalid choice, please choose again")
file.close()
listPatients()
break
file.close()
def keypress():
input("Press any key to continue...")
def menu():
choice = 0
while choice != 6:
print("\n")
print("|----------------------------|")
print("| Hospital Management System |")
print("| ---------------------------|")
print('\n')
print("=============================")
print(" MENU")
print("=============================")
print("(1) Add a new Patient Record")
print("(2) Modify Existing Patient")
print("(3) Delete Existing Patient")
print("(4) Search for Patient")
print("(5) List Patients")
print("(6) Exit")
print("--------------------------------")
choice = getInt('Enter your Choice: ')
print("--------------------------------", end="\n")
if choice == 1:
newPatient()
keypress()
elif choice == 2:
editPatient()
keypress()
elif choice == 3:
delPatient()
keypress()
elif choice == 4:
searchPatient()
keypress()
elif choice == 5:
listPatients()
keypress()
elif choice == 6:
print("System Exited...")
break
else:
print("Error: Invalid choice, please choose again")
menu()