From 9b7a215555fbb6bfdc73d0a0c5f3556c051db3bd Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Wed, 26 Aug 2020 20:06:06 +0700 Subject: [PATCH] Add money config and operator --- .../com/cubetiqs/libra/moneyutils/Money.kt | 11 ---- .../cubetiqs/libra/moneyutils/MoneyConfig.kt | 53 +++++++++++++++++++ .../libra/moneyutils/MoneyOperator.kt | 13 +++++ .../com/cubetiqs/libra/moneyutils/StdMoney.kt | 5 ++ 4 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt create mode 100644 src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyOperator.kt diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt index 26dce95..f3d0359 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt @@ -5,14 +5,3 @@ data class Money( var currency: String = "USD" ) -operator fun Money.unaryMinus() = (-value) -operator fun Money.unaryPlus() = (+value) -operator fun Money.inc() = Money(value++) -operator fun Money.dec() = Money(value--) -operator fun Money.plus(other: Money) = Money(value + other.value) -operator fun Money.times(other: Money) = Money(value * other.value) -operator fun Money.div(other: Money) = Money(value / other.value) -operator fun Money.timesAssign(other: Money) { - this.value = this.value * other.value -} -operator fun Money.not() = this.value != this.value \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt new file mode 100644 index 0000000..e58792e --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt @@ -0,0 +1,53 @@ +package com.cubetiqs.libra.moneyutils + +/** + * Default money config in static object. + * + * @author sombochea + * @since 1.0 + */ +object MoneyConfig { + private const val SPLIT_ORD_1 = ',' + private const val SPLIT_ORD_2 = '=' + + /** + * Key is the currency + * Value is the rate + */ + private val config = mutableMapOf() + + fun parse(config: String): Map { + val result = mutableMapOf() + val rates = config.split(SPLIT_ORD_1) + /** + * USD=1 + * KHR=4000 + * EUR=0.99 + * + * USD -> currency + * 1 -> value + */ + rates.map { i -> + val temp = i.split(SPLIT_ORD_2) + if (temp.size == 2) { + val currency = temp[0] + val value = temp[1].toDouble() + result.put(currency.toUpperCase(), value) + } else { + throw IllegalArgumentException("invalid value!") + } + } + + // if you want to use all set currencies without remove from memory, we should remove this line + this.config.clear() + this.config.putAll(result) + return result + } + + // all currencies with its rate + fun getConfig() = this.config + + fun getRate(currency: String): Double { + return getConfig()[currency.toUpperCase()] ?: throw IllegalArgumentException("currency not found") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyOperator.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyOperator.kt new file mode 100644 index 0000000..aac7d0a --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyOperator.kt @@ -0,0 +1,13 @@ +package com.cubetiqs.libra.moneyutils + +operator fun Money.unaryMinus() = (-value) +operator fun Money.unaryPlus() = (+value) +operator fun Money.inc() = Money(value++) +operator fun Money.dec() = Money(value--) +operator fun Money.plus(other: Money) = Money(value + other.value) +operator fun Money.times(other: Money) = Money(value * other.value) +operator fun Money.div(other: Money) = Money(value / other.value) +operator fun Money.timesAssign(other: Money) { + this.value = this.value * other.value +} +operator fun Money.not() = this.value != this.value \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt index 9fa58e5..e4cec7c 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt @@ -3,6 +3,9 @@ package com.cubetiqs.libra.moneyutils /** * Default standard money style * We have only value and currency of money + * + * @author sombochea + * @since 1.0 */ interface StdMoney { /** @@ -14,6 +17,8 @@ interface StdMoney { /** * Get money's currency from current state. + * + * @return String */ fun getCurrency(): String } \ No newline at end of file