Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Created July 8, 2021 19:01
Show Gist options
  • Select an option

  • Save jeroenr/e8a0a9e2ecf6c005b733a63e5d1863cb to your computer and use it in GitHub Desktop.

Select an option

Save jeroenr/e8a0a9e2ecf6c005b733a63e5d1863cb to your computer and use it in GitHub Desktop.

Revisions

  1. jeroenr created this gist Jul 8, 2021.
    15 changes: 15 additions & 0 deletions Money.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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)
    }
    }