forked from VolodymyrPortianko/topjava03
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserMealsUtil.java
More file actions
43 lines (38 loc) · 2.02 KB
/
UserMealsUtil.java
File metadata and controls
43 lines (38 loc) · 2.02 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
package ru.javawebinar.topjava.util;
import ru.javawebinar.topjava.model.UserMeal;
import ru.javawebinar.topjava.model.UserMealWithExceed;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* GKislin
* 31.05.2015.
*/
public class UserMealsUtil {
public static void main(String[] args) {
List<UserMeal> mealList = Arrays.asList(
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 500),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 1000),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510)
);
List<UserMealWithExceed> filteredMealsWithExceeded = getFilteredMealsWithExceeded(mealList, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000);
filteredMealsWithExceeded.forEach(System.out::println);
}
public static List<UserMealWithExceed> getFilteredMealsWithExceeded(List<UserMeal> mealList, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
Map<LocalDate, Integer> caloriesSumByDate = mealList.stream().collect(Collectors.groupingBy(um -> um.getDateTime().toLocalDate(),
Collectors.summingInt(UserMeal::getCalories)));
return mealList.stream()
.filter(um->TimeUtil.isBetween(um.getDateTime().toLocalTime(), startTime, endTime))
.map(um->new UserMealWithExceed(um.getDateTime(), um.getDescription(), um.getCalories(),
caloriesSumByDate.get(um.getDateTime().toLocalDate())> caloriesPerDay))
.collect(Collectors.toList());
}
}