From bc61c5f6477674130c2601d0b4a4250a93f24f33 Mon Sep 17 00:00:00 2001
From: Lukas Karsch <lk224@hdm-stuttgart.de>
Date: Tue, 6 Jun 2023 10:14:46 +0200
Subject: [PATCH] removed some todos, shopping list now takes recipe manager.

---
 src/main/java/mi/hdm/TastyPages.java          |  4 ++--
 .../java/mi/hdm/filesystem/CSVParser.java     |  2 --
 .../java/mi/hdm/filesystem/FileManager.java   | 18 ++++++++--------
 .../java/mi/hdm/recipes/CategoryManager.java  | 10 ++++-----
 .../mi/hdm/recipes/IngredientManager.java     | 12 +++++------
 .../mi/hdm/recipes/NutritionCalculator.java   |  2 +-
 .../mi/hdm/shoppingList/ShoppingList.java     | 21 ++++++++++++-------
 .../typeAdapters/ShoppingListTypeAdapter.java |  9 +++++++-
 src/test/java/mi/hdm/filesystem/JsonTest.java |  3 ++-
 .../java/mi/hdm/recipes/ValidObjectsPool.java |  2 +-
 .../mi/hdm/shoppingList/ShoppingListTest.java |  9 +++++++-
 11 files changed, 55 insertions(+), 37 deletions(-)

diff --git a/src/main/java/mi/hdm/TastyPages.java b/src/main/java/mi/hdm/TastyPages.java
index e511041..ea65bde 100644
--- a/src/main/java/mi/hdm/TastyPages.java
+++ b/src/main/java/mi/hdm/TastyPages.java
@@ -38,7 +38,7 @@ public class TastyPages {
         this.recipeManager = new RecipeManager();
         this.categoryManager = new CategoryManager();
         this.mealPlan = new MealPlan(recipeManager);
-        this.shoppingList = new ShoppingList();
+        this.shoppingList = new ShoppingList(recipeManager);
         log.info("Loaded TastyPages with ingredient manager");
     }
 
@@ -50,7 +50,7 @@ public class TastyPages {
         this.ingredientManager = new IngredientManager();
         this.categoryManager = new CategoryManager();
         this.mealPlan = new MealPlan(recipeManager);
-        this.shoppingList = new ShoppingList();
+        this.shoppingList = new ShoppingList(recipeManager);
         log.info("Created empty instance of TastyPages");
     }
 
diff --git a/src/main/java/mi/hdm/filesystem/CSVParser.java b/src/main/java/mi/hdm/filesystem/CSVParser.java
index a2362d9..8888ec8 100644
--- a/src/main/java/mi/hdm/filesystem/CSVParser.java
+++ b/src/main/java/mi/hdm/filesystem/CSVParser.java
@@ -61,7 +61,6 @@ public class CSVParser {
     }
 
     private Ingredient getIngredientFromLine(String line, char split, int[] idx) throws NumberFormatException {
-        //TODO: take into account the measurement of the ingredient when reading it
         log.debug("Trying to parse line {}", line);
 
         final Measurement measurement = Measurement.GRAM;
@@ -71,7 +70,6 @@ public class CSVParser {
         final List<BigDecimal> nutrition = new ArrayList<>();
 
         for (int i = 1; i < idx.length; i++) {
-            //String element = splitLine.get(idx[i]).split(" ")[0];
             String element = splitLine.get(idx[i]);
             double quantity = parseNumberFromString(element);
             if (getMeasurementFromString(element).equals("mg")) {
diff --git a/src/main/java/mi/hdm/filesystem/FileManager.java b/src/main/java/mi/hdm/filesystem/FileManager.java
index 67c2c62..1ffffe9 100644
--- a/src/main/java/mi/hdm/filesystem/FileManager.java
+++ b/src/main/java/mi/hdm/filesystem/FileManager.java
@@ -44,7 +44,7 @@ public class FileManager {
         serializeRecipeManager(app.getRecipeManager(), Path.of(PATH_TO_USER_DATA, "recipes"));
         serializeCategoryManager(app.getCategoryManager(), Path.of(PATH_TO_USER_DATA, "categories.json"));
         serializeMealPlan(app.getMealPlan(), Path.of(PATH_TO_USER_DATA, "mealplan.json"));
-        serializeShoppingList(app.getShoppingList(), Path.of(PATH_TO_USER_DATA, "shoppingList.json"));
+        serializeShoppingList(app.getShoppingList(), app.getRecipeManager(), Path.of(PATH_TO_USER_DATA, "shoppingList.json"));
     }
 
     @SuppressWarnings("ResultOfMethodCallIgnored")
@@ -118,11 +118,11 @@ public class FileManager {
                 if (Files.exists(toShoppingListJson) && Files.isRegularFile(toShoppingListJson)) {
                     log.info("Loading shopping list from '{}'", toShoppingListJson);
                     String fileContent = Files.readString(toShoppingListJson);
-                    shoppingList = deserializeShoppingList(fileContent);
+                    shoppingList = deserializeShoppingList(fileContent, recipeManager);
                 } else {
                     log.info("Could not find /shoppingList.json, creating empty shopping list");
                     toShoppingListJson.toFile().createNewFile();
-                    shoppingList = new ShoppingList();
+                    shoppingList = new ShoppingList(recipeManager);
                 }
             }
             return new TastyPages(recipeManager, ingredientManager, categoryManager, mealPlan, shoppingList);
@@ -158,7 +158,7 @@ public class FileManager {
 
     private static RecipeManager deserializeRecipeManager(Path path) throws IOException {
         List<Path> recipePaths;
-        try (Stream<Path> stream = Files.list(path)) { //TODO: catch clause
+        try (Stream<Path> stream = Files.list(path)) {
             recipePaths = stream.toList();
         }
 
@@ -244,20 +244,20 @@ public class FileManager {
         }
     }
 
-    private static ShoppingList deserializeShoppingList(String json) {
+    private static ShoppingList deserializeShoppingList(String json, RecipeManager recipeManager) {
         if (json.isBlank()) {
             log.info("JSON file is blank, creating empty instance of Shopping List");
-            return new ShoppingList();
+            return new ShoppingList(recipeManager);
         }
         Gson gson = new GsonBuilder()
-                .registerTypeAdapter(ShoppingList.class, new ShoppingListTypeAdapter())
+                .registerTypeAdapter(ShoppingList.class, new ShoppingListTypeAdapter(recipeManager))
                 .create();
         return gson.fromJson(json, ShoppingList.class);
     }
 
-    private static void serializeShoppingList(ShoppingList shoppingList, Path path) {
+    private static void serializeShoppingList(ShoppingList shoppingList, RecipeManager recipeManager, Path path) {
         Gson gson = new GsonBuilder()
-                .registerTypeAdapter(ShoppingList.class, new ShoppingListTypeAdapter())
+                .registerTypeAdapter(ShoppingList.class, new ShoppingListTypeAdapter(recipeManager))
                 .create();
         String json = gson.toJson(shoppingList);
         try {
diff --git a/src/main/java/mi/hdm/recipes/CategoryManager.java b/src/main/java/mi/hdm/recipes/CategoryManager.java
index 3d2d9eb..addeb22 100644
--- a/src/main/java/mi/hdm/recipes/CategoryManager.java
+++ b/src/main/java/mi/hdm/recipes/CategoryManager.java
@@ -12,7 +12,7 @@ import java.util.Optional;
 public class CategoryManager {
     private static final Logger log = LogManager.getLogger(CategoryManager.class);
 
-    private final Map<Integer, Category> allCategories; //TODO: save in hash map with key being the code of each category
+    private final Map<Integer, Category> allCategories;
 
     public CategoryManager() {
         allCategories = new HashMap<>();
@@ -44,11 +44,11 @@ public class CategoryManager {
 
     public void addCategory(Category c) {
         if (allCategories.get(c.getCode()) != null || getCategoryByName(c.getName()).isPresent() || getCategoryByColourCode(c.getColourCode()).isPresent()) {
-            log.error("Category {} not added because it already exists", c.getName());
+            log.error("Category '{}' not added because it already exists", c.getName());
             throw new InvalidCategoryException("Category already exists.");
         } else {
             allCategories.put(c.getCode(), c);
-            log.info("Category {} added successfully.", c.getName());
+            log.info("Category '{}' added successfully.", c.getName());
         }
     }
 
@@ -56,7 +56,7 @@ public class CategoryManager {
         if (!allCategories.values().remove(c)) {
             throw new InvalidCategoryException("Category is not listed.");
         }
-        log.info("Category {} deleted successfully.", c.getName());
+        log.info("Category '{}' deleted successfully.", c.getName());
     }
 
     public void deleteCategory(String name) {
@@ -70,7 +70,7 @@ public class CategoryManager {
 
     public void clearCategories() {
         allCategories.clear();
-        log.info("List allCategories cleared successfully.");
+        log.info("List of categories 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 7e12253..7d5fe5d 100644
--- a/src/main/java/mi/hdm/recipes/IngredientManager.java
+++ b/src/main/java/mi/hdm/recipes/IngredientManager.java
@@ -12,9 +12,9 @@ import java.util.Optional;
 public class IngredientManager {
     private static final Logger log = LogManager.getLogger(IngredientManager.class);
 
-    private final Map<String, Ingredient> allIngredients; //Map Key of ingredient to ingredient object
+    private final Map<String, Ingredient> allIngredients; //Map key of ingredient to ingredient object
 
-    public IngredientManager() { //TODO: save in hash map with key being the uniquely generated code
+    public IngredientManager() {
         allIngredients = new HashMap<>();
     }
 
@@ -38,10 +38,10 @@ public class IngredientManager {
      */
     public void addIngredient(Ingredient in) {
         if (getIngredientByName(in.getName()).isPresent()) {
-            log.error("Ingredient {} not added because it already exists.", in.getName());
+            log.error("Ingredient '{}' not added because it already exists.", in.getName());
             throw new InvalidIngredientException("Ingredient already exists.");
         } else {
-            log.info("Ingredient {} added successfully.", in.getName());
+            log.info("Ingredient '{}' added successfully.", in.getName());
             allIngredients.put(in.getUniqueCode(), in);
         }
     }
@@ -53,12 +53,12 @@ public class IngredientManager {
 
     public void deleteIngredient(String name) {
         Ingredient ingredient = getIngredientByName(name).orElseThrow(() -> new InvalidIngredientException("No ingredient with name " + name));
-        log.info("Ingredient {} deleted successfully.", name);
+        log.info("Ingredient '{}' deleted successfully.", name);
         allIngredients.values().remove(ingredient);
     }
 
     public void clearIngredients() {
-        log.info("List allIngredients cleared successfully.");
+        log.info("List of ingredients 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 77bb1c8..8c2de0a 100644
--- a/src/main/java/mi/hdm/recipes/NutritionCalculator.java
+++ b/src/main/java/mi/hdm/recipes/NutritionCalculator.java
@@ -16,7 +16,7 @@ public class NutritionCalculator {
      *
      * @return the nutrition table for this recipe based on its ingredients. All nutrition values are added up.
      */
-    public static NutritionTable calculateNutritionTable(Map<RecipeComponent, Integer> ingredients) { //TODO: this needs to accept a map of keys
+    public static NutritionTable calculateNutritionTable(Map<RecipeComponent, Integer> ingredients) { //TODO: this needs to accept a map of keys & ing. manager + recipe manager
         if (ingredients == null || ingredients.isEmpty()) {
             log.info("Map ingredients is null or empty. Therefore new nutritionTable() was generated.");
             return NutritionTable.empty();
diff --git a/src/main/java/mi/hdm/shoppingList/ShoppingList.java b/src/main/java/mi/hdm/shoppingList/ShoppingList.java
index 142928d..d9cf385 100644
--- a/src/main/java/mi/hdm/shoppingList/ShoppingList.java
+++ b/src/main/java/mi/hdm/shoppingList/ShoppingList.java
@@ -3,6 +3,7 @@ package mi.hdm.shoppingList;
 import mi.hdm.exceptions.InvalidIngredientException;
 import mi.hdm.recipes.Ingredient;
 import mi.hdm.recipes.Recipe;
+import mi.hdm.recipes.RecipeManager;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -15,15 +16,19 @@ import java.util.Map;
 public class ShoppingList {
     private static final Logger log = LogManager.getLogger(ShoppingList.class);
 
+    private final RecipeManager recipeManager;
+
     private final Map<String, Boolean> list;
 
-    public ShoppingList() {
+    public ShoppingList(RecipeManager recipeManager) {
+        this.recipeManager = recipeManager;
         list = new HashMap<>();
         log.info("Created empty instance of ShoppingList");
     }
 
-    public ShoppingList(Map<String, Boolean> shoppingListMap) {
+    public ShoppingList(Map<String, Boolean> shoppingListMap, RecipeManager recipeManager) {
         list = new HashMap<>(shoppingListMap);
+        this.recipeManager = recipeManager;
         log.info("Created ShoppingList with {} entries.", list.size());
     }
 
@@ -37,7 +42,7 @@ public class ShoppingList {
             throw new InvalidIngredientException("Can not add ingredient that is null");
         }
         list.put(ingredient.getUniqueCode(), false);
-        log.info("Added {} to shopping list.", ingredient.getName());
+        log.info("Added '{}' to shopping list.", ingredient.getName());
     }
 
     public void addToShoppingList(String key) {
@@ -51,11 +56,11 @@ public class ShoppingList {
     public void addAllToShoppingList(Recipe recipe) {
         recipe.getIngredients().keySet()
                 .forEach(element -> {
-                            if (element.startsWith("i"))
-                                addToShoppingList(element);
-                            //adds recipes recursively (see below); comment next 2 lines out to avoid adding recipes recursively
-                            /*else
-                                addAllToShoppingList((Recipe) element);*/ //TODO: needs recipe manager here
+                    if (element.startsWith("i"))
+                        addToShoppingList(element);
+                        //adds recipes recursively (see below); comment next 2 lines out to avoid adding recipes recursively
+                    else
+                        addAllToShoppingList(recipeManager.getRecipeByCode(element));
                         }
                 );
     }
diff --git a/src/main/java/mi/hdm/typeAdapters/ShoppingListTypeAdapter.java b/src/main/java/mi/hdm/typeAdapters/ShoppingListTypeAdapter.java
index 412de54..b640d5d 100644
--- a/src/main/java/mi/hdm/typeAdapters/ShoppingListTypeAdapter.java
+++ b/src/main/java/mi/hdm/typeAdapters/ShoppingListTypeAdapter.java
@@ -5,12 +5,19 @@ import com.google.gson.TypeAdapter;
 import com.google.gson.reflect.TypeToken;
 import com.google.gson.stream.JsonReader;
 import com.google.gson.stream.JsonWriter;
+import mi.hdm.recipes.RecipeManager;
 import mi.hdm.shoppingList.ShoppingList;
 
 import java.io.IOException;
 import java.util.Map;
 
 public class ShoppingListTypeAdapter extends TypeAdapter<ShoppingList> {
+    private final RecipeManager recipeManager;
+
+    public ShoppingListTypeAdapter(RecipeManager recipeManager) {
+        this.recipeManager = recipeManager;
+    }
+
     @Override
     public void write(JsonWriter writer, ShoppingList shoppingList) throws IOException {
         final Map<String, Boolean> map = shoppingList.getList();
@@ -30,6 +37,6 @@ public class ShoppingListTypeAdapter extends TypeAdapter<ShoppingList> {
         }
         reader.endObject();
         if (map == null) return null;
-        return new ShoppingList(map);
+        return new ShoppingList(map, recipeManager);
     }
 }
diff --git a/src/test/java/mi/hdm/filesystem/JsonTest.java b/src/test/java/mi/hdm/filesystem/JsonTest.java
index 0876390..3ffbdc4 100644
--- a/src/test/java/mi/hdm/filesystem/JsonTest.java
+++ b/src/test/java/mi/hdm/filesystem/JsonTest.java
@@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 public class JsonTest {
     private final static IngredientManager ingredientManager = new IngredientManager();
     private final static CategoryManager categoryManager = new CategoryManager();
+    private final static RecipeManager recipeManager = new RecipeManager();
 
     @BeforeAll
     public static void setupAll() {
@@ -93,7 +94,7 @@ public class JsonTest {
     public void testShoppingListToJson() {
         ShoppingList expected = ValidObjectsPool.getShoppingList();
         Gson gson = new GsonBuilder()
-                .registerTypeAdapter(ShoppingList.class, new ShoppingListTypeAdapter())
+                .registerTypeAdapter(ShoppingList.class, new ShoppingListTypeAdapter(recipeManager))
                 .create();
         String json = gson.toJson(expected);
         System.out.println(json);
diff --git a/src/test/java/mi/hdm/recipes/ValidObjectsPool.java b/src/test/java/mi/hdm/recipes/ValidObjectsPool.java
index 7b5d3e4..252c597 100644
--- a/src/test/java/mi/hdm/recipes/ValidObjectsPool.java
+++ b/src/test/java/mi/hdm/recipes/ValidObjectsPool.java
@@ -19,7 +19,7 @@ public class ValidObjectsPool {
     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);
     private final static Map<LocalDate, String> recipeMap1 = Map.of(LocalDate.now(), recipeOne.getUniqueCode(), LocalDate.now().plusDays(1), recipeTwo.getUniqueCode());
     private final static MealPlan planOne = new MealPlan(recipeMap1, recipeManager);
-    private final static ShoppingList shoppingList = new ShoppingList(Map.of("r-192991", true, "r-89134", false));
+    private final static ShoppingList shoppingList = new ShoppingList(Map.of("r-192991", true, "r-89134", false), recipeManager);
 
     public static NutritionTable getValidNutritionTableOne() {
         return nutritionTableOne;
diff --git a/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java b/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java
index a44f3b3..808a42b 100644
--- a/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java
+++ b/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java
@@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class ShoppingListTest {
-    private final ShoppingList underTest = new ShoppingList();
+    private final ShoppingList underTest = new ShoppingList(ValidObjectsPool.getRecipeManager());
     private final static Ingredient ing1 = ValidObjectsPool.getValidIngredientOne();
     private final static Ingredient ing2 = ValidObjectsPool.getValidIngredientTwo();
 
@@ -21,6 +21,7 @@ class ShoppingListTest {
 
     @BeforeEach
     public void setup() {
+        ValidObjectsPool.getRecipeManager().clear();
         underTest.clear();
     }
 
@@ -48,6 +49,12 @@ class ShoppingListTest {
         assertEquals(expected, underTest.getList());
     }
 
+    @Test
+    public void shouldAllAllIngredientsRecursively() {
+        //TODO: write test for adding ingredients to shopping list recursively
+        throw new RuntimeException("not implemented");
+    }
+
     @Test
     public void testSetStatusByObject() {
         //given
-- 
GitLab