chore: etc
This commit is contained in:
parent
ecccebe7c5
commit
895aecba3d
19 changed files with 404 additions and 55 deletions
|
|
@ -76,4 +76,9 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
|
|||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), amount, unitSuffix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double kcal() {
|
||||
return ingredient.kcalPer100g() * amountInBaseUnit();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ import java.util.Objects;
|
|||
// TABLE named recipes
|
||||
@Entity
|
||||
@Table(name = "recipes")
|
||||
public class Recipe {
|
||||
public class Recipe implements Scalable<Recipe> {
|
||||
|
||||
// PRIMARY Key, unique id for recipe, generated automatically.
|
||||
@Id
|
||||
|
|
@ -191,5 +191,16 @@ public class Recipe {
|
|||
", preparationSteps=" + preparationSteps +
|
||||
'}';
|
||||
}
|
||||
public double kcal() {
|
||||
return this.ingredients.stream().mapToDouble(RecipeIngredient::kcal).sum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Recipe scaleBy(double factor) {
|
||||
return new Recipe(id, name, locale, ingredients.stream().map(ri -> switch (ri) {
|
||||
case FormalIngredient f -> f.scaleBy(factor);
|
||||
case VagueIngredient v -> v;
|
||||
default -> throw new IllegalArgumentException("Unexpected child class of RecipeIngredient!");
|
||||
}).toList(), preparationSteps);
|
||||
}
|
||||
}
|
||||
|
|
@ -92,4 +92,5 @@ public abstract class RecipeIngredient {
|
|||
public int hashCode() {
|
||||
return Objects.hash(id, ingredient);
|
||||
}
|
||||
public abstract double kcal();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package commons;
|
||||
|
||||
public interface Scalable<IngredientType extends RecipeIngredient> {
|
||||
default IngredientType scaleBy(int numPortions) {
|
||||
public interface Scalable<T> {
|
||||
default T scaleBy(int numPortions) {
|
||||
return scaleBy((double) numPortions);
|
||||
};
|
||||
IngredientType scaleBy(double factor);
|
||||
T scaleBy(double factor);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,4 +50,9 @@ public class VagueIngredient extends RecipeIngredient {
|
|||
public int hashCode() {
|
||||
return Objects.hashCode(description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double kcal() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue