Skip to content

Commit 40a02af

Browse files
committed
2 2 HW1 optional
1 parent e577016 commit 40a02af

File tree

5 files changed

+130
-8
lines changed

5 files changed

+130
-8
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,33 @@
55
import java.time.LocalTime;
66

77
public class Meal {
8+
private Integer id;
9+
810
private final LocalDateTime dateTime;
911

1012
private final String description;
1113

1214
private final int calories;
1315

1416
public Meal(LocalDateTime dateTime, String description, int calories) {
17+
this(null, dateTime, description, calories);
18+
}
19+
20+
public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
21+
this.id = id;
1522
this.dateTime = dateTime;
1623
this.description = description;
1724
this.calories = calories;
1825
}
1926

27+
public Integer getId() {
28+
return id;
29+
}
30+
31+
public void setId(Integer id) {
32+
this.id = id;
33+
}
34+
2035
public LocalDateTime getDateTime() {
2136
return dateTime;
2237
}
@@ -36,4 +51,18 @@ public LocalDate getDate() {
3651
public LocalTime getTime() {
3752
return dateTime.toLocalTime();
3853
}
54+
55+
public boolean isNew() {
56+
return id == null;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "Meal{" +
62+
"id=" + id +
63+
", dateTime=" + dateTime +
64+
", description='" + description + '\'' +
65+
", calories=" + calories +
66+
'}';
67+
}
3968
}

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.time.LocalDateTime;
44

55
public class MealWithExceed {
6+
private final Integer id;
7+
68
private final LocalDateTime dateTime;
79

810
private final String description;
@@ -11,17 +13,39 @@ public class MealWithExceed {
1113

1214
private final boolean exceed;
1315

14-
public MealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
16+
public MealWithExceed(Integer id, LocalDateTime dateTime, String description, int calories, boolean exceed) {
17+
this.id = id;
1518
this.dateTime = dateTime;
1619
this.description = description;
1720
this.calories = calories;
1821
this.exceed = exceed;
1922
}
2023

24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public LocalDateTime getDateTime() {
29+
return dateTime;
30+
}
31+
32+
public String getDescription() {
33+
return description;
34+
}
35+
36+
public int getCalories() {
37+
return calories;
38+
}
39+
40+
public boolean isExceed() {
41+
return exceed;
42+
}
43+
2144
@Override
2245
public String toString() {
23-
return "UserMealWithExceed{" +
24-
"dateTime=" + dateTime +
46+
return "MealWithExceed{" +
47+
"id=" + id +
48+
", dateTime=" + dateTime +
2549
", description='" + description + '\'' +
2650
", calories=" + calories +
2751
", exceed=" + exceed +
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.javawebinar.topjava.repository;
2+
3+
import ru.javawebinar.topjava.model.Meal;
4+
5+
import java.util.Collection;
6+
7+
public interface MealRepository {
8+
Meal save(Meal meal);
9+
10+
void delete(int id);
11+
12+
Meal get(int id);
13+
14+
Collection<Meal> getAll();
15+
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ public class MealsUtil {
2525

2626
public static final int DEFAULT_CALORIES_PER_DAY = 2000;
2727

28-
System.out.println(getFilteredWithExceededByCycle(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000));
29-
System.out.println(getFilteredWithExceededInOnePass(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000));
30-
System.out.println(getFilteredWithExceededInOnePass2(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000));
28+
public static List<MealWithExceed> getWithExceeded(Collection<Meal> meals, int caloriesPerDay) {
29+
return getFilteredWithExceeded(meals, caloriesPerDay, meal -> true);
3130
}
3231

33-
public static List<MealWithExceed> getFilteredWithExceeded(List<Meal> meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) {
32+
public static List<MealWithExceed> getFilteredWithExceeded(Collection<Meal> meals, int caloriesPerDay, LocalTime startTime, LocalTime endTime) {
33+
return getFilteredWithExceeded(meals, caloriesPerDay, meal -> DateTimeUtil.isBetween(meal.getTime(), startTime, endTime));
34+
}
35+
36+
private static List<MealWithExceed> getFilteredWithExceeded(Collection<Meal> meals, int caloriesPerDay, Predicate<Meal> filter) {
3437
Map<LocalDate, Integer> caloriesSumByDate = meals.stream()
3538
.collect(
3639
Collectors.groupingBy(Meal::getDate, Collectors.summingInt(Meal::getCalories))
@@ -44,6 +47,6 @@ public static List<MealWithExceed> getFilteredWithExceeded(List<Meal> meals, Loc
4447
}
4548

4649
public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) {
47-
return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded);
50+
return new MealWithExceed(meal.getId(), meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded);
4851
}
4952
}

src/main/webapp/mealForm.jsp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3+
4+
<html>
5+
<head>
6+
<title>Meal</title>
7+
<style>
8+
dl {
9+
background: none repeat scroll 0 0 #FAFAFA;
10+
margin: 8px 0;
11+
padding: 0;
12+
}
13+
14+
dt {
15+
display: inline-block;
16+
width: 170px;
17+
}
18+
19+
dd {
20+
display: inline-block;
21+
margin-left: 8px;
22+
vertical-align: top;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
<section>
28+
<h3><a href="index.html">Home</a></h3>
29+
<h2>${param.action == 'create' ? 'Create meal' : 'Edit meal'}</h2>
30+
<hr>
31+
<jsp:useBean id="meal" type="ru.javawebinar.topjava.model.Meal" scope="request"/>
32+
<form method="post" action="meals">
33+
<input type="hidden" name="id" value="${meal.id}">
34+
<dl>
35+
<dt>DateTime:</dt>
36+
<dd><input type="datetime-local" value="${meal.dateTime}" name="dateTime" required></dd>
37+
</dl>
38+
<dl>
39+
<dt>Description:</dt>
40+
<dd><input type="text" value="${meal.description}" size=40 name="description" required></dd>
41+
</dl>
42+
<dl>
43+
<dt>Calories:</dt>
44+
<dd><input type="number" value="${meal.calories}" name="calories" required></dd>
45+
</dl>
46+
<button type="submit">Save</button>
47+
<button onclick="window.history.back()" type="button">Cancel</button>
48+
</form>
49+
</section>
50+
</body>
51+
</html>

0 commit comments

Comments
 (0)