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

changed Categories to CategoryManager and added IngredientManager with...

changed Categories to CategoryManager and added IngredientManager with respective methods (partly moved from RecipeManager to IngredientManager)
parent dd74a0ad
Branches
No related tags found
No related merge requests found
......@@ -7,17 +7,17 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class Categories {
private static final Categories instance = new Categories();
private static final Logger log = LogManager.getLogger(Categories.class);
public class CategoryManager {
private static final CategoryManager instance = new CategoryManager();
private static final Logger log = LogManager.getLogger(CategoryManager.class);
private final List<Category> allCategories;
private Categories() {
private CategoryManager() {
allCategories = new ArrayList<>();
}
public static Categories getInstance() {
public static CategoryManager getInstance() {
return instance;
}
......
package mi.hdm.recipes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class IngredientManager {
private static final Logger log = LogManager.getLogger(IngredientManager.class);
private static final IngredientManager instance = new IngredientManager();
private final List<Ingredient> allIngredients;
private IngredientManager() {
allIngredients = new ArrayList<>();
}
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
* @return True if the ingredient has been added (meaning no equal ingredient existed before), otherwise false.
*/
public boolean addIngredient(Ingredient in) {
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;
}
}
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;
}
}
public Ingredient deleteIngredient(int i) {
return allIngredients.remove(i);
}
public boolean deleteIngredient(String name) {
Ingredient ingredient = getIngredientByName(name).orElseThrow();
return allIngredients.remove(ingredient);
}
public List<Ingredient> getAllIngredients() {
return allIngredients;
}
private Optional<Ingredient> getIngredientByName(String name) {
for (final Ingredient i: allIngredients) {
if (name.equals(i.getName())) {
return Optional.of(i);
}
}
return Optional.empty();
}
}
......@@ -8,12 +8,12 @@ import java.util.Optional;
public class RecipeManager {
private final List<Recipe> recipes;
private final Categories categories;
private final List<Ingredient> ingredients;
private final CategoryManager categories;
private final IngredientManager ingredients;
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, CategoryManager categories, IngredientManager ingredients, NutritionCalculator nutritionCalculator) {
this.recipes = recipes;
this.categories = categories;
this.ingredients = ingredients;
......@@ -47,29 +47,7 @@ public class RecipeManager {
return new RecipeEditor(recipe);
}
public boolean addIngredient(Ingredient in) {
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) {
return ingredients.remove(i);
}
public boolean deleteIngredient(String name) {
Ingredient ingredient = getIngredientByName(name).orElseThrow();
return ingredients.remove(ingredient);
}
public List<Ingredient> getIngredients() {
return ingredients;
}
private Optional<Recipe> getRecipeByName(String name) {
for (final Recipe r : recipes) {
......@@ -80,12 +58,5 @@ public class RecipeManager {
return Optional.empty();
}
private Optional<Ingredient> getIngredientByName(String name) {
for (final Ingredient i: ingredients) {
if (name.equals(i.getName())) {
return Optional.of(i);
}
}
return Optional.empty();
}
}
......@@ -2,13 +2,14 @@ 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;
import static org.junit.Assert.assertNotEquals;
public class CategoryTest {
Categories underTest = Categories.getInstance();
CategoryManager underTest = CategoryManager.getInstance();
@Test
public void testEquals() {
......
......@@ -11,7 +11,7 @@ import static org.junit.Assert.assertNotEquals;
public class IngredientTest {
RecipeManager recipeManager = new RecipeManager(null, null, new ArrayList<>(), null);
IngredientManager underTest = IngredientManager.getInstance();
NutritionTable tt = ValidObjectsPool.getValidNutritionTable();
@Test
......@@ -25,11 +25,11 @@ public class IngredientTest {
assertNotEquals(i2, i3);
}
@Test
public void shouldNotBeAbleToAddIdenticalName() {
recipeManager.addIngredient(new Ingredient(Measurement.GRAM, "Banana", tt));
recipeManager.addIngredient(new Ingredient(Measurement.PIECE, "Banana", tt));
@Test public void shouldNotBeAbleToAddIdenticalName () {
underTest.addIngredient(Measurement.GRAM, "Banana", tt);
underTest.addIngredient(Measurement.PIECE, "Banana", tt);
Assert.assertEquals(1, recipeManager.getIngredients().size());
Assert.assertEquals(1, underTest.getAllIngredients().size());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment