Skip to content
Snippets Groups Projects
Commit f85ffaf9 authored by Karsch Lukas's avatar Karsch Lukas
Browse files

added checks to Category constructor.

parent c9bb6ab9
No related branches found
No related tags found
No related merge requests found
...@@ -30,11 +30,11 @@ public class Categories { ...@@ -30,11 +30,11 @@ public class Categories {
public boolean addCategory(String name, int colourCode){ public boolean addCategory(String name, int colourCode){
Category c = new Category(name, colourCode); Category c = new Category(name, colourCode);
if(allCategories.contains(c)) { if(allCategories.contains(c)) {
log.debug("Category: {} not added", c.getName()); log.error("Category: {} not added because it already exists", c.getName());
return false; return false;
} else { } else {
allCategories.add(c); allCategories.add(c);
log.debug("Category {} added successfully.", c.getName()); log.info("Category {} added successfully.", c.getName());
return true; return true;
} }
} }
...@@ -44,7 +44,7 @@ public class Categories { ...@@ -44,7 +44,7 @@ public class Categories {
} }
public boolean deleteCategory(String name) { public boolean deleteCategory(String name) {
Category c = getCategoryByName(name).orElseThrow(); Category c = getCategoryByName(name).orElseThrow(() -> new RuntimeException("Category not found."));
return allCategories.remove(c); return allCategories.remove(c);
} }
......
package mi.hdm.recipes; package mi.hdm.recipes;
import mi.hdm.exceptions.InvalidCategoryException;
public class Category { public class Category {
private final String name; private final String name;
...@@ -7,6 +9,12 @@ public class Category { ...@@ -7,6 +9,12 @@ public class Category {
public Category (String name, int colourCode) { 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.name = name;
this.colourCode = colourCode; this.colourCode = colourCode;
} }
......
...@@ -8,7 +8,7 @@ import java.util.Set; ...@@ -8,7 +8,7 @@ import java.util.Set;
public class Recipe implements RecipeComponent { public class Recipe implements RecipeComponent {
private Map<RecipeComponent, Integer> ingredients; private Map<RecipeComponent, Integer> ingredients;
private String name; private String name;
private String desription; private String description;
private List<String> preparation; private List<String> preparation;
private Set<Category> categories; private Set<Category> categories;
private Integer preparationTimeMins; private Integer preparationTimeMins;
...@@ -18,14 +18,14 @@ public class Recipe implements RecipeComponent { ...@@ -18,14 +18,14 @@ public class Recipe implements RecipeComponent {
public Recipe( public Recipe(
Map<RecipeComponent,Integer> ingredients, Map<RecipeComponent,Integer> ingredients,
String name, String name,
String desription, String description,
List<String> preparation, List<String> preparation,
Set<Category> categories, Set<Category> categories,
Integer preparationTimeMins, Integer preparationTimeMins,
NutritionTable nutritionTable) { NutritionTable nutritionTable) {
this.ingredients = ingredients; this.ingredients = ingredients;
this.name = name; this.name = name;
this.desription = desription; this.description = description;
this.preparation = preparation; this.preparation = preparation;
this.categories = categories; this.categories = categories;
this.preparationTimeMins = preparationTimeMins; this.preparationTimeMins = preparationTimeMins;
...@@ -42,12 +42,12 @@ public class Recipe implements RecipeComponent { ...@@ -42,12 +42,12 @@ public class Recipe implements RecipeComponent {
return name; return name;
} }
public String getDesription() { public String getDescription() {
return desription; return description;
} }
public void setDesription(String desription) { public void setDescription(String description) {
this.desription = desription; this.description = description;
} }
public List<String> getPreparation() { public List<String> getPreparation() {
......
...@@ -23,16 +23,24 @@ public class RecipeManager { ...@@ -23,16 +23,24 @@ public class RecipeManager {
public Recipe deleteRecipe(int i) { public Recipe deleteRecipe(int i) {
return recipes.remove(i); return recipes.remove(i);
} }
public boolean deleteRecipe(String name) { public boolean deleteRecipe(String name) {
Recipe r = getRecipeByName(name).orElseThrow(); Recipe r = getRecipeByName(name).orElseThrow();
return recipes.remove(r); return recipes.remove(r);
} }
public void editRecipe (int i) { public RecipeEditor editRecipe (int i) {
Recipe recipe = recipes.get(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(); Recipe recipe = getRecipeByName(name).orElseThrow();
return new RecipeEditor(recipe);
}
public RecipeEditor editRecipe(Recipe recipe) {
return new RecipeEditor(recipe);
} }
public void addIngredient(Ingredient ingredient) { public void addIngredient(Ingredient ingredient) {
...@@ -42,6 +50,7 @@ public class RecipeManager { ...@@ -42,6 +50,7 @@ public class RecipeManager {
public Ingredient deleteIngredient(int i) { public Ingredient deleteIngredient(int i) {
return ingredients.remove(i); return ingredients.remove(i);
} }
public boolean deleteIngredient(String name) { public boolean deleteIngredient(String name) {
Ingredient ingredient = getIngredientByName(name).orElseThrow(); Ingredient ingredient = getIngredientByName(name).orElseThrow();
return ingredients.remove(ingredient); return ingredients.remove(ingredient);
......
...@@ -70,16 +70,16 @@ public class CategoryTest { ...@@ -70,16 +70,16 @@ public class CategoryTest {
@Test(expected = InvalidCategoryException.class) @Test(expected = InvalidCategoryException.class)
public void canNotAddCategoryWithNullName() { public void canNotAddCategoryWithNullName() {
Category emptyName = new Category(null, 0x1000); Category nullName = new Category(null, 0x1000);
} }
@Test(expected = InvalidCategoryException.class) @Test(expected = InvalidCategoryException.class)
public void canNotAddCategoryWithNegativeCode() { public void canNotAddCategoryWithNegativeCode() {
Category emptyName = new Category("foo", -100); Category negativeCode = new Category("foo", -100);
} }
@Test(expected = InvalidCategoryException.class) @Test(expected = InvalidCategoryException.class)
public void canNotAddCategoryWithCodeTooHigh() { public void canNotAddCategoryWithCodeTooHigh() {
Category emptyName = new Category("foo", Integer.MAX_VALUE); Category tooHighCode = new Category("foo", Integer.MAX_VALUE);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment