Skip to content

Commit

Permalink
Make Money comparable and and allow comparison accross currencies and…
Browse files Browse the repository at this point in the history
… also add tests for sorting.
  • Loading branch information
tobiasschuerg committed Aug 30, 2020
1 parent 905bd74 commit 902ce63
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
Empty file modified gradlew
100644 → 100755
Empty file.
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 8
versionName "0.8.0"
versionCode 9
versionName "0.9.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
9 changes: 5 additions & 4 deletions money/src/main/java/de/tobiasschuerg/money/Money.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.math.RoundingMode
* Created by Tobias Schürg on 28.07.2017.
*/
@Suppress("TooManyFunctions")
data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currency) {
data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currency) : Comparable<Money> {

constructor(amount: Double, currency: Currency) : this(BigDecimal(amount), currency)
constructor(amount: Long, currency: Currency) : this(BigDecimal(amount), currency)
Expand All @@ -21,9 +21,10 @@ data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currenc
return Money(sum, currency)
}

operator fun compareTo(money: Money): Int {
requireSameCurrency(money)
return this.amount.compareTo(money.amount)
override operator fun compareTo(other: Money): Int {
val thisBaseCurrencyAmount = amount.divide(currency.rate, MathContext.DECIMAL32)
val otherBaseCurrencyAmount = other.amount.divide(other.currency.rate, MathContext.DECIMAL32)
return thisBaseCurrencyAmount.compareTo(otherBaseCurrencyAmount)
}

override fun toString(): String {
Expand Down
42 changes: 42 additions & 0 deletions money/src/test/java/de/tobiasschuerg/money/MoneySortingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.tobiasschuerg.money

import de.tobiasschuerg.money.Currencies.EURO
import de.tobiasschuerg.money.Currencies.USDOLLAR
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test

class MoneySortingTest {

@Test
fun `test money sorting`() {

val money1 = Money(2, EURO)
val money2 = Money(17.11, EURO)
val money3 = Money(4.25, EURO)

val moneyListUnsorted = listOf(money1, money2, money3)

val sortedlist = moneyListUnsorted.sorted()
assertNotEquals(moneyListUnsorted, sortedlist)

val moneyListSortedManually = listOf(money1, money3, money2)
assertEquals(moneyListSortedManually, sortedlist)
}

@Test
fun `test money sorting with different currencies`() {

val money1 = Money(2, EURO)
val money2 = Money(2, USDOLLAR)
val money3 = Money(3, EURO)

val moneyListUnsorted = listOf(money1, money2, money3)

val sortedlist = moneyListUnsorted.sorted()
assertNotEquals(moneyListUnsorted, sortedlist)

val moneyListSortedManually = listOf(money2, money1, money3)
assertEquals(moneyListSortedManually, sortedlist)
}
}
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'

}

0 comments on commit 902ce63

Please sign in to comment.