Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Created July 8, 2021 19:01
Show Gist options
  • Save jeroenr/e8a0a9e2ecf6c005b733a63e5d1863cb to your computer and use it in GitHub Desktop.
Save jeroenr/e8a0a9e2ecf6c005b733a63e5d1863cb to your computer and use it in GitHub Desktop.
Value Object example
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