Merge branch 'fix/normalized-units' into 'main'
Units now get normalized Closes #76 See merge request cse1105/2025-2026/teams/csep-team-76!83
This commit is contained in:
commit
167a80c84a
2 changed files with 115 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ package commons;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
|
|
||||||
|
import java.text.DecimalFormat;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
@ -15,6 +16,13 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
|
||||||
private double amount;
|
private double amount;
|
||||||
private String unitSuffix;
|
private String unitSuffix;
|
||||||
|
|
||||||
|
private static final int tbspToPoundConvert = 32;
|
||||||
|
private static final int tbspToCupConvert = 16;
|
||||||
|
private static final int tbspToOunceConvert = 2;
|
||||||
|
private static final int OunceToPoundConvert = 16;
|
||||||
|
private static final DecimalFormat numberFormat = new DecimalFormat("#.00");
|
||||||
|
|
||||||
|
|
||||||
public double getAmount() {
|
public double getAmount() {
|
||||||
return amount;
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
@ -31,6 +39,8 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
|
||||||
this.unitSuffix = unitSuffix;
|
this.unitSuffix = unitSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public FormalIngredient(Long id, Ingredient ingredient, double amount, String unitSuffix) {
|
public FormalIngredient(Long id, Ingredient ingredient, double amount, String unitSuffix) {
|
||||||
// For testing
|
// For testing
|
||||||
super(id, ingredient);
|
super(id, ingredient);
|
||||||
|
|
@ -55,8 +65,54 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
|
||||||
}
|
}
|
||||||
return amount * unit.get().conversionFactor;
|
return amount * unit.get().conversionFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String normalisedUnit(){
|
||||||
|
Optional<Unit> 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() {
|
public String toString() {
|
||||||
return amount + unitSuffix + " of " + ingredient.name;
|
return normalisedUnit()+ " of " + ingredient.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
58
commons/src/test/java/commons/FormalIngredientTest.java
Normal file
58
commons/src/test/java/commons/FormalIngredientTest.java
Normal file
|
|
@ -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,"<NONE>");
|
||||||
|
FormalIngredient toSmall = new FormalIngredient(ingredient,10,"g");
|
||||||
|
|
||||||
|
assertEquals(informal.normalisedUnit(),"10.00<NONE>");
|
||||||
|
assertEquals(toSmall.normalisedUnit(),"10.00g");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue