November 2015 Health Clinic
1 package november2015_healthclinic;
2
3 public class Patient
4 {
5 private int id;
6 private String name;
7 private int priority;
8 private String doctor;
9
10 public Patient(){
11
12 }
13
14 public Patient(int i, String n, int p)
15 {
16 id = i;
17 name = n;
18 priority = p;
19 doctor = null;
20 }
21
22 public void setId(int i) { id = i; }
23 public void setName(String n) { name = n; }
24 public void setPriority(int p) { priority = p; }
25 public void setDoctor(String d) { doctor = d; }
26
27 public int getId() { return id; }
28 public String getName() { return name; }
29 public int getPriority() { return priority; }
30 public String getDoctor() { return doctor; }
31
32 public String toString() { return id+" "+name+" "+priority+" "+doctor; }
33 }
34
1 package november2015_healthclinic;
2
3 public class WaitingRoom
4 {
5 private Patient[] patients = new Patient[10];
6 // uses default constructor
7
8 public void add(Patient newPatient)
9 // adds the new patient in the next empty array location
10 {
11 int i = 0;
12 while ((patients[i] != null) && (i < 10))
13 {
14 i=i+1;
15 }
16 if (i==10) { System.out.println("No more space in the waiting room."); }
17 else { patients[i] = newPatient; }
18 }
19
20 public void callNextPatient()
21 // finds the next patient, outputs their details
22 // and removes the patient from the array
23 {
24 int index = 0;
25 if (patients[0]==null)
26 {
27 System.out.println("The waiting room is empty.");
28 }
29 else
30 {
31 index = findNextPatientIndex();
32 remove(index);
33 }
34 }
35
36 private int findNextPatientIndex()
37 // returns the index of the first patient with the
38 // highest priority in the array patients
39 {
40 int max = 0;
41 //... code missing ...
42 return max;
43 }
44
45 private void remove(int n)
46 // outputs the data of the patient instance at array index n
47 // and removes that patient by shifting all remaining patients
48 // by one index towards the front of the array
49 {
50 //... code missing ...
51 }
52 }
53
1 package november2015_healthclinic;
2
3 import java.util.LinkedList;
4
5 public class WaitingRoomDynamic
6 {
7 private LinkedList<Patient> PatientList = new LinkedList<Patient>();
8 // methods
9
10 public void add(Patient P)
11 // adds a patient at the end of the list
12 {
13 PatientList.addLast(P);
14 }
15
16 public void remove()
17 // outputs the name of the next patient to see a doctor and
18 // removes this patient instance from the list
19 {
20 int index = findNextPatientIndex();
21 System.out.println(PatientList.get(index).getName());
22 PatientList.remove(index);
23 }
24
25 private int findNextPatientIndex()
26 {
27 int i = 0, result = 0;
28 Patient current, firstup;
29 firstup = new Patient();
30 firstup.setPriority(0);
31 while (i < PatientList.size())
32 {
33 current = PatientList.get(i);
34 if (current.getPriority() > firstup.getPriority())
35 {
36 firstup = current;
37 result = i;
38 }
39 i=i+1;
40 }
41 return result;
42 }
43 }
44