-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Hw0 #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Hw0 #155
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
55db3cc
Apply prepare_to_HW0_patch.patch
b5573e2
HW0
b652601
HW0
19da412
HW0 and Optional
e8b0eb7
HW0 and Optional
4b271b5
1_0_rename.patch
08d9ddc
Merge remote-tracking branch 'origin/HW0' into HW0
4e566cd
update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
src/main/java/ru/javawebinar/topjava/model/Meal.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.javawebinar.topjava.model; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public class Meal { | ||
| private final LocalDateTime dateTime; | ||
|
|
||
| private final String description; | ||
|
|
||
| private final int calories; | ||
|
|
||
| public Meal(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; | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
src/main/java/ru/javawebinar/topjava/model/MealTo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.javawebinar.topjava.model; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public class MealTo { | ||
| private final LocalDateTime dateTime; | ||
|
|
||
| private final String description; | ||
|
|
||
| private final int calories; | ||
|
|
||
| private final boolean excess; | ||
|
|
||
| public MealTo(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 "MealTo{" + | ||
| "dateTime=" + dateTime + | ||
| ", description='" + description + '\'' + | ||
| ", calories=" + calories + | ||
| ", excess=" + excess + | ||
| '}'; | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
src/main/java/ru/javawebinar/topjava/util/MealsUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package ru.javawebinar.topjava.util; | ||
|
|
||
| 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.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class MealsUtil { | ||
| public static void main(String[] args) { | ||
| List<Meal> 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<MealTo> 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<MealTo> filteredByCycles(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { | ||
| Map<LocalDate, Integer> caloriesSumPerDates = new HashMap<>(); | ||
| for (Meal meal : meals) { | ||
| caloriesSumPerDates.merge(meal.getDateTime().toLocalDate(), meal.getCalories(), Integer::sum); | ||
| } | ||
| List<MealTo> 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<MealTo> filteredByStreams(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { | ||
| Map<LocalDate, Integer> 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); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
src/main/java/ru/javawebinar/topjava/util/TimeUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.