都说大量使用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;
}
解决方案 »
- 关于JRadioButton中fireActionPerformed里面的执行顺序的问题
- 求教:有谁知道java中的this的用法,并能够举例说明!再这里感谢了!!!
- 菜鸟提问 :这个图片怎么显示不出来,是哪里的问题,代码如下
- 如何修改JFrame的标题栏?
- 为什么有两套jre
- 比较字符大小的问题
- 关于定时的程序
- 为开发小巧软件,50分征求大家意见
- 我从db2某表中取出记录写入到txt文件,在txt文件中只要显示汉字的地方显示问号:???,好像是编码转换问题,请问高手怎样转
- 滚动新闻
- Integer a = new Integer(3); 与int a=3; 都是堆栈里生成的吗??
- 关于List泛型继承的问题
可以适当加点注释啊 这样就不会看晕了...
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就少了