From a83885745c46218cd8036cfb3cd3d1f3487c38e2 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Wed, 17 Dec 2025 15:35:14 +0100 Subject: [PATCH] make the Unit Class enums --- commons/src/main/java/commons/Unit.java | 41 ++++++++----------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/commons/src/main/java/commons/Unit.java b/commons/src/main/java/commons/Unit.java index 05c174e..16627f4 100644 --- a/commons/src/main/java/commons/Unit.java +++ b/commons/src/main/java/commons/Unit.java @@ -1,47 +1,32 @@ 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; - - +public enum Unit { //formal units //weight units - public static final Unit GRAM = new Unit("GRAM", true, GRAMS); - public static final Unit KILOGRAM = new Unit("KILOGRAM", true, KILOGRAMS ); + GRAM("GRAM", true, 1.0), + KILOGRAM("KILOGRAM", true, 1000.0 ), //volume units - 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); + MILLILITER("MILLILITER",true, 1.0), + LITER("LITER", true, 1000.0), + TABLESPOON("TABLESPOON",true, 15.0), + TEASPOON("TEASPOON", true, 5), + CUP ("CUP", true, 240.0), //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, PIECES); + PIECE("PIECE", true, 1.0), //informal units - 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); + PINCH("PINCH", false, 0.0), + HANDFUL("HANDFUL", false, 0.0), + TO_TASTE("TO_TASTE", false, 0.0); public final String name; public final boolean formal; public final double conversionFactor; - private Unit(String name, boolean formal, double conversionFactor) { + Unit(String name, boolean formal, double conversionFactor) { this.name = name; this.formal = formal; this.conversionFactor = conversionFactor;