...

/

Solution Review: Task VI to Task IX

Solution Review: Task VI to Task IX

Have a look at the solution to the previously assigned challenges: Task VI to Task IX.

Task VI: Book an appointment

The task was to book an appointment for the patient. Look at the code below.

Press + to interact
Driver.java
Hospital.java
Patient.java
Doctor.java
Cardiologist.java
Neurosurgeon.java
Dietitian.java
import java.util.ArrayList;
class Driver
{
public static void main(String args[])
{
/* Create a hospital */
Hospital h = new Hospital("Public Care Center");
/* Adding two cardiologists*/
h.addDoctor("David", 43, "cardio");
h.addDoctor("Linda", 31, "cardio");
/* Adding two neurosurgeons*/
h.addDoctor("Mike", 37, "neuro");
h.addDoctor("Katherine", 50, "neuro");
/* Adding two dietiants*/
h.addDoctor("Bob", 29, "diet");
h.addDoctor("Chris", 41, "diet");
/* Booking an appointment */
h.bookAppointment("Dave", 21, "neurosurgeon");
h.bookAppointment("Kim", 19, "cardiologist");
h.bookAppointment("Sarah", 27, "dietitian");
}
}

Look at the Patient.java file. We add a new static variable: totalPatients (at line 7). We also increment value in the constructor at line 15. Thus means that with every new patient, the total count of totalPatients will increase by 11.

Look at line 92 in the Hospital.java file. The bookAppointment method takes the name and age of the patient. Additionally, it takes the docType (type of the doctor) in String form. Now there are three cases:

  • If docType is equal to cardiologist, we create a random number, rInt, using the Math.random() function. Here, ...