88import  java .time .LocalTime ;
99import  java .time .Month ;
1010import  java .util .*;
11+ import  java .util .stream .Collectors ;
1112
1213public  class  UserMealsUtil  {
1314    public  static  void  main (String [] args ) {
@@ -23,27 +24,36 @@ public static void main(String[] args) {
2324
2425        List <UserMealWithExcess > mealsTo  = filteredByCycles (meals , LocalTime .of (7 , 0 ), LocalTime .of (12 , 0 ), 2000 );
2526        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 ));
2829    }
2930
3031    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 <>();
3233        for  (UserMeal  meal  : meals ) {
33-             caloriesSumPerDate .merge (meal .getDateTime ().toLocalDate (), meal .getCalories (), Integer ::sum );
34+             caloriesSumPerDates .merge (meal .getDateTime ().toLocalDate (), meal .getCalories (), Integer ::sum );
3435        }
35-         List <UserMealWithExcess > userMealWithExcesses  = new  ArrayList <>();
36+         List <UserMealWithExcess > userMealsWithExcesses  = new  ArrayList <>();
3637        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 ));
4142            }
4243        }
43-         return  userMealWithExcesses ;
44+         return  userMealsWithExcesses ;
4445    }
4546
4647    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 );
4858    }
4959}
0 commit comments