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

implemented algorithm to calculate nutrition tables for recipes from their...

implemented algorithm to calculate nutrition tables for recipes from their ingredients -> added tests.
implemented equals() method for NutritionTable -> added tests.

slight refactoring...
parent 2e50099f
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,7 @@ public class IngredientManager {
public static IngredientManager getInstance() {
return instance;
}
/**
* Adds an ingredient if there is no equal ingredient (no ingredient with the same name).
* @param in Ingredient that is to be added
......@@ -34,16 +35,10 @@ public class IngredientManager {
return true;
}
}
public boolean addIngredient(Measurement unit, String name, NutritionTable nutritionTable) {
Ingredient in = new Ingredient(unit, name, nutritionTable);
if (allIngredients.contains(in)) {
log.error("Ingredient: {} not added because it already exists", in.getName());
return false;
} else {
log.info("Ingredient {} added successfully.", in.getName());
allIngredients.add(in);
return true;
}
return addIngredient(in);
}
public Ingredient deleteIngredient(int i) {
......
......@@ -2,8 +2,37 @@ package mi.hdm.recipes;
import mi.hdm.mealPlan.MealPlan;
import java.util.Map;
public class NutritionCalculator {
public static NutritionTable calculateNutritionTable(Recipe recipe) { return null; }
/**
* Method to calculate the nutrition table for a recipe out of its ingredients.
*
* @return the nutrition table for this recipe based on its ingredients. All nutrition values are added up.
*/
public static NutritionTable calculateNutritionTable(Map<RecipeComponent, Integer> ingredients) {
double totalCals = 0,
totalCarbs = 0,
totalFats = 0,
totalProteins = 0,
totalFibers = 0,
totalSalt = 0;
for (RecipeComponent entry : ingredients.keySet()) {
final Map<Nutrition, Double> nutritionTable = entry.getNutritionTable().getTable();
totalCals += nutritionTable.get(Nutrition.CALORIES) * ingredients.get(entry) / 100;
totalCarbs += nutritionTable.get(Nutrition.CARBS) * ingredients.get(entry) / 100;
totalFats += nutritionTable.get(Nutrition.FAT) * ingredients.get(entry) / 100;
totalProteins += nutritionTable.get(Nutrition.PROTEINS) * ingredients.get(entry) / 100;
totalFibers += nutritionTable.get(Nutrition.FIBERS) * ingredients.get(entry) / 100;
totalSalt += nutritionTable.get(Nutrition.SALT) * ingredients.get(entry) / 100;
}
return new NutritionTable(totalCals, totalCarbs, totalFats, totalProteins, totalFibers, totalSalt);
}
public static NutritionTable calculateNutritionTable(MealPlan mealPlan) { return null; }
public static NutritionTable calculateNutritionTable(MealPlan mealPlan) {
return null;
}
}
......@@ -9,7 +9,8 @@ public class NutritionTable {
private final Map<Nutrition, Double> table;
/**
* Creates a new nutrition table that can be used in an ingredient. Expects values for all the nutrition values in order: Calories, Carbs, Fats, Proteins, Fibers, Salt
* Creates a new nutrition table that can be used in an ingredient. Expects values for all the nutrition values in order: Calories, Carbs, Fats, Proteins, Fibers, Salt.
* All values supplied should be in relation to 100 grams or 1 piece.
*/
public NutritionTable(double calories, double carbs, double fats, double proteins, double fibers, double salt) {
this.table = new HashMap<>();
......@@ -32,4 +33,20 @@ public class NutritionTable {
public NutritionTable get() {
return this;
}
@Override
public boolean equals(Object o) {
if (o instanceof NutritionTable) {
NutritionTable n = (NutritionTable) o;
double delta = 0.03;
return n.getTable().get(Nutrition.CALORIES) - table.get(Nutrition.CALORIES) < delta
&& n.getTable().get(Nutrition.CARBS) - table.get(Nutrition.CARBS) < delta
&& n.getTable().get(Nutrition.FAT) - table.get(Nutrition.FAT) < delta
&& n.getTable().get(Nutrition.PROTEINS) - table.get(Nutrition.PROTEINS) < delta
&& n.getTable().get(Nutrition.FIBERS) - table.get(Nutrition.FIBERS) < delta
&& n.getTable().get(Nutrition.SALT) - table.get(Nutrition.SALT) < delta;
}
return false;
}
}
......@@ -5,35 +5,33 @@ import mi.hdm.exceptions.InvalidRecipeException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Recipe implements RecipeComponent {
private Map<RecipeComponent, Integer> ingredients;
private String name;
private String description;
private List<String> preparation;
private Set<Category> categories;
private List<Category> categories;
private Integer preparationTimeMins;
private LocalDateTime created;
private NutritionTable nutritionTable;
private final LocalDateTime creationTime;
public Recipe(
Map<RecipeComponent,Integer> ingredients,
Map<RecipeComponent, Integer> ingredients,
String name,
String description,
List<String> preparation,
Set<Category> categories,
Integer preparationTimeMins,
NutritionTable nutritionTable) {
List<Category> categories,
Integer preparationTimeMins) {
this.ingredients = ingredients;
this.name = name;
this.description = description;
this.preparation = preparation;
this.categories = categories;
this.preparationTimeMins = preparationTimeMins;
this.nutritionTable = nutritionTable;
nutritionTable = NutritionCalculator.calculateNutritionTable(ingredients);
created = LocalDateTime.now();
creationTime = LocalDateTime.now();
}
public void setName(String name) {
......@@ -63,27 +61,31 @@ public class Recipe implements RecipeComponent {
this.preparation = preparation;
}
public Set<Category> getCategories() {
public List<Category> getCategories() {
return categories;
}
public void setCategories(Set<Category> categories) {
public void setCategories(List<Category> categories) {
this.categories = categories;
}
public void addCategory(Category category) {
categories.add(category);
}
public Integer getPreparationTimeMins() {
return preparationTimeMins;
}
public void setPreparationTimeMins(int preparationTimeMins) {
if (preparationTimeMins < 0) {
throw new InvalidRecipeException("PreparationTime must be a positive value");
}
if (preparationTimeMins < 0) {
throw new InvalidRecipeException("PreparationTime must be a positive value");
}
this.preparationTimeMins = preparationTimeMins;
}
public LocalDateTime getCreated() {
return created;
public LocalDateTime getCreationTime() {
return creationTime;
}
public void setNutritionTable(NutritionTable nutritionTable) {
......
......@@ -2,7 +2,6 @@ package mi.hdm.recipes;
import mi.hdm.exceptions.InvalidCategoryException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
......@@ -12,7 +11,7 @@ public class CategoryTest {
CategoryManager underTest = CategoryManager.getInstance();
@Test
public void testEquals() {
public void testEqualCategories() {
underTest.clearCategories();
Category c1 = new Category("Abend", 0x00FF00);
Category c2 = new Category("LEcker", 0x13);
......
......@@ -3,19 +3,16 @@ package mi.hdm.recipes;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class IngredientTest {
IngredientManager underTest = IngredientManager.getInstance();
NutritionTable tt = ValidObjectsPool.getValidNutritionTable();
NutritionTable tt = ValidObjectsPool.getValidNutritionTableOne();
@Test
public void testEquals() {
public void testEqualIngredients() {
Ingredient i1 = new Ingredient(Measurement.GRAM, "Banana", tt);
Ingredient i2 = new Ingredient(Measurement.PIECE, "Banana", tt);
Ingredient i3 = new Ingredient(Measurement.GRAM, "Apple", tt);
......
package mi.hdm.recipes;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class NutritionCalculatorTest {
List<Ingredient> ingredients = ValidObjectsPool.getValidIngredientList();
Map<RecipeComponent, Integer> recipeMap;
@Before
public void setUp() {
recipeMap = new HashMap<>();
ingredients.forEach(ingredient -> recipeMap.put(ingredient, 100));
}
@Test
public void testCalculateNutritionTableFromIngredients() {
//given
NutritionTable expected = new NutritionTable(363.1, 42.4, 9.6, 4.3, 2.4, 1.2);
//when
NutritionTable actual = NutritionCalculator.calculateNutritionTable(recipeMap);
//expect
assertEquals(expected, actual);
}
@Test
public void testCalculateNutritionTableFromRecipes() {
//TODO
assertTrue(true);
}
}
......@@ -7,6 +7,7 @@ import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class NutritionTableTest {
......@@ -40,4 +41,14 @@ public class NutritionTableTest {
assertEquals(expected, underTest.getTable());
}
@Test
public void testEqualNutritionTable() {
NutritionTable underTestOne = ValidObjectsPool.getValidNutritionTableOne();
NutritionTable underTestTwo = ValidObjectsPool.getValidNutritionTableTwo();
NutritionTable underTestEqualToOne = new NutritionTable(100.0, 17.2, 9.4, 4.3, 2.4, 0.4);
assertNotEquals(underTestEqualToOne, underTestTwo);
assertEquals(underTestEqualToOne, underTestEqualToOne);
}
}
\ No newline at end of file
package mi.hdm.recipes;
import java.util.List;
public class ValidObjectsPool {
private final static NutritionTable nutritionTable = new NutritionTable(100.0, 17.2, 9.4, 4.3, 2.4, 0.4);
private final static Category categoryOne = new Category("Frühstück", 0xFF0000);
private final static Category categoryTwo = new Category("Abendessen", 0x00FF00);
private final static NutritionTable nutritionTableOne = new NutritionTable(100.0, 17.2, 9.4, 4.3, 2.4, 0.4);
private final static NutritionTable nutritionTableTwo = new NutritionTable(263.1, 25.2, 0.2, 0.0, 0.0, 0.8);
private final static Ingredient ingredientOne = new Ingredient(Measurement.GRAM, "Zucker", nutritionTableOne);
private final static Ingredient ingredientTwo = new Ingredient(Measurement.PIECE, "Apple", nutritionTableTwo);
public static NutritionTable getValidNutritionTable() {
return nutritionTable;
public static NutritionTable getValidNutritionTableOne() {
return nutritionTableOne;
}
public static NutritionTable getValidNutritionTableTwo() {
return nutritionTableTwo;
}
public static Ingredient getValidIngredient() {
return ingredientOne;
}
public static List<Ingredient> getValidIngredientList() {
return List.of(ingredientOne, ingredientTwo);
}
public static Category getValidCategory() {
return categoryOne;
}
public static List<Category> getValidCategoryList() {
return List.of(categoryOne, categoryTwo);
}
}
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