Created
July 8, 2021 19:01
-
-
Save jeroenr/e8a0a9e2ecf6c005b733a63e5d1863cb to your computer and use it in GitHub Desktop.
Value Object example
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
enum class Currency { USD, EUR } | |
data class Money( | |
val amount: BigDecimal, | |
val currency: Currency, | |
) { | |
val largerThanZero = amount > BigDecimal.ZERO | |
fun add(o: Money): Money { | |
if(currency != o.currency) throw IllegalArgumentException() | |
return Money(amount.add(o.amount), currency) | |
} | |
fun convert(exchangeRate: ExchangeRateDto): Money { | |
return Money(amount.multiply(exchangeRate.rate), exchangeRate.currency) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment