diff --git a/src/main/java/mi/hdm/recipes/Categories.java b/src/main/java/mi/hdm/recipes/Categories.java index 5b367898a6b327b5eb90250c6260d6b11f9deaa4..fc3fc0f4c71fee563235604f3095bd98328ad6de 100644 --- a/src/main/java/mi/hdm/recipes/Categories.java +++ b/src/main/java/mi/hdm/recipes/Categories.java @@ -30,11 +30,11 @@ public class Categories { public boolean addCategory(String name, int colourCode){ Category c = new Category(name, colourCode); if(allCategories.contains(c)) { - log.debug("Category: {} not added", c.getName()); + log.error("Category: {} not added because it already exists", c.getName()); return false; } else { allCategories.add(c); - log.debug("Category {} added successfully.", c.getName()); + log.info("Category {} added successfully.", c.getName()); return true; } } @@ -44,7 +44,7 @@ public class Categories { } public boolean deleteCategory(String name) { - Category c = getCategoryByName(name).orElseThrow(); + Category c = getCategoryByName(name).orElseThrow(() -> new RuntimeException("Category not found.")); return allCategories.remove(c); } diff --git a/src/main/java/mi/hdm/recipes/Category.java b/src/main/java/mi/hdm/recipes/Category.java index 9f35043695eb501675f913b13756f3691e6db219..95b40ff7ee8a2c8e3e68bd660ed4b7eeae08d6bc 100644 --- a/src/main/java/mi/hdm/recipes/Category.java +++ b/src/main/java/mi/hdm/recipes/Category.java @@ -1,5 +1,7 @@ package mi.hdm.recipes; +import mi.hdm.exceptions.InvalidCategoryException; + public class Category { private final String name; @@ -7,6 +9,12 @@ public class Category { public Category (String name, int colourCode) { + if(name == null || name.equals("")) { + throw new InvalidCategoryException("Can not add category with name " + name); + } + if(colourCode < 0 || colourCode > 0xFFFFFF) { + throw new InvalidCategoryException("Can not add category with color code " + colourCode); + } this.name = name; this.colourCode = colourCode; } diff --git a/src/main/java/mi/hdm/recipes/Recipe.java b/src/main/java/mi/hdm/recipes/Recipe.java index c0766df90455630a8c8f8b708553525914f10ea6..2c4f4e9f6fd8fab12d7cab16a709b8ab77bfb3e1 100644 --- a/src/main/java/mi/hdm/recipes/Recipe.java +++ b/src/main/java/mi/hdm/recipes/Recipe.java @@ -8,7 +8,7 @@ import java.util.Set; public class Recipe implements RecipeComponent { private Map<RecipeComponent, Integer> ingredients; private String name; - private String desription; + private String description; private List<String> preparation; private Set<Category> categories; private Integer preparationTimeMins; @@ -18,14 +18,14 @@ public class Recipe implements RecipeComponent { public Recipe( Map<RecipeComponent,Integer> ingredients, String name, - String desription, + String description, List<String> preparation, Set<Category> categories, Integer preparationTimeMins, NutritionTable nutritionTable) { this.ingredients = ingredients; this.name = name; - this.desription = desription; + this.description = description; this.preparation = preparation; this.categories = categories; this.preparationTimeMins = preparationTimeMins; @@ -42,12 +42,12 @@ public class Recipe implements RecipeComponent { return name; } - public String getDesription() { - return desription; + public String getDescription() { + return description; } - public void setDesription(String desription) { - this.desription = desription; + public void setDescription(String description) { + this.description = description; } public List<String> getPreparation() { diff --git a/src/main/java/mi/hdm/recipes/RecipeManager.java b/src/main/java/mi/hdm/recipes/RecipeManager.java index 7b817f3e3da08272fa4972ea18c27cb1bff135da..ed3801fafc60e3eed8245729193d4546c309b47c 100644 --- a/src/main/java/mi/hdm/recipes/RecipeManager.java +++ b/src/main/java/mi/hdm/recipes/RecipeManager.java @@ -23,16 +23,24 @@ public class RecipeManager { public Recipe deleteRecipe(int i) { return recipes.remove(i); } + public boolean deleteRecipe(String name) { Recipe r = getRecipeByName(name).orElseThrow(); return recipes.remove(r); } - public void editRecipe (int i) { + public RecipeEditor editRecipe (int i) { Recipe recipe = recipes.get(i); + return new RecipeEditor(recipe); } - public void editRecipe (String name) { + + public RecipeEditor editRecipe (String name) { Recipe recipe = getRecipeByName(name).orElseThrow(); + return new RecipeEditor(recipe); + } + + public RecipeEditor editRecipe(Recipe recipe) { + return new RecipeEditor(recipe); } public void addIngredient(Ingredient ingredient) { @@ -42,6 +50,7 @@ public class RecipeManager { public Ingredient deleteIngredient(int i) { return ingredients.remove(i); } + public boolean deleteIngredient(String name) { Ingredient ingredient = getIngredientByName(name).orElseThrow(); return ingredients.remove(ingredient); diff --git a/src/test/java/mi/hdm/recipes/CategoryTest.java b/src/test/java/mi/hdm/recipes/CategoryTest.java index 4e68832d31229c5c81e9cb44d9317856ab3cc3d3..db1242251e7aed58ea4c8bfcaaad9c0ed34df473 100644 --- a/src/test/java/mi/hdm/recipes/CategoryTest.java +++ b/src/test/java/mi/hdm/recipes/CategoryTest.java @@ -70,16 +70,16 @@ public class CategoryTest { @Test(expected = InvalidCategoryException.class) public void canNotAddCategoryWithNullName() { - Category emptyName = new Category(null, 0x1000); + Category nullName = new Category(null, 0x1000); } @Test(expected = InvalidCategoryException.class) public void canNotAddCategoryWithNegativeCode() { - Category emptyName = new Category("foo", -100); + Category negativeCode = new Category("foo", -100); } @Test(expected = InvalidCategoryException.class) public void canNotAddCategoryWithCodeTooHigh() { - Category emptyName = new Category("foo", Integer.MAX_VALUE); + Category tooHighCode = new Category("foo", Integer.MAX_VALUE); } }