From 4472b7998f3b36889993aaa855c865cde418da33 Mon Sep 17 00:00:00 2001 From: Anastasiia Iakimova Date: Mon, 26 Sep 2022 14:17:27 +0300 Subject: [PATCH 1/5] Prepare to HW0 --- .../javawebinar/topjava/model/UserMeal.java | 29 ++++++++++++++ .../topjava/model/UserMealWithExcess.java | 30 ++++++++++++++ .../ru/javawebinar/topjava/util/TimeUtil.java | 9 +++++ .../topjava/util/UserMealsUtil.java | 39 +++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/main/java/ru/javawebinar/topjava/model/UserMeal.java create mode 100644 src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java create mode 100644 src/main/java/ru/javawebinar/topjava/util/TimeUtil.java create mode 100644 src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java 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..3c171b4a5972 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -0,0 +1,39 @@ +package ru.javawebinar.topjava.util; + +import ru.javawebinar.topjava.model.UserMeal; +import ru.javawebinar.topjava.model.UserMealWithExcess; + +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.util.Arrays; +import java.util.List; + +public class UserMealsUtil { + public static void main(String[] args) { + List 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) + ); + + List mealsTo = filteredByCycles(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); + mealsTo.forEach(System.out::println); + +// System.out.println(filteredByStreams(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000)); + } + + public static List filteredByCycles(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { + // TODO return filtered list with excess. Implement by cycles + return null; + } + + public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { + // TODO Implement by streams + return null; + } +} From 1ebc5bb0bd21fb35e9f3cc3422c58ad9faffbd24 Mon Sep 17 00:00:00 2001 From: Anastasiia Iakimova Date: Wed, 28 Sep 2022 15:54:55 +0300 Subject: [PATCH 2/5] HWO --- .../topjava/util/UserMealsUtil.java | 53 ++++++++++++++++--- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java index 3c171b4a5972..a019efae2740 100644 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -3,11 +3,12 @@ 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.Arrays; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; public class UserMealsUtil { public static void main(String[] args) { @@ -21,19 +22,57 @@ public static void main(String[] args) { new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 31, 20, 0), "Ужин", 410) ); - List mealsTo = filteredByCycles(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); - mealsTo.forEach(System.out::println); + List mealsToCycles = filteredByCycles(meals, LocalTime.of(0, 0), LocalTime.of(21, 0), 1000); + mealsToCycles.forEach(System.out::println); + System.out.println("8---------------8"); + List mealsToStreams = filteredByStreams(meals, LocalTime.of(0, 0), LocalTime.of(21, 0), 1000); + mealsToStreams.forEach(System.out::println); + -// System.out.println(filteredByStreams(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000)); } public static List filteredByCycles(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { // TODO return filtered list with excess. Implement by cycles - return null; + List filteredMeals = new ArrayList<>(); + Map mapForCaloriesPerDay = new HashMap<>(); + for (UserMeal mUserMeal : meals) { + boolean checkedTime = (mUserMeal.getDateTime().toLocalTime().isAfter(startTime) + && mUserMeal.getDateTime().toLocalTime().isBefore(endTime)); + if (checkedTime) { + filteredMeals.add(mUserMeal); + } + if (checkedTime && !mapForCaloriesPerDay.containsKey(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth()))) { + mapForCaloriesPerDay.put(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth()), mUserMeal.getCalories()); + } else if (checkedTime) { + mapForCaloriesPerDay.replace(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth()), + (mapForCaloriesPerDay.get(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth())) + mUserMeal.getCalories())); + } + + } + List userMealWithExcesses = new ArrayList<>(); + for (UserMeal userMeal : filteredMeals) { + boolean checkedPerDay = mapForCaloriesPerDay.get(LocalDate.ofEpochDay(userMeal.getDateTime().getDayOfMonth())) > 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) { // TODO Implement by streams - return null; + List userMealList = meals.stream().filter(mUserMeal -> mUserMeal.getDateTime().toLocalTime().isBefore(endTime) && + mUserMeal.getDateTime().toLocalTime().isAfter(startTime)).collect(Collectors.toList()); + + Map filteredMeals = userMealList.stream(). + collect(Collectors.groupingBy(UserM -> LocalDate.ofEpochDay(UserM.getDateTime().getDayOfMonth()), + Collectors.summingInt(UserMeal::getCalories))); + + + return userMealList.stream().map(userMeal -> { + return (new UserMealWithExcess(userMeal.getDateTime(), userMeal.getDescription(), userMeal.getCalories(), + (filteredMeals.get(LocalDate.ofEpochDay(userMeal.getDateTime().getDayOfMonth())) > caloriesPerDay))); + }). + collect(Collectors.toList()); } } From c1a6af0cc0890c872ebe737945a723ebb76bbde9 Mon Sep 17 00:00:00 2001 From: Anastasiia Iakimova Date: Thu, 29 Sep 2022 16:14:43 +0300 Subject: [PATCH 3/5] HW0 Fixed --- .../topjava/util/UserMealsUtil.java | 59 ++++++++----------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java index a019efae2740..113f0fdc4647 100644 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -3,7 +3,6 @@ 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; @@ -22,57 +21,45 @@ public static void main(String[] args) { new UserMeal(LocalDateTime.of(2020, Month.JANUARY, 31, 20, 0), "Ужин", 410) ); - List mealsToCycles = filteredByCycles(meals, LocalTime.of(0, 0), LocalTime.of(21, 0), 1000); + 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(0, 0), LocalTime.of(21, 0), 1000); + 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) { - // TODO return filtered list with excess. Implement by cycles - List filteredMeals = new ArrayList<>(); - Map mapForCaloriesPerDay = new HashMap<>(); - for (UserMeal mUserMeal : meals) { - boolean checkedTime = (mUserMeal.getDateTime().toLocalTime().isAfter(startTime) - && mUserMeal.getDateTime().toLocalTime().isBefore(endTime)); - if (checkedTime) { - filteredMeals.add(mUserMeal); - } - if (checkedTime && !mapForCaloriesPerDay.containsKey(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth()))) { - mapForCaloriesPerDay.put(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth()), mUserMeal.getCalories()); - } else if (checkedTime) { - mapForCaloriesPerDay.replace(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth()), - (mapForCaloriesPerDay.get(LocalDate.ofEpochDay(mUserMeal.getDateTime().getDayOfMonth())) + mUserMeal.getCalories())); - } - + Map mapForCaloriesPerDay = new HashMap<>(); + for (UserMeal userMealForMap : meals) { + int day = userMealForMap.getDateTime().getDayOfMonth(); + mapForCaloriesPerDay.merge(day, userMealForMap.getCalories(), Integer::sum); } + List userMealWithExcesses = new ArrayList<>(); - for (UserMeal userMeal : filteredMeals) { - boolean checkedPerDay = mapForCaloriesPerDay.get(LocalDate.ofEpochDay(userMeal.getDateTime().getDayOfMonth())) > caloriesPerDay; - UserMealWithExcess element = new UserMealWithExcess(userMeal.getDateTime(), userMeal.getDescription(), - userMeal.getCalories(), checkedPerDay); - userMealWithExcesses.add(element); + for (UserMeal userMeal : meals) { + if (TimeUtil.isBetweenHalfOpen(userMeal.getDateTime().toLocalTime(), startTime, endTime)) { + boolean checkedPerDay = (mapForCaloriesPerDay.get(userMeal.getDateTime().getDayOfMonth()) > 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) { - // TODO Implement by streams - List userMealList = meals.stream().filter(mUserMeal -> mUserMeal.getDateTime().toLocalTime().isBefore(endTime) && - mUserMeal.getDateTime().toLocalTime().isAfter(startTime)).collect(Collectors.toList()); - - Map filteredMeals = userMealList.stream(). - collect(Collectors.groupingBy(UserM -> LocalDate.ofEpochDay(UserM.getDateTime().getDayOfMonth()), - Collectors.summingInt(UserMeal::getCalories))); + Map groupMealsOfDays = new HashMap<>(); + meals.forEach(m -> groupMealsOfDays.merge(m.getDateTime().getDayOfMonth(), m.getCalories(), Integer::sum)); - return userMealList.stream().map(userMeal -> { - return (new UserMealWithExcess(userMeal.getDateTime(), userMeal.getDescription(), userMeal.getCalories(), - (filteredMeals.get(LocalDate.ofEpochDay(userMeal.getDateTime().getDayOfMonth())) > caloriesPerDay))); - }). - collect(Collectors.toList()); + 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)))) + .collect(Collectors.toList()); } } From 705eb9ebacac4e5694899cdc252d6de8bc85cd5b Mon Sep 17 00:00:00 2001 From: Anastasiia Iakimova Date: Fri, 30 Sep 2022 11:04:07 +0300 Subject: [PATCH 4/5] HW0 fixed1 --- .../topjava/util/UserMealsUtil.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java index 113f0fdc4647..59f6a0f14868 100644 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -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 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 mealsToCycles = filteredByCycles(meals, @@ -28,21 +31,18 @@ public static void main(String[] args) { 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<>(); + Map 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 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); @@ -50,16 +50,14 @@ public static List filteredByCycles(List meals, Lo } return userMealWithExcesses; } - public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - - Map groupMealsOfDays = new HashMap<>(); - meals.forEach(m -> groupMealsOfDays.merge(m.getDateTime().getDayOfMonth(), m.getCalories(), Integer::sum)); + Map 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()); } } From 4b6832e17ecd5a7ccd6d7d216f347d6a9209e80a Mon Sep 17 00:00:00 2001 From: Anastasiia Iakimova Date: Sat, 1 Oct 2022 20:02:48 +0300 Subject: [PATCH 5/5] HWO --- src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java index 59f6a0f14868..e867a6134c00 100644 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -10,7 +10,6 @@ import java.util.*; import java.util.stream.Collectors; -import static java.util.stream.Collectors.toMap; public class UserMealsUtil { public static void main(String[] args) { @@ -50,9 +49,10 @@ public static List filteredByCycles(List meals, Lo } 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, HashMap::new)); + .collect(Collectors.toMap(userMeal -> userMeal.getDateTime().toLocalDate(), UserMeal::getCalories, Integer::sum)); return meals.stream() .filter(userMeal -> TimeUtil.isBetweenHalfOpen(userMeal.getDateTime().toLocalTime(), startTime, endTime))