都说大量使用if else不好,使代码有坏味道,像下面这种代码应该如何改善呢?就是如何去除那些if else?
小弟拜谢~! public Integer getShouldFilledWorkload(Date startDate, Date endDate) {
Long count = 0l; //起始时间和结束时间之间的天数
Integer shouldFilledWorkDay = 0; //除去周六和周日剩下的天数
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
count = (endDate.getTime() - startDate.getTime())/1000/60/60/24 + 1;
for(int i=0;i<count;i++) {
if(cal.get(Calendar.DAY_OF_WEEK)!=Calendar.SATURDAY&&cal.get(Calendar.DAY_OF_WEEK)!=Calendar.SUNDAY){
shouldFilledWorkDay+=1;
}
cal.add(Calendar.DAY_OF_WEEK, 1);
}
return shouldFilledWorkDay*8;
}
public Integer getDayCount(List<DomainActorVO> l, Date startDate, Date endDate) {
Integer workDayCount = 0;
if(startDate==null||endDate==null||l==null) {
return 0;
}
for(DomainActorVO daVO:l) {
if(daVO.getJoinTime()==null) {
continue;
}
if(daVO.getLeaveTime()==null) {
if(endDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
if(startDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += (getShouldFilledWorkload(startDate,daVO.getJoinTime())-8);
}
}else {
if(startDate.compareTo(daVO.getLeaveTime())>0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
if(endDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
if(startDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += (getShouldFilledWorkload(startDate,daVO.getJoinTime())-8);
}
if(endDate.compareTo(daVO.getLeaveTime())>0) {
workDayCount += (getShouldFilledWorkload(daVO.getLeaveTime(),endDate)-8);
}
}
}
return workDayCount;
}
小弟拜谢~! public Integer getShouldFilledWorkload(Date startDate, Date endDate) {
Long count = 0l; //起始时间和结束时间之间的天数
Integer shouldFilledWorkDay = 0; //除去周六和周日剩下的天数
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
count = (endDate.getTime() - startDate.getTime())/1000/60/60/24 + 1;
for(int i=0;i<count;i++) {
if(cal.get(Calendar.DAY_OF_WEEK)!=Calendar.SATURDAY&&cal.get(Calendar.DAY_OF_WEEK)!=Calendar.SUNDAY){
shouldFilledWorkDay+=1;
}
cal.add(Calendar.DAY_OF_WEEK, 1);
}
return shouldFilledWorkDay*8;
}
public Integer getDayCount(List<DomainActorVO> l, Date startDate, Date endDate) {
Integer workDayCount = 0;
if(startDate==null||endDate==null||l==null) {
return 0;
}
for(DomainActorVO daVO:l) {
if(daVO.getJoinTime()==null) {
continue;
}
if(daVO.getLeaveTime()==null) {
if(endDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
if(startDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += (getShouldFilledWorkload(startDate,daVO.getJoinTime())-8);
}
}else {
if(startDate.compareTo(daVO.getLeaveTime())>0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
if(endDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
if(startDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += (getShouldFilledWorkload(startDate,daVO.getJoinTime())-8);
}
if(endDate.compareTo(daVO.getLeaveTime())>0) {
workDayCount += (getShouldFilledWorkload(daVO.getLeaveTime(),endDate)-8);
}
}
}
return workDayCount;
}
可以适当加点注释啊 这样就不会看晕了...
if(startDate.compareTo(daVO.getLeaveTime())>0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
if(endDate.compareTo(daVO.getJoinTime())<0) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
}
改为:
if((startDate.compareTo(daVO.getLeaveTime())>0) || (endDate.compareTo(daVO.getJoinTime())<0)) {
workDayCount += getShouldFilledWorkload(startDate,endDate);
continue;
} 尽量把兼容条件,这样if else就少了