diff options
author | Torsten Grote <t@grobox.de> | 2020-03-24 09:08:00 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-03-24 09:08:00 -0300 |
commit | 864160280872fdb400c2e0e61aaaa1b858fba3f8 (patch) | |
tree | 5d3f8b24d261a593364d9bcb70033b4814fd954a /taler-kotlin-common/src | |
parent | a690087d82e8b0ee774542d9956698a349408824 (diff) | |
download | taler-android-864160280872fdb400c2e0e61aaaa1b858fba3f8.tar.gz taler-android-864160280872fdb400c2e0e61aaaa1b858fba3f8.tar.bz2 taler-android-864160280872fdb400c2e0e61aaaa1b858fba3f8.zip |
Allow fractional withdrawals with cashier app
Diffstat (limited to 'taler-kotlin-common/src')
-rw-r--r-- | taler-kotlin-common/src/main/java/net/taler/common/Amount.kt | 27 | ||||
-rw-r--r-- | taler-kotlin-common/src/test/java/net/taler/common/AmountTest.kt | 21 |
2 files changed, 43 insertions, 5 deletions
diff --git a/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt b/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt index 48bd643..49b699f 100644 --- a/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt +++ b/taler-kotlin-common/src/main/java/net/taler/common/Amount.kt @@ -64,7 +64,7 @@ data class Amount( * of 50_000_000 would correspond to 50 cents. */ val fraction: Int -) { +) : Comparable<Amount> { companion object { @@ -88,10 +88,14 @@ data class Amount( fun fromJSONString(str: String): Amount { val split = str.split(":") if (split.size != 2) throw AmountParserException("Invalid Amount Format") - // currency - val currency = checkCurrency(split[0]) + return fromString(split[0], split[1]) + } + + @Throws(AmountParserException::class) + @SuppressLint("CheckedExceptions") + fun fromString(currency: String, str: String): Amount { // value - val valueSplit = split[1].split(".") + val valueSplit = str.split(".") val value = checkValue(valueSplit[0].toLongOrNull()) // fraction val fraction: Int = if (valueSplit.size > 1) { @@ -103,7 +107,7 @@ data class Amount( ?.roundToInt() checkFraction(fraction) } else 0 - return Amount(currency, value, fraction) + return Amount(checkCurrency(currency), value, fraction) } @Throws(AmountParserException::class) @@ -197,4 +201,17 @@ data class Amount( return "$amountStr $currency" } + override fun compareTo(other: Amount): Int { + check(currency == other.currency) { "Can only compare amounts with the same currency" } + when { + value == other.value -> { + if (fraction < other.fraction) return -1 + if (fraction > other.fraction) return 1 + return 0 + } + value < other.value -> return -1 + else -> return 1 + } + } + } diff --git a/taler-kotlin-common/src/test/java/net/taler/common/AmountTest.kt b/taler-kotlin-common/src/test/java/net/taler/common/AmountTest.kt index c09da3c..97d9667 100644 --- a/taler-kotlin-common/src/test/java/net/taler/common/AmountTest.kt +++ b/taler-kotlin-common/src/test/java/net/taler/common/AmountTest.kt @@ -275,6 +275,27 @@ class AmountTest { assertFalse(Amount.fromJSONString("EUR:0001.0").isZero()) } + @Test + fun `test comparision`() { + assertTrue(Amount.fromJSONString("EUR:0") <= Amount.fromJSONString("EUR:0")) + assertTrue(Amount.fromJSONString("EUR:0") <= Amount.fromJSONString("EUR:0.00000001")) + assertTrue(Amount.fromJSONString("EUR:0") < Amount.fromJSONString("EUR:0.00000001")) + assertTrue(Amount.fromJSONString("EUR:0") < Amount.fromJSONString("EUR:1")) + assertTrue(Amount.fromJSONString("EUR:0") == Amount.fromJSONString("EUR:0")) + assertTrue(Amount.fromJSONString("EUR:42") == Amount.fromJSONString("EUR:42")) + assertTrue(Amount.fromJSONString("EUR:42.00000001") == Amount.fromJSONString("EUR:42.00000001")) + assertTrue(Amount.fromJSONString("EUR:42.00000001") >= Amount.fromJSONString("EUR:42.00000001")) + assertTrue(Amount.fromJSONString("EUR:42.00000002") >= Amount.fromJSONString("EUR:42.00000001")) + assertTrue(Amount.fromJSONString("EUR:42.00000002") > Amount.fromJSONString("EUR:42.00000001")) + assertTrue(Amount.fromJSONString("EUR:0.00000002") > Amount.fromJSONString("EUR:0.00000001")) + assertTrue(Amount.fromJSONString("EUR:0.00000001") > Amount.fromJSONString("EUR:0")) + assertTrue(Amount.fromJSONString("EUR:2") > Amount.fromJSONString("EUR:1")) + + assertThrows<IllegalStateException>("could compare amounts with different currencies") { + Amount.fromJSONString("EUR:0.5") < Amount.fromJSONString("USD:0.50000001") + } + } + private inline fun <reified T : Throwable> assertThrows( msg: String? = null, function: () -> Any |