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

Test pull request #196

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>ru.javawebinar</groupId>
<artifactId>topjava</artifactId>
<packaging>jar</packaging>
<packaging>war</packaging>

<version>1.0-SNAPSHOT</version>

Expand All @@ -19,21 +19,31 @@

<build>
<finalName>topjava</finalName>
<defaultGoal>install</defaultGoal>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
39 changes: 39 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,39 @@
package ru.javawebinar.topjava.model;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

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;
}

public LocalTime getTime() {
return dateTime.toLocalTime();
}

public LocalDate getDate() {
return dateTime.toLocalDate();
}
}
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 "UserMealWithExcess{" +
"dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
", excess=" + excess +
'}';
}
}
45 changes: 45 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,45 @@
package ru.javawebinar.topjava.util;

import org.jetbrains.annotations.NotNull;
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.Map;
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)
);
}

public static List<MealTo> filteredByStreams(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
Map<LocalDate, Integer> caloriesSumByDate = meals.stream()
.collect(
Collectors.groupingBy(Meal::getDate, Collectors.summingInt(Meal::getCalories))
// Collectors.toMap(Meal::getDate, Meal::getCalories, Integer::sum)
);

return meals.stream()
.filter(meal -> TimeUtil.isBetweenHalfOpen(meal.getTime(), startTime, endTime))
.map(meal -> createTo(meal, caloriesSumByDate.get(meal.getDate()) > caloriesPerDay))
.collect(Collectors.toList());
}

private static MealTo createTo(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;
}
}
7 changes: 7 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>TopJava</display-name>
</web-app>
10 changes: 10 additions & 0 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Java Enterprise (Topjava)</title>
</head>
<body>
<h3>Проект <a href="https://github.com/JavaWebinar/topjava" target="_blank">Java Enterprise (Topjava)</a></h3>
</body>
</html>