:的java比較多個值,並找到我需要找到最匹配的員工工資在數據庫記錄作爲最佳匹配
Name: City: State:
A (null) (null)
A (null) DEL
(null) (null) (null)
A SAKET DEL
比賽順序應該是:
NAME =名稱,狀態=州,城市=城市
2. NAME =名,STATE =州,城市= NULL
NAME =名稱,狀態= NULL,CITY = NULL
4. NAME = NULL,州= NULL,CITY = NULL
意味着如果在一排,所有屬性相符的 - 它應選擇,如果我們沒有那種數據,我們應該去下一個最好的選擇,如選擇州和城市爲NULL等
我的代碼如下,給我正確的結果,但我需要一個更高效辦法。
private static BigDecimal getsalaryForBestMatch(ResultSet results, EmployeeRq request) throws Exception{
BigDecimal salary = null;
BigDecimal salaryWithState = null;
BigDecimal salaryWithName = null;
BigDecimal salaryWithNoMatch = null;
while (results.next()) {
String billerName = results.getString("EMP_NAME") != null ? results.getString("EMP_NAME").trim() : null;
String city = results.getString("CITY") != null ? results.getString("CITY").trim() : null;
String state = results.getString("STATE") != null ? results.getString("STATE").trim() : null;
BigDecimal salaryRslt = null;
if(results.getString("SALARY") != null){
salaryRslt = BigDecimal.valueOf(results.getDouble("SALARY"));
}
if(billerName != null && !billerName.equals("") && billerName.equals(request.getBillPaymentsalaryCalculateInfo().getBillerName())){
if(city != null && !city.equals("") && city.equals(request.getMsgRqHdr().getCity()) &&
state != null && !state.equals("") && state.equalsIgnoreCase(request.getMsgRqHdr().getstate())){
salary = salaryRslt;
break;
} else if((city == null || city.equals("")) && state != null && !state.equals("") &&
state.equalsIgnoreCase(request.getMsgRqHdr().getState())){
salaryWithState = salaryRslt;
} else if((city == null || city.equals("")) && (state == null || state.equals(""))){
salaryWithName = salaryRslt;
}
} else if((billerName == null || billerName.equals("")) && (city == null || city.equals("")) &&
(state == null || state.equals(""))){
salaryWithNoMatch = salaryRslt;
}
}
if(salary != null){
return salary;
} else if(salaryWithState != null){
salary = salaryWithState;
} else if(salaryWithName != null){
salary = salaryWithName;
} else if(salaryWithNoMatch != null){
salary = salaryWithNoMatch;
}
return salary;
}
編輯:我不想使用3個額外的變量:salaryWithState,salaryWithName,salaryWithNoMatch。
這可能會更好Code Review – JonK
Use-StringUtils.isBlank()。它將以單鏡頭檢查空白,空白和空字符串。這將在一定程度上簡化您的代碼。 –
@KeyurPanchal或OP可能會創建一個靜態方法來做到這一點,如果他不想使用庫類..但它是一個很好的建議,但 – Yahya