I have an entity City like below
@Entity
public class City {
private Country country;
private ClosestCity closestCity;
private NearestCity nearestCity;
private SisterCity sisterCity;
}
ClosestCity, NearestCity and SisterCity are city with foreign keys in the entity city. I have a list of doctors that are situated in the different cities and I want to allocate a patient to doctor who's city is closest to that of a patient, else allocate to the nearest city, else allocate to the sister city else if none of the above questions are not meet , put the patient to a waiting list.
What I have tried:
Below is my method to allocate and its not working.
public boolean allocateDoctorToPatient(Patient patient) {
Practitioner closestPractitioner = null;
List<practitioner> practitioners = practitionerService.findAll().get();
for (Practitioner p : practitioners) {
//same city
if (closestPractitioner != null) {
for (Practitioner p1 : practitioners) {
if (patient.getCity().equals(p1.getCity())) {
closestPractitioner = p1;
break;
}
}
}
//loops to find closest city
else if (closestPractitioner == null) {
for (Practitioner p2 : practitioners) {
if (patient.getCity().equals(p2.getCity().getClosestCity())) {
closestPractitioner = p2;
break;
}
}
}
//loops to find nearest city
else if (closestPractitioner == null) {
for (Practitioner p3 : practitioners) {
if (patient.getCity().equals(p3.getCity().getNearestCity())) {
closestPractitioner = p3;
break;
}
}
}
//loops to find sister city
else if(closestPractitioner == null) {
for (Practitioner p4 : practitioners) {
if (patient.getCity().equals(p4.getCity().getSisterCity())) {
closestPractitioner = p4;
break;
}
}
}
}
if (closestPractitioner != null) {
Allocate allocate =new Allocate();
allocate.setPatient(patient);
allocate.setPractitioner(closestPractitioner)
save(allocate);
}
else {
WaitingList waitingList=new WaitingList();
waitingList.setPatient(patient);
waitingListService.save(waitingList);
}
return closestPractitioner != null;
}