Search⌘ K

Solution Review: Task VI to Task IX

Explore key Java programming tasks to manage a hospital system, including booking patient appointments with specific doctors, printing lists of different specialists, setting doctor availability, and controlling patient flow based on doctor status. This lesson deepens your understanding of object-oriented programming and method implementation within a healthcare context.

Task VI: Book an appointment

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

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, rInt can be any number in the range: [0,[0, cardiologists.size()) ...