Skip to content

Commit be7544f

Browse files
author
Alex Meleshko
committed
Prepare to HW1
1 parent 1c2c6bc commit be7544f

12 files changed

Lines changed: 243 additions & 66 deletions

File tree

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ out
33
target
44
*.iml
55
log
6-
7-
8-
6+
*.patch

pom.xml

Lines changed: 35 additions & 2 deletions
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

@@ -15,11 +15,15 @@
1515
<java.version>1.8</java.version>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
18+
19+
<!-- Logging -->
20+
<logback.version>1.2.2</logback.version>
21+
<slf4j.version>1.7.25</slf4j.version>
1822
</properties>
1923

2024
<build>
2125
<finalName>topjava</finalName>
22-
<defaultGoal>install</defaultGoal>
26+
<defaultGoal>package</defaultGoal>
2327
<plugins>
2428
<plugin>
2529
<groupId>org.apache.maven.plugins</groupId>
@@ -34,6 +38,35 @@
3438
</build>
3539

3640
<dependencies>
41+
<!-- Logging with SLF4J & LogBack -->
42+
<dependency>
43+
<groupId>org.slf4j</groupId>
44+
<artifactId>slf4j-api</artifactId>
45+
<version>${slf4j.version}</version>
46+
<scope>compile</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>jcl-over-slf4j</artifactId>
52+
<version>${slf4j.version}</version>
53+
<scope>runtime</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>ch.qos.logback</groupId>
58+
<artifactId>logback-classic</artifactId>
59+
<version>${logback.version}</version>
60+
<scope>runtime</scope>
61+
</dependency>
62+
63+
<!--Web-->
64+
<dependency>
65+
<groupId>javax.servlet</groupId>
66+
<artifactId>javax.servlet-api</artifactId>
67+
<version>3.1.0</version>
68+
<scope>provided</scope>
69+
</dependency>
3770
</dependencies>
3871

3972
<profiles>

src/main/java/ru/javawebinar/topjava/model/UserMeal.java renamed to src/main/java/ru/javawebinar/topjava/model/Meal.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package ru.javawebinar.topjava.model;
22

3+
import java.time.LocalDate;
34
import java.time.LocalDateTime;
5+
import java.time.LocalTime;
46

57
/**
68
* GKislin
79
* 11.01.2015.
810
*/
9-
public class UserMeal {
11+
public class Meal {
1012
private final LocalDateTime dateTime;
1113

1214
private final String description;
1315

1416
private final int calories;
1517

16-
public UserMeal(LocalDateTime dateTime, String description, int calories) {
18+
public Meal(LocalDateTime dateTime, String description, int calories) {
1719
this.dateTime = dateTime;
1820
this.description = description;
1921
this.calories = calories;
@@ -30,4 +32,12 @@ public String getDescription() {
3032
public int getCalories() {
3133
return calories;
3234
}
35+
36+
public LocalDate getDate() {
37+
return dateTime.toLocalDate();
38+
}
39+
40+
public LocalTime getTime() {
41+
return dateTime.toLocalTime();
42+
}
3343
}
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 MealWithExceed {
10+
private final LocalDateTime dateTime;
11+
12+
private final String description;
13+
14+
private final int calories;
15+
16+
private final boolean exceed;
17+
18+
public MealWithExceed(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+
}

src/main/java/ru/javawebinar/topjava/model/UserMealWithExceed.java

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package ru.javawebinar.topjava.util;
2+
3+
import ru.javawebinar.topjava.model.Meal;
4+
import ru.javawebinar.topjava.model.MealWithExceed;
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.*;
11+
import java.util.stream.Collectors;
12+
13+
/**
14+
* GKislin
15+
* 31.05.2015.
16+
*/
17+
public class MealsUtil {
18+
public static void main(String[] args) {
19+
List<Meal> meals = Arrays.asList(
20+
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500),
21+
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000),
22+
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500),
23+
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 1000),
24+
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 500),
25+
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510)
26+
);
27+
List<MealWithExceed> mealsWithExceeded = getFilteredWithExceeded(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000);
28+
mealsWithExceeded.forEach(System.out::println);
29+
30+
System.out.println(getFilteredWithExceededByCycle(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000));
31+
}
32+
33+
public static List<MealWithExceed> getFilteredWithExceeded(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
34+
Map<LocalDate, Integer> caloriesSumByDate = meals.stream()
35+
.collect(
36+
Collectors.groupingBy(Meal::getDate, Collectors.summingInt(Meal::getCalories))
37+
// Collectors.toMap(Meal::getDate, Meal::getCalories, Integer::sum)
38+
);
39+
40+
return meals.stream()
41+
.filter(meal -> TimeUtil.isBetween(meal.getTime(), startTime, endTime))
42+
.map(meal -> createWithExceed(meal, caloriesSumByDate.get(meal.getDate()) > caloriesPerDay))
43+
.collect(Collectors.toList());
44+
}
45+
46+
public static List<MealWithExceed> getFilteredWithExceededByCycle(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
47+
48+
final Map<LocalDate, Integer> caloriesSumByDate = new HashMap<>();
49+
meals.forEach(meal -> caloriesSumByDate.merge(meal.getDate(), meal.getCalories(), Integer::sum));
50+
51+
final List<MealWithExceed> mealsWithExceeded = new ArrayList<>();
52+
meals.forEach(meal -> {
53+
if (TimeUtil.isBetween(meal.getTime(), startTime, endTime)) {
54+
mealsWithExceeded.add(createWithExceed(meal, caloriesSumByDate.get(meal.getDate()) > caloriesPerDay));
55+
}
56+
});
57+
return mealsWithExceeded;
58+
}
59+
60+
public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) {
61+
return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded);
62+
}
63+
}

src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.javawebinar.topjava.web;
2+
3+
import org.slf4j.Logger;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
import java.io.IOException;
10+
11+
import static org.slf4j.LoggerFactory.getLogger;
12+
13+
14+
/**
15+
* User: gkislin
16+
* Date: 19.08.2014
17+
*/
18+
public class UserServlet extends HttpServlet {
19+
private static final Logger LOG = getLogger(UserServlet.class);
20+
21+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22+
LOG.debug("redirect to users");
23+
24+
// request.getRequestDispatcher("/users.jsp").forward(request, response);
25+
response.sendRedirect("users.jsp");
26+
}
27+
}

src/main/resources/logback.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration scan="true" scanPeriod="30 seconds">
3+
4+
<!-- To enable JMX Management -->
5+
<jmxConfigurator/>
6+
7+
<appender name="file" class="ch.qos.logback.core.FileAppender">
8+
<file>${TOPJAVA_ROOT}/log/topjava.log</file>
9+
10+
<encoder>
11+
<charset>UTF-8</charset>
12+
<pattern>%date %-5level %logger{0} [%file:%line] %msg%n</pattern>
13+
</encoder>
14+
</appender>
15+
16+
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
17+
<encoder>
18+
<charset>UTF-8</charset>
19+
<pattern>%-5level %logger{0} [%file:%line] %msg%n</pattern>
20+
</encoder>
21+
</appender>
22+
23+
<logger name="ru.javawebinar.topjava" level="debug"/>
24+
25+
<root level="info">
26+
<appender-ref ref="file"/>
27+
<appender-ref ref="console"/>
28+
</root>
29+
</configuration>

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+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
4+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5+
version="3.1">
6+
<display-name>Topjava</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>

0 commit comments

Comments
 (0)