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 0f753129b7c0c95aaba37d32ee9c498842e0b3ed..259b48055e64a2fc33929e02132b43a73e321a33 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 72318cd4f29e3228c6300bcd17bba64beb585805..8c70a3190d692cb18a758da424551b582f116a6c 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 0000000000000000000000000000000000000000..9c297312c0d7a4f3b5a87e2f3c6093dcd58f9d48 --- /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 edfd62401f0189aea59f8885a6232e94950b302a..39e84d8a1934d82e259762eba24639560f2d8078 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 36850d072b18816f5bdd231400eedef305e8cf84..8d0cd9e668d99d575673554f657000f9c347fa11 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 1ec98e95879698ac7e6088f46f18e54a2bf67062..566c19884ae66b1e5ddad70b4d72a1d6c6d33a77 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 369d7c43de2309fda39160a9cbe50b566075d800..162ae41fcea70715ff29cfe068cd02708a7cf660 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 46d4955720b089e6391f3e5f0aaa5f70fb014b06..da26a462859167ba64d190be288f6b1443ccefee 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()));