Skip to content

Commit 902ce63

Browse files
committed
Make Money comparable and and allow comparison accross currencies and also add tests for sorting.
1 parent 905bd74 commit 902ce63

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

gradlew

100644100755
File mode changed.

money/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99

1010
defaultConfig {
1111
minSdkVersion 14
12-
versionCode 8
13-
versionName "0.8.0"
12+
versionCode 9
13+
versionName "0.9.0"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616

money/src/main/java/de/tobiasschuerg/money/Money.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.math.RoundingMode
1010
* Created by Tobias Schürg on 28.07.2017.
1111
*/
1212
@Suppress("TooManyFunctions")
13-
data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currency) {
13+
data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currency) : Comparable<Money> {
1414

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

24-
operator fun compareTo(money: Money): Int {
25-
requireSameCurrency(money)
26-
return this.amount.compareTo(money.amount)
24+
override operator fun compareTo(other: Money): Int {
25+
val thisBaseCurrencyAmount = amount.divide(currency.rate, MathContext.DECIMAL32)
26+
val otherBaseCurrencyAmount = other.amount.divide(other.currency.rate, MathContext.DECIMAL32)
27+
return thisBaseCurrencyAmount.compareTo(otherBaseCurrencyAmount)
2728
}
2829

2930
override fun toString(): String {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package de.tobiasschuerg.money
2+
3+
import de.tobiasschuerg.money.Currencies.EURO
4+
import de.tobiasschuerg.money.Currencies.USDOLLAR
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Assert.assertNotEquals
7+
import org.junit.Test
8+
9+
class MoneySortingTest {
10+
11+
@Test
12+
fun `test money sorting`() {
13+
14+
val money1 = Money(2, EURO)
15+
val money2 = Money(17.11, EURO)
16+
val money3 = Money(4.25, EURO)
17+
18+
val moneyListUnsorted = listOf(money1, money2, money3)
19+
20+
val sortedlist = moneyListUnsorted.sorted()
21+
assertNotEquals(moneyListUnsorted, sortedlist)
22+
23+
val moneyListSortedManually = listOf(money1, money3, money2)
24+
assertEquals(moneyListSortedManually, sortedlist)
25+
}
26+
27+
@Test
28+
fun `test money sorting with different currencies`() {
29+
30+
val money1 = Money(2, EURO)
31+
val money2 = Money(2, USDOLLAR)
32+
val money3 = Money(3, EURO)
33+
34+
val moneyListUnsorted = listOf(money1, money2, money3)
35+
36+
val sortedlist = moneyListUnsorted.sorted()
37+
assertNotEquals(moneyListUnsorted, sortedlist)
38+
39+
val moneyListSortedManually = listOf(money2, money1, money3)
40+
assertEquals(moneyListSortedManually, sortedlist)
41+
}
42+
}

sample/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ dependencies {
2828

2929
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3030
implementation 'androidx.appcompat:appcompat:1.2.0'
31-
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
31+
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
3232

3333
}

0 commit comments

Comments
 (0)