33import ru .javawebinar .topjava .model .UserMeal ;
44import ru .javawebinar .topjava .model .UserMealWithExcess ;
55
6+ import java .time .LocalDate ;
67import java .time .LocalDateTime ;
78import java .time .LocalTime ;
89import java .time .Month ;
9- import java .util .Arrays ;
10- import java .util .List ;
10+ import java .util .*;
1111
1212public class UserMealsUtil {
1313 public static void main (String [] args ) {
@@ -28,12 +28,22 @@ public static void main(String[] args) {
2828 }
2929
3030 public static List <UserMealWithExcess > filteredByCycles (List <UserMeal > meals , LocalTime startTime , LocalTime endTime , int caloriesPerDay ) {
31- // TODO return filtered list with excess. Implement by cycles
32- return null ;
31+ Map <LocalDate , Integer > caloriesSumPerDate = new HashMap <>();
32+ for (UserMeal meal : meals ) {
33+ caloriesSumPerDate .merge (meal .getDateTime ().toLocalDate (), meal .getCalories (), Integer ::sum );
34+ }
35+ List <UserMealWithExcess > userMealWithExcesses = new ArrayList <>();
36+ 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 ));
41+ }
42+ }
43+ return userMealWithExcesses ;
3344 }
3445
3546 public static List <UserMealWithExcess > filteredByStreams (List <UserMeal > meals , LocalTime startTime , LocalTime endTime , int caloriesPerDay ) {
36- // TODO Implement by streams
3747 return null ;
3848 }
3949}
0 commit comments