forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jet_vorobev
committed
Sep 8, 2016
1 parent
de90d0c
commit c59221d
Showing
10 changed files
with
180 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
34 changes: 34 additions & 0 deletions
34
src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java
This file contains 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,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
24
src/main/java/ru/javawebinar/topjava/model/UserMealWithExceed.java
This file was deleted.
Oops, something went wrong.
This file contains 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,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
35
src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java
This file was deleted.
Oops, something went wrong.
This file contains 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,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"); | ||
} | ||
} |
This file contains 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,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> |
This file contains 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,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> |
This file contains 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,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> |