Skip to content
Closed
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
33 changes: 33 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/UserMeal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.javawebinar.topjava.model;

import java.time.LocalDateTime;

/**
* GKislin
* 11.01.2015.
*/
public class UserMeal {
protected final LocalDateTime dateTime;

protected final String description;

protected 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;
}
}
24 changes: 24 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/UserMealWithExceed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.javawebinar.topjava.model;

import java.time.LocalDateTime;

/**
* GKislin
* 11.01.2015.
*/
public class UserMealWithExceed {
protected final LocalDateTime dateTime;

protected final String description;

protected final int calories;

protected final boolean exceed;

public UserMealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
this.exceed = exceed;
}
}
13 changes: 13 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,13 @@
package ru.javawebinar.topjava.util;

import java.time.LocalTime;

/**
* GKislin
* 07.01.2015.
*/
public class TimeUtil {
public static boolean isBetween(LocalTime lt, LocalTime startTime, LocalTime endTime) {
return lt.compareTo(startTime) >= 0 && lt.compareTo(endTime) <= 0;
}
}
51 changes: 51 additions & 0 deletions src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.javawebinar.topjava.util;

import ru.javawebinar.topjava.model.UserMeal;
import ru.javawebinar.topjava.model.UserMealWithExceed;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
* GKislin
* 31.05.2015.
*/
public class UserMealsUtil {
public static void main(String[] args) {
List<UserMeal> mealList = Arrays.asList(
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 1000),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 500),
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510)
);
List<UserMealWithExceed> exceeded = getFilteredMealsWithExceeded(mealList, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000);
System.out.println(exceeded.toString());
// .toLocalDate();
// .toLocalTime();
}

public static List<UserMealWithExceed> getFilteredMealsWithExceeded(List<UserMeal> mealList, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
Map<LocalDate, Integer> consumedCaloriesPerDay = mealList.stream().collect(Collectors.groupingBy(userMeal -> userMeal.getDateTime().toLocalDate(), Collectors.summingInt(UserMeal::getCalories)));

return mealList.stream()
.filter(meal -> TimeUtil.isBetween(meal.getDateTime().toLocalTime(), startTime, endTime))
.map(userMeal -> new UserMealWithExceed(
userMeal.getDateTime(),
userMeal.getDescription(),
userMeal.getCalories(),
consumedCaloriesPerDay.get(userMeal.getDateTime().toLocalDate()) > caloriesPerDay))
.collect(Collectors.toList());

}
}