From 55db3cc1b922bf303f0a013c9c549a2be46ac96a Mon Sep 17 00:00:00 2001 From: bazhukovsi <1q2w3e4R!@> Date: Thu, 19 May 2022 16:56:21 +0300 Subject: [PATCH 1/7] Apply prepare_to_HW0_patch.patch --- .../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 b5573e24d2c49b2889630302027662e9fd8debdc Mon Sep 17 00:00:00 2001 From: bazhukovsi <1q2w3e4R!@> Date: Thu, 19 May 2022 17:04:36 +0300 Subject: [PATCH 2/7] HW0 --- src/main/java/ru/javawebinar/topjava/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ru/javawebinar/topjava/Main.java b/src/main/java/ru/javawebinar/topjava/Main.java index c2f9cc618f7c..b9df118371fc 100644 --- a/src/main/java/ru/javawebinar/topjava/Main.java +++ b/src/main/java/ru/javawebinar/topjava/Main.java @@ -6,6 +6,6 @@ */ public class Main { public static void main(String[] args) { - System.out.format("Hello TopJava Enterprise!"); + System.out.format("Hello TopJava Enterprise!!!!!!!!!!!!!!!!!"); } } From b6526010bfdf6bc8c31f8e081e6cd16a3ff646d8 Mon Sep 17 00:00:00 2001 From: bazhukovsi <1q2w3e4R!@> Date: Thu, 19 May 2022 22:51:41 +0300 Subject: [PATCH 3/7] HW0 --- .../topjava/util/UserMealsUtil.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 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..d4456fa2c217 100644 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -3,11 +3,11 @@ 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.*; public class UserMealsUtil { public static void main(String[] args) { @@ -28,12 +28,22 @@ public static void main(String[] args) { } public static List filteredByCycles(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - // TODO return filtered list with excess. Implement by cycles - return null; + Map caloriesSumPerDate = new HashMap<>(); + for (UserMeal meal : meals) { + caloriesSumPerDate.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); + } + List userMealWithExcesses = new ArrayList<>(); + for (UserMeal meal : meals) { + LocalDateTime dateTime = meal.getDateTime(); + if (TimeUtil.isBetweenHalfOpen(dateTime.toLocalTime(), startTime, endTime)) { + userMealWithExcesses.add(new UserMealWithExcess(meal.getDateTime(), meal.getDescription(), + meal.getCalories(), caloriesSumPerDate.get(dateTime.toLocalDate()) > caloriesPerDay)); + } + } + return userMealWithExcesses; } public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - // TODO Implement by streams return null; } } From 19da41207cde6d206348ae9c40db26d9bd1b109b Mon Sep 17 00:00:00 2001 From: bazhukovsi <1q2w3e4R!@> Date: Thu, 19 May 2022 23:58:10 +0300 Subject: [PATCH 4/7] HW0 and Optional HW0 and Optional HW0 and Optional --- .../topjava/util/UserMealsUtil.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java index d4456fa2c217..f95f3adf3dfd 100644 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -8,6 +8,7 @@ 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) { @@ -23,27 +24,36 @@ public static void main(String[] args) { 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)); + 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) { - Map caloriesSumPerDate = new HashMap<>(); + Map caloriesSumPerDates = new HashMap<>(); for (UserMeal meal : meals) { - caloriesSumPerDate.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); + caloriesSumPerDates.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); } - List userMealWithExcesses = new ArrayList<>(); + List userMealsWithExcesses = new ArrayList<>(); for (UserMeal meal : meals) { - LocalDateTime dateTime = meal.getDateTime(); - if (TimeUtil.isBetweenHalfOpen(dateTime.toLocalTime(), startTime, endTime)) { - userMealWithExcesses.add(new UserMealWithExcess(meal.getDateTime(), meal.getDescription(), - meal.getCalories(), caloriesSumPerDate.get(dateTime.toLocalDate()) > caloriesPerDay)); + LocalDateTime mealDateTime = meal.getDateTime(); + if (TimeUtil.isBetweenHalfOpen(mealDateTime.toLocalTime(), startTime, endTime)) { + userMealsWithExcesses.add(createUserWithExcess(meal, + caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)); } } - return userMealWithExcesses; + return userMealsWithExcesses; } public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - return null; + Map caloriesSumPerDates = meals.stream() + .collect(Collectors.groupingBy(meal -> meal.getDateTime().toLocalDate(),Collectors.summingInt(UserMeal::getCalories))); + return meals.stream() + .filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getDateTime().toLocalTime(), startTime, endTime)) + .map(meal -> createUserWithExcess(meal, caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)) + .collect(Collectors.toList()); + } + + private static UserMealWithExcess createUserWithExcess(UserMeal meal, boolean excess) { + return new UserMealWithExcess(meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess); } } From e8b0eb746adc4afaaaaa1de48d3156da7c666b7e Mon Sep 17 00:00:00 2001 From: bazhukovsi <1q2w3e4R!@> Date: Thu, 19 May 2022 23:58:10 +0300 Subject: [PATCH 5/7] HW0 and Optional HW0 and Optional HW0 and Optional HW0 and Optional --- .../topjava/util/UserMealsUtil.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java index d4456fa2c217..f068e1cf62d0 100644 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java @@ -8,6 +8,7 @@ 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) { @@ -23,27 +24,37 @@ public static void main(String[] args) { 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)); + 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) { - Map caloriesSumPerDate = new HashMap<>(); + Map caloriesSumPerDates = new HashMap<>(); for (UserMeal meal : meals) { - caloriesSumPerDate.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); + caloriesSumPerDates.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); } - List userMealWithExcesses = new ArrayList<>(); + List userMealsWithExcesses = new ArrayList<>(); for (UserMeal meal : meals) { - LocalDateTime dateTime = meal.getDateTime(); - if (TimeUtil.isBetweenHalfOpen(dateTime.toLocalTime(), startTime, endTime)) { - userMealWithExcesses.add(new UserMealWithExcess(meal.getDateTime(), meal.getDescription(), - meal.getCalories(), caloriesSumPerDate.get(dateTime.toLocalDate()) > caloriesPerDay)); + LocalDateTime mealDateTime = meal.getDateTime(); + if (TimeUtil.isBetweenHalfOpen(mealDateTime.toLocalTime(), startTime, endTime)) { + userMealsWithExcesses.add(createUserWithExcess(meal, + caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)); } } - return userMealWithExcesses; + return userMealsWithExcesses; } public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - return null; + Map caloriesSumPerDates = meals.stream() + .collect(Collectors.groupingBy(meal -> meal.getDateTime().toLocalDate(),Collectors.summingInt(UserMeal::getCalories))); + return meals.stream() + .filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getDateTime().toLocalTime(), startTime, endTime)) + .map(meal -> createUserWithExcess(meal, caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)) + .collect(Collectors.toList()); + } + + private static UserMealWithExcess createUserWithExcess(UserMeal meal, boolean excess) { + return new UserMealWithExcess(meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess); } + } From 4b271b5a75dd1b7f5f018ea4090249b9f97f04a7 Mon Sep 17 00:00:00 2001 From: bazhukovsi <1q2w3e4R!@> Date: Thu, 2 Jun 2022 12:28:22 +0300 Subject: [PATCH 6/7] 1_0_rename.patch --- .../model/{UserMeal.java => Meal.java} | 4 +- .../{UserMealWithExcess.java => MealTo.java} | 6 +- .../javawebinar/topjava/util/MealsUtil.java | 39 ++++++++++++ .../topjava/util/UserMealsUtil.java | 60 ------------------- 4 files changed, 44 insertions(+), 65 deletions(-) rename src/main/java/ru/javawebinar/topjava/model/{UserMeal.java => Meal.java} (83%) rename src/main/java/ru/javawebinar/topjava/model/{UserMealWithExcess.java => MealTo.java} (77%) create mode 100644 src/main/java/ru/javawebinar/topjava/util/MealsUtil.java delete 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/Meal.java similarity index 83% rename from src/main/java/ru/javawebinar/topjava/model/UserMeal.java rename to src/main/java/ru/javawebinar/topjava/model/Meal.java index d8f91b127f6a..f546cef0f74a 100644 --- a/src/main/java/ru/javawebinar/topjava/model/UserMeal.java +++ b/src/main/java/ru/javawebinar/topjava/model/Meal.java @@ -2,14 +2,14 @@ import java.time.LocalDateTime; -public class UserMeal { +public class Meal { private final LocalDateTime dateTime; private final String description; private final int calories; - public UserMeal(LocalDateTime dateTime, String description, int calories) { + public Meal(LocalDateTime dateTime, String description, int calories) { this.dateTime = dateTime; this.description = description; this.calories = calories; diff --git a/src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java b/src/main/java/ru/javawebinar/topjava/model/MealTo.java similarity index 77% rename from src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java rename to src/main/java/ru/javawebinar/topjava/model/MealTo.java index d0aa431a35d9..07f04f8dbb9f 100644 --- a/src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java +++ b/src/main/java/ru/javawebinar/topjava/model/MealTo.java @@ -2,7 +2,7 @@ import java.time.LocalDateTime; -public class UserMealWithExcess { +public class MealTo { private final LocalDateTime dateTime; private final String description; @@ -11,7 +11,7 @@ public class UserMealWithExcess { private final boolean excess; - public UserMealWithExcess(LocalDateTime dateTime, String description, int calories, boolean excess) { + public MealTo(LocalDateTime dateTime, String description, int calories, boolean excess) { this.dateTime = dateTime; this.description = description; this.calories = calories; @@ -20,7 +20,7 @@ public UserMealWithExcess(LocalDateTime dateTime, String description, int calori @Override public String toString() { - return "UserMealWithExcess{" + + return "MealTo{" + "dateTime=" + dateTime + ", description='" + description + '\'' + ", calories=" + calories + diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java new file mode 100644 index 000000000000..bb5ddbf5ccc5 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java @@ -0,0 +1,39 @@ +package ru.javawebinar.topjava.util; + +import ru.javawebinar.topjava.model.Meal; +import ru.javawebinar.topjava.model.MealTo; + +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.util.Arrays; +import java.util.List; + +public class MealsUtil { + public static void main(String[] args) { + List meals = Arrays.asList( + new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 10, 0), "Завтрак", 500), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 13, 0), "Обед", 1000), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 30, 20, 0), "Ужин", 500), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 0, 0), "Еда на граничное значение", 100), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 10, 0), "Завтрак", 1000), + new Meal(LocalDateTime.of(2020, Month.JANUARY, 31, 13, 0), "Обед", 500), + new Meal(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; + } +} diff --git a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java deleted file mode 100644 index f068e1cf62d0..000000000000 --- a/src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java +++ /dev/null @@ -1,60 +0,0 @@ -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.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("--------------------------------------------"); - 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) { - Map caloriesSumPerDates = new HashMap<>(); - for (UserMeal meal : meals) { - caloriesSumPerDates.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); - } - List userMealsWithExcesses = new ArrayList<>(); - for (UserMeal meal : meals) { - LocalDateTime mealDateTime = meal.getDateTime(); - if (TimeUtil.isBetweenHalfOpen(mealDateTime.toLocalTime(), startTime, endTime)) { - userMealsWithExcesses.add(createUserWithExcess(meal, - caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)); - } - } - return userMealsWithExcesses; - } - - public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - Map caloriesSumPerDates = meals.stream() - .collect(Collectors.groupingBy(meal -> meal.getDateTime().toLocalDate(),Collectors.summingInt(UserMeal::getCalories))); - return meals.stream() - .filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getDateTime().toLocalTime(), startTime, endTime)) - .map(meal -> createUserWithExcess(meal, caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)) - .collect(Collectors.toList()); - } - - private static UserMealWithExcess createUserWithExcess(UserMeal meal, boolean excess) { - return new UserMealWithExcess(meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess); - } - -} From 4e566cd8002d9ce7ea1f3ee16f085d5100a51b04 Mon Sep 17 00:00:00 2001 From: bazhukovsi <1q2w3e4R!@> Date: Thu, 2 Jun 2022 12:59:41 +0300 Subject: [PATCH 7/7] update --- .../javawebinar/topjava/util/MealsUtil.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java index bb5ddbf5ccc5..99685aceaab1 100644 --- a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java @@ -3,11 +3,12 @@ import ru.javawebinar.topjava.model.Meal; import ru.javawebinar.topjava.model.MealTo; +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 MealsUtil { public static void main(String[] args) { @@ -23,17 +24,36 @@ public static void main(String[] args) { 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)); + 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; + Map caloriesSumPerDates = new HashMap<>(); + for (Meal meal : meals) { + caloriesSumPerDates.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); + } + List userMealsWithExcesses = new ArrayList<>(); + for (Meal meal : meals) { + LocalDateTime mealDateTime = meal.getDateTime(); + if (TimeUtil.isBetweenHalfOpen(mealDateTime.toLocalTime(), startTime, endTime)) { + userMealsWithExcesses.add(createUserWithExcess(meal, + caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)); + } + } + return userMealsWithExcesses; } public static List filteredByStreams(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - // TODO Implement by streams - return null; + Map caloriesSumPerDates = meals.stream() + .collect(Collectors.groupingBy(meal -> meal.getDateTime().toLocalDate(),Collectors.summingInt(Meal::getCalories))); + return meals.stream() + .filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getDateTime().toLocalTime(), startTime, endTime)) + .map(meal -> createUserWithExcess(meal, caloriesSumPerDates.get(meal.getDateTime().toLocalDate()) > caloriesPerDay)) + .collect(Collectors.toList()); + } + + private static MealTo createUserWithExcess(Meal meal, boolean excess) { + return new MealTo(meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess); } }