- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个类模块,其中包含
private LocalDate startDate;
private LocalDate endDate;
private Map<Teacher, Integer> workedHoursPerDay;
每个老师都有一个属性
private int hourlyWage;
请求是获取按月分组的所有教师的每月预算。预期返回的是Map。我进行了搜索,但我不知道如何使用 startDate 和 endDate 来做到这一点。
单个模块的每月支出是分配给该模块的所有教师在该月所有工作日内的累计成本。
逻辑是:-从所有模块中获取(Set)教授该模块的老师和小时数(MapworkingHoursPerDay)。然后将每位教师的工作时间乘以小时工资。最后,使用每个模块的 startDate 和 endDate 按月对成本进行分组。
我已经做了类似的事情,但没有得到预期的结果
公共(public) map calculateCumulativeMonthlySpends() {
//Get the min startDate of all the modules
LocalDate localDateStart = modules.stream()
.flatMap(modules -> module.getWorkingDays().stream())
.min(LocalDate::compareTo).get();
//Get the max endDate of all the modules
LocalDate localDateEnd = modules.stream()
.flatMap(module -> module.getWorkingDays().stream())
.max(LocalDate::compareTo).get();
//Iterate from the start date to the end date, calculate the daily cost for all the module
//and sum the cost of all the days for the all modules
//Then create a map with moth as a key and the cost of all the modules for each month
//Finally sort the map by key
return Stream.iterate(localDateStart, d -> d.plusDays(1))
.limit(ChronoUnit.DAYS.between(localDateStart, localDateEnd) + 1)
.collect(Collectors.toMap(LocalDate::getMonth, day -> dailyCost(day, modules), Integer::sum))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
}
/**
* Calculate the daily cost for all the modules
* Check if the modules's working days contain the attribute day
* If yes sum(), add the daily cost of this module to the other modules' daily cost
*
* @param day
* @param modules
* @return
*/
private Integer dailyCost(LocalDate day, Set<Module> modules) {
return modules.stream()
.filter(module -> module.getWorkingDays().contains(day))
.flatMap(p -> p.getCommittedHoursPerDay().entrySet().stream())
.mapToInt(e -> e.getKey().getHourlyWage() * e.getValue())
.sum();
}
代码已修改。我现在已经得到了预期的结果,但我希望有更好的方法来做到这一点。
预计:{1月=19529、2月=26909、3月=35593、4月=60243、5月=79172、6月=70135、7月=72470、8月=40161、9月=21750、10月=8636、十一月=1344}
最佳答案
如果没有更多信息,很难找到代码中的错误。但解释一下如何自己找到它可能会对您更有帮助。
dailyCost
中的单个表达式太复杂,无法调试。 Java 流非常棒,但它们有一个非常大的问题:使用交互式调试器调试它们非常困难(请参阅 Problems using interactive debuggers with Java 8 streams 不幸的是没有有用的答案)。
秘诀是让你的运算符(operator)链简短而明显。在可能的情况下分成代表单一职责并且可以单独进行单元测试的单独方法。然后您就可以放心地将这些方法构建到您的解决方案中。
例如:
.filter(p -> (p.getStartDate().isBefore( day) || p.getStartDate().equals(day)))
.filter(p -> p.getEndDate().isAfter(day) || p.getEndDate().equals(day))
可能成为:
.filter(p -> p.wasActiveOnDay(day))
还有:
.mapToInt(p -> p.getCommittedHoursPerDay().entrySet().stream()
.mapToInt(e -> e.getKey().getHourlyWage() * e.getValue()).sum())
可能成为:
.mapToInt(Module:getTotalWages)
其中每一个都可以作为模块
测试的一部分进行单元测试。这会给你留下:
return modules.stream()
.filter(m -> m.wasActiveOnDay(day))
.mapToInt(Module::getTotalWages).sum();
该语句更容易调试。
关于java - 使用 startDate 和 endDate 按月分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59079848/
我正在使用predicateForEventsWithStartDate:endDate:calendars查找在给定的24小时内发生的所有事件。我希望当天发生任何 Activity 的所有 Acti
数据来自 Google Sheets API,因此 employees.stratdate、employees.enddate 等。 我需要能够仅显示过去在选定月份工作的员工。如果他们不再工作,但曾经
我有这个 Bootstrap 日期选择器配置: datePickerConfig: { changeMonth: true, numberOfMonths: 1, endDat
我需要对 StartDate 和 EndDate 进行验证 验证: StartDate 的设置值必须小于或等于 Endate。 EndDate 必须设置为大于或等于 startdate。 到目前为止我
我的数据库中有以下行。 profileID | startDate | endDate -------------------|-----------
有人可以告诉我这个查询中缺少什么吗?它不断出现: Error 1067: invalid default value for EndDate CREATE TABLE `course_info`( `
我有一个类模块,其中包含 private LocalDate startDate; private LocalDate endDate; private Map worked
当我将约会插入到 Exchange 中时,出现以下问题“EndDate 早于 StartDate”,有人可以帮忙解决这个问题吗? Appointment appt=new Appointment();
如何使用 linq 或 lambda 表达式获取开始日期和结束日期之间的日期列表。 DateTime StartDate = DateTime.Now; DateTime EndDate = Date
我正在努力进行日期计算来定义我自己的条形图系列范围。 我的动态日期是时间戳参数“timestamp1”、“timestamp2”、“timestamp3”、“timestamp4”,而我想创建间隔 i
我一直在编写一个脚本来获取一些证书的详细信息,而不是我已经在解决格式问题的过程中得到了格式化,现在当我尝试解析 EndDate="openssl s_client 时,脚本挂起-connect $ho
我有日期数组,我只需要获取介于两个不同日期(StartDate 和 EndDate)之间的那些日期。 谁能帮我解决这个问题... 提前致谢 最佳答案 假设您有 startDate 和 endDate
我制作了一个函数,它可以很好地处理 32 位日期(strtotime 可以接受),但它不可扩展。我需要随机生成人们的出生日期,所以我必须使用 DateTime(而且只有 format(DateTime
这个问题在这里已经有了答案: How do I loop through a date range? (17 个答案) 关闭 6 年前。 我有一个 DateTime StartDate 和 EndD
我想根据选项表单中选择的值动态设置 Bootstrap 日期选择器的 startDate 和 endDate 。 因此,如果我在选项中选择了 Boby,那么日期选择器将设置 startDate = 2
我在该命令的 enddate 输出中获得了额外的空间: $ export IP=google.com; nc -z -w 3 $IP 443 && (echo | openssl s_client
我有两个 LocalDate 声明如下: val startDate = LocalDate.of(2019, 10, 31) // 2019-10-31 val endDate = LocalDa
如何将 UK endDate 转换为 UTC,以便与 Node 上的 UTC 当前时间 (nowTime) 进行比较?还是换一种方式更好?将当前 UTC 时间转换为伦敦时区,然后与英国 endDate
在我的日期分页器中,我可以调用选定的日期并且它正在工作,但 startDate 和 endDate 不起作用。 我正在使用这个库 http://jondmiles.com/bootstrap-date
在我的数据库中有两列和如下数据 StartDate Enddate 2015-10-01 2015-10-30 2015-10-15 2015-11-15
我是一名优秀的程序员,十分优秀!