From bc6d69a2e69307f01de45d4f701a8becad969f9f Mon Sep 17 00:00:00 2001 From: Lukas Karsch <lk224@hdm-stuttgart.de> Date: Wed, 3 May 2023 14:06:39 +0200 Subject: [PATCH] equals method for recipes and more --- .../java/mi/hdm/recipes/CategoryManager.java | 9 +-- .../mi/hdm/recipes/NutritionCalculator.java | 8 ++- .../java/mi/hdm/recipes/RecipeManager.java | 31 ++++++---- .../mi/hdm/recipes/RecipeManagerTest.java | 57 +++++++++++++++++++ src/test/java/mi/hdm/recipes/RecipeTest.java | 8 ++- .../java/mi/hdm/recipes/ValidObjectsPool.java | 11 ++++ 6 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 src/test/java/mi/hdm/recipes/RecipeManagerTest.java diff --git a/src/main/java/mi/hdm/recipes/CategoryManager.java b/src/main/java/mi/hdm/recipes/CategoryManager.java index a526d9f..ab3b6d9 100644 --- a/src/main/java/mi/hdm/recipes/CategoryManager.java +++ b/src/main/java/mi/hdm/recipes/CategoryManager.java @@ -1,5 +1,6 @@ package mi.hdm.recipes; +import mi.hdm.exceptions.InvalidCategoryException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -40,19 +41,19 @@ public class CategoryManager { } public boolean deleteCategory(Category c) { - log.info("Category {} deleted successfully."); + log.info("Category {} deleted successfully.", c.getName()); return allCategories.remove(c); } public boolean deleteCategory(String name) { - Category c = getCategoryByName(name).orElseThrow(() -> new NullPointerException("Category not found.")); - log.info("Category {} deleted successfully."); + Category c = getCategoryByName(name).orElseThrow(() -> new InvalidCategoryException("Category not found.")); + log.info("Category {} deleted successfully.", name); return allCategories.remove(c); } - public List<Category> getAllCategories(){ + public List<Category> getAllCategories() { return allCategories; } diff --git a/src/main/java/mi/hdm/recipes/NutritionCalculator.java b/src/main/java/mi/hdm/recipes/NutritionCalculator.java index f0e21c5..c10bf68 100644 --- a/src/main/java/mi/hdm/recipes/NutritionCalculator.java +++ b/src/main/java/mi/hdm/recipes/NutritionCalculator.java @@ -1,14 +1,15 @@ package mi.hdm.recipes; import mi.hdm.mealPlan.MealPlan; - -import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.Map; + public class NutritionCalculator { private static final Logger log = LogManager.getLogger(NutritionCalculator.class); + /** * Method to calculate the nutrition table for a recipe out of its ingredients. * @@ -30,6 +31,7 @@ public class NutritionCalculator { for (RecipeComponent entry : ingredients.keySet()) { final Map<Nutrition, Double> nutritionTable = entry.getNutritionTable().getTable(); + //TODO: This doesnt apply if the measurement is PIECE or PINCH! FIX! totalCals += nutritionTable.get(Nutrition.CALORIES) * ingredients.get(entry) / 100; totalCarbs += nutritionTable.get(Nutrition.CARBS) * ingredients.get(entry) / 100; totalFats += nutritionTable.get(Nutrition.FAT) * ingredients.get(entry) / 100; @@ -41,7 +43,7 @@ public class NutritionCalculator { return new NutritionTable(totalCals, totalCarbs, totalFats, totalProteins, totalFibers, totalSalt); } - //TODO: implement calcuating nutrition table from meal plan + //TODO: implement calcuating nutrition table from meal plan and List<Recipe> public static NutritionTable calculateNutritionTable(MealPlan mealPlan) { return null; } diff --git a/src/main/java/mi/hdm/recipes/RecipeManager.java b/src/main/java/mi/hdm/recipes/RecipeManager.java index edcae99..25f05ea 100644 --- a/src/main/java/mi/hdm/recipes/RecipeManager.java +++ b/src/main/java/mi/hdm/recipes/RecipeManager.java @@ -4,6 +4,7 @@ import mi.hdm.exceptions.InvalidRecipeException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -11,36 +12,43 @@ public class RecipeManager { private final List<Recipe> recipes; private final CategoryManager categories; private final IngredientManager ingredients; - private final NutritionCalculator nutritionCalculator; private static final Logger log = LogManager.getLogger(RecipeManager.class); - public RecipeManager(List<Recipe> recipes, CategoryManager categories, IngredientManager ingredients, NutritionCalculator nutritionCalculator) { + public RecipeManager(List<Recipe> recipes, CategoryManager categories, IngredientManager ingredients) { this.recipes = recipes; this.categories = categories; this.ingredients = ingredients; - this.nutritionCalculator = nutritionCalculator; } - public void addRecipe(Recipe recipe){ + public RecipeManager() { + recipes = new ArrayList<>(); + categories = CategoryManager.getInstance(); + ingredients = IngredientManager.getInstance(); + } + public void addRecipe(Recipe recipe) { if (getRecipeByName(recipe.getName()).isEmpty()) { recipes.add(recipe); - log.info("Recipe {} added successfully."); + log.info("Recipe {} added successfully.", recipe.getName()); } else { - log.error("Recipe {} not added because it already exists."); + log.error("Recipe {} not added because it already exists.", recipe.getName()); throw new InvalidRecipeException("Recipe name already exists"); } - //TODO: Exception or false? } public Recipe deleteRecipe(int i) { - log.info("Recipe {} deleted successfully."); + log.info("Recipe {} deleted successfully.", recipes.get(i).getName()); return recipes.remove(i); } public boolean deleteRecipe(String name) { - Recipe r = getRecipeByName(name).orElseThrow(); - log.info("Recipe {} deleted successfully."); + Recipe r = getRecipeByName(name).orElseThrow(() -> new InvalidRecipeException("A recipe with this name does not exist")); + log.info("Recipe {} deleted successfully.", name); + return recipes.remove(r); + } + + //TODO: this returns false, other methods throw an exception. whats the best approach? + public boolean deleteRecipe(Recipe r) { return recipes.remove(r); } @@ -53,4 +61,7 @@ public class RecipeManager { return Optional.empty(); } + public List<Recipe> getRecipes() { + return recipes; + } } diff --git a/src/test/java/mi/hdm/recipes/RecipeManagerTest.java b/src/test/java/mi/hdm/recipes/RecipeManagerTest.java new file mode 100644 index 0000000..0feea92 --- /dev/null +++ b/src/test/java/mi/hdm/recipes/RecipeManagerTest.java @@ -0,0 +1,57 @@ +package mi.hdm.recipes; + +import mi.hdm.exceptions.InvalidRecipeException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class RecipeManagerTest { + private RecipeManager underTest; + private final Recipe recipeOne = ValidObjectsPool.getValidRecipeOne(); + private final Recipe recipeTwo = ValidObjectsPool.getValidRecipeTwo(); + + @BeforeEach + public void setup() { + underTest = new RecipeManager(); + } + + @Test + public void shouldAddRecipeWhenNotDuplicate() { + //when + underTest.addRecipe(recipeOne); + + //expect + List<Recipe> expected = List.of(recipeOne); + assertEquals(expected, underTest.getRecipes()); + } + + @Test + public void shouldNotAddDuplicateRecipe() { + //given + underTest.addRecipe(recipeOne); + + //expect + assertThrows(InvalidRecipeException.class, () -> underTest.addRecipe(recipeOne)); + } + + @Test + public void canDeleteRecipeByName() { + //given + underTest.addRecipe(recipeOne); + + //when + underTest.deleteRecipe(recipeOne.getName()); + + //expect + assertEquals(List.of(), underTest.getRecipes()); + } + + @Test + public void removingInvalidRecipes() { + assertFalse(underTest.deleteRecipe(recipeOne)); + assertThrows(InvalidRecipeException.class, () -> underTest.deleteRecipe("This recipe does not exist")); + } +} diff --git a/src/test/java/mi/hdm/recipes/RecipeTest.java b/src/test/java/mi/hdm/recipes/RecipeTest.java index 4caf909..8fdc169 100644 --- a/src/test/java/mi/hdm/recipes/RecipeTest.java +++ b/src/test/java/mi/hdm/recipes/RecipeTest.java @@ -43,6 +43,12 @@ class RecipeTest { assertThrows(InvalidRecipeException.class, () -> new Recipe("Valid name", recipeMap, "Description!", List.of(), null, 100)); } + @Test + @DisplayName("Test invalid prep time: Recipes with an invalid preparation time (number is negative) should not be created") + void shouldNotCreateRecipeWithInvalidPrepTime() { + assertThrows(InvalidRecipeException.class, () -> new Recipe("Valid name", recipeMap, "Description!", null, null, -1)); + } + @Test void getNutritionTableWhenSetManually() { //given @@ -103,4 +109,4 @@ class RecipeTest { underTest.getCategories() ); } -} \ No newline at end of file +} diff --git a/src/test/java/mi/hdm/recipes/ValidObjectsPool.java b/src/test/java/mi/hdm/recipes/ValidObjectsPool.java index c9a1bd0..013cdf9 100644 --- a/src/test/java/mi/hdm/recipes/ValidObjectsPool.java +++ b/src/test/java/mi/hdm/recipes/ValidObjectsPool.java @@ -1,6 +1,7 @@ package mi.hdm.recipes; import java.util.List; +import java.util.Map; public class ValidObjectsPool { private final static Category categoryOne = new Category("Frühstück", 0xFF0000); @@ -9,6 +10,8 @@ public class ValidObjectsPool { private final static NutritionTable nutritionTableTwo = new NutritionTable(263.1, 25.2, 0.2, 0.0, 0.0, 0.8); private final static Ingredient ingredientOne = new Ingredient(Measurement.GRAM, "Zucker", nutritionTableOne); private final static Ingredient ingredientTwo = new Ingredient(Measurement.PIECE, "Apple", nutritionTableTwo); + private final static Recipe recipeOne = new Recipe("Valid recipe 1", Map.of(ingredientOne, 100, ingredientTwo, 200), "Description for this recipe", List.of("step one", "step two"), List.of(categoryOne), 15); + private final static Recipe recipeTwo = new Recipe("Apfelkuchen", Map.of(ingredientOne, 250), "Mein liebster APfelkuchen", List.of("mjam mjam", "ich liebe kochen"), List.of(categoryTwo, categoryOne), 25); public static NutritionTable getValidNutritionTableOne() { return nutritionTableOne; @@ -41,4 +44,12 @@ public class ValidObjectsPool { public static List<Category> getValidCategoryList() { return List.of(categoryOne, categoryTwo); } + + public static Recipe getValidRecipeOne() { + return recipeOne; + } + + public static Recipe getValidRecipeTwo() { + return recipeTwo; + } } -- GitLab