From 5efe6ee40f91d0a56d2a85c1ef86d56574590076 Mon Sep 17 00:00:00 2001
From: Pedro Cardona <pedropca@stud.ntnu.no>
Date: Tue, 2 May 2023 17:14:44 +0200
Subject: [PATCH] Some algorthims has been improved

---
 .../v233/SmartMat/util/ProductUtil.java       | 59 +++++++------------
 .../v233/SmartMat/util/StatisticUtil.java     |  2 +-
 2 files changed, 22 insertions(+), 39 deletions(-)

diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java
index c7701652..40ba1f50 100644
--- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java
+++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java
@@ -4,7 +4,9 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 /**
@@ -16,52 +18,33 @@ import java.util.regex.Pattern;
 public class ProductUtil {
 
 
-    private static final String[] VOLUME_UNITS = {"ml", "cl", "dl", "l", "g", "kg"};
+    private static final String VOLUME_REGEX = "(\\d+(\\.\\d+)?)\\s*(mls?|centiliters?|deciliters?|liters?|grams?|kilograms?)";
+    private static final Map<String, List<String>> VOLUME_UNIT_VARIATIONS = Map.of(
+            "ml", List.of("ml", "milliliters", "millilitres"),
+            "cl", List.of("cl", "centiliters", "centilitres"),
+            "dl", List.of("dl", "deciliters", "decilitres"),
+            "l", List.of("l", "liters", "litres"),
+            "g", List.of("g", "grams"),
+            "kg", List.of("kg", "kilograms")
+    );
 
-    /**
-     * Gets the volume of a product, if it exists
-     * By looking at the name and description of the product
-     * it will try to first find the volume in the name, and then in the description
-     * It uses pre defined volume units to find the volume
-     * Todo: Make it be able to find volume if the size is definde by count
-     * @param product The product to get the volume from
-     * @return The volume of the product, if it exists
-     */
     public static Optional<List<String>> getVolumeFromProduct(Product product) {
-        for (String desc : Arrays.asList(product.getName(), product.getDescription())) {
-            List<String> words = List.of(desc.split(" "));
-            if (words.size() > 1) {
-                String volume = "";
-                for (String unit : VOLUME_UNITS) {
-                    int i = words.indexOf(unit);
-                    if (i != -1) {
-                        return Optional.of(List.of(words.get(i - 1), unit));
-                    }
-                }
-
-                volume = words.stream().map(word -> Arrays.stream(VOLUME_UNITS).map(unit -> {
-                                    int index = word.indexOf(unit);
-                                    if (index == -1) {
-                                        if (!Pattern.matches("[a-zA-Z]+", word) && ProductUtil.hasNumbers(word)) {
-                                            return word;
-                                        }
-                                        return "";
-                                    }
-                                    return word.substring(0, index) + " " + word.substring(index);
-                                }).findAny().orElse(""))
-                        .filter(ProductUtil::hasNumbers)
-                        .findAny()
-                        .orElse("");
-                if (!volume.equals("")){
-                    return Optional.of(List.of(volume.split(" ")));
+        String desc = product.getName() + " " + product.getDescription();
+        Matcher matcher = Pattern.compile(VOLUME_REGEX).matcher(desc);
+        if (matcher.find()) {
+            String volumeString = matcher.group(1);
+            double volume = Double.parseDouble(volumeString);
+            String unitString = matcher.group(3);
+            for (Map.Entry<String, List<String>> entry : VOLUME_UNIT_VARIATIONS.entrySet()) {
+                if (entry.getValue().contains(unitString)) {
+                    return Optional.of(List.of(String.valueOf(volume), entry.getKey()));
                 }
             }
         }
-
-
         return Optional.empty();
     }
 
+
     /**
      * Checks if a string contains any numbers
      * @param s The string to check
diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java
index a05b097b..9bc24cb4 100644
--- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java
+++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java
@@ -113,7 +113,7 @@ public class StatisticUtil {
                 case "ml" -> sum += waste.getAmount() * 0.000998;
                 case "cl" -> sum += waste.getAmount() * 0.00998;
                 case "dl" -> sum += waste.getAmount() * 0.0998;
-                default -> sum += 0.1;
+                default -> sum += 0.1 * waste.getAmount();
             }
         }
         return sum;
-- 
GitLab