backend stuff for ingredients and nutritions AND FIXED MAGIC NUMBERS
This commit is contained in:
parent
88dc43613f
commit
78e7f90024
3 changed files with 69 additions and 32 deletions
|
|
@ -3,25 +3,39 @@ package commons;
|
|||
//what is a record class and why is it recommended
|
||||
public final class Unit {
|
||||
|
||||
|
||||
//stupid magic numbers
|
||||
private static final double GRAMS = 1.0;
|
||||
private static final double KILOGRAMS = 1000.0;
|
||||
private static final double MILLILITERS = 1.0;
|
||||
private static final double LITERS = 1000.0;
|
||||
private static final double TABLESPOONS = 15.0;
|
||||
private static final double TEASPOONS = 5.0;
|
||||
private static final double CUPS = 240.0;
|
||||
private static final double PIECES = 1.0;
|
||||
|
||||
private static final double NoMeaningfulValue = 0.0;
|
||||
|
||||
|
||||
//formal units
|
||||
//weight units
|
||||
public static final Unit GRAM = new Unit("GRAM", true, 1.0);
|
||||
public static final Unit KILOGRAM = new Unit("KILOGRAM", true, 1000.0);
|
||||
public static final Unit GRAM = new Unit("GRAM", true, GRAMS);
|
||||
public static final Unit KILOGRAM = new Unit("KILOGRAM", true, KILOGRAMS );
|
||||
|
||||
//volume units
|
||||
public static final Unit MILLILITER = new Unit("MILLILITER",true, 1.0);
|
||||
public static final Unit LITER = new Unit("LITER", true, 1000.0);
|
||||
public static final Unit TABLESPOON = new Unit("TABLESPOON",true, 15.0);
|
||||
public static final Unit TEASPOON = new Unit("TEASPOON", true, 5.0);
|
||||
public static final Unit CUP = new Unit("CUP", true, 240.0);
|
||||
public static final Unit MILLILITER = new Unit("MILLILITER",true, MILLILITERS);
|
||||
public static final Unit LITER = new Unit("LITER", true, LITERS);
|
||||
public static final Unit TABLESPOON = new Unit("TABLESPOON",true, TABLESPOONS);
|
||||
public static final Unit TEASPOON = new Unit("TEASPOON", true, TEASPOONS);
|
||||
public static final Unit CUP = new Unit("CUP", true, CUPS);
|
||||
|
||||
//piece should be a formal unit to converse for portions like 3eggs can become 1,5 eggs this way
|
||||
public static final Unit PIECE = new Unit("PIECE", true, 1.0);
|
||||
public static final Unit PIECE = new Unit("PIECE", true, PIECES);
|
||||
|
||||
//informal units
|
||||
public static final Unit PINCH = new Unit("PINCH", false, 0.0);
|
||||
public static final Unit HANDFUL = new Unit("HANDFUL", false, 0.0);
|
||||
public static final Unit TO_TASTE = new Unit("TO_TASTE", false, 0.0);
|
||||
public static final Unit PINCH = new Unit("PINCH", false, NoMeaningfulValue);
|
||||
public static final Unit HANDFUL = new Unit("HANDFUL", false, NoMeaningfulValue);
|
||||
public static final Unit TO_TASTE = new Unit("TO_TASTE", false, NoMeaningfulValue);
|
||||
|
||||
public final String name;
|
||||
public final boolean formal;
|
||||
|
|
|
|||
|
|
@ -6,31 +6,41 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
|
||||
class IngredientTest {
|
||||
|
||||
// === Meaningful constants ===
|
||||
private static final double ZERO = 0.0;
|
||||
private static final double HUNDRED = 100.0;
|
||||
|
||||
private static final double PROTEIN_10 = 10.0;
|
||||
private static final double FAT_5 = 5.0;
|
||||
private static final double CARBS_20 = 20.0;
|
||||
|
||||
private static final double EXPECTED_KCAL_165 = 165.0;
|
||||
|
||||
|
||||
@Test
|
||||
void checkConstructorIngredient() {
|
||||
Ingredient sugar = new Ingredient("Sugar", 0.0, 0.0, 100.0);
|
||||
Ingredient sugar = new Ingredient("Sugar", ZERO, ZERO, HUNDRED);
|
||||
|
||||
assertEquals("Sugar", sugar.name);
|
||||
assertEquals(0.0 , sugar.proteinPer100g);
|
||||
assertEquals(0.0, sugar.fatPer100g);
|
||||
assertEquals(100.0, sugar.carbsPer100g);
|
||||
assertEquals(ZERO, sugar.proteinPer100g);
|
||||
assertEquals(ZERO, sugar.fatPer100g);
|
||||
assertEquals(HUNDRED, sugar.carbsPer100g);
|
||||
assertEquals(0L, sugar.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkConstructorIngredientIfEverything0() {
|
||||
Ingredient water = new Ingredient("Water", 0.0, 0.0, 0.0);
|
||||
|
||||
assertEquals(0.0, water.kcalPer100g());
|
||||
Ingredient water = new Ingredient("Water", ZERO, ZERO, ZERO);
|
||||
|
||||
assertEquals(ZERO, water.kcalPer100g());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkConstructorIngredientIfEverything100() {
|
||||
Ingredient test = new Ingredient("Test", 10.0, 5.0, 20.0);
|
||||
Ingredient test = new Ingredient("Test", PROTEIN_10, FAT_5, CARBS_20);
|
||||
|
||||
double expectedKcal = 165.0;
|
||||
assertEquals(expectedKcal, test.kcalPer100g());
|
||||
assertEquals(EXPECTED_KCAL_165, test.kcalPer100g());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,19 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||
|
||||
class UnitTest {
|
||||
|
||||
//stupid magic numbers
|
||||
private static final double GRAMS = 1.0;
|
||||
private static final double KILOGRAMS = 1000.0;
|
||||
private static final double MILLILITERS = 1.0;
|
||||
private static final double LITERS = 1000.0;
|
||||
private static final double TABLESPOONS = 15.0;
|
||||
private static final double TEASPOONS = 5.0;
|
||||
private static final double CUPS = 240.0;
|
||||
private static final double PIECES = 1.0;
|
||||
|
||||
private static final double NoMeaningfulValue = 0.0;
|
||||
|
||||
|
||||
@Test
|
||||
void formalUnitMarkedFormal(){
|
||||
assertTrue(Unit.GRAM.isFormal());
|
||||
|
|
@ -28,18 +41,18 @@ class UnitTest {
|
|||
|
||||
@Test
|
||||
void conversionIsCorrect() {
|
||||
assertEquals(1.0, Unit.GRAM.conversionFactor);
|
||||
assertEquals(1000.0, Unit.KILOGRAM.conversionFactor);
|
||||
assertEquals(1000.0, Unit.LITER.conversionFactor);
|
||||
assertEquals(240.0, Unit.CUP.conversionFactor);
|
||||
assertEquals(1.0, Unit.MILLILITER.conversionFactor);
|
||||
assertEquals(1.0, Unit.PIECE.conversionFactor);
|
||||
assertEquals(15.0, Unit.TABLESPOON.conversionFactor);
|
||||
assertEquals(5.0, Unit.TEASPOON.conversionFactor);
|
||||
assertEquals(GRAMS, Unit.GRAM.conversionFactor);
|
||||
assertEquals(KILOGRAMS, Unit.KILOGRAM.conversionFactor);
|
||||
assertEquals(LITERS, Unit.LITER.conversionFactor);
|
||||
assertEquals(CUPS, Unit.CUP.conversionFactor);
|
||||
assertEquals(MILLILITERS, Unit.MILLILITER.conversionFactor);
|
||||
assertEquals(PIECES, Unit.PIECE.conversionFactor);
|
||||
assertEquals(TABLESPOONS, Unit.TABLESPOON.conversionFactor);
|
||||
assertEquals(TEASPOONS, Unit.TEASPOON.conversionFactor);
|
||||
|
||||
assertEquals(0.0, Unit.PINCH.conversionFactor);
|
||||
assertEquals(0.0, Unit.HANDFUL.conversionFactor);
|
||||
assertEquals(0.0, Unit.TO_TASTE.conversionFactor);
|
||||
assertEquals(NoMeaningfulValue, Unit.PINCH.conversionFactor);
|
||||
assertEquals(NoMeaningfulValue, Unit.HANDFUL.conversionFactor);
|
||||
assertEquals(NoMeaningfulValue, Unit.TO_TASTE.conversionFactor);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue