From cb28bc0d6a46e6a379cb5b6ab4f40f7c45d4639c Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Wed, 26 Aug 2020 21:32:06 +0700 Subject: [PATCH] Add compute rate --- .../com/cubetiqs/libra/moneyutils/Money.kt | 21 ++++++++----------- .../libra/moneyutils/MoneyExchangeUtils.kt | 7 +++++++ .../libra/moneyutils/MoneyOperator.kt | 4 ++-- .../com/cubetiqs/libra/moneyutils/StdMoney.kt | 4 ++-- 4 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExchangeUtils.kt diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt index 1e41823..43a2a68 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt @@ -1,26 +1,23 @@ package com.cubetiqs.libra.moneyutils open class Money( - private var value: Double, - private var currency: String = "USD" + var value: Double, + var currency: String = "USD" ) : StdMoney { - override fun getValue(): Double { + + //////////////////// - PROPERTIES - //////////////////// + + override fun getMoneyValue(): Double { return this.value } - fun setValue(value: Double) { - this.value = value - } - - override fun getCurrency(): String { + override fun getMoneyCurrency(): String { return this.currency.toUpperCase() } - fun setCurrency(currency: String) { - this.currency = currency - } + //////////////////// - GENERIC - //////////////////// override fun toString(): String { - return "Money(value=${getValue()}, currency='${getCurrency()}')" + return "Money(value=${getMoneyValue()}, currency='${getMoneyCurrency()}')" } } diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExchangeUtils.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExchangeUtils.kt new file mode 100644 index 0000000..ba6a2ce --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExchangeUtils.kt @@ -0,0 +1,7 @@ +package com.cubetiqs.libra.moneyutils + +object MoneyExchangeUtils { + fun computeRate(rateFrom: Double, rateTo: Double, baseRate: Double = 1.0, amountFrom: Double = 1.0): Double { + return amountFrom * ((baseRate / rateFrom) / (baseRate / rateTo)) + } +} \ 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 index aac7d0a..83f1bb8 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyOperator.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyOperator.kt @@ -1,7 +1,7 @@ package com.cubetiqs.libra.moneyutils -operator fun Money.unaryMinus() = (-value) -operator fun Money.unaryPlus() = (+value) +operator fun Money.unaryMinus() = (-getMoneyValue()) +operator fun Money.unaryPlus() = (+getMoneyValue()) operator fun Money.inc() = Money(value++) operator fun Money.dec() = Money(value--) operator fun Money.plus(other: Money) = Money(value + other.value) diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt index e4cec7c..81ed748 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt @@ -13,12 +13,12 @@ interface StdMoney { * * @return Double */ - fun getValue(): Double + fun getMoneyValue(): Double /** * Get money's currency from current state. * * @return String */ - fun getCurrency(): String + fun getMoneyCurrency(): String } \ No newline at end of file