From d077c01ada5b6a2702b70963203e514923672045 Mon Sep 17 00:00:00 2001 From: Ivan Romanov Date: Mon, 27 Mar 2017 21:56:25 +0200 Subject: [PATCH 1/6] done HW1 with one mistake. CSS styles is not applicable for table line. --- pom.xml | 8 +++++ .../topjava/model/MealWithExceed.java | 20 +++++++++++ .../javawebinar/topjava/util/MealsUtil.java | 10 ++++++ .../ru/javawebinar/topjava/util/TimeUtil.java | 4 +++ src/main/webapp/WEB-INF/web.xml | 9 +++++ src/main/webapp/index.html | 3 ++ src/main/webapp/meals.jsp | 35 +++++++++++++++++++ 7 files changed, 89 insertions(+) create mode 100644 src/main/webapp/meals.jsp diff --git a/pom.xml b/pom.xml index 68c2a88..cebc8b8 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,14 @@ 3.1.0 provided + + + javax.servlet + jstl + 1.2 + provided + + diff --git a/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java b/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java index d14128b..095c7e2 100644 --- a/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java +++ b/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java @@ -1,5 +1,6 @@ package ru.javawebinar.topjava.model; +import java.time.LocalDate; import java.time.LocalDateTime; /** @@ -22,6 +23,25 @@ public MealWithExceed(LocalDateTime dateTime, String description, int calories, this.exceed = exceed; } + public LocalDateTime getDateTime() { + return dateTime; + } + public LocalDate getDate() { + return dateTime.toLocalDate(); + } + + public String getDescription() { + return description; + } + + public int getCalories() { + return calories; + } + + public boolean isExceed() { + return exceed; + } + @Override public String toString() { return "UserMealWithExceed{" + diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java index d6476fc..4f1a925 100644 --- a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java @@ -57,6 +57,16 @@ public static List getFilteredWithExceededByCycle(List mea return mealsWithExceeded; } + public static List mealFactory(){ + return Arrays.asList( + new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500, false), + new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000, false), + new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500, false), + new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 1000, true), + new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 500, true), + new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510, true)); + } + public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) { return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded); } diff --git a/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java b/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java index 02399b7..d316024 100644 --- a/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/TimeUtil.java @@ -1,6 +1,10 @@ package ru.javawebinar.topjava.util; +import ru.javawebinar.topjava.model.Meal; + import java.time.LocalTime; +import java.util.ArrayList; +import java.util.List; /** * GKislin diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index f346a5e..06d69be 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -14,5 +14,14 @@ userServlet /users + + mealServlet + ru.javawebinar.topjava.web.MealServlet + 0 + + + mealServlet + /meals + diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 056f090..f0ebd74 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -10,5 +10,8 @@

Проект User List + diff --git a/src/main/webapp/meals.jsp b/src/main/webapp/meals.jsp new file mode 100644 index 0000000..762492a --- /dev/null +++ b/src/main/webapp/meals.jsp @@ -0,0 +1,35 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Meals + + + + + + + + + + + + +
+ + +
+ +
+ + + + + + + +
ДатаОписаниеКалории
+ + \ No newline at end of file From 5ceae807f0d55dd8611d73c6056914bf3b609c32 Mon Sep 17 00:00:00 2001 From: Ivan Romanov Date: Mon, 27 Mar 2017 22:01:25 +0200 Subject: [PATCH 2/6] done HW1 with one mistake. CSS styles is not applicable for table line. --- .../javawebinar/topjava/web/MealServlet.java | 27 +++++++++++++++++++ src/main/webapp/css/style.css | 25 +++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/ru/javawebinar/topjava/web/MealServlet.java create mode 100644 src/main/webapp/css/style.css diff --git a/src/main/java/ru/javawebinar/topjava/web/MealServlet.java b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java new file mode 100644 index 0000000..12a7c20 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java @@ -0,0 +1,27 @@ +package ru.javawebinar.topjava.web; + +import org.slf4j.Logger; +import ru.javawebinar.topjava.util.MealsUtil; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import static org.slf4j.LoggerFactory.getLogger; + +/** + * Created by Администратор on 26.03.2017. + */ +public class MealServlet extends HttpServlet { + private static final Logger LOG = getLogger(MealServlet.class); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + LOG.debug("MealServlet doGet"); + req.setAttribute("mealList", MealsUtil.mealFactory()); +// req.setAttribute("applicationName", "topjava"); + req.getRequestDispatcher("meals.jsp").forward(req, resp); + } +} diff --git a/src/main/webapp/css/style.css b/src/main/webapp/css/style.css new file mode 100644 index 0000000..b1c7a95 --- /dev/null +++ b/src/main/webapp/css/style.css @@ -0,0 +1,25 @@ +body { + padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ + padding-bottom: 40px; + background-color: #f5f5f5; +} + +.form-narrow { + max-width: 490px; + padding: 19px 29px 29px; + margin: 0 auto 20px; + background-color: #fff; + border: 1px solid #e5e5e5; + -webkit-border-radius: 5px; + -moz-border-radius: 5px; + border-radius: 5px; + -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05); + -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05); + box-shadow: 0 1px 2px rgba(0,0,0,.05); +} +div.red_record { + color:red; +} +div.blue_record{ + color:green; +} \ No newline at end of file From 2690af3f9c7ce7c6830579b3c073f652dde269d8 Mon Sep 17 00:00:00 2001 From: Ivan Romanov Date: Mon, 27 Mar 2017 22:43:30 +0200 Subject: [PATCH 3/6] The bug fixing HW1. --- src/main/webapp/css/style.css | 32 ++++++++++++++------------------ src/main/webapp/meals.jsp | 18 ++++++++++-------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/webapp/css/style.css b/src/main/webapp/css/style.css index b1c7a95..f2f7751 100644 --- a/src/main/webapp/css/style.css +++ b/src/main/webapp/css/style.css @@ -1,25 +1,21 @@ -body { - padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ - padding-bottom: 40px; - background-color: #f5f5f5; +table { + border-collapse: collapse; + width: 40%; } -.form-narrow { - max-width: 490px; - padding: 19px 29px 29px; - margin: 0 auto 20px; - background-color: #fff; - border: 1px solid #e5e5e5; - -webkit-border-radius: 5px; - -moz-border-radius: 5px; - border-radius: 5px; - -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05); - -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05); - box-shadow: 0 1px 2px rgba(0,0,0,.05); +th, td { + text-align: left; + padding: 8px; } -div.red_record { +tr:nth-child(even){background-color: #f2f2f2} + +th { + background-color: #4CAF50; + color: white; +} +.red_record{ color:red; } -div.blue_record{ +.blue_record{ color:green; } \ No newline at end of file diff --git a/src/main/webapp/meals.jsp b/src/main/webapp/meals.jsp index 762492a..bfffcb2 100644 --- a/src/main/webapp/meals.jsp +++ b/src/main/webapp/meals.jsp @@ -16,19 +16,21 @@ + -
+ + + + + -
- - - - - + + + -
+
From 819878d361a0ce6548ef0ed1ad2681d7e39467c8 Mon Sep 17 00:00:00 2001 From: Ivan Romanov Date: Tue, 28 Mar 2017 00:22:50 +0200 Subject: [PATCH 4/6] The bug fixing HW1. Took List from memory and gave it to JSP list --- .../ru/javawebinar/topjava/util/MealsUtil.java | 14 +++++++------- .../ru/javawebinar/topjava/web/MealServlet.java | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java index 4f1a925..c7e4e45 100644 --- a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java @@ -57,14 +57,14 @@ public static List getFilteredWithExceededByCycle(List mea return mealsWithExceeded; } - public static List mealFactory(){ + public static List mealFactory(){ return Arrays.asList( - new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500, false), - new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000, false), - new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500, false), - new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 1000, true), - new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 500, true), - new MealWithExceed(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510, true)); + 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)); } public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) { diff --git a/src/main/java/ru/javawebinar/topjava/web/MealServlet.java b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java index 12a7c20..b229ac8 100644 --- a/src/main/java/ru/javawebinar/topjava/web/MealServlet.java +++ b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java @@ -8,6 +8,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.time.LocalDateTime; +import java.time.LocalTime; import static org.slf4j.LoggerFactory.getLogger; @@ -20,8 +22,7 @@ public class MealServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { LOG.debug("MealServlet doGet"); - req.setAttribute("mealList", MealsUtil.mealFactory()); -// req.setAttribute("applicationName", "topjava"); + req.setAttribute("mealList", MealsUtil.getFilteredWithExceeded(MealsUtil.mealFactory() , LocalTime.MIN, LocalTime.MAX,2000)); req.getRequestDispatcher("meals.jsp").forward(req, resp); } } From 5ce3472dd377ba6ae02099645beaf28c82926858 Mon Sep 17 00:00:00 2001 From: Ivan Romanov Date: Wed, 29 Mar 2017 02:28:06 +0200 Subject: [PATCH 5/6] The bug fixing HW1 optional. Bad version (teamplate) --- .../java/ru/javawebinar/topjava/Main.java | 14 ----- .../ru/javawebinar/topjava/dao/DaoMeal.java | 23 +++++++++ .../dao/DaoMealMemoryImplementation.java | 51 +++++++++++++++++++ .../javawebinar/topjava/dao/MealService.java | 42 +++++++++++++++ .../ru/javawebinar/topjava/model/Meal.java | 29 +++++++++-- .../topjava/model/MealWithExceed.java | 6 ++- .../javawebinar/topjava/util/MealsUtil.java | 30 +++++------ .../javawebinar/topjava/web/MealServlet.java | 26 +++++++++- src/main/webapp/css/style.css | 2 +- src/main/webapp/meals.jsp | 11 ++++ 10 files changed, 198 insertions(+), 36 deletions(-) delete mode 100644 src/main/java/ru/javawebinar/topjava/Main.java create mode 100644 src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java create mode 100644 src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java create mode 100644 src/main/java/ru/javawebinar/topjava/dao/MealService.java diff --git a/src/main/java/ru/javawebinar/topjava/Main.java b/src/main/java/ru/javawebinar/topjava/Main.java deleted file mode 100644 index f94cdb8..0000000 --- a/src/main/java/ru/javawebinar/topjava/Main.java +++ /dev/null @@ -1,14 +0,0 @@ -package ru.javawebinar.topjava; - -/** - * User: gkislin - * Date: 05.08.2015 - * - * @link http://caloriesmng.herokuapp.com/ - * @link https://github.com/JavaOPs/topjava - */ -public class Main { - public static void main(String[] args) { - System.out.format("Hello Topjava Enterprise!"); - } -} \ No newline at end of file diff --git a/src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java b/src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java new file mode 100644 index 0000000..c6cf3cb --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java @@ -0,0 +1,23 @@ +package ru.javawebinar.topjava.dao; + +import ru.javawebinar.topjava.model.Meal; + +import java.util.List; + +/** + * Created by Администратор on 28.03.2017. + */ +public interface DaoMeal { + + public void addMeal(Meal meal); + + public Meal getMealNyId(int num); + + public void updateMeal(Meal meal, int id); + + public void removeMeal(int id); + + public void createNewMeals(List list); + + public List getAllrecord(); +} \ No newline at end of file diff --git a/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java b/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java new file mode 100644 index 0000000..c04f0c6 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java @@ -0,0 +1,51 @@ +package ru.javawebinar.topjava.dao; + +import ru.javawebinar.topjava.model.Meal; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Администратор on 28.03.2017. + */ +public class DaoMealMemoryImplementation implements DaoMeal { + List mealsList = new ArrayList<>(); + + @Override + public void addMeal(Meal meal) { + try { + Meal old = mealsList.get(meal.getId()); + } catch (IndexOutOfBoundsException e) { + mealsList.add(meal); + } + } + + @Override + public Meal getMealNyId(int num) { + return mealsList.get(num); + } + + @Override + public void updateMeal(Meal meal, int id) { + try { + Meal old = mealsList.get(id); + } catch (IndexOutOfBoundsException e) { + addMeal(meal); + } + } + + @Override + public void removeMeal(int id) { + this.mealsList.remove(id); + } + + @Override + public void createNewMeals(List list) { + this.mealsList = list; + } + + @Override + public List getAllrecord() { + return mealsList; + } +} diff --git a/src/main/java/ru/javawebinar/topjava/dao/MealService.java b/src/main/java/ru/javawebinar/topjava/dao/MealService.java new file mode 100644 index 0000000..8de6019 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/dao/MealService.java @@ -0,0 +1,42 @@ +package ru.javawebinar.topjava.dao; + +import ru.javawebinar.topjava.dao.DaoMeal; +import ru.javawebinar.topjava.model.Meal; + +import java.util.List; + +/** + * Created by Администратор on 29.03.2017. + */ +public class MealService { + private DaoMeal daoMeal; + + public MealService(DaoMeal daoMeal) { + this.daoMeal = daoMeal; + } + + + public void addMeal(Meal meal) { + this.daoMeal.addMeal(meal); + } + public List getMeallist() { + return this.daoMeal.getAllrecord(); + + } +// public Meal getMealNyId(int num) { +// +// } +// +// public void updateMeal(Meal meal, int id) { +// +// } +// +// public void removeMeal(int id) { +// +// } +// +// public void createNewMeals(List list) { +// +// } + +} diff --git a/src/main/java/ru/javawebinar/topjava/model/Meal.java b/src/main/java/ru/javawebinar/topjava/model/Meal.java index 006d5ca..30b2694 100644 --- a/src/main/java/ru/javawebinar/topjava/model/Meal.java +++ b/src/main/java/ru/javawebinar/topjava/model/Meal.java @@ -3,24 +3,29 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.util.concurrent.atomic.AtomicInteger; /** * GKislin * 11.01.2015. */ public class Meal { - private final LocalDateTime dateTime; + private LocalDateTime dateTime; - private final String description; + private String description; - private final int calories; + private int calories; + + private final int id; + + private static AtomicInteger idCounter = new AtomicInteger(0); public Meal(LocalDateTime dateTime, String description, int calories) { this.dateTime = dateTime; this.description = description; this.calories = calories; + this.id = idCounter.getAndIncrement(); } - public LocalDateTime getDateTime() { return dateTime; } @@ -40,4 +45,20 @@ public LocalDate getDate() { public LocalTime getTime() { return dateTime.toLocalTime(); } + + public int getId() { + return id; + } + + public void setDateTime(LocalDateTime dateTime) { + this.dateTime = dateTime; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setCalories(int calories) { + this.calories = calories; + } } diff --git a/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java b/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java index 095c7e2..97e821c 100644 --- a/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java +++ b/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java @@ -16,16 +16,20 @@ public class MealWithExceed { private final boolean exceed; - public MealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) { + private final int id; + + public MealWithExceed(LocalDateTime dateTime, String description, int calories, int id, boolean exceed) { this.dateTime = dateTime; this.description = description; this.calories = calories; this.exceed = exceed; + this.id = id; } public LocalDateTime getDateTime() { return dateTime; } + public LocalDate getDate() { return dateTime.toLocalDate(); } diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java index c7e4e45..51a1c6a 100644 --- a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java @@ -15,20 +15,20 @@ * 31.05.2015. */ public class MealsUtil { - public static void main(String[] args) { - List 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 mealsWithExceeded = getFilteredWithExceeded(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); - mealsWithExceeded.forEach(System.out::println); - - System.out.println(getFilteredWithExceededByCycle(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000)); - } +// public static void main(String[] args) { +// List 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 mealsWithExceeded = getFilteredWithExceeded(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); +// mealsWithExceeded.forEach(System.out::println); +// +// System.out.println(getFilteredWithExceededByCycle(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000)); +// } public static List getFilteredWithExceeded(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { Map caloriesSumByDate = meals.stream() @@ -68,6 +68,6 @@ public static List mealFactory(){ } public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) { - return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), exceeded); + return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), meal.getId(), exceeded); } } \ No newline at end of file diff --git a/src/main/java/ru/javawebinar/topjava/web/MealServlet.java b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java index b229ac8..8688a0d 100644 --- a/src/main/java/ru/javawebinar/topjava/web/MealServlet.java +++ b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java @@ -1,6 +1,10 @@ package ru.javawebinar.topjava.web; import org.slf4j.Logger; +import ru.javawebinar.topjava.dao.DaoMeal; +import ru.javawebinar.topjava.dao.DaoMealMemoryImplementation; +import ru.javawebinar.topjava.dao.MealService; +import ru.javawebinar.topjava.model.Meal; import ru.javawebinar.topjava.util.MealsUtil; import javax.servlet.ServletException; @@ -18,11 +22,31 @@ */ public class MealServlet extends HttpServlet { private static final Logger LOG = getLogger(MealServlet.class); + MealService mealService; + + @Override + public void init() throws ServletException { + DaoMeal myDao = new DaoMealMemoryImplementation(); + myDao.createNewMeals(MealsUtil.mealFactory()); + this.mealService = new MealService(myDao); + } + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { LOG.debug("MealServlet doGet"); - req.setAttribute("mealList", MealsUtil.getFilteredWithExceeded(MealsUtil.mealFactory() , LocalTime.MIN, LocalTime.MAX,2000)); + req.setAttribute("mealList", MealsUtil.getFilteredWithExceeded(mealService.getMeallist(), LocalTime.MIN, LocalTime.MAX, 2000)); + req.getRequestDispatcher("meals.jsp").forward(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + LOG.debug("doPost"); + req.setAttribute("mealList", MealsUtil.getFilteredWithExceeded(mealService.getMeallist(), LocalTime.MIN, LocalTime.MAX, 2000)); +// LocalDateTime localDateTime = new LocalDateTime(); // + int calories = Integer.valueOf(req.getParameter("Calories")); + String description = String.valueOf(req.getParameter("Description")); + mealService.addMeal(new Meal(LocalDateTime.now()/*спешил*/, description, calories)); req.getRequestDispatcher("meals.jsp").forward(req, resp); } } diff --git a/src/main/webapp/css/style.css b/src/main/webapp/css/style.css index f2f7751..965d421 100644 --- a/src/main/webapp/css/style.css +++ b/src/main/webapp/css/style.css @@ -1,4 +1,4 @@ -table { +table , form1 { border-collapse: collapse; width: 40%; } diff --git a/src/main/webapp/meals.jsp b/src/main/webapp/meals.jsp index bfffcb2..f7411e6 100644 --- a/src/main/webapp/meals.jsp +++ b/src/main/webapp/meals.jsp @@ -15,6 +15,17 @@ Калории +
+ Введите порядковый номер для редактирования блюда:
+ <%----%> + + + + +
+ +
+ From 5bb5de5a7388fe6e4843cf722c6a3ff53233da40 Mon Sep 17 00:00:00 2001 From: Ivan Romanov Date: Thu, 30 Mar 2017 01:52:32 +0200 Subject: [PATCH 6/6] The bug fixing HW1 optional. Bad version (teamplate) --- .../ru/javawebinar/topjava/dao/DaoMeal.java | 14 ++-- .../dao/DaoMealMemoryImplementation.java | 43 +++++----- .../javawebinar/topjava/dao/MealService.java | 42 ---------- .../ru/javawebinar/topjava/model/Meal.java | 19 ++--- .../topjava/model/MealWithExceed.java | 5 ++ .../javawebinar/topjava/util/MealsUtil.java | 78 +++++++++---------- .../topjava/web/MealRemoveServlet.java | 32 ++++++++ .../javawebinar/topjava/web/MealServlet.java | 26 ++++--- .../javawebinar/topjava/web/UserServlet.java | 1 - src/main/webapp/WEB-INF/web.xml | 9 +++ src/main/webapp/css/style.css | 2 +- src/main/webapp/meals.jsp | 45 ++++++----- 12 files changed, 164 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/ru/javawebinar/topjava/dao/MealService.java create mode 100644 src/main/java/ru/javawebinar/topjava/web/MealRemoveServlet.java diff --git a/src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java b/src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java index c6cf3cb..0493d8b 100644 --- a/src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java +++ b/src/main/java/ru/javawebinar/topjava/dao/DaoMeal.java @@ -2,22 +2,22 @@ import ru.javawebinar.topjava.model.Meal; -import java.util.List; +import java.util.concurrent.ConcurrentMap; /** * Created by Администратор on 28.03.2017. */ public interface DaoMeal { - public void addMeal(Meal meal); + public void add(Meal meal); - public Meal getMealNyId(int num); + public Meal get(int id); - public void updateMeal(Meal meal, int id); + public void update(Meal meal); - public void removeMeal(int id); + public void remove(int id); - public void createNewMeals(List list); + public void factoryMethod(); - public List getAllrecord(); + public ConcurrentMap getAllrecords(); } \ No newline at end of file diff --git a/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java b/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java index c04f0c6..eeac722 100644 --- a/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java +++ b/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java @@ -1,51 +1,48 @@ package ru.javawebinar.topjava.dao; import ru.javawebinar.topjava.model.Meal; +import ru.javawebinar.topjava.util.MealsUtil; -import java.util.ArrayList; -import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; /** * Created by Администратор on 28.03.2017. */ public class DaoMealMemoryImplementation implements DaoMeal { - List mealsList = new ArrayList<>(); + static AtomicInteger id; + ConcurrentMap map = new ConcurrentHashMap<>(); + @Override - public void addMeal(Meal meal) { - try { - Meal old = mealsList.get(meal.getId()); - } catch (IndexOutOfBoundsException e) { - mealsList.add(meal); - } + public void add(Meal meal) { + map.put(id.incrementAndGet() , meal); } @Override - public Meal getMealNyId(int num) { - return mealsList.get(num); + public Meal get(int id) { + return map.get(id); } @Override - public void updateMeal(Meal meal, int id) { - try { - Meal old = mealsList.get(id); - } catch (IndexOutOfBoundsException e) { - addMeal(meal); - } + public void update(Meal meal) { + map.put(meal.getId() , meal); } @Override - public void removeMeal(int id) { - this.mealsList.remove(id); + public void remove(int id) { + this.map.remove(id); } @Override - public void createNewMeals(List list) { - this.mealsList = list; + public void factoryMethod() { + this.map = MealsUtil.mealFactory(); + id = new AtomicInteger(map.size() > 0 ? map.size()-1 : 0); //put value in counter. For first -1 } @Override - public List getAllrecord() { - return mealsList; + public ConcurrentMap getAllrecords() { + return map; } } diff --git a/src/main/java/ru/javawebinar/topjava/dao/MealService.java b/src/main/java/ru/javawebinar/topjava/dao/MealService.java deleted file mode 100644 index 8de6019..0000000 --- a/src/main/java/ru/javawebinar/topjava/dao/MealService.java +++ /dev/null @@ -1,42 +0,0 @@ -package ru.javawebinar.topjava.dao; - -import ru.javawebinar.topjava.dao.DaoMeal; -import ru.javawebinar.topjava.model.Meal; - -import java.util.List; - -/** - * Created by Администратор on 29.03.2017. - */ -public class MealService { - private DaoMeal daoMeal; - - public MealService(DaoMeal daoMeal) { - this.daoMeal = daoMeal; - } - - - public void addMeal(Meal meal) { - this.daoMeal.addMeal(meal); - } - public List getMeallist() { - return this.daoMeal.getAllrecord(); - - } -// public Meal getMealNyId(int num) { -// -// } -// -// public void updateMeal(Meal meal, int id) { -// -// } -// -// public void removeMeal(int id) { -// -// } -// -// public void createNewMeals(List list) { -// -// } - -} diff --git a/src/main/java/ru/javawebinar/topjava/model/Meal.java b/src/main/java/ru/javawebinar/topjava/model/Meal.java index 30b2694..7354839 100644 --- a/src/main/java/ru/javawebinar/topjava/model/Meal.java +++ b/src/main/java/ru/javawebinar/topjava/model/Meal.java @@ -3,7 +3,6 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; -import java.util.concurrent.atomic.AtomicInteger; /** * GKislin @@ -16,16 +15,22 @@ public class Meal { private int calories; - private final int id; - - private static AtomicInteger idCounter = new AtomicInteger(0); + private int id; public Meal(LocalDateTime dateTime, String description, int calories) { this.dateTime = dateTime; this.description = description; this.calories = calories; - this.id = idCounter.getAndIncrement(); } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public LocalDateTime getDateTime() { return dateTime; } @@ -46,10 +51,6 @@ public LocalTime getTime() { return dateTime.toLocalTime(); } - public int getId() { - return id; - } - public void setDateTime(LocalDateTime dateTime) { this.dateTime = dateTime; } diff --git a/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java b/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java index 97e821c..72dd26e 100644 --- a/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java +++ b/src/main/java/ru/javawebinar/topjava/model/MealWithExceed.java @@ -18,6 +18,7 @@ public class MealWithExceed { private final int id; + public MealWithExceed(LocalDateTime dateTime, String description, int calories, int id, boolean exceed) { this.dateTime = dateTime; this.description = description; @@ -26,6 +27,10 @@ public MealWithExceed(LocalDateTime dateTime, String description, int calories, this.id = id; } + public int getId() { + return id; + } + public LocalDateTime getDateTime() { return dateTime; } diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java index 51a1c6a..758cbb8 100644 --- a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java +++ b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java @@ -8,6 +8,8 @@ import java.time.LocalTime; import java.time.Month; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; /** @@ -15,57 +17,51 @@ * 31.05.2015. */ public class MealsUtil { -// public static void main(String[] args) { -// List 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 mealsWithExceeded = getFilteredWithExceeded(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000); -// mealsWithExceeded.forEach(System.out::println); -// -// System.out.println(getFilteredWithExceededByCycle(meals, LocalTime.of(7, 0), LocalTime.of(12, 0), 2000)); -// } - public static List getFilteredWithExceeded(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - Map 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 ConcurrentMap mealFactory() { + List list = 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)); + ConcurrentMap result = new ConcurrentHashMap<>(); + for (int i = 0; i < list.size(); i++) { + Meal curr = list.get(i); + curr.setId(i); + result.put(i, curr); + } + return result; } - public static List getFilteredWithExceededByCycle(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { +// public static Map getFilteredWithExceeded(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { +// Map 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.toMap( )); +// } + + public static ConcurrentMap getFilteredWithExceededByCycle(ConcurrentMap meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { - final Map caloriesSumByDate = new HashMap<>(); - meals.forEach(meal -> caloriesSumByDate.merge(meal.getDate(), meal.getCalories(), Integer::sum)); + final ConcurrentMap caloriesSumByDate = new ConcurrentHashMap<>(); + meals.forEach( (key, val) -> caloriesSumByDate.merge(val.getDate(), val.getCalories(), Integer::sum)); - final List mealsWithExceeded = new ArrayList<>(); - meals.forEach(meal -> { - if (TimeUtil.isBetween(meal.getTime(), startTime, endTime)) { - mealsWithExceeded.add(createWithExceed(meal, caloriesSumByDate.get(meal.getDate()) > caloriesPerDay)); + final ConcurrentMap mealsWithExceeded = new ConcurrentHashMap<>(); + meals.forEach((key, val) -> { + if (TimeUtil.isBetween(val.getTime(), startTime, endTime)) { + mealsWithExceeded.put(key, createWithExceed(val, caloriesSumByDate.get(val.getDate()) > caloriesPerDay)); } }); return mealsWithExceeded; } - public static List mealFactory(){ - return 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)); - } public static MealWithExceed createWithExceed(Meal meal, boolean exceeded) { return new MealWithExceed(meal.getDateTime(), meal.getDescription(), meal.getCalories(), meal.getId(), exceeded); diff --git a/src/main/java/ru/javawebinar/topjava/web/MealRemoveServlet.java b/src/main/java/ru/javawebinar/topjava/web/MealRemoveServlet.java new file mode 100644 index 0000000..8d4a537 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/web/MealRemoveServlet.java @@ -0,0 +1,32 @@ +package ru.javawebinar.topjava.web; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.javawebinar.topjava.dao.DaoMeal; +import ru.javawebinar.topjava.util.MealsUtil; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.time.LocalTime; + +/** + * Created by Администратор on 30.03.2017. + */ +public class MealRemoveServlet extends HttpServlet { + private static final Logger LOG = LoggerFactory.getLogger(MealRemoveServlet.class); + DaoMeal myDao; + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + LOG.debug("start removing Meal from Map"); + myDao = (DaoMeal) req.getAttribute("myDao"); + myDao.remove(Integer.valueOf(req.getParameter("ID"))); + + req.setAttribute("myDao",myDao); + req.setAttribute("mealMap", MealsUtil.getFilteredWithExceededByCycle(myDao.getAllrecords(), LocalTime.MIN, LocalTime.MAX, 2000)); + req.getRequestDispatcher("meals.jsp").forward(req, resp); + LOG.debug("end removing Meal from Map"); + } +} diff --git a/src/main/java/ru/javawebinar/topjava/web/MealServlet.java b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java index 8688a0d..193cac8 100644 --- a/src/main/java/ru/javawebinar/topjava/web/MealServlet.java +++ b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java @@ -3,7 +3,6 @@ import org.slf4j.Logger; import ru.javawebinar.topjava.dao.DaoMeal; import ru.javawebinar.topjava.dao.DaoMealMemoryImplementation; -import ru.javawebinar.topjava.dao.MealService; import ru.javawebinar.topjava.model.Meal; import ru.javawebinar.topjava.util.MealsUtil; @@ -22,31 +21,40 @@ */ public class MealServlet extends HttpServlet { private static final Logger LOG = getLogger(MealServlet.class); - MealService mealService; + DaoMeal myDao; @Override public void init() throws ServletException { - DaoMeal myDao = new DaoMealMemoryImplementation(); - myDao.createNewMeals(MealsUtil.mealFactory()); - this.mealService = new MealService(myDao); + myDao = new DaoMealMemoryImplementation(); + myDao.factoryMethod(); + } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { LOG.debug("MealServlet doGet"); - req.setAttribute("mealList", MealsUtil.getFilteredWithExceeded(mealService.getMeallist(), LocalTime.MIN, LocalTime.MAX, 2000)); + req.setAttribute("mealMap", MealsUtil.getFilteredWithExceededByCycle(myDao.getAllrecords(), LocalTime.MIN, LocalTime.MAX, 2000)); + req.setAttribute("myDao", myDao); req.getRequestDispatcher("meals.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { LOG.debug("doPost"); - req.setAttribute("mealList", MealsUtil.getFilteredWithExceeded(mealService.getMeallist(), LocalTime.MIN, LocalTime.MAX, 2000)); -// LocalDateTime localDateTime = new LocalDateTime(); // + int calories = Integer.valueOf(req.getParameter("Calories")); String description = String.valueOf(req.getParameter("Description")); - mealService.addMeal(new Meal(LocalDateTime.now()/*спешил*/, description, calories)); + if (calories > 0 && !description.isEmpty()){ + myDao.add(new Meal(LocalDateTime.now()/*спешил*/, description, calories)); + } + req.setAttribute("myDao", myDao); + req.setAttribute("mealMap", MealsUtil.getFilteredWithExceededByCycle(myDao.getAllrecords(), LocalTime.MIN, LocalTime.MAX, 2000)); req.getRequestDispatcher("meals.jsp").forward(req, resp); } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } } diff --git a/src/main/java/ru/javawebinar/topjava/web/UserServlet.java b/src/main/java/ru/javawebinar/topjava/web/UserServlet.java index 3371178..450194e 100644 --- a/src/main/java/ru/javawebinar/topjava/web/UserServlet.java +++ b/src/main/java/ru/javawebinar/topjava/web/UserServlet.java @@ -20,7 +20,6 @@ public class UserServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LOG.debug("redirect to users"); - // request.getRequestDispatcher("/users.jsp").forward(request, response); response.sendRedirect("users.jsp"); } diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 06d69be..d42cbc1 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -23,5 +23,14 @@ mealServlet /meals + + MealRemoveServlet + ru.javawebinar.topjava.web.MealRemoveServlet + 0 + + + MealRemoveServlet + /remove + diff --git a/src/main/webapp/css/style.css b/src/main/webapp/css/style.css index 965d421..62c250f 100644 --- a/src/main/webapp/css/style.css +++ b/src/main/webapp/css/style.css @@ -16,6 +16,6 @@ th { .red_record{ color:red; } -.blue_record{ +.green_record{ color:green; } \ No newline at end of file diff --git a/src/main/webapp/meals.jsp b/src/main/webapp/meals.jsp index f7411e6..82106e0 100644 --- a/src/main/webapp/meals.jsp +++ b/src/main/webapp/meals.jsp @@ -10,37 +10,44 @@ + - + Введите порядковый номер для редактирования блюда:
- <%----%> - - - - + + + + + + + Введите порядковый номер для редактирования блюда:
+ +
- - - - - - - + + + + + + + + - - - - - - + + + + + + +
ID Дата Описание Калории