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/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..0493d8b --- /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.concurrent.ConcurrentMap; + +/** + * Created by Администратор on 28.03.2017. + */ +public interface DaoMeal { + + public void add(Meal meal); + + public Meal get(int id); + + public void update(Meal meal); + + public void remove(int id); + + public void factoryMethod(); + + 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 new file mode 100644 index 0000000..eeac722 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/dao/DaoMealMemoryImplementation.java @@ -0,0 +1,48 @@ +package ru.javawebinar.topjava.dao; + +import ru.javawebinar.topjava.model.Meal; +import ru.javawebinar.topjava.util.MealsUtil; + +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 { + static AtomicInteger id; + ConcurrentMap map = new ConcurrentHashMap<>(); + + + @Override + public void add(Meal meal) { + map.put(id.incrementAndGet() , meal); + } + + @Override + public Meal get(int id) { + return map.get(id); + } + + @Override + public void update(Meal meal) { + map.put(meal.getId() , meal); + } + + @Override + public void remove(int id) { + this.map.remove(id); + } + + @Override + 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 ConcurrentMap getAllrecords() { + return map; + } +} diff --git a/src/main/java/ru/javawebinar/topjava/model/Meal.java b/src/main/java/ru/javawebinar/topjava/model/Meal.java index 006d5ca..7354839 100644 --- a/src/main/java/ru/javawebinar/topjava/model/Meal.java +++ b/src/main/java/ru/javawebinar/topjava/model/Meal.java @@ -9,11 +9,13 @@ * 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 int id; public Meal(LocalDateTime dateTime, String description, int calories) { this.dateTime = dateTime; @@ -21,6 +23,14 @@ public Meal(LocalDateTime dateTime, String description, int calories) { this.calories = calories; } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public LocalDateTime getDateTime() { return dateTime; } @@ -40,4 +50,16 @@ public LocalDate getDate() { public LocalTime getTime() { return dateTime.toLocalTime(); } + + 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 d14128b..72dd26e 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; /** @@ -15,11 +16,39 @@ 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 int getId() { + return id; + } + + 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 diff --git a/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java b/src/main/java/ru/javawebinar/topjava/util/MealsUtil.java index d6476fc..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,49 +17,53 @@ * 31.05.2015. */ public class MealsUtil { - public static void main(String[] args) { - List meals = Arrays.asList( + + 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) - ); - 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)); + 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 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 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 List getFilteredWithExceededByCycle(List meals, LocalTime startTime, LocalTime endTime, int caloriesPerDay) { + 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 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/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/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 new file mode 100644 index 0000000..193cac8 --- /dev/null +++ b/src/main/java/ru/javawebinar/topjava/web/MealServlet.java @@ -0,0 +1,60 @@ +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.model.Meal; +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.LocalDateTime; +import java.time.LocalTime; + +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); + DaoMeal myDao; + + @Override + public void init() throws ServletException { + myDao = new DaoMealMemoryImplementation(); + myDao.factoryMethod(); + + } + + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + LOG.debug("MealServlet doGet"); + 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"); + + int calories = Integer.valueOf(req.getParameter("Calories")); + String description = String.valueOf(req.getParameter("Description")); + 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 f346a5e..d42cbc1 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -14,5 +14,23 @@ userServlet /users + + mealServlet + ru.javawebinar.topjava.web.MealServlet + 0 + + + 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 new file mode 100644 index 0000000..62c250f --- /dev/null +++ b/src/main/webapp/css/style.css @@ -0,0 +1,21 @@ +table , form1 { + border-collapse: collapse; + width: 40%; +} + +th, td { + text-align: left; + padding: 8px; +} +tr:nth-child(even){background-color: #f2f2f2} + +th { + background-color: #4CAF50; + color: white; +} +.red_record{ + color:red; +} +.green_record{ + color:green; +} \ No newline at end of file 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..82106e0 --- /dev/null +++ b/src/main/webapp/meals.jsp @@ -0,0 +1,55 @@ +<%@ 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 + + + + + + + + + + + + + Введите порядковый номер для редактирования блюда:
+ + + + + + + Введите порядковый номер для редактирования блюда:
+ + + + +
+ + + + + + + + + + + + + + + + + + + + +
IDДатаОписаниеКалории
+ + \ No newline at end of file