Skip to content
Snippets Groups Projects
Commit 5ad59511 authored by Blersch Lara's avatar Blersch Lara
Browse files

some testing for Ingredient

parent d066b6f4
No related branches found
No related tags found
No related merge requests found
...@@ -26,4 +26,13 @@ public class Ingredient implements RecipeComponent { ...@@ -26,4 +26,13 @@ public class Ingredient implements RecipeComponent {
public NutritionTable getNutritionTable() { public NutritionTable getNutritionTable() {
return nutritionTable; return nutritionTable;
} }
@Override
public boolean equals(Object o){
if (o instanceof Ingredient) {
Ingredient in = (Ingredient) o;
return this.name.equals(in.getName());
}
return false;
}
} }
package mi.hdm.recipes; package mi.hdm.recipes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -8,6 +11,7 @@ public class RecipeManager { ...@@ -8,6 +11,7 @@ public class RecipeManager {
private final Categories categories; private final Categories categories;
private final List<Ingredient> ingredients; private final List<Ingredient> ingredients;
private final NutritionCalculator nutritionCalculator; private final NutritionCalculator nutritionCalculator;
private static final Logger log = LogManager.getLogger(RecipeManager.class);
public RecipeManager(List<Recipe> recipes, Categories categories, List<Ingredient> ingredients, NutritionCalculator nutritionCalculator) { public RecipeManager(List<Recipe> recipes, Categories categories, List<Ingredient> ingredients, NutritionCalculator nutritionCalculator) {
this.recipes = recipes; this.recipes = recipes;
...@@ -43,8 +47,15 @@ public class RecipeManager { ...@@ -43,8 +47,15 @@ public class RecipeManager {
return new RecipeEditor(recipe); return new RecipeEditor(recipe);
} }
public void addIngredient(Ingredient ingredient) { public boolean addIngredient(Ingredient in) {
ingredients.add(ingredient); if (ingredients.contains(in)) {
log.error("Ingredient: {} not added because it already exists", in.getName());
return false;
} else {
log.info("Ingredient {} added successfully.", in.getName());
ingredients.add(in);
return true;
}
} }
public Ingredient deleteIngredient(int i) { public Ingredient deleteIngredient(int i) {
......
package mi.hdm.recipes;
import mi.hdm.exceptions.InvalidIngredientException;
import org.junit.Test;
import org.junit.Assert;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class IngredientTest {
List<Ingredient> ingredients = new ArrayList<Ingredient>();
NutritionTable tt = new NutritionTable(null);
//Nutrition.CALORIES, 300, Nutrition.CARBS, 40, Nutrition.FAT, 15, Nutrition.PROTEINS, 7, Nutrition.FIBERS, 3.2, Nutrition.SALT, 0.6
@Test public void testEquals () {
Ingredient i1 = new Ingredient(Measurement.GRAM, "Banana", tt);
Ingredient i2 = new Ingredient(Measurement.PIECE, "Banana", tt);
Ingredient i3 = new Ingredient(Measurement.GRAM, "Apple", tt);
assertEquals(i1, i2);
assertNotEquals(i1, i3);
assertNotEquals(i2, i3);
}
/*@Test public void shouldNotBeAbleToAddIdenticalName () {
ingredients.add(Measurement.GRAM, "Banana", tt);
ingredients.add(Measurement.PIECE, "Banana", tt);
Assert.assertEquals(1, ingredients.size());
}*/
}
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