Skip to content

Commit

Permalink
some patches applied
Browse files Browse the repository at this point in the history
  • Loading branch information
jet_vorobev committed Sep 8, 2016
1 parent de90d0c commit c59221d
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 63 deletions.
12 changes: 10 additions & 2 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,7 +19,7 @@

<build>
<finalName>topjava</finalName>
<defaultGoal>install</defaultGoal>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -34,6 +34,14 @@
</build>

<dependencies>

<!--Web-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package ru.javawebinar.topjava.model;

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

/**
* GKislin
* 11.01.2015.
*/
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;
Expand All @@ -30,4 +32,12 @@ public String getDescription() {
public int getCalories() {
return calories;
}

public LocalDate getDate() {
return dateTime.toLocalDate();
}

public LocalTime getTime() {
return dateTime.toLocalTime();
}
}
34 changes: 34 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.javawebinar.topjava.model;

import java.time.LocalDateTime;

/**
* GKislin
* 11.01.2015.
*/
public class MealWithExceed {
private final LocalDateTime dateTime;

private final String description;

private final int calories;

private final boolean exceed;

public MealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
this.exceed = exceed;
}

@Override
public String toString() {
return "UserMealWithExceed{" +
"dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
", exceed=" + exceed +
'}';
}
}
24 changes: 0 additions & 24 deletions src/main/java/ru/javawebinar/topjava/model/UserMealWithExceed.java

This file was deleted.

63 changes: 63 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,63 @@
package ru.javawebinar.topjava.util;

import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.model.MealWithExceed;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.*;
import java.util.stream.Collectors;

/**
* GKislin
* 31.05.2015.
*/
public class MealsUtil {
public static void main(String[] args) {
List<Meal> meals = Arrays.asList(
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500),
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000),
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500),
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 1000),
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 500),
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510)
);
List<MealWithExceed> filteredMealsWithExceeded = getFilteredWithExceeded(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000);
filteredMealsWithExceeded.forEach(System.out::println);

System.out.println(getFilteredWithExceededByCycle(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000));
}

public static List<MealWithExceed> getFilteredWithExceeded(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.isBetween(meal.getTime(), startTime, endTime))
.map(meal -> createWithExceed(meal, caloriesSumByDate.get(meal.getDate()) > caloriesPerDay))
.collect(Collectors.toList());
}

public static List<MealWithExceed> getFilteredWithExceededByCycle(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {

final Map<LocalDate, Integer> caloriesSumByDate = new HashMap<>();
meals.forEach(meal -> caloriesSumByDate.merge(meal.getDate(), meal.getCalories(), Integer::sum));

final List<MealWithExceed> mealExceeded = new ArrayList<>();
meals.forEach(meal -> {
if (TimeUtil.isBetween(meal.getTime(), startTime, endTime)) {
mealExceeded.add(createWithExceed(meal, caloriesSumByDate.get(meal.getDate()) > caloriesPerDay));
}
});
return mealExceeded;
}

public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) {
return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded);
}
}
35 changes: 0 additions & 35 deletions src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/ru/javawebinar/topjava/web/UserServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.javawebinar.topjava.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* User: gkislin
* Date: 19.08.2014
*/
public class UserServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.getRequestDispatcher("/userList.jsp").forward(request, response);
response.sendRedirect("userList.jsp");
}
}
18 changes: 18 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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_3_1.xsd"
version="3.1">
<display-name>Topjava</display-name>

<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>ru.javawebinar.topjava.web.UserServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>/users</url-pattern>
</servlet-mapping>

</web-app>
14 changes: 14 additions & 0 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Java Enterprise (Topjava)</title>
</head>
<body>
<h2>Проект "<a href="https://github.com/JavaWebinar/topjava08" target="_blank">Java Enterprise (Topjava)"</a></h2>
<hr>
<ul>
<li><a href="users">User List</a></li>
</ul>
</body>
</html>
10 changes: 10 additions & 0 deletions src/main/webapp/userList.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>User list</title>
</head>
<body>
<h2><a href="index.html">Home</a></h2>
<h2>User list</h2>
</body>
</html>

0 comments on commit c59221d

Please sign in to comment.