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
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()}')"
}
}

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

View File

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