forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from RuslanSatybaev/HW01
HW01: Implement servlet with storage in food list table
- Loading branch information
Showing
20 changed files
with
453 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ log | |
|
||
|
||
/src/main/java/META-INF/MANIFEST.MF | ||
code/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
src/main/java/ru/javawebinar/topjava/model/UserMealWithExcess.java
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/main/java/ru/javawebinar/topjava/repository/InMemoryUserMealRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/ru/javawebinar/topjava/repository/MealRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
src/main/java/ru/javawebinar/topjava/util/UserMealsUtil.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.