Changed the stdmoney for get fields without money just value and currency for all relates

This commit is contained in:
Sambo Chea 2021-02-08 21:34:17 +07:00
parent f3f41e9c67
commit 1b7c7000bb
8 changed files with 52 additions and 52 deletions

View File

@ -6,23 +6,23 @@ open class Money(
) : StdMoney, StdMoney.Operator<Money>, StdMoney.ExchangeOperator {
//////////////////// - PROPERTIES - ////////////////////
override fun getMoneyValue(): Double {
override fun getValue(): Double {
return this.value
}
// not imply with exchange rate yet
override fun StdMoney.getExchangedTo(currency: StdMoney.Currency): Double {
return MoneyExchangeUtils.exchange(this, currency).getMoneyValue()
return MoneyExchangeUtils.exchange(this, currency).getValue()
}
override fun getMoneyCurrency(): StdMoney.Currency {
override fun getCurrency(): StdMoney.Currency {
return this.currency
}
//////////////////// - GENERIC - ////////////////////
override fun toString(): String {
return "Money(value=${getMoneyValue()}, currency='${getMoneyCurrency().getCurrency()}')"
return "Money(value=${getValue()}, currency='${getCurrency().getCurrency()}')"
}
override fun inc(): Money = apply {
@ -49,7 +49,7 @@ open class Money(
}
override fun multiply(other: StdMoney): Money {
val temp = this.value * other.getExchangedTo(this.getMoneyCurrency())
val temp = this.value * other.getExchangedTo(this.getCurrency())
return Money(value = temp, currency = this.currency)
}
@ -66,7 +66,7 @@ open class Money(
}
override fun multiplyAssign(other: StdMoney): Money = apply {
this.value = this.value * other.getExchangedTo(this.getMoneyCurrency())
this.value = this.value * other.getExchangedTo(this.getCurrency())
}
companion object {
@ -91,7 +91,7 @@ open class Money(
}
fun from(money: StdMoney): Money {
return create(value = money.getMoneyValue(), currency = money.getMoneyCurrency())
return create(value = money.getValue(), currency = money.getCurrency())
}
}
}

View File

@ -94,7 +94,7 @@ object MoneyConfig {
* "...": ...
* }
*/
fun fromJson(configJson: String, clearAllStates: Boolean = false) {
fun fromJsonRates(configJson: String, clearAllStates: Boolean = false) {
val transformValues = configJson
.removePrefix("{")
.removeSuffix("}")

View File

@ -2,9 +2,9 @@ package com.cubetiqs.money
object MoneyExchangeUtils {
fun exchange(exchangeFrom: StdMoney, exchangeToCurrency: StdMoney.Currency): StdMoney {
val rateFrom = MoneyConfig.getRate(exchangeFrom.getMoneyCurrency())
val rateFrom = MoneyConfig.getRate(exchangeFrom.getCurrency())
val rateTo = MoneyConfig.getRate(exchangeToCurrency)
return Money(value = computeRate(rateFrom, rateTo, amountFrom = exchangeFrom.getMoneyValue()), currency = exchangeToCurrency)
return Money(value = computeRate(rateFrom, rateTo, amountFrom = exchangeFrom.getValue()), currency = exchangeToCurrency)
}
private fun computeRate(rateFrom: Double, rateTo: Double, baseRate: Double = 1.0, amountFrom: Double = 1.0): Double {

View File

@ -16,51 +16,51 @@ infix fun StdMoney.divideWith(other: StdMoney): StdMoney = this / other
infix fun StdMoney.multiplyWith(other: StdMoney): StdMoney = this * other
infix fun StdMoney.plusOf(value: Number): StdMoney = object : StdMoney {
override fun getMoneyCurrency(): StdMoney.Currency {
return this@plusOf.getMoneyCurrency()
override fun getCurrency(): StdMoney.Currency {
return this@plusOf.getCurrency()
}
override fun getMoneyValue(): Double {
return this@plusOf.getMoneyValue() + value.toDouble()
override fun getValue(): Double {
return this@plusOf.getValue() + value.toDouble()
}
}
infix fun StdMoney.minusOf(value: Number): StdMoney = object : StdMoney {
override fun getMoneyCurrency(): StdMoney.Currency {
return this@minusOf.getMoneyCurrency()
override fun getCurrency(): StdMoney.Currency {
return this@minusOf.getCurrency()
}
override fun getMoneyValue(): Double {
return this@minusOf.getMoneyValue() - value.toDouble()
override fun getValue(): Double {
return this@minusOf.getValue() - value.toDouble()
}
}
infix fun StdMoney.divideOf(value: Number): StdMoney = object : StdMoney {
override fun getMoneyCurrency(): StdMoney.Currency {
return this@divideOf.getMoneyCurrency()
override fun getCurrency(): StdMoney.Currency {
return this@divideOf.getCurrency()
}
override fun getMoneyValue(): Double {
return this@divideOf.getMoneyValue() / value.toDouble()
override fun getValue(): Double {
return this@divideOf.getValue() / value.toDouble()
}
}
infix fun StdMoney.multiplyOf(value: Number): StdMoney = object : StdMoney {
override fun getMoneyCurrency(): StdMoney.Currency {
return this@multiplyOf.getMoneyCurrency()
override fun getCurrency(): StdMoney.Currency {
return this@multiplyOf.getCurrency()
}
override fun getMoneyValue(): Double {
return this@multiplyOf.getMoneyValue() * value.toDouble()
override fun getValue(): Double {
return this@multiplyOf.getValue() * value.toDouble()
}
}
infix fun Number.withCurrency(currency: StdMoney.Currency): StdMoney = object : StdMoney {
override fun getMoneyCurrency(): StdMoney.Currency {
override fun getCurrency(): StdMoney.Currency {
return currency
}
override fun getMoneyValue(): Double {
override fun getValue(): Double {
return this@withCurrency.toDouble()
}
}
@ -72,8 +72,8 @@ infix fun Number.withCurrency(currency: String): StdMoney = this withCurrency ob
}
// toString function for StdMoney interface
fun StdMoney.asString(): String = "StdMoney(value=${getMoneyValue()}, currency=${getMoneyCurrency().getCurrency()})"
fun StdMoney.asMoneyString(): String = "${getMoneyValue()}:${getMoneyCurrency().getCurrency()}"
fun StdMoney.asString(): String = "StdMoney(value=${getValue()}, currency=${getCurrency().getCurrency()})"
fun StdMoney.asMoneyString(): String = "${getValue()}:${getCurrency().getCurrency()}"
fun String?.fromStringToMoney(): StdMoney {
val values = this?.split(":")
if (values.isNullOrEmpty()) {
@ -84,18 +84,18 @@ fun String?.fromStringToMoney(): StdMoney {
val value = values.lastOrNull()?.toDouble() ?: 0.0
return object : StdMoney {
override fun getMoneyCurrency(): StdMoney.Currency {
override fun getCurrency(): StdMoney.Currency {
return currency
}
override fun getMoneyValue(): Double {
override fun getValue(): Double {
return value
}
}
}
fun StdMoney.isMatchedCurrency(currency: StdMoney.Currency) =
this.getMoneyCurrency().getCurrency().equals(currency.getCurrency(), ignoreCase = true)
this.getCurrency().getCurrency().equals(currency.getCurrency(), ignoreCase = true)
inline fun buildMoneyConfigProperties(
builderAction: MoneyConfig.MoneyConfigProperties.MoneyConfigPropertiesBuilder.() -> Unit

View File

@ -9,16 +9,16 @@ import java.util.*
* @author sombochea
*/
class MoneyObject(
var value: Double,
var currency: String,
private var value: Double,
private var currency: String,
var operator: MoneyOperator? = null,
var with: MoneyObject? = null,
) : StdMoney {
override fun getMoneyCurrency(): StdMoney.Currency {
override fun getCurrency(): StdMoney.Currency {
return StdMoney.initCurrency(currency)
}
override fun getMoneyValue(): Double {
override fun getValue(): Double {
return value
}
@ -55,9 +55,9 @@ class MoneyObject(
else -> this plusWith next
}
// move the temp value that computed
this.value = temp.getMoneyValue()
this.value = temp.getValue()
// move the currency into parent, if changed
this.currency = temp.getMoneyCurrency().getCurrency()
this.currency = temp.getCurrency().getCurrency()
// move the first operator to previous next operator
// example: first ops = +, then next ops = -, then inside next ops = *, n
// return next: first ops = -, then next ops = *, then n

View File

@ -1,8 +1,8 @@
package com.cubetiqs.money
// unary operators
operator fun StdMoney.unaryMinus() = (-getMoneyValue())
operator fun StdMoney.unaryPlus() = (+getMoneyValue())
operator fun StdMoney.unaryMinus() = (-getValue())
operator fun StdMoney.unaryPlus() = (+getValue())
// operators
operator fun StdMoney.inc() = Money.from(this).inc()

View File

@ -13,14 +13,14 @@ interface StdMoney {
*
* @return Double
*/
fun getMoneyValue(): Double
fun getValue(): Double
/**
* Get money's currency from current state.
*
* @return String
*/
fun getMoneyCurrency(): Currency
fun getCurrency(): Currency
/**
* Allow for money currency called and implemented
@ -68,11 +68,11 @@ interface StdMoney {
fun initMoney(initValue: Double, currency: Currency = USD): StdMoney {
return object : StdMoney {
override fun getMoneyCurrency(): Currency {
override fun getCurrency(): Currency {
return currency
}
override fun getMoneyValue(): Double {
override fun getValue(): Double {
return initValue
}
}

View File

@ -10,7 +10,7 @@ class MoneyTests {
setDeliSplit(',')
})
fromJson(MyBatchRates.getJsonRates())
fromJsonRates(MyBatchRates.getJsonRates())
}
}
@ -46,13 +46,13 @@ class MoneyTests {
val moneyKhr = moneyUsd exchangeTo "khr"
// Is correct exchange?
Assert.assertEquals(8000.0, moneyKhr.getMoneyValue(), 0.0)
Assert.assertEquals(8000.0, moneyKhr.getValue(), 0.0)
// complex operators and exchanging
val sum =
((moneyUsd + moneyKhr) * Money.TEN) exchangeTo MoneyCurrency.KHR minusWith (Money.ONE exchangeTo MoneyCurrency.KHR)
Assert.assertEquals(156000.0, sum.getMoneyValue(), 0.0)
Assert.assertEquals(156000.0, sum.getValue(), 0.0)
}
@Test
@ -76,7 +76,7 @@ class MoneyTests {
val result = moneyGen.compute()
Assert.assertEquals(2.75, result.getMoneyValue(), 0.0)
Assert.assertEquals(2.75, result.getValue(), 0.0)
}
@Test
@ -100,7 +100,7 @@ class MoneyTests {
println(result)
Assert.assertEquals(expected, result.getMoneyValue(), 0.0)
Assert.assertEquals(expected, result.getValue(), 0.0)
}
@Test
@ -132,7 +132,7 @@ class MoneyTests {
.compute()
val result2 = builder.compute()
Assert.assertEquals(expected1, result1.getMoneyValue(), 0.0)
Assert.assertEquals(expected2, result2.getMoneyValue(), 0.0)
Assert.assertEquals(expected1, result1.getValue(), 0.0)
Assert.assertEquals(expected2, result2.getValue(), 0.0)
}
}