-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathMealsUtil.java
More file actions
59 lines (52 loc) · 3.07 KB
/
MealsUtil.java
File metadata and controls
59 lines (52 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ru.javawebinar.topjava.util;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.model.MealTo;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.*;
import java.util.stream.Collectors;
public class MealsUtil {
public static void main(String[] args) {
List<Meal> meals = Arrays.asList(
new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 10, 0), "Завтрак", 500),
new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 13, 0), "Обед", 1000),
new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 20, 0), "Ужин", 500),
new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 0, 0), "Еда на граничное значение", 100),
new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 10, 0), "Завтрак", 1000),
new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 13, 0), "Обед", 500),
new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 20, 0), "Ужин", 410)
);
List<MealTo> mealsTo = filteredByCycles(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000);
mealsTo.forEach(System.out::println);
System.out.println("--------------------------------------------");
System.out.println(filteredByStreams(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000));
}
public static List<MealTo> filteredByCycles(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
Map<LocalDate, Integer> caloriesSumPerDates = new HashMap<>();
for (Meal meal : meals) {
caloriesSumPerDates.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum);
}
List<MealTo> userMealsWithExcesses = new ArrayList<>();
for (Meal meal : meals) {
LocalDateTime mealDateTime = meal.getDateTime();
if (TimeUtil.isBetweenHalfOpen(mealDateTime.toLocalTime(), startTime, endTime)) {
userMealsWithExcesses.add(createUserWithExcess(meal,
caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay));
}
}
return userMealsWithExcesses;
}
public static List<MealTo> filteredByStreams(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
Map<LocalDate, Integer> caloriesSumPerDates = meals.stream()
.collect(Collectors.groupingBy(meal -> meal.getDateTime().toLocalDate(),Collectors.summingInt(Meal::getCalories)));
return meals.stream()
.filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getDateTime().toLocalTime(), startTime, endTime))
.map(meal -> createUserWithExcess(meal, caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay))
.collect(Collectors.toList());
}
private static MealTo createUserWithExcess(Meal meal, boolean excess) {
return new MealTo(meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess);
}
}