Skip to content
Snippets Groups Projects
Commit b1af746e authored by Karsch Lukas's avatar Karsch Lukas
Browse files

final tweaks to meal plan

parent cff9ba3e
No related branches found
No related tags found
No related merge requests found
package mi.hdm.exceptions;
public class InvalidMealPlanException extends RuntimeException {
public InvalidMealPlanException(String message) {
super(message);
}
}
package mi.hdm.mealPlan;
import mi.hdm.exceptions.InvalidMealPlanException;
import mi.hdm.recipes.NutritionCalculator;
import mi.hdm.recipes.NutritionTable;
import mi.hdm.recipes.Recipe;
......@@ -8,6 +9,7 @@ import org.apache.logging.log4j.Logger;
import java.time.LocalDate;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
......@@ -18,6 +20,11 @@ public class MealPlan {
private final Map<LocalDate, Recipe> plan;
public MealPlan(Map<LocalDate, Recipe> plan) {
if (!plan.isEmpty()) {
if (plan.keySet().stream().anyMatch(Objects::isNull))
throw new InvalidMealPlanException("The specified map is invalid (null key)");
}
this.plan = plan;
}
......@@ -26,10 +33,8 @@ public class MealPlan {
throw new NullPointerException("Date can not be null");
}
LocalDate invalid = LocalDate.now().plusDays(7);
if (date.isBefore(LocalDate.now()) || invalid.isBefore(date)) {
throw new UnsupportedOperationException("You can only add recipes between today and in 7 days from now!");
if (!isDateValid(date)) {
throw new InvalidMealPlanException("You can only add recipes between today and in 7 days from now!");
}
plan.put(date, recipe);
}
......@@ -38,6 +43,7 @@ public class MealPlan {
if (date == null) {
throw new NullPointerException("Date can not be null");
}
if (plan.containsKey(date)) {
log.info("Day {} cleared successfully.", date);
plan.remove(date);
......@@ -70,4 +76,8 @@ public class MealPlan {
return NutritionCalculator.calculateNutritionTable(this);
}
private boolean isDateValid(LocalDate date) {
LocalDate invalid = LocalDate.now().plusDays(7);
return !(date.isBefore(LocalDate.now()) || invalid.isBefore(date));
}
}
package mi.hdm.mealPlan;
import mi.hdm.exceptions.InvalidMealPlanException;
import mi.hdm.recipes.Recipe;
import mi.hdm.recipes.ValidObjectsPool;
import org.junit.jupiter.api.BeforeEach;
......@@ -22,6 +23,14 @@ class MealPlanTest {
underTest = new MealPlan(new HashMap<>());
}
@Test
public void shouldNotInitializeWithInvalidMap() {
//given
LocalDate date = LocalDate.now();
assertThrows(NullPointerException.class, () -> underTest = new MealPlan(Map.of(null, recipeOne, date, recipeTwo)));
}
@Test
public void canAddToMealPlan() {
//given
......@@ -47,8 +56,8 @@ class MealPlanTest {
LocalDate past = LocalDate.of(2000, 1, 1);
//expect throw
assertThrows(UnsupportedOperationException.class, () -> underTest.addRecipeToMealPlan(recipeOne, future));
assertThrows(UnsupportedOperationException.class, () -> underTest.addRecipeToMealPlan(recipeOne, past));
assertThrows(InvalidMealPlanException.class, () -> underTest.addRecipeToMealPlan(recipeOne, future));
assertThrows(InvalidMealPlanException.class, () -> underTest.addRecipeToMealPlan(recipeOne, past));
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment