8
8
import java .time .LocalTime ;
9
9
import java .time .Month ;
10
10
import java .util .*;
11
+ import java .util .stream .Collectors ;
11
12
12
13
public class UserMealsUtil {
13
14
public static void main (String [] args ) {
@@ -23,27 +24,36 @@ public static void main(String[] args) {
23
24
24
25
List <UserMealWithExcess > mealsTo = filteredByCycles (meals , LocalTime .of (7 , 0 ), LocalTime .of (12 , 0 ), 2000 );
25
26
mealsTo .forEach (System .out ::println );
26
-
27
- // System.out.println(filteredByStreams(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000));
27
+ System . out . println ( "--------------------------------------------" );
28
+ System .out .println (filteredByStreams (meals , LocalTime .of (7 , 0 ), LocalTime .of (12 , 0 ), 2000 ));
28
29
}
29
30
30
31
public static List <UserMealWithExcess > filteredByCycles (List <UserMeal > meals , LocalTime startTime , LocalTime endTime , int caloriesPerDay ) {
31
- Map <LocalDate , Integer > caloriesSumPerDate = new HashMap <>();
32
+ Map <LocalDate , Integer > caloriesSumPerDates = new HashMap <>();
32
33
for (UserMeal meal : meals ) {
33
- caloriesSumPerDate .merge (meal .getDateTime ().toLocalDate (), meal .getCalories (), Integer ::sum );
34
+ caloriesSumPerDates .merge (meal .getDateTime ().toLocalDate (), meal .getCalories (), Integer ::sum );
34
35
}
35
- List <UserMealWithExcess > userMealWithExcesses = new ArrayList <>();
36
+ List <UserMealWithExcess > userMealsWithExcesses = new ArrayList <>();
36
37
for (UserMeal meal : meals ) {
37
- LocalDateTime dateTime = meal .getDateTime ();
38
- if (TimeUtil .isBetweenHalfOpen (dateTime .toLocalTime (), startTime , endTime )) {
39
- userMealWithExcesses .add (new UserMealWithExcess (meal . getDateTime (), meal . getDescription () ,
40
- meal . getCalories (), caloriesSumPerDate . get ( dateTime .toLocalDate ()) > caloriesPerDay ));
38
+ LocalDateTime mealDateTime = meal .getDateTime ();
39
+ if (TimeUtil .isBetweenHalfOpen (mealDateTime .toLocalTime (), startTime , endTime )) {
40
+ userMealsWithExcesses .add (createUserWithExcess (meal ,
41
+ caloriesSumPerDates . get ( meal . getDateTime () .toLocalDate ()) > caloriesPerDay ));
41
42
}
42
43
}
43
- return userMealWithExcesses ;
44
+ return userMealsWithExcesses ;
44
45
}
45
46
46
47
public static List <UserMealWithExcess > filteredByStreams (List <UserMeal > meals , LocalTime startTime , LocalTime endTime , int caloriesPerDay ) {
47
- return null ;
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 ))
53
+ .collect (Collectors .toList ());
54
+ }
55
+
56
+ private static UserMealWithExcess createUserWithExcess (UserMeal meal , boolean excess ) {
57
+ return new UserMealWithExcess (meal .getDateTime (), meal .getDescription (), meal .getCalories (), excess );
48
58
}
49
59
}
0 commit comments