diff --git a/src/main/java/mi/hdm/filesystem/CSVParser.java b/src/main/java/mi/hdm/filesystem/CSVParser.java index 59fa405cd904c649ca39b6174822fe224faa6148..b691a4bd0771f2dd681ea8c90034da14c0ed3523 100644 --- a/src/main/java/mi/hdm/filesystem/CSVParser.java +++ b/src/main/java/mi/hdm/filesystem/CSVParser.java @@ -65,7 +65,11 @@ public class CSVParser { for (int i = 1; i < idx.length; i++) { String element = splitLine.get(idx[i]).split(" ")[0]; - nutrition.add(parseNumberFromString(element)); + double quantity = parseNumberFromString(element); + if (getMeasurementFromString(element).equals("mg")) { + quantity = quantity /1000; + } + nutrition.add(quantity); } final NutritionTable nutritionTable = new NutritionTable(nutrition); @@ -101,6 +105,12 @@ public class CSVParser { return -1; } + /** + * Get only the quantity of a nutrition value without its measurement + * + * @param candidate the nutrition value the quantity is extracted from + * @return quantity of nutrition value as double (that is without measurement) + */ private double parseNumberFromString(String candidate) { if (candidate.isBlank()) return 0.0; @@ -120,6 +130,25 @@ public class CSVParser { return Double.parseDouble(numValue.toString()); } + /** + * Get only the measurement of a nutrition value + * + * @param candidate the nutrition value the measurement is extracted from + * @return measurement of nutrition value as String + */ + private String getMeasurementFromString(String candidate) { + if (candidate.isBlank()) return ""; + + StringBuilder unit = new StringBuilder(); + for (int i = 0; i < candidate.length(); i++) { + char c = candidate.charAt(i); + if (!Character.isDigit(c) && !(c == ' ')) { + unit.append(c); + } + } + return unit.toString(); + } + /** * Split a line of CSV in its individual tokens based on a character while accounting for strings inside of columns * diff --git a/src/main/java/mi/hdm/recipes/CategoryManager.java b/src/main/java/mi/hdm/recipes/CategoryManager.java index df33f5cca1aadfd457f8c31183ad8b967b7fc391..c217958cdb45cb0e731987009ab1ef9b53e45937 100644 --- a/src/main/java/mi/hdm/recipes/CategoryManager.java +++ b/src/main/java/mi/hdm/recipes/CategoryManager.java @@ -31,7 +31,7 @@ public class CategoryManager { */ public boolean addCategory(String name, int colourCode){ Category c = new Category(name, colourCode); - if(allCategories.contains(c)) { + if (getCategoryByName(name).isPresent()) { log.error("Category: {} not added because it already exists", c.getName()); return false; } else { diff --git a/src/main/java/mi/hdm/recipes/IngredientManager.java b/src/main/java/mi/hdm/recipes/IngredientManager.java index d462084da757c3eecb43035c2463c71f0211e311..09fd6fde12a3aeb94358068abe974438abe91668 100644 --- a/src/main/java/mi/hdm/recipes/IngredientManager.java +++ b/src/main/java/mi/hdm/recipes/IngredientManager.java @@ -29,7 +29,7 @@ public class IngredientManager { * @return True if the ingredient has been added (meaning no equal ingredient existed before), otherwise false. */ public boolean addIngredient(Ingredient in) { - if (allIngredients.contains(in)) { + if (getIngredientByName(in.getName()).isPresent()) { log.error("Ingredient {} not added because it already exists.", in.getName()); return false; //TODO: Exception or false? diff --git a/src/test/java/mi/hdm/filesystem/CSVParserTest.java b/src/test/java/mi/hdm/filesystem/CSVParserTest.java index f872a7b40eb3a3b7dfb224fea06044ba19e01262..8106095fbc9f646ad6d57b85a4e21b5e93a8d959 100644 --- a/src/test/java/mi/hdm/filesystem/CSVParserTest.java +++ b/src/test/java/mi/hdm/filesystem/CSVParserTest.java @@ -62,7 +62,7 @@ class CSVParserTest { String absolutePath = path.toFile().getAbsolutePath(); String[] extract = new String[]{"name", "calories", "carbohydrate", "fat", "protein", "fiber", "sodium"}; - List<Ingredient> expected = List.of(new Ingredient(Measurement.GRAM, "Cornstarch", new NutritionTable(381.0, 91.27, 0.05, 0.26, 0.9, 9.0))); + List<Ingredient> expected = List.of(new Ingredient(Measurement.GRAM, "Cornstarch", new NutritionTable(381.0, 91.27, 0.05, 0.26, 0.9, 0.009))); List<Ingredient> actual = underTest.getIngredientsFromCSV(absolutePath, ',', extract); assertEquals(expected, actual);