Skip to content

Commit

Permalink
Merge pull request #9 from Caramel1004/refactor/#8
Browse files Browse the repository at this point in the history
♻️ [refactor] 송금 수수료 정책 추가, 코드 리팩토리 중
  • Loading branch information
Caramel1004 authored Apr 4, 2024
2 parents ab5c094 + aab76c1 commit 72ca5f7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.moin.remittance.application.service.v2.calculating;

import com.moin.remittance.application.service.v2.calculating.policy.RemittanceFeePolicy;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Currency;

public class ExchangeRateCalculator {
public static double calculateExchangeRate(long amount, double currencyUnit, double basePrice, String currencyCode) {
BigDecimal sourceAmount = new BigDecimal(amount);
BigDecimal fee = RemittanceFeePolicy.calculateFee(amount);

BigDecimal krw = sourceAmount.subtract(fee);
BigDecimal targetCurrencyToKrwExchangeRate = new BigDecimal(basePrice / currencyUnit).setScale(4, RoundingMode.HALF_UP);// 소수점 넷째자리에서 반올림
System.out.println(targetCurrencyToKrwExchangeRate);

int decimalPlaces = Currency.getInstance(currencyCode).getDefaultFractionDigits();

BigDecimal targetAmount = krw.divide(targetCurrencyToKrwExchangeRate, decimalPlaces, BigDecimal.ROUND_HALF_UP);// 자릿수 getDefaultFractionDigits

return Double.parseDouble(String.valueOf(targetAmount));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.moin.remittance.application.service.v2.calculating.policy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class RemittanceFeePolicy {

public static BigDecimal calculateFee(long amount) {
final double HUNDRED_EXCEED_FEE = 3000.0;
final double HUNDRED_EXCEED_FEE_RATE = 0.1;

final double HUNDRED_DOWN_FEE = 1000.0;
final double HUNDRED_DOWN_FEE_RATE = 0.2;

double sourceAmount = (double) amount;

double targetFee = (sourceAmount > 0.0 && sourceAmount <= 1000000.0)?
(sourceAmount * HUNDRED_DOWN_FEE_RATE) + HUNDRED_DOWN_FEE : (sourceAmount * HUNDRED_EXCEED_FEE_RATE) + HUNDRED_EXCEED_FEE;

return new BigDecimal(targetFee).setScale(1, RoundingMode.HALF_UP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import static java.lang.Math.round;

public class ExchangeRateCalculatorTest {
public class ExchangeRateCalculatorCalculatorTest {
@Test
@DisplayName("환율 계산")
public void calculateExchangeRate() {
Expand Down

0 comments on commit 72ca5f7

Please sign in to comment.