Skip to content
Open

Hw0 #164

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
HW0 fixed1
  • Loading branch information
Anastasiia Iakimova authored and Anastasiia Iakimova committed Sep 30, 2022
commit 705eb9ebacac4e5694899cdc252d6de8bc85cd5b
36 changes: 17 additions & 19 deletions src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
import ru.javawebinar.topjava.model.UserMeal;
import ru.javawebinar.topjava.model.UserMealWithExcess;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

public class UserMealsUtil {
public static void main(String[] args) {
List<UserMeal> meals = Arrays.asList(
new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 30, 10, 0), "Завтрак", 500),
new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 30, 13, 0), "Обед", 1000),
new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 30, 20, 0), "Ужин", 500),
new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 31, 0, 0), "Еда на граничное значение", 100),
new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 31, 10, 0), "Завтрак", 1000),
new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 31, 13, 0), "Обед", 500),
new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 31, 20, 0), "Ужин", 410)
new UserMeal(LocalDateTime.of(2020, Month.MARCH, 30, 10, 0), "Завтрак", 500),
new UserMeal(LocalDateTime.of(2020, Month.MARCH, 30, 13, 0), "Обед", 1000),
new UserMeal(LocalDateTime.of(2020, Month.MARCH, 30, 20, 0), "Ужин", 500),
new UserMeal(LocalDateTime.of(2020, Month.MARCH, 31, 0, 0), "Еда на граничное значение", 100),
new UserMeal(LocalDateTime.of(2020, Month.MARCH, 31, 10, 0), "Завтрак", 1000),
new UserMeal(LocalDateTime.of(2020, Month.MARCH, 31, 13, 0), "Обед", 500),
new UserMeal(LocalDateTime.of(2020, Month.MARCH, 31, 20, 0), "Ужин", 410)
);

List<UserMealWithExcess> mealsToCycles = filteredByCycles(meals,
Expand All @@ -28,38 +31,33 @@ public static void main(String[] args) {
List<UserMealWithExcess> mealsToStreams = filteredByStreams(meals,
LocalTime.of(7, 0), LocalTime.of(12, 0), 2000);
mealsToStreams.forEach(System.out::println);


}

public static List<UserMealWithExcess> filteredByCycles(List<UserMeal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
Map<Integer, Integer> mapForCaloriesPerDay = new HashMap<>();
Map<LocalDate, Integer> mapForCaloriesPerDay = new HashMap<>();
for (UserMeal userMealForMap : meals) {
int day = userMealForMap.getDateTime().getDayOfMonth();
mapForCaloriesPerDay.merge(day, userMealForMap.getCalories(), Integer::sum);
LocalDate dateTimeOfMeal = userMealForMap.getDateTime().toLocalDate();
mapForCaloriesPerDay.merge(dateTimeOfMeal, userMealForMap.getCalories(), Integer::sum);
}

List<UserMealWithExcess> userMealWithExcesses = new ArrayList<>();
for (UserMeal userMeal : meals) {
if (TimeUtil.isBetweenHalfOpen(userMeal.getDateTime().toLocalTime(), startTime, endTime)) {
boolean checkedPerDay = (mapForCaloriesPerDay.get(userMeal.getDateTime().getDayOfMonth()) > caloriesPerDay);
boolean checkedPerDay = (mapForCaloriesPerDay.get(userMeal.getDateTime().toLocalDate()) > caloriesPerDay);
UserMealWithExcess element = new UserMealWithExcess(userMeal.getDateTime(), userMeal.getDescription(),
userMeal.getCalories(), checkedPerDay);
userMealWithExcesses.add(element);
}
}
return userMealWithExcesses;
}

public static List<UserMealWithExcess> filteredByStreams(List<UserMeal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {

Map<Integer, Integer> groupMealsOfDays = new HashMap<>();
meals.forEach(m -> groupMealsOfDays.merge(m.getDateTime().getDayOfMonth(), m.getCalories(), Integer::sum));
Map<LocalDate, Integer> mapForCaloriesPerDay = meals.stream()
.collect(Collectors.toMap(userMeal -> userMeal.getDateTime().toLocalDate(), UserMeal::getCalories, Integer::sum, HashMap::new));

return meals.stream()
.filter(userMeal -> TimeUtil.isBetweenHalfOpen(userMeal.getDateTime().toLocalTime(), startTime, endTime))
.map(userMeal -> (new UserMealWithExcess(userMeal.getDateTime(), userMeal.getDescription(), userMeal.getCalories(),
(groupMealsOfDays.get(userMeal.getDateTime().getDayOfMonth()) > caloriesPerDay))))
(mapForCaloriesPerDay.get(userMeal.getDateTime().toLocalDate()) > caloriesPerDay))))
.collect(Collectors.toList());
}
}