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 b1e6ca9ee4b9bb3bc0e380576e6c6ce14b1e2c8c..72318cd4f29e3228c6300bcd17bba64beb585805 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 @@ -1,5 +1,6 @@ package no.ntnu.idatt1002.demo.data.recipes; +import java.util.ArrayList; import java.util.List; /** @@ -11,11 +12,11 @@ import java.util.List; */ public class IngredientsAtHand { - private List<Ingredient> ingredientsAtHand; + private final List<Ingredient> ingredientsAtHand = new ArrayList<>(); /** * The method returns the collection of ingredients at hand as an arraylist of ingredient objects. - * @return + * @return The collection of ingredients at hand to the user. */ public List<Ingredient> getIngredientsAtHand() { return ingredientsAtHand; @@ -35,6 +36,7 @@ public class IngredientsAtHand { * @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); @@ -46,18 +48,26 @@ public class IngredientsAtHand { * 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 void 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 { getIngredient(ingredientType).setAmount(amount); getIngredient(ingredientType).setUnit(unit); + } catch (IllegalArgumentException e) { - e.printStackTrace(); + return false; } } else { - addIngredient(new Ingredient(ingredientType, amount, unit)); + try { + addIngredient(new Ingredient(ingredientType, amount, unit)); + } catch (IllegalArgumentException e) { + return false; + } } + return true; } /** 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 new file mode 100644 index 0000000000000000000000000000000000000000..369d7c43de2309fda39160a9cbe50b566075d800 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java @@ -0,0 +1,92 @@ +package no.ntnu.idatt1002.demo.data.recipes; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class IngredientsAtHandTest { + + IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); + + @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)); + + } + + @Test + @DisplayName("The getIngredient method returns the correct ingredient.") + void getIngredientReturnsSuccessfully() { + assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC), + ingredientsAtHand.getIngredient(FoodItem.LEMON)); + + } + + @Test + @DisplayName("The getIngredient method returns null if the ingredient is not in the collection.") + void getIngredientReturnsNullWhenNotFoundOrNull(){ + assertNull(ingredientsAtHand.getIngredient(FoodItem.ONION)); + assertNull(ingredientsAtHand.getIngredient(null)); + } + + + @Test + @DisplayName("Altering ingredient successfully and return true.") + void alterIngredientSuccessfully() { + assertNotEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR), + ingredientsAtHand.getIngredient(FoodItem.ONION)); + + assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ONION, 500, MeasuringUnit.GR)); + + assertEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR), + ingredientsAtHand.getIngredient(FoodItem.ONION)); + + } + + @Test + @DisplayName("Altering ingredient that does not yet exist in collection adds it and returns true.") + void alterNewIngredientAddsIt() { + int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); + assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ORANGE, 8, MeasuringUnit.PC)); + + assertEquals(ingredientsAtStart + 1, ingredientsAtHand.getIngredientsAtHand().size()); + } + + @Test + @DisplayName("Attempting to alter ingredient in illegal way does not alter the collection and returns false.") + void alterIngredientUnchangedForInvalidChange() { + assertFalse(ingredientsAtHand.alterIngredient(null, 350, MeasuringUnit.GR)); + assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC), + ingredientsAtHand.getIngredient(FoodItem.LEMON)); + } + + @Test + @DisplayName("Altering an ingredients without changing values, leaves no change on the collection and returns true.") + void alterNothingLeavesCollectionIntact() { + int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); + + assertTrue(ingredientsAtHand.alterIngredient(FoodItem.LEMON, 3, MeasuringUnit.PC)); + + assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size()); + } + + @Test + @DisplayName("Ingredient is removed successfully and true is returned.") + void removeIngredientSuccessfully() { + int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); + assertTrue(ingredientsAtHand.removeIngredient(FoodItem.LEMON)); + assertEquals(ingredientsAtStart-1, ingredientsAtHand.getIngredientsAtHand().size()); + } + + @Test + @DisplayName("Removing ingredient that is not in the collection, leaves it unchanged and returns false.") + void removeIngredientNotInCollection() { + + assertFalse(ingredientsAtHand.removeIngredient(FoodItem.SALSA_SAUCE)); + } + +} \ No newline at end of file