-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: Money VO 도입 / #127 #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/org/creditto/core_banking/common/vo/Money.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.creditto.core_banking.common.vo; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import org.creditto.core_banking.global.common.CurrencyCode; | ||
| import org.creditto.core_banking.global.response.error.ErrorBaseCode; | ||
| import org.creditto.core_banking.global.response.exception.CustomBaseException; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
|
|
||
| @Getter | ||
| @EqualsAndHashCode | ||
| public final class Money { | ||
| private final BigDecimal amount; | ||
|
|
||
| private final CurrencyCode currency; | ||
|
|
||
| private Money(final BigDecimal amount, final CurrencyCode currency) { | ||
| if (currency == null) { | ||
| throw new CustomBaseException(ErrorBaseCode.CURRENCY_NOT_SUPPORTED); | ||
| } | ||
| if (amount == null || amount.signum() < 0) { | ||
| throw new CustomBaseException(ErrorBaseCode.BAD_REQUEST); | ||
| } | ||
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.amount = normalize(amount, currency); | ||
| this.currency = currency; | ||
| } | ||
|
|
||
| public static Money of(BigDecimal amount, CurrencyCode currency) { | ||
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new Money(amount, currency); | ||
| } | ||
|
|
||
| public Money plus(Money other) { | ||
| assertSameCurrency(other); | ||
| return new Money(this.amount.add(other.amount), this.currency); | ||
| } | ||
|
|
||
| public Money minus(Money other) { | ||
| assertSameCurrency(other); | ||
| BigDecimal result = this.amount.subtract(other.amount); | ||
| if (result.signum() < 0) { | ||
| throw new CustomBaseException(ErrorBaseCode.INSUFFICIENT_FUNDS); | ||
| } | ||
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new Money(result, this.currency); | ||
| } | ||
|
|
||
| public boolean gte(Money other) { | ||
| assertSameCurrency(other); | ||
| return this.amount.compareTo(other.amount) >= 0; | ||
| } | ||
|
|
||
| private void assertSameCurrency(Money other) { | ||
| if (other == null) { | ||
| throw new CustomBaseException(ErrorBaseCode.BAD_REQUEST); | ||
| } | ||
| if (this.currency != other.currency) { | ||
| throw new CustomBaseException(ErrorBaseCode.CURRENCY_NOT_SUPPORTED); | ||
| } | ||
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static BigDecimal normalize(BigDecimal value, CurrencyCode currency) { | ||
| int scale = (currency == CurrencyCode.KRW) ? 0 : 2; | ||
| return value.setScale(scale, RoundingMode.HALF_UP); | ||
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
JayongLee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/java/org/creditto/core_banking/common/vo/MoneyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package org.creditto.core_banking.common.vo; | ||
|
|
||
| import org.creditto.core_banking.global.common.CurrencyCode; | ||
| import org.creditto.core_banking.global.response.exception.CustomBaseException; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| class MoneyTest { | ||
|
|
||
| @Test | ||
| @DisplayName("같은 통화의 금액은 더할 수 있다") | ||
| void plus_sameCurrency_success() { | ||
| Money a = Money.of(new BigDecimal("1000"), CurrencyCode.KRW); | ||
| Money b = Money.of(new BigDecimal("2000"), CurrencyCode.KRW); | ||
|
|
||
| Money result = a.plus(b); | ||
|
|
||
| assertThat(result.getAmount()).isEqualByComparingTo("3000"); | ||
| assertThat(result.getCurrency()).isEqualTo(CurrencyCode.KRW); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("잔액보다 큰 금액을 빼면 예외가 발생한다") | ||
| void minus_insufficientFunds_fail() { | ||
| Money a = Money.of(new BigDecimal("1000"), CurrencyCode.KRW); | ||
| Money b = Money.of(new BigDecimal("1001"), CurrencyCode.KRW); | ||
|
|
||
| assertThatThrownBy(() -> a.minus(b)) | ||
| .isInstanceOf(CustomBaseException.class); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("통화가 다르면 연산할 수 없다") | ||
| void plus_differentCurrency_fail() { | ||
| Money krw = Money.of(new BigDecimal("1000"), CurrencyCode.KRW); | ||
| Money usd = Money.of(new BigDecimal("1"), CurrencyCode.USD); | ||
|
|
||
| assertThatThrownBy(() -> krw.plus(usd)) | ||
| .isInstanceOf(CustomBaseException.class); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("KRW는 소수점 없이 정규화된다") | ||
| void of_krw_normalizeScale_zero() { | ||
| Money krw = Money.of(new BigDecimal("1000.6"), CurrencyCode.KRW); | ||
|
|
||
| assertThat(krw.getAmount()).isEqualByComparingTo("1001"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.