Skip to content

Commit eb57532

Browse files
bazhukovsibazhukovsi
authored andcommitted
HW0 and Optional
1 parent 1abaf4e commit eb57532

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,31 @@ public static void main(String[] args) {
2929
}
3030

3131
public static List<UserMealWithExcess> filteredByCycles(List<UserMeal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
32-
Map<LocalDate, Integer> caloriesSumPerDate = new HashMap<>();
32+
Map<LocalDate, Integer> caloriesSumPerDates = new HashMap<>();
3333
for (UserMeal meal : meals) {
34-
caloriesSumPerDate.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum);
34+
caloriesSumPerDates.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum);
3535
}
36-
List<UserMealWithExcess> userMealWithExcesses = new ArrayList<>();
36+
List<UserMealWithExcess> userMealsWithExcesses = new ArrayList<>();
3737
for (UserMeal meal : meals) {
38-
LocalDateTime dateTime = meal.getDateTime();
39-
if (TimeUtil.isBetweenHalfOpen(dateTime.toLocalTime(), startTime, endTime)) {
40-
userMealWithExcesses.add(createUserWithExcess(meal,
41-
caloriesSumPerDate.get(meal.getDateTime().toLocalDate()) > caloriesPerDay));
38+
LocalDateTime dateTimeMeal = meal.getDateTime();
39+
if (TimeUtil.isBetweenHalfOpen(dateTimeMeal.toLocalTime(), startTime, endTime)) {
40+
userMealsWithExcesses.add(createUserWithExcess(meal,
41+
caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay));
4242
}
4343
}
44-
return userMealWithExcesses;
44+
return userMealsWithExcesses;
4545
}
4646

4747
public static List<UserMealWithExcess> filteredByStreams(List<UserMeal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
48-
Map<LocalDate, Integer> caloriesSumPerDate = meals.stream().collect(Collectors.groupingBy(meal -> meal.getDateTime().toLocalDate(),
49-
Collectors.summingInt(UserMeal::getCalories)));
50-
return meals.stream().filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getDateTime().toLocalTime(), startTime, endTime))
51-
.map(meal -> createUserWithExcess(meal, caloriesSumPerDate.get(meal.getDateTime().toLocalDate()) > caloriesPerDay))
48+
Map<LocalDate, Integer> caloriesSumPerDates = meals.stream()
49+
.collect(Collectors.groupingBy(meal -> meal.getDateTime().toLocalDate(),Collectors.summingInt(UserMeal::getCalories)));
50+
return meals.stream()
51+
.filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getDateTime().toLocalTime(), startTime, endTime))
52+
.map(meal -> createUserWithExcess(meal, caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay))
5253
.collect(Collectors.toList());
5354
}
5455

55-
public static UserMealWithExcess createUserWithExcess(UserMeal meal, boolean excess) {
56+
private static UserMealWithExcess createUserWithExcess(UserMeal meal, boolean excess) {
5657
return new UserMealWithExcess(meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess);
5758
}
5859
}

0 commit comments

Comments
 (0)