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/FileHandler.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..2cd7667d93a359dfcdae639a5398acde94b4ff8f --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java @@ -0,0 +1,180 @@ +package no.ntnu.idatt1002.demo.data.recipes; + +import java.io.*; +import java.util.List; +import java.util.Scanner; + +//TODO: Refactor to make use of generics, which requires +// refactoring of more classes and write mor eunit tests and JavaDoc. +public class FileHandler { + + private static final String fileType = ".register"; + private static final String registerName = "RecipeRegister"; + private static final String filePath = "src/main/resources/recipes/"; + + + public static void writeRegister(RecipeRegister recipeRegister, String title) { + if (recipeRegister == null) { + throw new IllegalArgumentException("Only a valid register object can be written to file."); + } + + try (FileWriter fileWriter = new FileWriter(filePath + title + fileType);) { + recipeRegister.getRecipes().forEach((recipe) -> + { + try { + fileWriter.write(formatRecipe(recipe).toString()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + ); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static StringBuilder formatRecipe(Recipe recipe) { + StringBuilder sb = new StringBuilder(); + sb.append("# ") + .append(recipe.getName()) + .append("\n") + .append(formatIngredientList(recipe.getIngredientList())) + .append("\n") + .append(recipe.getInstructions()) + .append("\n\n"); + return sb; + } + + public static StringBuilder formatIngredientList(List<RecipeIngredient> ingredientList) { + StringBuilder sb = new StringBuilder(); + + ingredientList.forEach((ingredient) -> { + sb.append("- ") + .append(ingredient.getFoodType()) + .append(" | ") + .append(ingredient.getAmount()) + .append(" | ") + .append(ingredient.getUnit()) + .append("\n"); + }); + + return sb; + } + + + //TODO: Midlertidig for testing. + public static void readToTerminal(String title) throws FileNotFoundException { + File file = new File(filePath + title + fileType); + Scanner sc = new Scanner(file); + while (sc.hasNext()) { + System.out.println(sc.nextLine()); + } + } + + + public static RecipeRegister readRecipeRegister(String title) throws FileNotFoundException { + File file = new File(filePath + title + fileType); + + RecipeRegister register = new RecipeRegister(); + + try (Scanner sc = new Scanner(file)) { + sc.useDelimiter("#"); + String line; + + while (sc.hasNext()) { + line = sc.next(); + if (!line.isBlank()) { + register.addRecipe(readRecipe(line)); + } + } + } catch (FileNotFoundException e) { + System.out.println("The file was not found."); + return null; + } + + return register; + } + + public static Recipe readRecipe(String readRecipe) { + + Scanner sc = new Scanner(readRecipe); + + Recipe recipe; + String instructions = "None"; + String recipeName = sc.nextLine().strip(); + StringBuilder sb = new StringBuilder(); + + + String line; + recipe = new Recipe(recipeName, instructions); + + while (sc.hasNextLine()) { + line = sc.nextLine(); + + if (line.startsWith("-")) { + String[] ingredientParts = line.split("\\|"); + + FoodItem ingredientType = FoodItem.valueOf(ingredientParts[0].replaceFirst("-", "").strip()); + double ingredientAmount = Double.parseDouble(ingredientParts[1].strip()); + MeasuringUnit ingredientUnit = MeasuringUnit.valueOf(ingredientParts[2].strip()); + + recipe.addIngredient(ingredientType, ingredientAmount, ingredientUnit); + + } else { + if (!line.isBlank()) { + sb.append(line).append("\n"); + } + } + } + recipe.setInstructions(String.valueOf(sb)); + + return recipe; + } + + + // ================ Ingredients at hand =============================== + + public static void writeIngredientsAtHand(IngredientsAtHand ingredientsAtHand, String title) throws IOException { + if (ingredientsAtHand == null) { + throw new IllegalArgumentException("Only a valid register object can be written to file."); + } + + StringBuilder sb = new StringBuilder(); + + try (FileWriter fileWriter = new FileWriter(filePath + title + fileType);) { + ingredientsAtHand.getIngredientsAtHand().forEach((ingredient) -> { + sb.append(ingredient).append("\n"); + + }); + try { + fileWriter.write(String.valueOf(sb)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + + public static IngredientsAtHand readIngredientsAtHand(String title) { + File file = new File(filePath + title + fileType); + + IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); + + try (Scanner sc = new Scanner(file)) { + String line; + + while (sc.hasNext()) { + line = sc.next(); + if (!line.isBlank()) { + ingredientsAtHand.addIngredient(FoodItem.valueOf(line)); + } + } + } catch (FileNotFoundException e) { + System.out.println("The file was not found."); + return null; + } + + return ingredientsAtHand; + } +} \ No newline at end of file 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..d3708f3f461a9814539f6db281a65f6d503791d3 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,49 +20,15 @@ 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); } - /** - * 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); + //TODO: Unit tests and javadoc + public boolean atHand(FoodItem foodItem) { + return ingredientsAtHand.stream().anyMatch( (in) -> in.equals(foodItem)); } - /** - * 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 @@ -78,7 +38,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/main/resources/recipes/testReadRegisterFromFile.register b/src/main/resources/recipes/testReadRegisterFromFile.register new file mode 100644 index 0000000000000000000000000000000000000000..1a66a2003461c6abd9a64856fa4bad2bf45c2195 --- /dev/null +++ b/src/main/resources/recipes/testReadRegisterFromFile.register @@ -0,0 +1,23 @@ +# Meat, cheese and potatoes +- MINCED_MEAT | 500.0 | GR +- POTATO | 750.0 | GR +- YELLOW_CHEESE | 2.0 | DL + +Instructions must be written continuously in one line. No line shifts. +Testing another line of instructions! + +# Meat, cheese and lemons +- MINCED_MEAT | 500.0 | GR +- LEMON | 750.0 | GR +- YELLOW_CHEESE | 2.0 | DL + +Instructions + + + +# Another recipe added for testing +- ONION | 5.0 | PC +- LEMON | 750.0 | GR +- POTATO | 10.0 | PC + +Instructions for another dish. \ No newline at end of file diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9e3ad51f7531c74460f0738950e9771d908c1b71 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java @@ -0,0 +1,89 @@ +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 java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.*; + +class FileHandlerTest { + + RecipeRegister recipeRegister = new RecipeRegister(); + IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); + + Recipe recipe1; + Recipe recipe2; + + @BeforeEach + void beforeEach() { + recipe1 = new Recipe("Meat, cheese and potatoes", "Instructions"); + + recipe1.addIngredient(FoodItem.MINCED_MEAT, 500, MeasuringUnit.GR); + recipe1.addIngredient(FoodItem.POTATO, 750, MeasuringUnit.GR); + recipe1.addIngredient(FoodItem.YELLOW_CHEESE, 2, MeasuringUnit.DL); + + recipe2 = new Recipe("Meat, cheese and lemons", "Instructions"); + + recipe2.addIngredient(FoodItem.MINCED_MEAT, 500, MeasuringUnit.GR); + recipe2.addIngredient(FoodItem.LEMON, 750, MeasuringUnit.GR); + recipe2.addIngredient(FoodItem.YELLOW_CHEESE, 2, MeasuringUnit.DL); + + recipeRegister.addRecipe(recipe1); + recipeRegister.addRecipe(recipe2); + + // For AtHand: + + ingredientsAtHand.addIngredient(FoodItem.POTATO); + ingredientsAtHand.addIngredient(FoodItem.MILK); + ingredientsAtHand.addIngredient(FoodItem.LEMON); + ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT); + + + } + + + @Test + @DisplayName("Write recipe register correctly to file as text.") + void writeRecipeRegisterToFile() throws FileNotFoundException { + assertAll(() -> FileHandler.writeRegister(recipeRegister, "RecipeRegister")); + FileHandler.readToTerminal("RecipeRegister"); + } + + @Test + @DisplayName("Read recipe register correctly from file to object.") + void readRecipeRegisterFromFile() throws FileNotFoundException { + // Read test register into a register object. + // Write the object and print to terminal. + + assertAll(() -> FileHandler.readRecipeRegister("testReadRegisterFromFile")); + + RecipeRegister registerReadFromTestFile = FileHandler.readRecipeRegister("testReadRegisterFromFile"); + + // Write the register object to file and read to terminal: + FileHandler.writeRegister(registerReadFromTestFile, "ReadAndWrittenRegister"); + + FileHandler.readToTerminal("ReadAndWrittenRegister"); + + // Another iteration to spot accumulating spaces or new lines. + RecipeRegister secondRegister = FileHandler.readRecipeRegister("ReadAndWrittenRegister"); + FileHandler.writeRegister(secondRegister, "secondGeneration"); + + } + + + // Ingredients at hand nesting + + + @Test + @DisplayName("Write ingredients at hand to file.") + void writeIngredientsAtHandToFile() throws FileNotFoundException { + assertAll(() -> FileHandler.writeIngredientsAtHand(ingredientsAtHand, "AtHandRegister")); + FileHandler.readToTerminal("AtHandRegister"); + } + + +} \ No newline at end of file diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailedTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailedTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e1e9ad7486c283dbc57cc295afb4210fcb6a57f5 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailedTest.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 IngredientsAtHandDetailedTest { + + IngredientsAtHandDetailed ingredientsAtHand = new IngredientsAtHandDetailed(); + + @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 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..39a009adcffad307d858ff5d5aa4432d884e87cd 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,66 +12,17 @@ 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 - @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()); + @DisplayName("The atHand method returns true/false correctly.") + void atHandReturnsSuccessfully() { + assertTrue(ingredientsAtHand.atHand(FoodItem.LEMON)); + assertFalse(ingredientsAtHand.atHand(FoodItem.YELLOW_CHEESE)); } @Test 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()));