Add compute rate

This commit is contained in:
Sambo Chea 2020-08-26 21:32:06 +07:00
parent ad27257db7
commit cb28bc0d6a
4 changed files with 20 additions and 16 deletions

View File

@ -1,26 +1,23 @@
package com.cubetiqs.libra.moneyutils package com.cubetiqs.libra.moneyutils
open class Money( open class Money(
private var value: Double, var value: Double,
private var currency: String = "USD" var currency: String = "USD"
) : StdMoney { ) : StdMoney {
override fun getValue(): Double {
//////////////////// - PROPERTIES - ////////////////////
override fun getMoneyValue(): Double {
return this.value return this.value
} }
fun setValue(value: Double) { override fun getMoneyCurrency(): String {
this.value = value
}
override fun getCurrency(): String {
return this.currency.toUpperCase() return this.currency.toUpperCase()
} }
fun setCurrency(currency: String) { //////////////////// - GENERIC - ////////////////////
this.currency = currency
}
override fun toString(): String { override fun toString(): String {
return "Money(value=${getValue()}, currency='${getCurrency()}')" return "Money(value=${getMoneyValue()}, currency='${getMoneyCurrency()}')"
} }
} }

View File

@ -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))
}
}

View File

@ -1,7 +1,7 @@
package com.cubetiqs.libra.moneyutils package com.cubetiqs.libra.moneyutils
operator fun Money.unaryMinus() = (-value) operator fun Money.unaryMinus() = (-getMoneyValue())
operator fun Money.unaryPlus() = (+value) operator fun Money.unaryPlus() = (+getMoneyValue())
operator fun Money.inc() = Money(value++) operator fun Money.inc() = Money(value++)
operator fun Money.dec() = Money(value--) operator fun Money.dec() = Money(value--)
operator fun Money.plus(other: Money) = Money(value + other.value) operator fun Money.plus(other: Money) = Money(value + other.value)

View File

@ -13,12 +13,12 @@ interface StdMoney {
* *
* @return Double * @return Double
*/ */
fun getValue(): Double fun getMoneyValue(): Double
/** /**
* Get money's currency from current state. * Get money's currency from current state.
* *
* @return String * @return String
*/ */
fun getCurrency(): String fun getMoneyCurrency(): String
} }