Skip to content
Snippets Groups Projects
Commit ed680f9e authored by Blersch Lara's avatar Blersch Lara
Browse files

improved add-methods and knew method to check csv for other measurements than...

improved add-methods and knew method to check csv for other measurements than g and convert them to gram
parent dd9b13ba
No related branches found
No related tags found
No related merge requests found
...@@ -65,7 +65,11 @@ public class CSVParser { ...@@ -65,7 +65,11 @@ public class CSVParser {
for (int i = 1; i < idx.length; i++) { for (int i = 1; i < idx.length; i++) {
String element = splitLine.get(idx[i]).split(" ")[0]; 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); final NutritionTable nutritionTable = new NutritionTable(nutrition);
...@@ -101,6 +105,12 @@ public class CSVParser { ...@@ -101,6 +105,12 @@ public class CSVParser {
return -1; 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) { private double parseNumberFromString(String candidate) {
if (candidate.isBlank()) return 0.0; if (candidate.isBlank()) return 0.0;
...@@ -120,6 +130,25 @@ public class CSVParser { ...@@ -120,6 +130,25 @@ public class CSVParser {
return Double.parseDouble(numValue.toString()); 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 * Split a line of CSV in its individual tokens based on a character while accounting for strings inside of columns
* *
......
...@@ -31,7 +31,7 @@ public class CategoryManager { ...@@ -31,7 +31,7 @@ public class CategoryManager {
*/ */
public boolean addCategory(String name, int colourCode){ public boolean addCategory(String name, int colourCode){
Category c = new Category(name, 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()); log.error("Category: {} not added because it already exists", c.getName());
return false; return false;
} else { } else {
......
...@@ -29,7 +29,7 @@ public class IngredientManager { ...@@ -29,7 +29,7 @@ public class IngredientManager {
* @return True if the ingredient has been added (meaning no equal ingredient existed before), otherwise false. * @return True if the ingredient has been added (meaning no equal ingredient existed before), otherwise false.
*/ */
public boolean addIngredient(Ingredient in) { 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()); log.error("Ingredient {} not added because it already exists.", in.getName());
return false; return false;
//TODO: Exception or false? //TODO: Exception or false?
......
...@@ -62,7 +62,7 @@ class CSVParserTest { ...@@ -62,7 +62,7 @@ class CSVParserTest {
String absolutePath = path.toFile().getAbsolutePath(); String absolutePath = path.toFile().getAbsolutePath();
String[] extract = new String[]{"name", "calories", "carbohydrate", "fat", "protein", "fiber", "sodium"}; 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); List<Ingredient> actual = underTest.getIngredientsFromCSV(absolutePath, ',', extract);
assertEquals(expected, actual); assertEquals(expected, actual);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment