HIT软件构造lab3中DutyRosterApp中函数isfull()的实现
首先该函数的需求是用户可随时检查当前排班是否已满(即所有时间段都已被安排了特定员工值班)、若未满,则展示给用户哪些时间段未安排、未安排的时间段占总时间段的比例。
用户输入的时间一般为xx年xx月xx日,我们需要通过函数
LocalDate.of(int year, int month, int day)来将输入的年月日转化为大小为输入日期到1970年1月1日之间的天数的整形数,通过此函数得到排班开始日期startdate, 排班结束日期enddate
首先创建一个大小为排班日期的boolean数组timelist
period = enddate - startdate;
boolean[] timelist = new boolean[(int)period];
然后通过遍历所有有有排班的员工,将有排班的时间段赋值为true
其中worker类为员工,包含员工信息
for(worker p : schedule.labels()) {
start = p.getstart();
end = p.getend();
for(long i = start - startdate; i < end - startdate; i++) {
timelist[(int)i] = true;
}
}
处理完此数组后再次进行循环来输出空闲时间段设置count计数器来记录空闲时间的数量,flag为判断位
for(int j = 0; j < (int)period; j++) {
if(timelist[j] == false) {
count++;
if(flag == 0) {
sparestart = j + startdate;
flag = 1;
}
}
else {
if(flag == 1) {
spareend = j + startdate -1;
System.out.println(LocalDate.ofEpochDay(sparestart) + "->" + LocalDate.ofEpochDay(spareend));
flag = 0;
}
}
}
循环中,若当前块为空且flag为0,将此块设置为空闲时间段的起点(加上startdate)继续遍历,碰到第一个非空闲块时将flag设回0,并将空闲时间段结尾段设为j-1+enddate并输出LocalDate.ofEpochDay(sparestart) + “->” + LocalDate.ofEpochDay(spareend)
LocalDate.ofEpochDay()为将天数转回日期
最后输出空白占比count/period(若count为0则不输出)