· 8 years ago · Jan 24, 2018, 05:48 PM
1import java.io.File;
2import java.io.FileWriter;
3import java.util.Enumeration;
4import java.util.Hashtable;
5import java.util.Iterator;
6import java.util.Scanner;
7import java.util.Set;
8
9
10public class ManagePatients
11{
12
13 public static Scanner sc = new Scanner(System.in);
14 // Create hash table with a keyed list
15 public static Hashtable<String, Patient> patients = new Hashtable<String, Patient>();
16
17 // Create hash table with a keyed list
18 public static Hashtable<String, Surgeon> surgeons = new Hashtable<String, Surgeon>();
19
20 public static void main(String[] args) throws Exception
21 {
22 boolean doWrite = true;
23 try
24 {
25 // Create scanner which uses the patients.txt file as input
26 Scanner scanner = null;
27
28 try
29 {
30 scanner = new Scanner(new File("patients.txt"));
31 }
32 catch (Exception e)
33 {
34
35 }
36
37
38 // loop until the end of the patients file
39 while (scanner!= null && scanner.hasNext())
40 {
41 // Get line from patients file
42 String patientLine = scanner.nextLine();
43
44 // split line on tab to get patient details
45 String[] bits = patientLine.split("\t");
46
47 // If there are not 6 items delimeted by tab then this file is invalid
48 if (bits.length != 6)
49 {
50 throw new FileReadException("BAD PATIENT FILE");
51 }
52
53 // Create Patient object if the first term is 'Patient'
54 if (bits[0].equalsIgnoreCase("patient"))
55 {
56 patients.put(bits[1], new Patient(bits[1], bits[2], bits[3], bits[4], bits[5]));
57 }
58 // Create Patient object if the first term is 'Inpatient'
59 else if (bits[0].equalsIgnoreCase("inpatient"))
60 {
61 patients.put(bits[1], new Inpatient(bits[1], bits[2], bits[3], bits[4], bits[5]));
62 }
63 // If neither Patient or Inpatient then the file is bad
64 else
65 {
66 throw new FileReadException("BAD PATIENT FILE");
67 }
68
69
70 }
71
72 scanner = null;
73
74 try
75 {
76 scanner = new Scanner(new File("surgeons.txt"));
77 }
78 catch (Exception e)
79 {
80
81 }
82
83 // loop until the end of the surgeon file
84 while (scanner != null && scanner.hasNext())
85 {
86 // Get line from surgeon file
87 String surgeonLine = scanner.nextLine();
88
89 // split line on tab to get surgeon details
90 String[] bits = surgeonLine.split("\t");
91 if (bits.length < 3)
92 {
93 throw new FileReadException("BAD SURGEON FILE");
94 }
95
96 // Create GeneralSurgeon object if the first term is 'General'
97 if (bits[0].equalsIgnoreCase("general"))
98 {
99 // If there are not 3 items delimeted by tab then this file is invalid
100 if (bits.length != 3)
101 {
102 throw new FileReadException("BAD SURGEON FILE");
103 }
104 try
105 {
106 //if this throws exception, convert to double
107 Double.parseDouble(bits[2]);
108 }
109 catch (Exception e)
110 {
111 throw new FileReadException("BAD SURGEON FILE");
112 }
113 //create new surgeon and put into hash table
114 surgeons.put(bits[1], new GeneralSurgeon(bits[1], Double.parseDouble(bits[2])));
115 }
116 // Create SpecialistSurgeon object if the first term is 'Specialist'
117 else if (bits[0].equalsIgnoreCase("specialist"))
118 {
119 // If there are not 5 items delimeted by tab then this file is invalid
120 if (bits.length != 5)
121 {
122 throw new FileReadException("BAD SURGEON FILE");
123 }
124 try
125 {
126 //tries to convert string to double, throws exception if either is not
127 Double.parseDouble(bits[2]);
128 Double.parseDouble(bits[4]);
129 }
130 catch (Exception e)
131 {
132 throw new FileReadException("BAD SURGEON FILE");
133 }
134 //creates surgeon and puts into hash table
135 surgeons.put(bits[1], new SpecialistSurgeon(bits[1], Double.parseDouble(bits[2]), bits[3], Double.parseDouble(bits[4])));
136 }
137 // If neither specialist or general then the file is bad
138 else
139 {
140 throw new FileReadException("BAD SURGEON FILE");
141 }
142 }
143
144 //menu
145 String selectionInput;
146 int selection = 0;
147 boolean isError;
148 do
149 {
150 //get user input
151 printMenu();
152 selectionInput = sc.nextLine();
153
154 // reset error flag
155 isError = false;
156 try
157 {
158 //transform string into an integer
159 selection = Integer.parseInt(selectionInput);
160 }
161 catch (Exception e)
162 {
163 //default to 0
164 selection= 0;
165 }
166
167 try
168 {
169 // process user's selection
170 switch(selection)
171 {
172 case 1 :
173 displayAllPatients();
174 break;
175 case 2 :
176 admitPatient();
177 break;
178 case 3 :
179 scheduleProcedure();
180 break;
181 case 4 :
182 recordProcedure();
183 break;
184 case 5 :
185 cancelProcedure();
186 break;
187 case 6 :
188 dischargePatient();
189 break;
190 case 7 :
191 addPatient();
192 break;
193 case 8 :
194 addSurgeon();
195 break;
196 case 9 :
197 displaySurgeons();
198 break;
199
200 case 10 :
201 System.out.println("Exiting...");
202 break;
203 default :
204 isError = true;
205 }
206 //if selection is invalid, output error message
207 if (isError == true)
208 {
209 System.out.println("Invalid selection! Please try again...");
210 }
211 }
212 //catch exception and output error message
213 catch (Exception e)
214 {
215 System.out.println(e.getLocalizedMessage());
216 }
217 //loop until user selects to exit
218 } while (selection != 10);
219
220 }
221 //file read exception
222 catch (FileReadException fre)
223 {
224 doWrite = false;
225 }
226 //print out error message if error is thrown
227 catch (Exception e)
228 {
229 System.out.println(e.getLocalizedMessage());
230 }
231 //no matter what happens, do this>>>
232 finally
233 {
234 if (doWrite)
235 {
236 //Use file writer to overwrite contents of patients.txt
237 FileWriter fw = new FileWriter(new File("patients.txt"));
238 //Loop through patients enumeration
239 Enumeration<Patient> pats = patients.elements();
240 while (pats.hasMoreElements())
241 {
242 //write type of patient
243 Patient patient = pats.nextElement();
244 if (patient instanceof Inpatient)
245 {
246 fw.write("Inpatient\t");
247 }
248 else
249 {
250 fw.write("Patient\t");
251 }
252 //writing patient data to file
253 fw.write(patient.fileString());
254 fw.write("\n");
255 //flush contents in file writer to disk
256 fw.flush();
257 }
258 //releasing the lock on the file
259 fw.close();
260
261
262 //Use file writer to overwrite contents of surgeons.txt
263 fw = new FileWriter(new File("surgeons.txt"));
264 //Loop through surgeons enumeration
265 Enumeration<Surgeon> surg = surgeons.elements();
266 while (surg.hasMoreElements())
267 {
268 //write type of surgeon
269 Surgeon surgeon = surg.nextElement();
270 if (surgeon instanceof SpecialistSurgeon)
271 {
272 fw.write("Specialist\t");
273 }
274 else
275 {
276 fw.write("General\t");
277 }
278 //writing surgeon data to file
279 fw.write(surgeon.fileString());
280 fw.write("\n");
281 //flush contents in file writer to disk
282 fw.flush();
283 }
284 //releasing the lock on the file
285 fw.close();
286 }
287 }
288 }
289
290 public static void printMenu()
291 {
292 System.out.printf(" SportsFix Inc. Menu \n\n");
293 System.out.printf("%-30s%s\n", "Display All Patients", "1");
294 System.out.printf("%-30s%s\n", "Admit Patient", "2");
295 System.out.printf("%-30s%s\n", "Schedule Inpatient Procedure", "3");
296 System.out.printf("%-30s%s\n", "Record Procedure", "4");
297 System.out.printf("%-30s%s\n", "Cancel Procedure(s)", "5");
298 System.out.printf("%-30s%s\n", "Discharge Patient", "6");
299 System.out.printf("%-30s%s\n", "Add new Patient", "7");
300 System.out.printf("%-30s%s\n", "Add new Surgeon", "8");
301 System.out.printf("%-30s%s\n", "Display all Surgeons", "9");
302 System.out.printf("%-30s%s\n\n", "Exit", "10");
303 System.out.print("Enter your selection : ");
304 }
305
306 // method used whenever a specific Patient object needs to be found
307 public static Patient getPatient(String patientNo)
308 {
309 if (patients.containsKey(patientNo)){
310 return patients.get(patientNo);
311 }
312 else
313 {
314 return null;
315 }
316
317 }
318
319 // method for displaying all patients
320 public static void displayAllPatients()
321 {
322 // print final summary of all patient details
323 System.out.println("\n*** Final Patient Details Summary ***\n\n");
324
325 // print headers (required some fiddling with field widths)
326 System.out.printf("%-12s%-15s%-12s%-13s%-20s%-13s%s\n",
327 "Patient No", "Patient Name", "Proc Date",
328 "Proc Length", "Injury", "Doctor",
329 "Patient Status");
330
331 System.out.printf("%-12s%-15s%-12s%-13s%-20s%-13s%s\n\n",
332 "-----------", "--------------", "----------",
333 "------------", "-------", "-------",
334 "---------------");
335
336 // print details for each patient
337 Set<String> patientSet = patients.keySet();
338
339 //Just repeat for each key in the set
340 Iterator<String> it = patientSet.iterator();
341
342 //repeat for each key in the set
343 while (it.hasNext())
344 {
345 String key = it.next();
346 Patient p = patients.get(key);
347 p.print();
348 System.out.println("----------------------\n");
349 }
350 System.out.println();
351 }
352
353 public static void admitPatient() throws Exception
354 {
355 String patientNo;
356 Patient p;
357
358 System.out.print("Enter Patient No: ");
359 patientNo = sc.nextLine();
360
361 // find patient with specified patient number
362 p = getPatient(patientNo);
363
364 // patient not found
365 if (p == null)
366 {
367 throw new Exception("Error - patient " + patientNo + " not found!");
368 }
369 else
370 {
371 p.admit();
372
373 System.out.println("- Patient " + patientNo +
374 " admitted successfully!");
375 }
376 System.out.println();
377 }
378
379 // method for scheduling procedures
380 public static void scheduleProcedure() throws Exception
381 {
382 String patientNo;
383 String procedure, doctor;
384 boolean result;
385 Patient p;
386
387 System.out.print("Enter Patient No: ");
388 patientNo = sc.nextLine();
389
390 // find patient with specified patient number
391 p = getPatient(patientNo);
392
393 // patient not found
394 if (p == null)
395 {
396 throw new Exception("Error - patient " + patientNo + " not found!");
397 }
398
399 // patient object is not an Inpatient
400 else if (!(p instanceof Inpatient))
401 {
402 System.out.println("Error! " +
403 "Cannot schedule additional procedures " +
404 "for basic Patients!");
405 }
406 else
407 {
408 // get required input and call schedule procedure method
409 System.out.print("\nEnter procedure description: ");
410 procedure = sc.nextLine();
411
412 System.out.print("\nEnter doctor name: ");
413 doctor = sc.nextLine();
414
415 //if doctor does not exist in hash table, throw error
416 if(!surgeons.containsKey(doctor))
417 {
418 throw new Exception ("Doctor Name does now Exist");
419 }
420
421 // need to apply a typecast to the Patient reference to turn it
422 // into an Inpatient (subclass) reference so that we can call
423 // the scheduleProcedure() method
424
425 Inpatient inp = (Inpatient)p;
426
427 result = inp.scheduleProcedure(procedure, doctor);
428
429 // check result to see if patient was admitted
430 if (result == true)
431 {
432 System.out.println("- New procedure scheduled for patient " +
433 patientNo + " successfully!");
434 }
435 else
436 {
437 throw new Exception("- New procedure could not be scheduled " +
438 "for patient " + patientNo + "!");
439 }
440 }
441 System.out.println();
442 }
443
444 public static void recordProcedure() throws Exception
445 {
446 String patientNo;
447 String notes;
448 double procedureTime;
449 Patient p;
450
451 System.out.print("Enter Patient No: ");
452 patientNo = sc.nextLine();
453
454 // find patient with specified patient number
455 p = getPatient(patientNo);
456
457 // patient not found
458 if (p == null)
459 {
460 throw new Exception("Error - patient " + patientNo + " not found!");
461 }
462 else
463 {
464 // get required input and call schedule procedure method
465 System.out.print("\nEnter procedure length: ");
466 procedureTime = sc.nextDouble();
467
468 if(sc.hasNextLine())
469 {
470 sc.nextLine();
471 }
472
473 System.out.print("\nEnter procedure notes: ");
474 notes = sc.nextLine();
475
476
477 p.recordProcedure(procedureTime, notes);
478
479 System.out.println("- Procedure notes recorded for patient " +
480 patientNo + " successfully!");
481 }
482 System.out.println();
483 }
484
485 public static void cancelProcedure() throws Exception
486 {
487 String patientNo;
488 Patient p;
489
490 System.out.print("Enter Patient No: ");
491 patientNo = sc.nextLine();
492
493 // find patient with specified patient number
494 p = getPatient(patientNo);
495
496 // patient not found
497 if (p == null)
498 {
499 throw new Exception("- Error - patient " + patientNo + " not found!");
500 }
501 else
502 {
503 p.cancel();
504
505 System.out.println("- Patient " + patientNo +
506 "'s procedure(s) have been " +
507 "cancelled successfully!");
508
509 }
510 System.out.println();
511 }
512
513 public static void dischargePatient() throws Exception
514 {
515 String patientNo;
516 Patient p;
517
518 System.out.print("Enter Patient No: ");
519 patientNo = sc.nextLine();
520
521 // find patient with specified patient number
522 p = getPatient(patientNo);
523
524 // patient not found
525 if (p == null)
526 {
527 throw new Exception("- Error - patient " + patientNo + " not found!");
528 }
529 else
530 {
531 p.discharge();
532
533 String sdoc = p.getSupervisingDoctor();
534//calculate patients fee
535 double len = p.getProcedureLength();
536 Surgeon doc = surgeons.get(sdoc);
537 double fee = doc.calculateFee(len);
538 //if the patient is an inpatient, calculate the hospital daily fee
539 if (p instanceof Inpatient)
540 {
541 //calculating the daily hospital fee
542 int daycharge = (int)(len / 24) * 100;
543 if (len % 24 > 0)
544 {
545 daycharge += 100;
546 }
547//add hospital fee to total fee
548 fee += (double)daycharge;
549
550 }
551 System.out.printf("Patient %s owes doctor %s $%2.2f\n", p.getPatientName(), sdoc, fee);
552
553 System.out.println("- Patient " + patientNo +
554 " discharged successfully!");
555 }
556 System.out.println();
557 }
558
559
560 public static void addPatient() throws Exception
561 {
562 //user input for patient or inpatient
563 System.out.print("Create Inpatient (1) or Outpatient (2): ");
564 String type = sc.nextLine();
565 if (type.charAt(0) != '1' &&
566 type.charAt(0) != '2')
567 {
568 throw new Exception("Bad patient type choice.");
569 }
570//user input for patient ID and checks if ID is already stored
571 System.out.print("New Patient ID: ");
572 String id = sc.nextLine();
573 if (patients.containsKey(id))
574 {
575 throw new Exception("Patient ID already exists");
576 }
577
578 System.out.print("New Patient Name: ");
579 String name = sc.nextLine();
580
581 System.out.print("New Patient Procedure Date: ");
582 String date = sc.nextLine();
583
584 System.out.print("New Patient injury: ");
585 String injury = sc.nextLine();
586//user input for doctor, if doctor does not exist, throw error message
587 System.out.print("Supervising Doctors Name: ");
588 String doc = sc.nextLine();
589 if(!surgeons.containsKey(doc))
590 {
591 throw new Exception("Surgeon does not exist");
592 }
593//make inpatient or outpatient depending on user input, which we got earlier
594 if (type.charAt(0) == '1')
595 {
596 patients.put(id, new Inpatient(id, name, date, injury, doc));
597
598 }
599 else
600 {
601 patients.put(id, new Patient(id, name, date, injury, doc));
602 }
603 }
604
605
606 public static void addSurgeon() throws Exception
607 {
608 //get user input for surgeon type
609 System.out.print("Create Specialist (1) or General (2) Surgeon: ");
610 String type = sc.nextLine();
611 if (type.charAt(0) != '1' &&
612 type.charAt(0) != '2')
613 {
614 throw new Exception("Bad Surgeon type choice.");
615 }
616//get surgeon name, if already exist throw error
617 System.out.print("New Surgeon Name: ");
618 String name = sc.nextLine();
619 if (surgeons.containsKey(name))
620 {
621 throw new Exception("Surgeon already exists");
622 }
623
624 System.out.print("Surgeons Hourly Rate: ");
625 String rate = sc.nextLine();
626
627 double drate = 0;
628 try
629 {
630 //convert user input string to double
631 drate = Double.parseDouble(rate);
632 }
633 catch (Exception e)
634 {
635 throw new Exception("Invalid hourly rate");
636 }
637
638 if (type.charAt(0) == '2')
639 {
640 //create surgeon in hash table
641 surgeons.put(name, new GeneralSurgeon(name, drate));
642 return;
643 }
644
645 System.out.print("Surgeons Speciality: ");
646 String speciality = sc.nextLine();
647
648 System.out.print("Consultation Fee: ");
649 String fee = sc.nextLine();
650
651 double dfee = 0;
652
653 try
654 {
655 //convert string to double
656 dfee = Double.parseDouble(fee);
657 }
658 catch (Exception e)
659 {
660 throw new Exception("Invalid consultation rate");
661 }
662//create specialist surgeon in hash table
663 surgeons.put(name, new SpecialistSurgeon(name, drate, speciality, dfee));
664
665 }
666
667
668 public static void displaySurgeons()
669 {
670
671 // print final summary of all patient details
672 System.out.println("\n*** Final Surgeons Details Summary ***\n\n");
673
674 // print headers (required some fiddling with field widths)
675 System.out.printf("%-20s%-20s%-20s%-20s%-20s\n",
676 "Surgeon Type", "Surgeon Name", "Hourly Rate",
677 "Speciality", "Consult Fee");
678
679 System.out.printf("%-20s%-20s%-20s%-20s%-20s\n",
680 "-----------", "--------------", "----------",
681 "------------", "-------");
682
683 // print details for each SURGEON
684 Set<String> surgeonSet = surgeons.keySet();
685
686 //Just repeat for each key in the set
687 Iterator<String> it = surgeonSet.iterator();
688
689 //repeat for each key in the set
690 while (it.hasNext())
691 {
692 // TODO - FIXSURGEON PRINT FUNCTION TO PRINT OUT PROPERLY
693 String key = it.next();
694 Surgeon s = surgeons.get(key);
695 s.print();
696 System.out.println("----------------------\n");
697 }
698 System.out.println();
699 }
700
701}