feat(commons): create new Scalable<T extends RecipeIngredient> interface
Scalable<T extends Recipe> supplies two scaleBy() functions that implementors should create. This allows for custom handling if more types of ingredients are handled in the future.
This commit is contained in:
parent
b77f4aee76
commit
2d1df8fc11
4 changed files with 48 additions and 1 deletions
|
|
@ -10,7 +10,7 @@ import java.util.Optional;
|
|||
* @see RecipeIngredient
|
||||
*/
|
||||
@Entity
|
||||
public class FormalIngredient extends RecipeIngredient {
|
||||
public class FormalIngredient extends RecipeIngredient implements Scalable<FormalIngredient> {
|
||||
private double amount;
|
||||
private String unitSuffix;
|
||||
|
||||
|
|
@ -50,4 +50,14 @@ public class FormalIngredient extends RecipeIngredient {
|
|||
public String toString() {
|
||||
return amount + unitSuffix + " of " + ingredient.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FormalIngredient scaleBy(int numPortions) {
|
||||
return new FormalIngredient(getIngredient(), amount * numPortions, unitSuffix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FormalIngredient scaleBy(double factor) {
|
||||
return new FormalIngredient(getIngredient(), amount, unitSuffix);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,4 +57,20 @@ public abstract class RecipeIngredient {
|
|||
//store it in the field
|
||||
this.ingredient = ingredient;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setIngredient(Ingredient ingredient) {
|
||||
this.ingredient = ingredient;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Ingredient getIngredient() {
|
||||
return ingredient;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
6
commons/src/main/java/commons/Scalable.java
Normal file
6
commons/src/main/java/commons/Scalable.java
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package commons;
|
||||
|
||||
public interface Scalable<IngredientType extends RecipeIngredient> {
|
||||
IngredientType scaleBy(int numPortions);
|
||||
IngredientType scaleBy(double factor);
|
||||
}
|
||||
|
|
@ -16,6 +16,21 @@ public enum Unit {
|
|||
KILOGRAMME("kg", true, 1_000.0 ),
|
||||
TONNE("t", true, 1_000_000.0),
|
||||
|
||||
// TODO Consider more fine-tuned volume unit definitions
|
||||
// TODO Use density-based calculations?
|
||||
/*
|
||||
Volume units are used with the assumption that 1L of ingredient = 1kg.
|
||||
*/
|
||||
LITRE("l", true, 1_000.0),
|
||||
MILLILITRE("ml", true, 1.0),
|
||||
|
||||
// We love our American friends!!
|
||||
// Source:
|
||||
// https://whatscookingamerica.net/equiv.htm
|
||||
TABLESPOON("tbsp", true, 14.0),
|
||||
OUNCE("oz", true, 28.35),
|
||||
POUND("lb", true, 453.6),
|
||||
CUP("cup", true, 225),
|
||||
// A special informal unit
|
||||
INFORMAL("<NONE>", false, 0.0);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue