Skip to content

Commit

Permalink
Merge pull request #1 from RuslanSatybaev/HW01
Browse files Browse the repository at this point in the history
HW01: Implement servlet with storage in food list table
  • Loading branch information
RuslanSatybaev authored Feb 24, 2023
2 parents 3db22fa + eed5bbd commit 2372f5d
Show file tree
Hide file tree
Showing 20 changed files with 453 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ log


/src/main/java/META-INF/MANIFEST.MF
code/
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<profiles>
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/ru/javawebinar/topjava/Main.java

This file was deleted.

60 changes: 60 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/Meal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.javawebinar.topjava.model;

import java.time.LocalDateTime;

public class Meal {
private Integer id;

private final LocalDateTime dateTime;
private final String description;
private final int calories;

public Meal(LocalDateTime dateTime, String description, int calories) {
this(null, dateTime, description, calories);
}

public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
this.id = id;
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}

public void setId(int id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public LocalDateTime getDateTime() {
return dateTime;
}

public String getDescription() {
return description;
}

public int getCalories() {
return calories;
}

public boolean isNew() {
return id == null;
}

@Override
public String toString() {
return "Meal{" +
"id=" + id +
", dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import java.time.LocalDateTime;

public class UserMeal {
public class MealTo {
protected Integer id;
private final LocalDateTime dateTime;

private final String description;

private final int calories;
private final boolean excess;

public UserMeal(LocalDateTime dateTime, String description, int calories) {
public MealTo(Integer id, LocalDateTime dateTime, String description, int calories, boolean excess) {
this.id = id;
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
this.excess = excess;
}

public LocalDateTime getDateTime() {
Expand All @@ -26,4 +28,16 @@ public String getDescription() {
public int getCalories() {
return calories;
}
}

public boolean isExcess() {
return excess;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}
30 changes: 0 additions & 30 deletions src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ru.javawebinar.topjava.repository;

import ru.javawebinar.topjava.model.Meal;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class InMemoryUserMealRepository implements MealRepository {
private Map<Integer, Meal> repository = new ConcurrentHashMap<>();
private AtomicInteger counter = new AtomicInteger(0);

{
save(new Meal(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500));
save(new Meal(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000));
save(new Meal(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500));
save(new Meal(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 1000));
save(new Meal(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 500));
save(new Meal(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510));
}

@Override
public Collection<Meal> getAll() {
return repository.values();
}

@Override
public Meal save(Meal meal) {
if (meal.isNew()) {
meal.setId(counter.incrementAndGet());
}
return repository.put(meal.getId(), meal);
}

@Override
public boolean delete(int id) {
return repository.remove(id) != null;
}

@Override
public Meal get(int id) {
return repository.get(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.javawebinar.topjava.repository;

import ru.javawebinar.topjava.model.Meal;

import java.util.Collection;

public interface MealRepository {
// null if not found, when updated
Meal save(Meal meal);

// false if not found
boolean delete(int id);

// null if not found
Meal get(int id);

Collection<Meal> getAll();
}
18 changes: 18 additions & 0 deletions src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.javawebinar.topjava.util;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class DateTimeUtil {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

public static boolean isBetween(LocalTime lt, LocalTime startTime, LocalTime endTime) {
return lt.compareTo(startTime) >= 0 && lt.compareTo(endTime) <= 0;
}

public static String toString(LocalDateTime ldt) {
return ldt == null ? "" : ldt.format(DATE_TIME_FORMATTER);
}
}

38 changes: 38 additions & 0 deletions src/main/java/ru/javawebinar/topjava/util/MealsUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.javawebinar.topjava.util;

import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.model.MealTo;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class MealsUtil {

public static final int DEFAULT_CALORIES_PER_DAY = 1000;

public static List<MealTo> getTos(Collection<Meal> meals, int caloriesPerDay) {
return getFiltered(meals, caloriesPerDay, meal -> true);
}

public static List<MealTo> getFilteredTos(Collection<Meal> meals, int caloriesPerDay, LocalTime startTime, LocalTime endTime) {
return getFiltered(meals, caloriesPerDay, meal -> DateTimeUtil.isBetween(meal.getDateTime().toLocalTime(), startTime, endTime));
}

private static List<MealTo> getFiltered(Collection<Meal> meals, int caloriesPerDay, Predicate<Meal> filter) {
Map<LocalDateTime, Integer> caloriesSumByDate = meals.stream()
.collect(Collectors.groupingBy(Meal::getDateTime, Collectors.summingInt(Meal::getCalories)));
return meals.stream()
.filter(filter)
.map(meal -> createTo(meal, caloriesSumByDate.get(meal.getDateTime()) > caloriesPerDay))
.collect(Collectors.toList());
}

private static MealTo createTo(Meal meal, boolean excess) {
return new MealTo(meal.getId(), meal.getDateTime(), meal.getDescription(), meal.getCalories(), excess);
}
}
9 changes: 0 additions & 9 deletions src/main/java/ru/javawebinar/topjava/util/TimeUtil.java

This file was deleted.

39 changes: 0 additions & 39 deletions src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java

This file was deleted.

Loading

0 comments on commit 2372f5d

Please sign in to comment.