From 9878f2cf96398cd51685f264cfa1a08682e440f0 Mon Sep 17 00:00:00 2001
From: Lara Blersch <lb210@hdm-stuttgart.de>
Date: Wed, 3 May 2023 13:51:07 +0200
Subject: [PATCH] logging added in several classes

---
 src/main/java/mi/hdm/recipes/CategoryManager.java     |  5 +++++
 src/main/java/mi/hdm/recipes/IngredientManager.java   |  6 +++++-
 src/main/java/mi/hdm/recipes/NutritionCalculator.java |  7 ++++++-
 src/main/java/mi/hdm/recipes/Recipe.java              | 10 ++++++++++
 src/main/java/mi/hdm/recipes/RecipeManager.java       |  6 ++++++
 src/main/java/mi/hdm/recipes/RecipeSearch.java        |  1 +
 src/main/java/mi/hdm/shoppingList/ShoppingList.java   |  1 +
 7 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/src/main/java/mi/hdm/recipes/CategoryManager.java b/src/main/java/mi/hdm/recipes/CategoryManager.java
index 3059e1f..a526d9f 100644
--- a/src/main/java/mi/hdm/recipes/CategoryManager.java
+++ b/src/main/java/mi/hdm/recipes/CategoryManager.java
@@ -40,12 +40,16 @@ public class CategoryManager {
     }
 
     public boolean deleteCategory(Category c) {
+        log.info("Category {} deleted successfully.");
         return allCategories.remove(c);
+
     }
 
     public boolean deleteCategory(String name) {
         Category c = getCategoryByName(name).orElseThrow(() -> new NullPointerException("Category not found."));
+        log.info("Category {} deleted successfully.");
         return allCategories.remove(c);
+
     }
 
     public List<Category> getAllCategories(){
@@ -54,6 +58,7 @@ public class CategoryManager {
 
     public void clearCategories() {
         allCategories.clear();
+        log.info("List allCategories cleared successfully.");
     }
 
     private Optional<Category> getCategoryByName(String name) {
diff --git a/src/main/java/mi/hdm/recipes/IngredientManager.java b/src/main/java/mi/hdm/recipes/IngredientManager.java
index 89260a3..1cef464 100644
--- a/src/main/java/mi/hdm/recipes/IngredientManager.java
+++ b/src/main/java/mi/hdm/recipes/IngredientManager.java
@@ -29,8 +29,9 @@ public class IngredientManager {
      */
     public boolean addIngredient(Ingredient in) {
         if (allIngredients.contains(in)) {
-            log.error("Ingredient: {} not added because it already exists", in.getName());
+            log.error("Ingredient {} not added because it already exists.", in.getName());
             return false;
+            //TODO: Exception or false?
         } else {
             log.info("Ingredient {} added successfully.", in.getName());
             allIngredients.add(in);
@@ -44,6 +45,7 @@ public class IngredientManager {
     }
 
     public Ingredient deleteIngredient(int i) {
+        log.info("Ingredient {} deleted successfully.");
         return allIngredients.remove(i);
     }
 
@@ -53,10 +55,12 @@ public class IngredientManager {
         return ingredient.isPresent() && allIngredients.remove(ingredient);*/
 
         Ingredient ingredient = getIngredientByName(name).orElseThrow(() -> new InvalidIngredientException("No ingredient with name " + name));
+        log.info("Ingredient {} deleted successfully.");
         return allIngredients.remove(ingredient);
     }
 
     public void clearIngredients() {
+        log.info("List allIngredients cleared successfully.");
         allIngredients.clear();
     }
 
diff --git a/src/main/java/mi/hdm/recipes/NutritionCalculator.java b/src/main/java/mi/hdm/recipes/NutritionCalculator.java
index 6233f3b..f0e21c5 100644
--- a/src/main/java/mi/hdm/recipes/NutritionCalculator.java
+++ b/src/main/java/mi/hdm/recipes/NutritionCalculator.java
@@ -3,8 +3,12 @@ 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;
 
 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.
      *
@@ -12,6 +16,7 @@ public class NutritionCalculator {
      */
     public static NutritionTable calculateNutritionTable(Map<RecipeComponent, Integer> ingredients) {
         if (ingredients == null || ingredients.isEmpty()) {
+            log.info("Map ingredients is null or empty. Therefore new nutritionTable() was generated.");
             return NutritionTable.empty();
         }
 
@@ -32,7 +37,7 @@ public class NutritionCalculator {
             totalFibers += nutritionTable.get(Nutrition.FIBERS) * ingredients.get(entry) / 100;
             totalSalt += nutritionTable.get(Nutrition.SALT) * ingredients.get(entry) / 100;
         }
-
+        log.info("NutritionTable calculated successfully.");
         return new NutritionTable(totalCals, totalCarbs, totalFats, totalProteins, totalFibers, totalSalt);
     }
 
diff --git a/src/main/java/mi/hdm/recipes/Recipe.java b/src/main/java/mi/hdm/recipes/Recipe.java
index 52b6cc1..841291f 100644
--- a/src/main/java/mi/hdm/recipes/Recipe.java
+++ b/src/main/java/mi/hdm/recipes/Recipe.java
@@ -1,6 +1,8 @@
 package mi.hdm.recipes;
 
 import mi.hdm.exceptions.InvalidRecipeException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import java.time.LocalDateTime;
 import java.util.ArrayList;
@@ -8,6 +10,7 @@ import java.util.List;
 import java.util.Map;
 
 public class Recipe implements RecipeComponent {
+    private static final Logger log = LogManager.getLogger(Recipe.class);
     private final Map<RecipeComponent, Integer> ingredients;
     private String name;
     private String description;
@@ -85,6 +88,7 @@ public class Recipe implements RecipeComponent {
             throw new InvalidRecipeException("Cannot set name to " + name);
         }
         this.name = name;
+        log.info("Name set successfully.");
     }
 
     public String getName() {
@@ -97,6 +101,7 @@ public class Recipe implements RecipeComponent {
 
     public void setDescription(String description) {
         this.description = description;
+        log.info("Description set successfully.");
     }
 
     public List<String> getPreparation() {
@@ -105,6 +110,7 @@ public class Recipe implements RecipeComponent {
 
     public void setPreparation(List<String> preparation) {
         this.preparation = preparation;
+        log.info("Preparation set successfully.");
     }
 
     public List<Category> getCategories() {
@@ -117,10 +123,12 @@ public class Recipe implements RecipeComponent {
         }
 
         this.categories = categories;
+        log.info("Categories set successfully.");
     }
 
     public void addCategory(Category category) {
         categories.add(category);
+        log.info("Category {} added successfully.");
     }
 
     public int getPreparationTimeMins() {
@@ -132,6 +140,7 @@ public class Recipe implements RecipeComponent {
             throw new InvalidRecipeException("PreparationTime must be a positive value");
         }
         this.preparationTimeMins = preparationTimeMins;
+        log.info("PreparationTimeMins set successfully.");
     }
 
     public LocalDateTime getCreationTime() {
@@ -143,6 +152,7 @@ public class Recipe implements RecipeComponent {
             throw new InvalidRecipeException("Nutrition Table does not exist");
         }
         this.nutritionTable = nutritionTable;
+        log.info("NutritionTable set successfully.");
     }
 
     public NutritionTable getNutritionTable() {
diff --git a/src/main/java/mi/hdm/recipes/RecipeManager.java b/src/main/java/mi/hdm/recipes/RecipeManager.java
index 189852d..edcae99 100644
--- a/src/main/java/mi/hdm/recipes/RecipeManager.java
+++ b/src/main/java/mi/hdm/recipes/RecipeManager.java
@@ -22,19 +22,25 @@ public class RecipeManager {
     }
 
     public void addRecipe(Recipe recipe){
+
         if (getRecipeByName(recipe.getName()).isEmpty()) {
             recipes.add(recipe);
+            log.info("Recipe {} added successfully.");
         } else {
+            log.error("Recipe {} not added because it already exists.");
             throw new InvalidRecipeException("Recipe name already exists");
         }
+        //TODO: Exception or false?
     }
 
     public Recipe deleteRecipe(int i) {
+        log.info("Recipe {} deleted successfully.");
         return recipes.remove(i);
     }
 
     public boolean deleteRecipe(String name) {
         Recipe r = getRecipeByName(name).orElseThrow();
+        log.info("Recipe {} deleted successfully.");
         return recipes.remove(r);
     }
 
diff --git a/src/main/java/mi/hdm/recipes/RecipeSearch.java b/src/main/java/mi/hdm/recipes/RecipeSearch.java
index 460a8fd..b96012d 100644
--- a/src/main/java/mi/hdm/recipes/RecipeSearch.java
+++ b/src/main/java/mi/hdm/recipes/RecipeSearch.java
@@ -3,6 +3,7 @@ package mi.hdm.recipes;
 import java.util.List;
 
 public class RecipeSearch {
+    //TODO: implement class
     List<Recipe> recipesToSearch;
 
     public RecipeSearch (List<Recipe> recipesToSearch) {}
diff --git a/src/main/java/mi/hdm/shoppingList/ShoppingList.java b/src/main/java/mi/hdm/shoppingList/ShoppingList.java
index bd9d88b..187fb3f 100644
--- a/src/main/java/mi/hdm/shoppingList/ShoppingList.java
+++ b/src/main/java/mi/hdm/shoppingList/ShoppingList.java
@@ -9,6 +9,7 @@ import java.util.Map;
  * Shopping list that the user can add ingredients to. The elements on the list can be marked as done or undone.
  */
 public class ShoppingList {
+    //TODO: implement class
     private final Map<Ingredient, Boolean> list;
 
     private static final ShoppingList shoppingList = new ShoppingList();
-- 
GitLab