diff --git a/src/main/java/ru/javawebinar/topjava/model/UserMeal.java b/src/main/java/ru/javawebinar/topjava/model/UserMeal.java new file mode 100644 index 000000000000..d8f91b127f6a --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/model/UserMeal.java @@ -0,0 +1,29 @@ +package ru.javawebinar.topjava.model; + +import java.time.LocalDateTime; + +public class UserMeal { + private final LocalDateTime dateTime; + + private final String description; + + private final int calories; + + public UserMeal(LocalDateTime dateTime, String description, int calories) { + this.dateTime = dateTime; + this.description = description; + this.calories = calories; + } + + public LocalDateTime getDateTime() { + return dateTime; + } + + public String getDescription() { + return description; + } + + public int getCalories() { + return calories; + } +} diff --git a/src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java b/src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java new file mode 100644 index 000000000000..d0aa431a35d9 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java @@ -0,0 +1,30 @@ +package ru.javawebinar.topjava.model; + +import java.time.LocalDateTime; + +public class UserMealWithExcess { + private final LocalDateTime dateTime; + + private final String description; + + private final int calories; + + private final boolean excess; + + public UserMealWithExcess(LocalDateTime dateTime, String description, int calories, boolean excess) { + this.dateTime = dateTime; + this.description = description; + this.calories = calories; + this.excess = excess; + } + + @Override + public String toString() { + return "UserMealWithExcess{" + + "dateTime=" + dateTime + + ", description='" + description + '\'' + + ", calories=" + calories + + ", excess=" + excess + + '}'; + } +} diff --git a/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java b/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java new file mode 100644 index 000000000000..0ebfdb5fcdcb --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java @@ -0,0 +1,9 @@ +package ru.javawebinar.topjava.util; + +import java.time.LocalTime; + +public class TimeUtil { + public static boolean isBetweenHalfOpen(LocalTime lt, LocalTime startTime, LocalTime endTime) { + return lt.compareTo(startTime) >= 0 && lt.compareTo(endTime) < 0; + } +} diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java new file mode 100644 index 000000000000..e867a6134c00 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -0,0 +1,63 @@ +package ru.javawebinar.topjava.util; + +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; + + +public class UserMealsUtil { + public static void main(String[] args) { + List meals = Arrays.asList( + 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 mealsToCycles = filteredByCycles(meals, + LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); + mealsToCycles.forEach(System.out::println); + System.out.println("8---------------8"); + List mealsToStreams = filteredByStreams(meals, + LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); + mealsToStreams.forEach(System.out::println); + } + + public static List filteredByCycles(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { + Map mapForCaloriesPerDay = new HashMap<>(); + for (UserMeal userMealForMap : meals) { + LocalDate dateTimeOfMeal = userMealForMap.getDateTime().toLocalDate(); + mapForCaloriesPerDay.merge(dateTimeOfMeal, userMealForMap.getCalories(), Integer::sum); + } + List userMealWithExcesses = new ArrayList<>(); + for (UserMeal userMeal : meals) { + if (TimeUtil.isBetweenHalfOpen(userMeal.getDateTime().toLocalTime(), startTime, endTime)) { + 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 filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { + Map mapForCaloriesPerDay = meals.stream() + .collect(Collectors.toMap(userMeal -> userMeal.getDateTime().toLocalDate(), UserMeal::getCalories, Integer::sum)); + + return meals.stream() + .filter(userMeal -> TimeUtil.isBetweenHalfOpen(userMeal.getDateTime().toLocalTime(), startTime, endTime)) + .map(userMeal -> (new UserMealWithExcess(userMeal.getDateTime(), userMeal.getDescription(), userMeal.getCalories(), + (mapForCaloriesPerDay.get(userMeal.getDateTime().toLocalDate()) > caloriesPerDay)))) + .collect(Collectors.toList()); + } +}