-
Notifications
You must be signed in to change notification settings - Fork 0
/
HospitalManagementSystem.java
485 lines (469 loc) · 17.5 KB
/
HospitalManagementSystem.java
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
import java.util.*;
import java.io.*;
class Hospital implements Serializable{
static String MonthsOfYear[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
static String DaysOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
static String am_pm[] = { "AM", "PM" };
static char[] adminPwd = "admin@xyzhospital".toCharArray();
static void Display() {
System.out.println("XYZ Hospital");
System.out.println("Phone Number: 1234567890");
System.out.println("Email: admin@xyzhospital.com");
System.out.println("Website: https://www.xyzhospital.org");
}
public static void clear() {
try {
if (System.getProperty("os.name").contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
} catch (IOException | InterruptedException ex) {}
}
public static void WriteObjectToFile(Object serObj, File fileName) {
try {
FileOutputStream fileOut = new FileOutputStream(fileName, true);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
objectOut.close();
System.out.println("The Object was succesfully written to a file");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void printRecords(File RecordFile) {
try {
if (RecordFile.length()!=0) {
FileInputStream F = new FileInputStream(RecordFile);
ObjectInputStream input = new ObjectInputStream(F);
Object obj = input.readObject();
while(obj!=null) {
try {
if (obj != null) {
if (obj instanceof Doctor) {
System.out.println("\n Name" + "\t\t\t Phone Number" + "\t\t\t Qualification" + "\t\t\t Specialization");
Doctor d = (Doctor) obj;
System.out.println('\n' + d.Name + "\t\t" + d.PhoneNumber + "\t\t\t" + d.Qualification + "\t\t" + d.Specialization);
}
else if (obj instanceof Patient) {
System.out.println("\n Name" + "\t\t\t Age" + "\t\t\t Address" + "\t\t\t Phone Number" + "\t\t\t Blood Group" + "\t\t\t Gender" + "\t\t\t Doctor Name" + "\t\t\t Appointment Date");
Patient p = (Patient) obj;
System.out.println('\n' + p.Name + "\t\t" + p.Age + "\t\t" + p.Address + "\t\t" + p.PhoneNumber + "\t\t" + p.BloodGroup + "\t\t" + p.Gender + "\t\t" + p.DocName + "\t\t" + p.AppDate);
}
obj = input.readObject();
}
} catch (Exception e) {
break;
}
}
input.close();
F.close();
}
else
System.out.println("File Empty.");
} catch (Exception ex) {
System.out.println("An error occurred.");
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int choice = 0;
do {
clear();
Calendar calendar = Calendar.getInstance();
System.out.print("Date: " + MonthsOfYear[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
System.out.print("\t\t\t\t\t\t\t\tDay: " + DaysOfWeek[calendar.get(Calendar.DAY_OF_WEEK)-1]);
System.out.print("\t\t\t\t\t\t\t\tTime: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND) + " " + am_pm[calendar.get(Calendar.AM_PM)]);
System.out.print("\n\nWelcome to ");
Display();
System.out.println("\nWhich Portal would you like to access?" + "\n1. Doctor" + "\n2. Patient" + "\n3. Admin" + "\n4. Exit");
System.out.print("Enter option number: ");
choice = sc.nextInt();
switch(choice) {
case 1: Doctor d = new Doctor();
break;
case 2: Patient p = new Patient();
break;
case 3: System.out.println("Enter Password: ");
Console con = System.console();
sc.nextLine();
char[] pwd = con.readPassword();
if (!Arrays.equals(pwd,adminPwd)) {
System.out.println("\nIncorrect Password. Entry to admin portal denied!");
sc.nextLine();
continue;
}
Admin a = new Admin();
break;
case 4: clear();
System.out.print("\nThank you for showing your trust in ");
Display();
break;
default: System.out.println("Invalid Choice. Please Re-enter.");
}
}while(choice!=4);
}
}
class Doctor extends Hospital {
String Name, PhoneNumber, Qualification, Specialization, AddDate, AddTime;
Doctor() {
Scanner sc = new Scanner(System.in);
int choice = 0;
do {
clear();
Calendar calendar = Calendar.getInstance();
System.out.print("Date: " + MonthsOfYear[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
System.out.print("\t\t\t\t\t\t\t\tDay: " + DaysOfWeek[calendar.get(Calendar.DAY_OF_WEEK)]);
System.out.print("\t\t\t\t\t\t\t\tTime: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND) + " " + am_pm[calendar.get(Calendar.AM_PM)]);
System.out.print("\n\nWelcome to the Doctor Portal of ");
Display();
System.out.println("\nWhat would you like to do?" + "\n1. View Patient Appointments" + "\n2. Back to Main Menu");
System.out.print("Enter option number: ");
choice = sc.nextInt();
switch(choice) {
case 1: clear();
ViewAppointments();
sc.nextLine();
sc.nextLine();
break;
case 2: break;
default: System.out.println("Invalid Choice. Please Re-enter.");
}
}while(choice!=2);
}
Doctor (String Name, String PhoneNumber, String Qualification, String Specialization, String AddDate, String AddTime) {
this.Name = Name;
this.PhoneNumber = PhoneNumber;
this.Qualification = Qualification;
this.Specialization = Specialization;
this.AddDate = AddDate;
this.AddTime = AddTime;
File F = new File("DoctorRecords.txt");
WriteObjectToFile(this, F);
}
static void ViewAppointments() {
Display();
Scanner sc = new Scanner(System.in);
String nm;
System.out.print("\n\nEnter your name to view the appointments booked with you: ");
nm = sc.nextLine();
boolean deleted = false;
try {
if ("PatientRecords.txt".length()!=0) {
FileInputStream F = new FileInputStream("PatientRecords.txt");
ObjectInputStream input = new ObjectInputStream(F);
Object obj = input.readObject();
File temp = new File("temp.txt");
while(obj!=null) {
try {
if (obj != null) {
if (obj instanceof Patient) {
Patient p= (Patient) obj;
if (nm.equalsIgnoreCase(p.DocName)) {
p.getDetails();
}
}
obj = input.readObject();
}
} catch (Exception e) {
break;
}
}
input.close();
F.close();
}
else
System.out.println("File Empty.");
} catch (Exception ex) {
System.out.println("An error occurred.");
}
}
}
class Patient extends Hospital {
String Name, Address, BloodGroup, Gender, PhoneNumber, DocName, AppDate, BookDate, BookTime;
int Age;
Patient() {
Scanner sc = new Scanner(System.in);
int choice = 0;
do {
clear();
Calendar calendar = Calendar.getInstance();
System.out.print("Date: " + MonthsOfYear[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
System.out.print("\t\t\t\t\t\t\t\tDay: " + DaysOfWeek[calendar.get(Calendar.DAY_OF_WEEK)]);
System.out.print("\t\t\t\t\t\t\t\tTime: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND) + " " + am_pm[calendar.get(Calendar.AM_PM)]);
System.out.print("\nWelcome to the Patient Portal of ");
Display();
System.out.println("\nWhat would you like to do?" + "\n1. View Hospital Facilities" + "\n2. View Doctor List" + "\n3. Book an Appointment" + "\n4. Back to Main Menu");
System.out.print("Enter option number: ");
choice = sc.nextInt();
switch(choice) {
case 1: clear();
viewFacilities();
sc.nextLine();
sc.nextLine();
break;
case 2: clear();
File f = new File("DoctorRecords.txt");
printRecords(f);
sc.nextLine();
sc.nextLine();
break;
case 3: getDetails();
File F = new File("PatientRecords.txt");
WriteObjectToFile(this, F);
System.out.println("\n\nBooking Successful. Here are the details we got from you:-");
printDetails();
sc.nextLine();
sc.nextLine();
break;
case 4: break;
default: System.out.println("Invalid Choice. Please Re-enter.");
}
}while(choice!=4);
}
void getDetails() {
Scanner sc = new Scanner(System.in);
System.out.print("Patient Name: ");
Name = sc.nextLine();
System.out.print("Patient Age: ");
Age = sc.nextInt();
System.out.print("Patient Gender: ");
sc.nextLine();
Gender = sc.nextLine();
System.out.print("Patient Address: ");
Address = sc.nextLine();
System.out.print("Patient Contact Number: ");
PhoneNumber = sc.nextLine();
System.out.println("\n\n");
if ("PatientRecords.txt".length()==0) {
do {
File f = new File("DoctorRecords.txt");
printRecords(f);
System.out.print("\n\nDoctor Name: ");
DocName = sc.nextLine();
Calendar calendar = Calendar.getInstance();
BookDate = MonthsOfYear[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.YEAR);
System.out.print("Appointment date eg: " + BookDate + ": ");
AppDate = sc.nextLine();
BookTime = calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND);
} while (!checkFreeApp(DocName, AppDate));
}
else {
File f = new File("DoctorRecords.txt");
printRecords(f);
System.out.print("\n\nDoctor Name: ");
DocName = sc.nextLine();
Calendar calendar = Calendar.getInstance();
BookDate = MonthsOfYear[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.YEAR);
System.out.print("Appointment date eg: " + BookDate + ": ");
AppDate = sc.nextLine();
BookTime = calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND);
}
}
void printDetails() {
System.out.println("Patient Name: " + Name);
System.out.println("Patient Age: " + Age);
System.out.println("Patient Gender: " + Gender);
System.out.println("Patient Address: " + Address);
System.out.println("Patient Contact Number: " + PhoneNumber);
System.out.println("Book Date: " + BookDate);
System.out.println("Book Time: " + BookTime);
}
public static boolean checkFreeApp(String DocName, String AppDate) {
int count=0;
try {
System.out.println("Doctor Name\t\t Contact Number \t Qualification\t\tSpecialization");
Scanner pw = new Scanner(new BufferedReader(new FileReader("DoctorRecords.txt")));
int i=0;
while(pw.hasNext()) {
String line = pw.nextLine();
String[] lineparts = line.split("\t", -1);
if (lineparts[7].equalsIgnoreCase(DocName) && lineparts[8].equalsIgnoreCase(AppDate))
count = count + 1;
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
if (count>10) {
System.out.println("Appointments for " + DocName + " for " + AppDate + " is full. Please try again for another doctor/date.");
return false;
}
else {
System.out.println("Appointment Successful.");
return true;
}
}
void viewFacilities() {
System.out.println("\n Radiology (X-Ray) \n Sonography \n CT Scan \n Physiotherapy \n ECG \n Ambulance Services \n Laboratory Services");
}
}
class Admin extends Hospital {
Admin() {
Scanner sc = new Scanner(System.in);
int choice = 0;
do {
clear();
Calendar calendar = Calendar.getInstance();
System.out.print("Date: " + MonthsOfYear[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
System.out.print("\t\t\t\t\t\t\t\tDay: " + DaysOfWeek[calendar.get(Calendar.DAY_OF_WEEK)]);
System.out.print("\t\t\t\t\t\t\t\tTime: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND) + " " + am_pm[calendar.get(Calendar.AM_PM)]);
System.out.print("\n\nWelcome to the Admin Portal of ");
Display();
System.out.println("\nWhat would you like to do?" + "\n1. Add to Doctor List" + "\n2. Delete from Doctor List" + "\n3. Allot Room" + "\n4. View Room Allotment Details" + "\n5. Back to Main Menu");
System.out.print("Enter option number: ");
choice = sc.nextInt();
switch(choice) {
case 1: AddDoc();
sc.nextLine();
sc.nextLine();
break;
case 2: if (DeleteDoc())
System.out.println("\nRecord Deleted Successfully");
else
System.out.println("\nRecord not Found");
sc.nextLine();
sc.nextLine();
break;
case 3: AllotRoom();
sc.nextLine();
sc.nextLine();
break;
case 4: ViewRoomAllotment();
sc.nextLine();
sc.nextLine();
break;
case 5: break;
default: System.out.println("Invalid Choice. Please Re-enter.");
}
} while(choice!=5);
}
static void AddDoc() {
Scanner sc = new Scanner(System.in);
String Name, PhoneNumber, Qualification, Specialization, AddDate, AddTime;
System.out.print("Doctor Name: ");
Name = sc.nextLine();
System.out.print("Doctor Contact Number: ");
PhoneNumber = sc.nextLine();
System.out.print("Doctor Qualification: ");
Qualification = sc.nextLine();
System.out.print("Doctor Specialization: ");
Specialization = sc.nextLine();
Calendar calendar = Calendar.getInstance();
AddDate = MonthsOfYear[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.YEAR);
AddTime = calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND);
Doctor d = new Doctor(Name, PhoneNumber, Qualification, Specialization, AddDate, AddTime);
}
static boolean DeleteDoc() {
Scanner sc = new Scanner(System.in);
String nm;
System.out.print("Enter doctor name to delete her/his details from the list: ");
nm = sc.nextLine();
boolean deleted = false;
try {
if ("DoctorRecords.txt".length()!=0) {
FileInputStream F = new FileInputStream("DoctorRecords.txt");
ObjectInputStream input = new ObjectInputStream(F);
Object obj = input.readObject();
File temp = new File("temp.txt");
while(obj!=null) {
try {
if (obj != null) {
if (obj instanceof Doctor) {
Doctor d = (Doctor) obj;
if (nm.equalsIgnoreCase(d.Name)) {
deleted = true;
}
else {
WriteObjectToFile(d, temp);
}
}
obj = input.readObject();
}
} catch (Exception e) {
break;
}
}
input.close();
F.close();
File oldFile = new File("DoctorRecords.txt");
File newFile = new File("temp.txt");
oldFile.delete();
newFile.renameTo(oldFile);
}
else
System.out.println("File Empty.");
} catch (Exception ex) {
System.out.println("An error occurred.");
}
return deleted;
}
static void AllotRoom() {
Scanner sc = new Scanner(System.in);
String PatientName, PatientId, RoomNo, TypeOfRoom="";
int ServiceCharges = 0;
System.out.print("\nPatient Name: ");
PatientName = sc.nextLine();
System.out.print("Patient id: ");
PatientId = sc.nextLine();
System.out.print("Room no: ");
RoomNo = sc.nextLine();
int RoomType = 0;
do {
System.out.print("\nSelect your Room Type:- \n1. General Ward \n2. Semi-ward \n3. Deluxe Room \nEnter option number: ");
RoomType = sc.nextInt();
switch(RoomType){
case 1:
System.out.println("You have alloted General Ward.");
ServiceCharges = 500;
TypeOfRoom = "General Ward";
break;
case 2:
System.out.println("You have alloted Semi-ward.");
ServiceCharges = 1000;
TypeOfRoom = "Semi-ward";
break;
case 3:
System.out.println("You have alloted Deluxe room.");
ServiceCharges = 1500;
TypeOfRoom = "Deluxe Room";
break;
default:
System.out.println("Invalid option. Please re-enter!");
}
} while(RoomType<1 || RoomType>3);
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter pw = null;
try {
fw = new FileWriter("AllotedRooms.txt", true);
bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
pw.write("\n" + PatientName + "\t" + PatientId + "\t" + RoomNo + "\t" + TypeOfRoom + "\t" + ServiceCharges);
pw.close();
} catch (IOException e) {
System.out.println("An error occurred.");
}
}
static void ViewRoomAllotment() {
if("AllotedRooms.txt".length()>0) {
System.out.println("\n\nPatient Name \t\t Patient Id \t Room Number \t\t TypeOfRoom \t\t Service Charges");
try {
Scanner pw = new Scanner(new BufferedReader(new FileReader("AllotedRooms.txt")));
int i;
while(pw.hasNext()) {
String line = pw.nextLine();
String[] lineparts = line.split("\t", -1);
i=0;
for(String data : lineparts)
if (i<5) {
System.out.print(data + "\t\t");
i++;
}
System.out.println();
}
} catch (IOException e) {
System.out.println("File Empty.");
}
}
}
}