Skip to content

Commit a3f0685

Browse files
committed
реализация дз через циклы
1 parent 0b8d782 commit a3f0685

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/main/java/ru/javawebinar/topjava/model/UserMealWithExceed.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ public UserMealWithExceed(LocalDateTime dateTime, String description, int calori
2121
this.calories = calories;
2222
this.exceed = exceed;
2323
}
24+
25+
@Override
26+
public String toString() {
27+
return "UserMealWithExceed{" +
28+
"dateTime=" + dateTime +
29+
", description='" + description + '\'' +
30+
", calories=" + calories +
31+
", exceed=" + exceed +
32+
'}';
33+
}
2434
}

src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import ru.javawebinar.topjava.model.UserMeal;
44
import ru.javawebinar.topjava.model.UserMealWithExceed;
55

6+
import java.time.LocalDate;
67
import java.time.LocalDateTime;
78
import java.time.LocalTime;
89
import java.time.Month;
9-
import java.util.Arrays;
10-
import java.util.List;
10+
import java.util.*;
11+
import java.util.stream.Stream;
1112

1213
/**
1314
* GKislin
@@ -23,13 +24,44 @@ public static void main(String[] args) {
2324
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31,13,0), "Обед", 500),
2425
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31,20,0), "Ужин", 510)
2526
);
26-
getFilteredMealsWithExceeded(mealList, LocalTime.of(7, 0), LocalTime.of(12,0), 2000);
27+
getFilteredMealsWithExceeded(mealList, LocalTime.of(7, 0), LocalTime.of(12,0), 2000).stream().forEach(u -> System.out.println(u.toString()));
2728
// .toLocalDate();
2829
// .toLocalTime();
2930
}
3031

3132
public static List<UserMealWithExceed> getFilteredMealsWithExceeded(List<UserMeal> mealList, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
32-
// TODO return filtered list with correctly exceeded field
33-
return null;
33+
34+
boolean exceeded;
35+
Map<LocalDate,Boolean> dates = new HashMap<>();
36+
37+
List<UserMealWithExceed>userMealWithExceeds = new ArrayList<>();
38+
Set<LocalDate> localDates = new HashSet<>();
39+
40+
for (UserMeal uM : mealList) {
41+
localDates.add(uM.getDateTime().toLocalDate());
42+
}
43+
44+
for (LocalDate date : localDates) {
45+
int calories = 0;
46+
for (UserMeal uM : mealList) {
47+
if (uM.getDateTime().toLocalDate().equals(date)) {
48+
calories += uM.getCalories();
49+
}
50+
}
51+
52+
if (calories > caloriesPerDay) exceeded = true;
53+
else exceeded = false;
54+
55+
dates.put(date,exceeded);
56+
}
57+
58+
for (UserMeal uM : mealList) {
59+
userMealWithExceeds.add(new UserMealWithExceed(uM.getDateTime(),
60+
uM.getDescription(),
61+
uM.getCalories(),
62+
dates.get(uM.getDateTime().toLocalDate())));
63+
}
64+
65+
return userMealWithExceeds;
3466
}
3567
}

0 commit comments

Comments
 (0)