Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
3
Indexable
Never
public BigDecimal calculateFinalAmount(BigDecimal originalAmount, String fromCurrency, String toCurrency) {

        BigDecimal finalAmount = null;

        switch (fromCurrency) {
            case "PLN":

                if (toCurrency.equals("USD")) {
                    finalAmount = originalAmount.divide(PL_USD_EXCHANGE_RATE, 2, RoundingMode.HALF_UP);
                    break;
                }

                finalAmount = originalAmount.divide(PL_EUR_EXCHANGE_RATE, 2, RoundingMode.HALF_UP);

                break;

            case "USD":

                if (toCurrency.equals("PLN")) {
                    finalAmount = originalAmount.multiply(PL_USD_EXCHANGE_RATE);
                    break;
                }

                finalAmount = originalAmount.divide(USD_EUR_EXCHANGE_RATE, 2, RoundingMode.HALF_UP);

                break;

            case "EUR":

                if (toCurrency.equals("PLN")) {
                    finalAmount = originalAmount.multiply(PL_EUR_EXCHANGE_RATE);
                    break;
                }

                finalAmount = originalAmount.multiply(USD_EUR_EXCHANGE_RATE);

                break;

            default:

                throw new RuntimeException("Unreachable");
        }

        return finalAmount;
    }