From eb177b1b7ad13760dd528d52f70acf14c5e2ea1c Mon Sep 17 00:00:00 2001 From: HSoreide <sofie.scisly@gmail.com> Date: Sat, 15 Apr 2023 20:59:38 +0200 Subject: [PATCH] Refactor simplified class for ingredients at hand --- .../demo/controller/RecipeController.java | 9 +- .../demo/data/recipes/IngredientsAtHand.java | 32 +++---- .../recipes/IngredientsAtHandDetailed.java | 84 +++++++++++++++++++ .../idatt1002/demo/data/recipes/Recipe.java | 22 ++++- .../demo/data/recipes/RecipeIngredient.java | 2 +- .../demo/data/recipes/RecipeRegister.java | 1 + .../data/recipes/IngredientsAtHandTest.java | 19 +++-- .../demo/data/recipes/RecipeTest.java | 4 +- 8 files changed, 140 insertions(+), 33 deletions(-) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailed.java diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java index 0f753129..259b4805 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java @@ -10,7 +10,6 @@ import no.ntnu.idatt1002.demo.data.recipes.*; import java.io.IOException; import java.net.URL; -import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; @@ -32,10 +31,10 @@ public class RecipeController implements Initializable { List<Recipe> suggestedRecipes = null; IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); - ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 6, MeasuringUnit.PC)); - ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR)); - ingredientsAtHand.addIngredient(new Ingredient(FoodItem.POTATO, 4, MeasuringUnit.PC)); - ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 8, MeasuringUnit.DL)); + ingredientsAtHand.addIngredient(FoodItem.LEMON); + ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT); + ingredientsAtHand.addIngredient(FoodItem.POTATO); + ingredientsAtHand.addIngredient(FoodItem.MILK); diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java index 72318cd4..8c70a319 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java @@ -3,22 +3,16 @@ package no.ntnu.idatt1002.demo.data.recipes; import java.util.ArrayList; import java.util.List; -/** - * The IngredientsAtHand class contains a collection of Ingredient objects that are currently available to the user. - * Only one instance of each ingredient type may exist in the collection. If an ingredient is already present in the - * collection, the old will be replaced by the new. - * - * @author hannesofie - */ public class IngredientsAtHand { +//TODO: go through JavaDoc after refactoring. - private final List<Ingredient> ingredientsAtHand = new ArrayList<>(); + private final ArrayList<FoodItem> ingredientsAtHand = new ArrayList<>(); /** * The method returns the collection of ingredients at hand as an arraylist of ingredient objects. * @return The collection of ingredients at hand to the user. */ - public List<Ingredient> getIngredientsAtHand() { + public List<FoodItem> getIngredientsAtHand() { return ingredientsAtHand; } @@ -26,21 +20,26 @@ public class IngredientsAtHand { * The method takes in an ingredient object and adds it to the collection of ingredients at hand. * @param ingredient The ingredient object to add to the collection of ingredients at hand. */ - public void addIngredient(Ingredient ingredient) { + public void addIngredient(FoodItem ingredient) { this.ingredientsAtHand.add(ingredient); } - /** + //TODO: Unit tests and javadoc + public boolean atHand(FoodItem foodItem) { + return ingredientsAtHand.stream().anyMatch( (in) -> in.equals(foodItem)); + } + +/* *//** * Returns null if no ingredient of the requested type is found in the collection. * @param ingredientType What type of food the ingredient is. * @return The ingredient of the specified type found among the ingredients at hand, null otherwise. - */ + *//* public Ingredient getIngredient(FoodItem ingredientType) { if(ingredientType == null) return null; return this.getIngredientsAtHand().stream() .filter((ingredient) -> ingredient.getFoodType() == ingredientType) .findFirst().orElse(null); - } + }*/ /** * The method takes in three parameters. The method first checks if the Ingredient is at hand in the first place. @@ -50,7 +49,7 @@ public class IngredientsAtHand { * @param amount The amount of the ingredient. * @return True if Ingredient is successfully altered or added, false if not. */ - public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) { +/* public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) { //TODO: Consider handling exceptions differently. if(ingredientsAtHand.stream().anyMatch((ingredient) -> ingredient.getFoodType() == ingredientType)) { try { @@ -68,7 +67,7 @@ public class IngredientsAtHand { } } return true; - } + }*/ /** * The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of @@ -78,7 +77,8 @@ public class IngredientsAtHand { * @return True if the ingredient was found among the ingredients at hand and removed, false otherwise. */ public boolean removeIngredient(FoodItem ingredientType) { - return ingredientsAtHand.removeIf((ingredient) -> ingredient.getFoodType() == ingredientType); + return ingredientsAtHand.remove(ingredientType); } + } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailed.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailed.java new file mode 100644 index 00000000..9c297312 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailed.java @@ -0,0 +1,84 @@ +package no.ntnu.idatt1002.demo.data.recipes; + +import java.util.ArrayList; +import java.util.List; + +/** + * The IngredientsAtHandDetailed class contains a collection of Ingredient objects that are currently available to the user. + * Only one instance of each ingredient type may exist in the collection. If an ingredient is already present in the + * collection, the old will be replaced by the new. + * + * @author hannesofie + */ +public class IngredientsAtHandDetailed { + + private final List<Ingredient> ingredientsAtHand = new ArrayList<>(); + + /** + * The method returns the collection of ingredients at hand as an arraylist of ingredient objects. + * @return The collection of ingredients at hand to the user. + */ + public List<Ingredient> getIngredientsAtHand() { + return ingredientsAtHand; + } + + /** + * The method takes in an ingredient object and adds it to the collection of ingredients at hand. + * @param ingredient The ingredient object to add to the collection of ingredients at hand. + */ + public void addIngredient(Ingredient ingredient) { + this.ingredientsAtHand.add(ingredient); + } + + /** + * Returns null if no ingredient of the requested type is found in the collection. + * @param ingredientType What type of food the ingredient is. + * @return The ingredient of the specified type found among the ingredients at hand, null otherwise. + */ + public Ingredient getIngredient(FoodItem ingredientType) { + if(ingredientType == null) return null; + return this.getIngredientsAtHand().stream() + .filter((ingredient) -> ingredient.getFoodType() == ingredientType) + .findFirst().orElse(null); + } + + /** + * The method takes in three parameters. The method first checks if the Ingredient is at hand in the first place. + * If it is, the old amount and unit of this ingredient are replaced by the provided amount and unit if they + * differ. If not, the ingredient is left as is. If the ingredient is not in the collection, + * @param ingredientType What type of food the ingredient is. + * @param amount The amount of the ingredient. + * @return True if Ingredient is successfully altered or added, false if not. + */ + public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) { + //TODO: Consider handling exceptions differently. + if(ingredientsAtHand.stream().anyMatch((ingredient) -> ingredient.getFoodType() == ingredientType)) { + try { + getIngredient(ingredientType).setAmount(amount); + getIngredient(ingredientType).setUnit(unit); + + } catch (IllegalArgumentException e) { + return false; + } + } else { + try { + addIngredient(new Ingredient(ingredientType, amount, unit)); + } catch (IllegalArgumentException e) { + return false; + } + } + return true; + } + + /** + * The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of + * ingredients at hand if it exists and returns true. If no ingredient of the given type was found in the + * collection, false is returned. + * @param ingredientType What type of food the ingredient is. + * @return True if the ingredient was found among the ingredients at hand and removed, false otherwise. + */ + public boolean removeIngredient(FoodItem ingredientType) { + return ingredientsAtHand.removeIf((ingredient) -> ingredient.getFoodType() == ingredientType); + } + +} diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java index edfd6240..39e84d8a 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java @@ -96,19 +96,35 @@ public class Recipe { throw new IllegalArgumentException("The collection of ingredients at hand is empty."); } else { missingIngredients = 0; + int notMissing = (int) ingredientList.stream().filter((inRecipe) -> ingredientsAtHand.atHand(inRecipe.getFoodType())).count(); ingredientList.forEach((inRecipe) -> { + inRecipe.setAtHand(ingredientsAtHand.atHand(inRecipe.getFoodType())); + }); + + }; + + /* ingredientList.forEach((inRecipe) -> { ingredientsAtHand.getIngredientsAtHand().forEach((atHand) -> { - if(inRecipe.getFoodType() == atHand.getFoodType()) { + + System.out.println("----"); + System.out.println(inRecipe.getFoodType()); + System.out.println(atHand); + System.out.println("----"); + + if(inRecipe.getFoodType() == atHand) { inRecipe.setAtHand(true); + + } else { inRecipe.setAtHand(false); missingIngredients += 1; } }); - }); - } + });*/ } + + public int getMissingIngredients() { return missingIngredients; } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java index 36850d07..8d0cd9e6 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java @@ -4,7 +4,7 @@ package no.ntnu.idatt1002.demo.data.recipes; * The RecipeIngredient class is an extension of the Ingredient class used as part of recipes * that also stored the boolean value 'atHand'. This value can be set to true whenever the given ingredient, * being part of a recipe, is also at hand to the user. Ingredients that the user has available are stored in - * the IngredientsAtHand class. + * the IngredientsAtHandDetailed class. * * @author hannesofie */ diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java index 1ec98e95..566c1988 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java @@ -28,4 +28,5 @@ public class RecipeRegister { filter((recipe) -> recipe.getName().equals(name)) .findFirst().orElse(null); } + } diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java index 369d7c43..162ae41f 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java @@ -12,19 +12,25 @@ class IngredientsAtHandTest { @BeforeEach void beforeEach() { - ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC)); - ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 1.5f, MeasuringUnit.L)); - ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR)); - + ingredientsAtHand.addIngredient(FoodItem.LEMON); + ingredientsAtHand.addIngredient(FoodItem.MILK); + ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT); } - @Test +/* @Test @DisplayName("The getIngredient method returns the correct ingredient.") void getIngredientReturnsSuccessfully() { - assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC), + assertEquals(FoodItem.LEMON), ingredientsAtHand.getIngredient(FoodItem.LEMON)); + }*/ + @Test + @DisplayName("The atHand method returns true/false correctly.") + void atHandReturnsSuccessfully() { + assertTrue(ingredientsAtHand.atHand(FoodItem.LEMON)); + assertFalse(ingredientsAtHand.atHand(FoodItem.YELLOW_CHEESE)); } +/* @Test @DisplayName("The getIngredient method returns null if the ingredient is not in the collection.") @@ -73,6 +79,7 @@ class IngredientsAtHandTest { assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size()); } +*/ @Test @DisplayName("Ingredient is removed successfully and true is returned.") diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/RecipeTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/RecipeTest.java index 46d49557..da26a462 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/RecipeTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/RecipeTest.java @@ -93,8 +93,8 @@ class RecipeTest { @DisplayName("Updating ingredient status works as expected.") void updateIngredientStatus() { IngredientsAtHand inFridge = new IngredientsAtHand(); - inFridge.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR)); - inFridge.addIngredient(new Ingredient(FoodItem.YELLOW_CHEESE, 500, MeasuringUnit.GR)); + inFridge.addIngredient(FoodItem.MINCED_MEAT); + inFridge.addIngredient(FoodItem.YELLOW_CHEESE); recipe.getIngredientList().forEach((ingredient) -> assertFalse(ingredient.isAtHand())); -- GitLab