Skip to content

Commit

Permalink
Enabled additon and substraction with Zero money.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasschuerg committed Aug 30, 2020
1 parent 902ce63 commit 8a0c575
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions money/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 9
versionName "0.9.0"
versionCode 10
versionName "0.9.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
21 changes: 14 additions & 7 deletions money/src/main/java/de/tobiasschuerg/money/Money.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currenc
constructor(amount: Double, currency: Currency) : this(BigDecimal(amount), currency)
constructor(amount: Long, currency: Currency) : this(BigDecimal(amount), currency)

operator fun plus(money: Money): Money {
requireSameCurrency(money)
val sum = amount.add(money.amount)
return Money(sum, currency)
operator fun plus(money: Money): Money = when {
this.isZero() -> money
money.isZero() -> this
else -> {
requireSameCurrency(money)
val sum = amount.add(money.amount)
Money(sum, currency)
}
}

override operator fun compareTo(other: Money): Int {
Expand Down Expand Up @@ -59,9 +63,12 @@ data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currenc
}

operator fun minus(money: Money): Money {
requireSameCurrency(money)
val result = amount.subtract(money.amount)
return Money(result, currency)
return if (money.isZero()) this
else {
requireSameCurrency(money)
val result = amount.subtract(money.amount)
Money(result, currency)
}
}

private fun requireSameCurrency(money: Money) {
Expand Down

0 comments on commit 8a0c575

Please sign in to comment.