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
Branches
No related tags found
No related merge requests found
......@@ -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);
}
......
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;
}
......
......@@ -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() {
......
......@@ -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);
......
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment