Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}
Posted
Updated 16-Jul-17 7:06am

1 solution

Use indentation to make your code readable. The problem seems to be that your usage of else. else block is only evaluated in case the previous if condition is not fullfilled and the code block after if is thus NOT executed. This means that closestPractitioner remains null. Simply try to remove all else and see if it works. The first if statement should also be possible to remove.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900