I'm doing a consultation center program a part of it is to select a doctor and enter the start time then enter for how many hours then check if the selected doctor already has a booked appointment at that time and if he has a appointment the program checks for other doctors who doesn't have a appointment at that time period and assign a doctor from available doctors randomly so. I created the program but the problem is if a consultation is not available the program identifies it but it doesn't randomly assign a doctor.
here is my code,
// Check if both the date and time inputs are in the correct format
if (dateMatcher.matches() && timeMatcher.matches()) {
// Concatenate the date input and time input to create a combined date-time string in the format "MM/dd/yyyy HH:mm"
// Convert the combined date-time input to a Date object
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date dateTime = null;
LocalDateTime ldt = null;
try {
dateTime = sdf.parse(dateInput + " " + startTimeInput);
ldt = dateTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
} catch (ParseException e) {
// Handle the exception
}
// Check if the combined date-time is in the future
assert dateTime != null;
if (dateTime.after(new Date())) {
// Get the index of the selected doctor in the doctorArrayList
int selectedIndex = doctorComboBox.getSelectedIndex();
// Get the selected doctor object from the doctorArrayList
Doctor selectedDoctor = doctorArrayList.get(selectedIndex);
// Check if the selected doctor is available at the specified date and time
if (selectedDoctor.isAvailable(selectedDoctor, ldt, Integer.parseInt(hourCountInput), Consultation.getConsultationList())) {
JOptionPane.showMessageDialog(null, "The selected doctor is available at the specified date and time.");
// Close the current frame
setVisible(false);
dispose();
// Open the patient details page
GUIPatientDetailsPage patientDetailsPage = new GUIPatientDetailsPage();
patientDetailsPage.patientDetailsPage(ldt, selectedIndex, hourCountInput);
} else {
// If the doctor is not available, create a list of available doctors
List<Doctor> availableDoctors = new ArrayList<>();
for (Doctor doc : doctorArrayList) {
if (doc.isAvailable(selectedDoctor, ldt, Integer.parseInt(hourCountInput), Consultation.getConsultationList())) {
availableDoctors.add(doc);
}
}
if (availableDoctors.isEmpty()) {
System.out.println("empty");
}
if (!availableDoctors.isEmpty()) {
// If there are available doctors, randomly select one of them
Random rand = new Random();
int index = rand.nextInt(availableDoctors.size());
Doctor newSelectedDoctor = availableDoctors.get(index);
// Open the patient details page
GUIPatientDetailsPage patientDetailsPage = new GUIPatientDetailsPage();
patientDetailsPage.patientDetailsPage(ldt, index, hourCountInput);
// Display a confirmation message
JOptionPane.showMessageDialog(null, "The Doctor you selected is not available at that time,But we have selected another doctor available at that time. Please enter your details below");
}
} //else {
// If there are no available doctors, display an error message
// JOptionPane.showMessageDialog(null, "Sorry, there are no doctors available at the specified date and time. Please try again.");
// }
}
}
});
And isAvailable
method is here,
public boolean isAvailable(Doctor doctor, LocalDateTime startTime, int duration, List<Consultation> consultationList) {
for (Consultation consultation : consultationList) {
// Check if the start time of the desired consultation falls within the duration of an existing consultation
if (startTime.isAfter(consultation.getDateTime()) && startTime.isBefore(consultation.getDateTime().plusHours(consultation.getHourCount()))) {
return false;
}
// Check if the end time of the desired consultation falls within the duration of an existing consultation
LocalDateTime endTime = startTime.plusHours(duration);
if (endTime.isAfter(consultation.getDateTime()) && endTime.isBefore(consultation.getDateTime().plusHours(consultation.getHourCount()))) {
return false;
}
// Check if the doctor of the desired consultation is the same as the doctor of an existing consultation
if (doctor == consultation.getDoctor()) {
return false;
}
}
return true;
}
in this input there's already appointment to this doctor in this time period so it should check and tell me if the appointment is available. if not it has to assign a random doctor then display a message.
This is what happens when I click submit.
it doesn't do anything from what I suspected was that with this for loop is not adding anything to availableDoctors
list
// If the doctor is not available, create a list of available doctors
List<Doctor> availableDoctors = new ArrayList<>();
for (Doctor doc : doctorArrayList) {
if (doc.isAvailable(selectedDoctor, ldt, Integer.parseInt(hourCountInput), Consultation.getConsultationList())) {
availableDoctors.add(doc);
}
}
So i added this part to verify,
if (availableDoctors.isEmpty()) {
System.out.println("empty");
}
and I was right it printed empty.
What can I do to make this correct?
You are looping through consultations and you return false at the first unavailability. However, you do not correctly check this.
You are returning false
if your startTime
is during a consultation, independently of whether that doctor is the one you are searching for. So, for instance, if you are searching for the availability of doctor1, but the first iteration of the loop finds a consultation of doctor2, who is not available at the startTime
of your choice, then you conclude - incorrectly - that doctor1 is unavailable.
You are returning false
if your endTime
is during a consultation, independently of whether that doctor is the one you are searching for. So, for instance, if you are searching for the availability of doctor1, but the first iteration of the loop finds a consultation of doctor2, who is not available at the endTime
of your choice, then you conclude - incorrectly - that doctor1 is unavailable.
You are returning false
for availability if the doctor is the same you are searching for. So, if you want an appointment to a doctor who has an appointment at any time, even if it does not overlap your time interval, then false
will be returned.
You need to avoid returning a truth value just because the doctor matches your searched doctor, because we are not interested in whether the doctor has some arbitrary appointments, but we are interested whether the doctor has an appointment at the time in search. Instead, wrap this if around the two other criterias, like below:
public boolean isAvailable(Doctor doctor, LocalDateTime startTime, int duration, List<Consultation> consultationList) {
for (Consultation consultation : consultationList) {
if (doctor == consultation.getDoctor()) {
// Check if the start time of the desired consultation falls within the duration of an existing consultation
if (startTime.isAfter(consultation.getDateTime()) && startTime.isBefore(consultation.getDateTime().plusHours(consultation.getHourCount()))) {
return false;
}
// Check if the end time of the desired consultation falls within the duration of an existing consultation
LocalDateTime endTime = startTime.plusHours(duration);
if (endTime.isAfter(consultation.getDateTime()) && endTime.isBefore(consultation.getDateTime().plusHours(consultation.getHourCount()))) {
return false;
}
}
}
return true;
}
You can also simplify your criteria by stating that if it's not true that startTime
is later than the end time of the consultation or endTime
is earlier than the start time of the consultation, then return false, but I avoided doing that refactor to keep the code close to your initial version.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.