Compare commits

...

4 Commits

7 changed files with 58 additions and 7 deletions

View File

@ -1,7 +1,28 @@
package com.cubetiqs.libra.moneyutils
data class Money(
open class Money(
var value: Double,
var currency: String = "USD"
)
private var currency: String = "USD"
) : StdMoney {
//////////////////// - PROPERTIES - ////////////////////
override fun getMoneyValue(): Double {
return this.value
}
override fun getMoneyCurrency(): String {
return this.currency.toUpperCase()
}
//////////////////// - GENERIC - ////////////////////
override fun toString(): String {
return "Money(value=${getMoneyValue()}, currency='${getMoneyCurrency()}')"
}
companion object {
val ZERO: StdMoney
get() = Money(value = 0.0)
}
}

View File

@ -15,6 +15,8 @@ object MoneyConfig {
* Value is the rate
*/
private val config: MutableMap<String, Double> = mutableMapOf()
private var valid: Boolean = false
fun isValid() = valid
/**
* Money properties for money config format
@ -56,6 +58,9 @@ object MoneyConfig {
throw MoneyCurrencyStateException("money config format is not valid!")
}
}
// validate the config is load or not
this.valid = this.config.isNotEmpty()
}
// all currencies with its rate

View File

@ -0,0 +1,13 @@
package com.cubetiqs.libra.moneyutils
object MoneyExchangeUtils {
fun exchange(exchangeFrom: StdMoney, exchangeToCurrency: String): StdMoney {
val rateFrom = MoneyConfig.getRate(exchangeFrom.getMoneyCurrency())
val rateTo = MoneyConfig.getRate(exchangeToCurrency)
return Money(value = computeRate(rateFrom, rateTo, amountFrom = exchangeFrom.getMoneyValue()), currency = exchangeToCurrency)
}
private 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
}

View File

@ -0,0 +1,6 @@
package com.cubetiqs.libra.moneyutils
interface StdMoneyFormation {
fun format(): String
fun toMoneyString(overrideSymbol: Char? = null): String
}

View File

@ -1,5 +1,6 @@
import com.cubetiqs.libra.moneyutils.Money
import com.cubetiqs.libra.moneyutils.MoneyConfig
import com.cubetiqs.libra.moneyutils.MoneyExchangeUtils
import com.cubetiqs.libra.moneyutils.plus
import com.cubetiqs.libra.moneyutils.times
import com.cubetiqs.libra.moneyutils.timesAssign
@ -27,5 +28,10 @@ class MoneyTests {
.parse("USD:1,KHR:4000")
println(MoneyConfig.getConfig())
val moneyUsd = Money(10.0)
val moneyKhr = MoneyExchangeUtils.exchange(moneyUsd, "KHR")
Assert.assertEquals(40000.0, moneyKhr.getMoneyValue(), 0.0)
}
}