如何优化代码以摆脱我的所有多个else-if条件?

时间:2016-01-23 06:16:48

标签: java

这个我的DAOImple课程::::

这里我有两个下拉框 第1个盒子选项是全部,在游泳池,池外。 2ed框选项为ALL,活动,非活动,Frozen,Baried,Delete。

public class FleetDetailsDAOImpl22 extends CustomJdbcDaoSupport implements FleetDetailsDAO {

@Autowired
private DataManager dataManager;
/** This method is called for getting all the vessels. */

/**
 * @param vesselStatus
 * @param poolStatus
 * @param poolid
 */

@Override
public List<FleetDetails> getFleetDetails(String vesselStatus,
        String poolStatus, int poolid, String fromDate, String toDate)throws CustomException {

List<FleetDetails> fleetList =new ArrayList<FleetDetails>();
String poolStatusValue = null;
String vesselStatusValue = null;
String displayFlag = null;

/*---check for poolStatus---------*/
if(poolStatus.equals("yes")) {
    poolStatusValue = "Y";
} 
if(poolStatus.equals("no")) {
    poolStatusValue = "N";
}
if(poolStatus.equals("all")) {
    poolStatusValue = "ALL";
}
/*---check for vessel status---------*/

if(vesselStatus.equals("active")) {
    vesselStatusValue = "Y";
    displayFlag = "Y";
}
if(vesselStatus.equals("inactive")) {
    vesselStatusValue = "N";
    displayFlag = "Y";
}
if(vesselStatus.equals("frozen")) {
    vesselStatusValue = "F";
    displayFlag ="Y";
}
if(vesselStatus.equals("buried")) {
    vesselStatusValue = "B";
    displayFlag = "B";
}
if(vesselStatus.equals("pending")) {
    vesselStatusValue = "P";
    displayFlag = "P";
}
if(vesselStatus.equals("delete")) {
    vesselStatusValue = "D";
    displayFlag = "D";
}
if(vesselStatus.equals("all")){
    vesselStatusValue = "ALL";
}

try {

    if(poolStatusValue.equals("Y") && vesselStatusValue.equals("ALL") ){

        String fleetDetailsQuery = dataManager.getQuery("FLEET_DETAILS", Queries.QUERY_GET_FLEET_DETAILS_ALL_NONPOOL);
        Object[] params = new Object[] { poolid, poolStatusValue };
        fleetList = getJdbcTemplate().query(fleetDetailsQuery,params, new FleetDetails.FleetDetailsRowMapper());
    } 
    else if(poolStatusValue.equals("ALL") && vesselStatusValue.equals("ALL")) {
        String allVesselsQuiry = dataManager.getQuery("FLEET_DETAILS",Queries.QUERY_GET_POOL_NONPOOL_VESSELS);  
        Object[] params = new Object[] { poolid };
        fleetList = getJdbcTemplate().query(allVesselsQuiry,params, new FleetDetails.FleetDetailsRowMapper());
    } 
    else if(poolStatusValue.equals("N")){
        String nonpoolVesselsQuiry = dataManager.getQuery("FLEET_DETAILS", Queries.QUERY_GET_NONPOOL_VESSELS);
        Object[] params = new Object[] { poolid, vesselStatusValue,poolStatusValue, displayFlag };
        fleetList = getJdbcTemplate().query(nonpoolVesselsQuiry,params, new FleetDetails.FleetDetailsRowMapper());
    } 
    else if(poolStatusValue.equals("Y")){
        String fleetDetailsQuery = dataManager.getQuery("FLEET_DETAILS", Queries.QUERY_GET_FLEET_DETAILS);
        Object[] params = new Object[] { poolid, vesselStatusValue,poolStatusValue, displayFlag };
        fleetList = getJdbcTemplate().query(fleetDetailsQuery,params, new FleetDetails.FleetDetailsRowMapper());
    }
    else {
        String fleetDetailsQuery = dataManager
                .getQuery("FLEET_DETAILS",Queries.QUERY_GET_STATUS_VESSELS_FOR_BOTH_POOL_NONPOOL);
        Object[] params = new Object[] { poolid, vesselStatusValue,displayFlag };
        fleetList = getJdbcTemplate().query(fleetDetailsQuery,params, new FleetDetails.FleetDetailsRowMapper());
    }

} catch(Exception e) {
    e.getMessage();
}


    return fleetList;
}

1 个答案:

答案 0 :(得分:0)

您已使用多个if条件来检查multiple的值而不是if elseif,这样如果一个条件成功,则不会检查next if条件。并且,为了避免这种混淆,最好使用“SWITCH CASE”声明。