Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79714dc

Browse files
committed
patch1.3
1 parent c36fac5 commit 79714dc

File tree

9 files changed

+180
-3
lines changed

9 files changed

+180
-3
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>ru.javawebinar</groupId>
66
<artifactId>topjava</artifactId>
7-
<packaging>jar</packaging>
7+
<packaging>war</packaging>
88

99
<version>1.0-SNAPSHOT</version>
1010

@@ -13,6 +13,7 @@
1313

1414
<properties>
1515
<java.version>1.8</java.version>
16+
1617
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1718
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1819
</properties>
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package ru.javawebinar.topjava;
22

3+
import java.util.function.Consumer;
4+
35
/**
46
* User: gkislin
57
* Date: 15.01.2015
68
*
79
* @link http://javawebinar.ru/topjava/
8-
* @link http://caloriesmng.herokuapp.com/
910
*/
1011
public class Main {
1112
public static void main(String[] args) {
12-
System.out.format("Hello Topjava!");
13+
execute(() -> {
14+
System.out.println("Hello Topjava!");
15+
});
16+
consume(System.out::println, "Hello Topjava!");
17+
}
18+
19+
private static void execute(Runnable runnable) {
20+
System.out.println("Start runner");
21+
runnable.run();
22+
System.out.println("End runner");
23+
}
24+
25+
private static <String> void consume(Consumer<String> consumer, String out) {
26+
System.out.println("Start consume");
27+
consumer.accept(out);
28+
System.out.println("End consume");
1329
}
1430
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
import java.time.LocalDateTime;
4+
5+
/**
6+
* GKislin
7+
* 11.01.2015.
8+
*/
9+
public class UserMeal {
10+
protected final LocalDateTime dateTime;
11+
12+
protected final String description;
13+
14+
protected final int calories;
15+
16+
public UserMeal(LocalDateTime dateTime, String description, int calories) {
17+
this.dateTime = dateTime;
18+
this.description = description;
19+
this.calories = calories;
20+
}
21+
22+
public LocalDateTime getDateTime() {
23+
return dateTime;
24+
}
25+
26+
public String getDescription() {
27+
return description;
28+
}
29+
30+
public int getCalories() {
31+
return calories;
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
import java.time.LocalDateTime;
4+
5+
/**
6+
* GKislin
7+
* 11.01.2015.
8+
*/
9+
public class UserMealWithExceed {
10+
protected final LocalDateTime dateTime;
11+
12+
protected final String description;
13+
14+
protected final int calories;
15+
16+
protected final boolean exceed;
17+
18+
public UserMealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
19+
this.dateTime = dateTime;
20+
this.description = description;
21+
this.calories = calories;
22+
this.exceed = exceed;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "UserMealWithExceed{" +
28+
"dateTime=" + dateTime +
29+
", description='" + description + '\'' +
30+
", calories=" + calories +
31+
", exceed=" + exceed +
32+
'}';
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.javawebinar.topjava.util;
2+
3+
import java.time.LocalTime;
4+
5+
/**
6+
* GKislin
7+
* 07.01.2015.
8+
*/
9+
public class TimeUtil {
10+
public static boolean isBetween(LocalTime lt, LocalTime startTime, LocalTime endTime) {
11+
return lt.compareTo(startTime) >= 0 && lt.compareTo(endTime) <= 0;
12+
}
13+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.javawebinar.topjava.util;
2+
3+
import ru.javawebinar.topjava.model.UserMeal;
4+
import ru.javawebinar.topjava.model.UserMealWithExceed;
5+
6+
import java.time.LocalDate;
7+
import java.time.LocalDateTime;
8+
import java.time.LocalTime;
9+
import java.time.Month;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
15+
/**
16+
* GKislin
17+
* 31.05.2015.
18+
*/
19+
public class UserMealsUtil {
20+
public static void main(String[] args) {
21+
List<UserMeal> mealList = Arrays.asList(
22+
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500),
23+
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000),
24+
new UserMeal(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500),
25+
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 500),
26+
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 1000),
27+
new UserMeal(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510)
28+
);
29+
List<UserMealWithExceed> filteredMealsWithExceeded = getFilteredMealsWithExceeded(mealList, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000);
30+
filteredMealsWithExceeded.forEach(System.out::println);
31+
}
32+
33+
public static List<UserMealWithExceed> getFilteredMealsWithExceeded(List<UserMeal> mealList, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
34+
Map<LocalDate, Integer> caloriesSumByDate = mealList.stream().collect(Collectors.groupingBy(um -> um.getDateTime().toLocalDate(),
35+
Collectors.summingInt(UserMeal::getCalories)));
36+
37+
return mealList.stream()
38+
.filter(um->TimeUtil.isBetween(um.getDateTime().toLocalTime(), startTime, endTime))
39+
.map(um->new UserMealWithExceed(um.getDateTime(), um.getDescription(), um.getCalories(),
40+
caloriesSumByDate.get(um.getDateTime().toLocalDate())> caloriesPerDay))
41+
.collect(Collectors.toList());
42+
}
43+
}

src/main/webapp/WEB-INF/web.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE web-app PUBLIC
2+
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3+
"http://java.sun.com/dtd/web-app_2_3.dtd" >
4+
5+
<web-app>
6+
<display-name>Archetype Created Web Application</display-name>
7+
8+
<servlet>
9+
<servlet-name>userServlet</servlet-name>
10+
<servlet-class>ru.javawebinar.topjava.web.UserServlet</servlet-class>
11+
<load-on-startup>0</load-on-startup>
12+
</servlet>
13+
<servlet-mapping>
14+
<servlet-name>userServlet</servlet-name>
15+
<url-pattern>/users</url-pattern>
16+
</servlet-mapping>
17+
18+
</web-app>

src/main/webapp/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Top Java.</title>
6+
</head>
7+
<body>
8+
<h2>Приложение вебинара "<a href="http://javawebinar.ru/topjava/" target="_blank">Top Java."</a></h2>
9+
</body>
10+
</html>

src/main/webapp/userList.jsp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
<head>
4+
<title>User list</title>
5+
</head>
6+
<body>
7+
<h2>User list</h2>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)