Skip to content
Snippets Groups Projects
Commit c0a9d87b authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch '244-add-endpoint-to-get-how-much-money-a-group-have-lost' into 'main'

Some algorthims has been improved

Closes #244

See merge request idatt2106-v23-03/backend!191
parents 22d37e32 f3ac04ac
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,9 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product; ...@@ -4,7 +4,9 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
...@@ -16,52 +18,33 @@ import java.util.regex.Pattern; ...@@ -16,52 +18,33 @@ import java.util.regex.Pattern;
public class ProductUtil { 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) { public static Optional<List<String>> getVolumeFromProduct(Product product) {
for (String desc : Arrays.asList(product.getName(), product.getDescription())) { String desc = product.getName() + " " + product.getDescription();
List<String> words = List.of(desc.split(" ")); Matcher matcher = Pattern.compile(VOLUME_REGEX).matcher(desc);
if (words.size() > 1) { if (matcher.find()) {
String volume = ""; String volumeString = matcher.group(1);
for (String unit : VOLUME_UNITS) { double volume = Double.parseDouble(volumeString);
int i = words.indexOf(unit); String unitString = matcher.group(3);
if (i != -1) { for (Map.Entry<String, List<String>> entry : VOLUME_UNIT_VARIATIONS.entrySet()) {
return Optional.of(List.of(words.get(i - 1), unit)); if (entry.getValue().contains(unitString)) {
} return Optional.of(List.of(String.valueOf(volume), entry.getKey()));
}
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(" ")));
} }
} }
} }
return Optional.empty(); return Optional.empty();
} }
/** /**
* Checks if a string contains any numbers * Checks if a string contains any numbers
* @param s The string to check * @param s The string to check
......
...@@ -113,7 +113,7 @@ public class StatisticUtil { ...@@ -113,7 +113,7 @@ public class StatisticUtil {
case "ml" -> sum += waste.getAmount() * 0.000998; case "ml" -> sum += waste.getAmount() * 0.000998;
case "cl" -> sum += waste.getAmount() * 0.00998; case "cl" -> sum += waste.getAmount() * 0.00998;
case "dl" -> sum += waste.getAmount() * 0.0998; case "dl" -> sum += waste.getAmount() * 0.0998;
default -> sum += 0.1; default -> sum += 0.1 * waste.getAmount();
} }
} }
return sum; return sum;
......
...@@ -134,7 +134,7 @@ public class ProductServiceTest { ...@@ -134,7 +134,7 @@ public class ProductServiceTest {
// Verify that the service returns the correct volume // Verify that the service returns the correct volume
Optional<List<String>> returnedVolume = productService.getProductVolume(productId); Optional<List<String>> returnedVolume = productService.getProductVolume(productId);
assertTrue(returnedVolume.isPresent()); assertTrue(returnedVolume.isPresent());
assertEquals(List.of("500", "ml"), returnedVolume.get()); assertEquals(List.of("500.0", "ml"), returnedVolume.get());
} }
@Test @Test
......
...@@ -22,7 +22,7 @@ class ProductUtilTest { ...@@ -22,7 +22,7 @@ class ProductUtilTest {
@Test @Test
void getVolumeFromProduct() { void getVolumeFromProduct() {
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get()); assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder() this.product = Product.builder()
.ean(123456789) .ean(123456789)
...@@ -30,7 +30,7 @@ class ProductUtilTest { ...@@ -30,7 +30,7 @@ class ProductUtilTest {
.description("Pepsi Original 24x500 ml") .description("Pepsi Original 24x500 ml")
.build(); .build();
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get()); assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder() this.product = Product.builder()
.ean(123456789) .ean(123456789)
...@@ -39,7 +39,7 @@ class ProductUtilTest { ...@@ -39,7 +39,7 @@ class ProductUtilTest {
.build(); .build();
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get()); assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder() this.product = Product.builder()
.ean(123456789) .ean(123456789)
...@@ -48,6 +48,6 @@ class ProductUtilTest { ...@@ -48,6 +48,6 @@ class ProductUtilTest {
.build(); .build();
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get()); assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
} }
} }
\ No newline at end of file
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