Newer
Older
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 final List<Ingredient> allIngredients;

Karsch Lukas
committed
public IngredientManager() {
allIngredients = new ArrayList<>();
}

Karsch Lukas
committed
public IngredientManager(List<Ingredient> ingredients) {
allIngredients = ingredients;
}

Karsch Lukas
committed
/**
* Adds an ingredient if there is no equal ingredient (no ingredient with the same name).

Karsch Lukas
committed
*
* @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 void addIngredient(Ingredient in) {
Blersch Lara
committed
if (getIngredientByName(in.getName()).isPresent()) {
log.error("Ingredient {} not added because it already exists.", in.getName());
throw new InvalidIngredientException("Ingredient already exists.");
} else {
log.info("Ingredient {} added successfully.", in.getName());
allIngredients.add(in);
}
}

Karsch Lukas
committed
public void addIngredient(Measurement unit, String name, NutritionTable nutritionTable) {
Ingredient in = new Ingredient(unit, name, nutritionTable);
addIngredient(in);
}
public Ingredient deleteIngredient(int i) {

Karsch Lukas
committed
log.info("Ingredient {} deleted successfully.", allIngredients.get(i).getName());
return allIngredients.remove(i);
}
public void deleteIngredient(String name) {
Ingredient ingredient = getIngredientByName(name).orElseThrow(() -> new InvalidIngredientException("No ingredient with name " + name));

Karsch Lukas
committed
log.info("Ingredient {} deleted successfully.", name);
allIngredients.remove(ingredient);
}
log.info("List allIngredients cleared successfully.");
public List<Ingredient> getAllIngredients() {
return allIngredients;
}
private Optional<Ingredient> getIngredientByName(String name) {
if (name.equals(i.getName())) {
return Optional.of(i);
}
}
return Optional.empty();
}
}