3
3
import ru .javawebinar .topjava .model .UserMeal ;
4
4
import ru .javawebinar .topjava .model .UserMealWithExcess ;
5
5
6
+ import java .time .LocalDate ;
6
7
import java .time .LocalDateTime ;
7
8
import java .time .LocalTime ;
8
9
import java .time .Month ;
9
- import java .util .Arrays ;
10
- import java .util .List ;
10
+ import java .util .*;
11
11
12
12
public class UserMealsUtil {
13
13
public static void main (String [] args ) {
@@ -28,12 +28,22 @@ public static void main(String[] args) {
28
28
}
29
29
30
30
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 ;
33
44
}
34
45
35
46
public static List <UserMealWithExcess > filteredByStreams (List <UserMeal > meals , LocalTime startTime , LocalTime endTime , int caloriesPerDay ) {
36
- // TODO Implement by streams
37
47
return null ;
38
48
}
39
49
}
0 commit comments