Skip to content
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

Hw0 #155

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Hw0 #155

Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/main/java/ru/javawebinar/topjava/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!!!!!!!!!!!!!!!!!");
}
}
29 changes: 29 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/Meal.java
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
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
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
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;
}
}