package com.juanpabloprado.ecommerceapi.product;

import com.juanpabloprado.ecommerceapi.integration.agenty.dto.Result;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
  @Override public Product fromIntegration(Result result) {
    try {
      Number number = NumberFormat.getCurrencyInstance(new Locale("es", "MX")).parse(result.getPrice());
      BigDecimal price = new BigDecimal(number.doubleValue());

      return new Product(result.getName(), price, null, result.getImageUrl());
    } catch (Exception ex ) {
      return new Product(result.getName(), new BigDecimal("0.00"), result.getPrice(), result.getImageUrl());
    }

  }
}

@@ -385,16 +385,75 @@
   try %7B%0A
+      String firstPrice = getFirstWord(result.getPrice());%0A
       Nu
@@ -465,16 +465,26 @@
 number =
+%0A         
  NumberF
@@ -539,33 +539,26 @@
 ).parse(
-result.ge
+firs
 tPrice
-()
 );%0A     
@@ -613,16 +613,57 @@
 Value())
+.setScale(2, BigDecimal.ROUND_HALF_EVEN);
 ;%0A%0A     
@@ -761,17 +761,16 @@
 ption ex
- 
 ) %7B%0A    
@@ -850,16 +850,26 @@
 Price(),
+%0A         
  result.
@@ -890,15 +890,187 @@
 ;%0A    %7D%0A
+  %7D%0A%0A  private String getFirstWord(String text) %7B%0A    if (text.indexOf(' ') %3E -1) %7B%0A      return text.substring(0, text.indexOf(' '));%0A    %7D else %7B%0A      return text;%0A    %7D
 %0A  %7D%0A%7D%0A

Parse correctly prices from amazon e.g. $400.61 - $1,418.64 to 400.61