Skip to content

Instantly share code, notes, and snippets.

@pawelpluta

pawelpluta/Company.java Secret

Created Dec 20, 2020
Embed
What would you like to do?
Article: Law of Demeter - company without division
class Company {
private List<Department> departments;
Map<DepartmentCode, BigDecimal> costPerDepartment() {
Map<DepartmentCode, BigDecimal> costPerDepartment = new HashMap<>();
departments.forEach(department -> {
DepartmentCode departmentCode = department.getCode();
BigDecimal employeesCost = department.getTeams().stream()
.map(Team::getMembers).flatMap(List::stream)
.map(Member::getCost).reduce(BigDecimal.ZERO, BigDecimal::add);
costPerDepartment.put(departmentCode, employeesCost);
});
return costPerDepartment;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment