-3
我有一個關於通過Java對象的列表流,如果條件是滿足突破的問題突破,流通過Java對象的列表,如果條件是滿足
我有一個實體市像下面
@Entity
public class City {
private Country country;
private ClosestCity closestCity;
private NearestCity nearestCity;
private SisterCity sisterCity;
}
ClosestCity,NearestCity和SisterCity是實體城市中的外鍵城市。 我有一張位於不同城市的醫生名單,我想分配一位病人給醫生,他的城市是最接近病人的,否則分配到最近的城市,否則分配給姊妹城市,否則如果沒有上述問題不符合,請耐心等待。 下面是我的方法分配和它的不工作。
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;
}
歡迎來到Stack Overflow!你似乎在要求某人爲你寫一些代碼。堆棧溢出是一個問答網站,而不是代碼寫入服務。請[見這裏](https://stackoverflow.com/help/how-to-ask)學習如何編寫有效的問題。 –
你不覺得這個問題只是找到離患者城市有醫生的最近的城市嗎?如果是這種情況,最近的城市,姐妹城市和等候名單是沒有意義的......除非我失去了一些東西 –
創建一個單獨的方法來找到最近的醫生,使用'return'而不是'break'並擺脫'如果完全分支 – Clashsoft