Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
769 B
4
Indexable
public BigDecimal calculateFinalAmount(BigDecimal originalAmount, String fromCurrency, String toCurrency) {

        BigDecimal finalAmount;
        BigDecimal temporaryAmount;

        BigDecimal fromCurrencyExchangeRate = exchangeRateService.getCurrentExchangeRate(fromCurrency);

        temporaryAmount = originalAmount.multiply(fromCurrencyExchangeRate);

        BigDecimal toCurrencyExchangeRate = exchangeRateService.getCurrentExchangeRate(toCurrency);

        if (temporaryAmount.compareTo(toCurrencyExchangeRate) > 0) {

            finalAmount = temporaryAmount.divide(toCurrencyExchangeRate, 2, RoundingMode.HALF_UP);

        } else {

            finalAmount = temporaryAmount.multiply(toCurrencyExchangeRate);
        }
        return finalAmount;
    }