diff --git a/commons/src/main/java/commons/FormalIngredient.java b/commons/src/main/java/commons/FormalIngredient.java index 2c5c055..75697a5 100644 --- a/commons/src/main/java/commons/FormalIngredient.java +++ b/commons/src/main/java/commons/FormalIngredient.java @@ -2,6 +2,7 @@ package commons; import jakarta.persistence.Entity; +import java.text.DecimalFormat; import java.util.Objects; import java.util.Optional; @@ -15,6 +16,13 @@ public class FormalIngredient extends RecipeIngredient implements Scalable unit = Unit.fromString(unitSuffix); + if (unit.isEmpty() || !unit.get().isFormal() || unit.get().conversionFactor <= 0) { + return numberFormat.format(amount) + unitSuffix; + } + + Unit currentUnit = unit.get(); + double baseAmount = amount * currentUnit.conversionFactor; + + switch (currentUnit){ + case GRAMME -> { + if(baseAmount >= Unit.TONNE.conversionFactor){ + return numberFormat.format(baseAmount /Unit.TONNE.conversionFactor) + Unit.TONNE.suffix; + }if(baseAmount >=Unit.KILOGRAMME.conversionFactor) { + return numberFormat.format(baseAmount / Unit.KILOGRAMME.conversionFactor) + Unit.KILOGRAMME.suffix; + } + } + + case MILLILITRE -> { + if (baseAmount >= Unit.LITRE.conversionFactor) { + return numberFormat.format(baseAmount /Unit.LITRE.conversionFactor) + Unit.LITRE.suffix; + } + } + + case TABLESPOON -> { + if(amount>=tbspToPoundConvert){ + return numberFormat.format(amount/tbspToPoundConvert) + Unit.POUND.suffix; + } + if(amount>=tbspToCupConvert){ + return numberFormat.format(amount /tbspToCupConvert) + Unit.CUP.suffix; + } + if(amount>=tbspToOunceConvert){ + return numberFormat.format(amount /tbspToOunceConvert) + Unit.OUNCE.suffix; + } + } + + case OUNCE -> { + if (baseAmount >= OunceToPoundConvert) { + return numberFormat.format(amount / OunceToPoundConvert) + Unit.POUND.suffix; + } + } + } + return numberFormat.format(amount) + currentUnit.suffix; + } + public String toString() { - return amount + unitSuffix + " of " + ingredient.name; + return normalisedUnit()+ " of " + ingredient.name; } @Override diff --git a/commons/src/test/java/commons/FormalIngredientTest.java b/commons/src/test/java/commons/FormalIngredientTest.java new file mode 100644 index 0000000..94edcd1 --- /dev/null +++ b/commons/src/test/java/commons/FormalIngredientTest.java @@ -0,0 +1,58 @@ +package commons; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FormalIngredientTest { + + @Test + void normaliseGramsToTest(){ + Ingredient ingredient = new Ingredient("Bread", 1, 2, 3); + FormalIngredient toKg = new FormalIngredient(ingredient,1_000,"g"); + FormalIngredient toTonne = new FormalIngredient(ingredient,1_000_000,"g"); + + + assertEquals(toKg.normalisedUnit(),"1.00kg"); + assertEquals(toTonne.normalisedUnit(),"1.00t"); + } + + @Test + void normaliseMillilitresToLitresTest(){ + Ingredient ingredient = new Ingredient("Bread", 1, 2, 3); + FormalIngredient toKg = new FormalIngredient(ingredient,1_000,"ml"); + + assertEquals(toKg.normalisedUnit(),"1.00l"); + } + + @Test + void normaliseOunceToPoundTest(){ + Ingredient ingredient = new Ingredient("Bread", 1, 2, 3); + FormalIngredient toPound = new FormalIngredient(ingredient,16,"oz"); + + assertEquals(toPound.normalisedUnit(),"1.00lb"); + } + + @Test + void normaliseTablespoonToTest(){ + Ingredient ingredient = new Ingredient("Bread", 1, 2, 3); + FormalIngredient toCup = new FormalIngredient(ingredient,16,"tbsp"); + FormalIngredient toPound = new FormalIngredient(ingredient,32,"tbsp"); + FormalIngredient toOunce = new FormalIngredient(ingredient,2,"tbsp"); + + assertEquals(toCup.normalisedUnit(),"1.00cup(s)"); + assertEquals(toPound.normalisedUnit(),"1.00lb"); + assertEquals(toOunce.normalisedUnit(),"1.00oz"); + } + + + @Test + void noNormaliseTest(){ + Ingredient ingredient = new Ingredient("Bread", 1, 2, 3); + FormalIngredient informal = new FormalIngredient(ingredient,10,""); + FormalIngredient toSmall = new FormalIngredient(ingredient,10,"g"); + + assertEquals(informal.normalisedUnit(),"10.00"); + assertEquals(toSmall.normalisedUnit(),"10.00g"); + } +} \ No newline at end of file