Changed the stdmoney for get fields without money just value and currency for all relates
This commit is contained in:
parent
f3f41e9c67
commit
1b7c7000bb
@ -6,23 +6,23 @@ open class Money(
|
|||||||
) : StdMoney, StdMoney.Operator<Money>, StdMoney.ExchangeOperator {
|
) : StdMoney, StdMoney.Operator<Money>, StdMoney.ExchangeOperator {
|
||||||
//////////////////// - PROPERTIES - ////////////////////
|
//////////////////// - PROPERTIES - ////////////////////
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return this.value
|
return this.value
|
||||||
}
|
}
|
||||||
|
|
||||||
// not imply with exchange rate yet
|
// not imply with exchange rate yet
|
||||||
override fun StdMoney.getExchangedTo(currency: StdMoney.Currency): Double {
|
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
|
return this.currency
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////// - GENERIC - ////////////////////
|
//////////////////// - GENERIC - ////////////////////
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "Money(value=${getMoneyValue()}, currency='${getMoneyCurrency().getCurrency()}')"
|
return "Money(value=${getValue()}, currency='${getCurrency().getCurrency()}')"
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun inc(): Money = apply {
|
override fun inc(): Money = apply {
|
||||||
@ -49,7 +49,7 @@ open class Money(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun multiply(other: StdMoney): 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)
|
return Money(value = temp, currency = this.currency)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ open class Money(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun multiplyAssign(other: StdMoney): Money = apply {
|
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 {
|
companion object {
|
||||||
@ -91,7 +91,7 @@ open class Money(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun from(money: StdMoney): Money {
|
fun from(money: StdMoney): Money {
|
||||||
return create(value = money.getMoneyValue(), currency = money.getMoneyCurrency())
|
return create(value = money.getValue(), currency = money.getCurrency())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ object MoneyConfig {
|
|||||||
* "...": ...
|
* "...": ...
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
fun fromJson(configJson: String, clearAllStates: Boolean = false) {
|
fun fromJsonRates(configJson: String, clearAllStates: Boolean = false) {
|
||||||
val transformValues = configJson
|
val transformValues = configJson
|
||||||
.removePrefix("{")
|
.removePrefix("{")
|
||||||
.removeSuffix("}")
|
.removeSuffix("}")
|
||||||
|
@ -2,9 +2,9 @@ package com.cubetiqs.money
|
|||||||
|
|
||||||
object MoneyExchangeUtils {
|
object MoneyExchangeUtils {
|
||||||
fun exchange(exchangeFrom: StdMoney, exchangeToCurrency: StdMoney.Currency): StdMoney {
|
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)
|
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 {
|
private fun computeRate(rateFrom: Double, rateTo: Double, baseRate: Double = 1.0, amountFrom: Double = 1.0): Double {
|
||||||
|
@ -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.multiplyWith(other: StdMoney): StdMoney = this * other
|
||||||
|
|
||||||
infix fun StdMoney.plusOf(value: Number): StdMoney = object : StdMoney {
|
infix fun StdMoney.plusOf(value: Number): StdMoney = object : StdMoney {
|
||||||
override fun getMoneyCurrency(): StdMoney.Currency {
|
override fun getCurrency(): StdMoney.Currency {
|
||||||
return this@plusOf.getMoneyCurrency()
|
return this@plusOf.getCurrency()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return this@plusOf.getMoneyValue() + value.toDouble()
|
return this@plusOf.getValue() + value.toDouble()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun StdMoney.minusOf(value: Number): StdMoney = object : StdMoney {
|
infix fun StdMoney.minusOf(value: Number): StdMoney = object : StdMoney {
|
||||||
override fun getMoneyCurrency(): StdMoney.Currency {
|
override fun getCurrency(): StdMoney.Currency {
|
||||||
return this@minusOf.getMoneyCurrency()
|
return this@minusOf.getCurrency()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return this@minusOf.getMoneyValue() - value.toDouble()
|
return this@minusOf.getValue() - value.toDouble()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun StdMoney.divideOf(value: Number): StdMoney = object : StdMoney {
|
infix fun StdMoney.divideOf(value: Number): StdMoney = object : StdMoney {
|
||||||
override fun getMoneyCurrency(): StdMoney.Currency {
|
override fun getCurrency(): StdMoney.Currency {
|
||||||
return this@divideOf.getMoneyCurrency()
|
return this@divideOf.getCurrency()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return this@divideOf.getMoneyValue() / value.toDouble()
|
return this@divideOf.getValue() / value.toDouble()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun StdMoney.multiplyOf(value: Number): StdMoney = object : StdMoney {
|
infix fun StdMoney.multiplyOf(value: Number): StdMoney = object : StdMoney {
|
||||||
override fun getMoneyCurrency(): StdMoney.Currency {
|
override fun getCurrency(): StdMoney.Currency {
|
||||||
return this@multiplyOf.getMoneyCurrency()
|
return this@multiplyOf.getCurrency()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return this@multiplyOf.getMoneyValue() * value.toDouble()
|
return this@multiplyOf.getValue() * value.toDouble()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun Number.withCurrency(currency: StdMoney.Currency): StdMoney = object : StdMoney {
|
infix fun Number.withCurrency(currency: StdMoney.Currency): StdMoney = object : StdMoney {
|
||||||
override fun getMoneyCurrency(): StdMoney.Currency {
|
override fun getCurrency(): StdMoney.Currency {
|
||||||
return currency
|
return currency
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return this@withCurrency.toDouble()
|
return this@withCurrency.toDouble()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -72,8 +72,8 @@ infix fun Number.withCurrency(currency: String): StdMoney = this withCurrency ob
|
|||||||
}
|
}
|
||||||
|
|
||||||
// toString function for StdMoney interface
|
// toString function for StdMoney interface
|
||||||
fun StdMoney.asString(): String = "StdMoney(value=${getMoneyValue()}, currency=${getMoneyCurrency().getCurrency()})"
|
fun StdMoney.asString(): String = "StdMoney(value=${getValue()}, currency=${getCurrency().getCurrency()})"
|
||||||
fun StdMoney.asMoneyString(): String = "${getMoneyValue()}:${getMoneyCurrency().getCurrency()}"
|
fun StdMoney.asMoneyString(): String = "${getValue()}:${getCurrency().getCurrency()}"
|
||||||
fun String?.fromStringToMoney(): StdMoney {
|
fun String?.fromStringToMoney(): StdMoney {
|
||||||
val values = this?.split(":")
|
val values = this?.split(":")
|
||||||
if (values.isNullOrEmpty()) {
|
if (values.isNullOrEmpty()) {
|
||||||
@ -84,18 +84,18 @@ fun String?.fromStringToMoney(): StdMoney {
|
|||||||
val value = values.lastOrNull()?.toDouble() ?: 0.0
|
val value = values.lastOrNull()?.toDouble() ?: 0.0
|
||||||
|
|
||||||
return object : StdMoney {
|
return object : StdMoney {
|
||||||
override fun getMoneyCurrency(): StdMoney.Currency {
|
override fun getCurrency(): StdMoney.Currency {
|
||||||
return currency
|
return currency
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun StdMoney.isMatchedCurrency(currency: StdMoney.Currency) =
|
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(
|
inline fun buildMoneyConfigProperties(
|
||||||
builderAction: MoneyConfig.MoneyConfigProperties.MoneyConfigPropertiesBuilder.() -> Unit
|
builderAction: MoneyConfig.MoneyConfigProperties.MoneyConfigPropertiesBuilder.() -> Unit
|
||||||
|
@ -9,16 +9,16 @@ import java.util.*
|
|||||||
* @author sombochea
|
* @author sombochea
|
||||||
*/
|
*/
|
||||||
class MoneyObject(
|
class MoneyObject(
|
||||||
var value: Double,
|
private var value: Double,
|
||||||
var currency: String,
|
private var currency: String,
|
||||||
var operator: MoneyOperator? = null,
|
var operator: MoneyOperator? = null,
|
||||||
var with: MoneyObject? = null,
|
var with: MoneyObject? = null,
|
||||||
) : StdMoney {
|
) : StdMoney {
|
||||||
override fun getMoneyCurrency(): StdMoney.Currency {
|
override fun getCurrency(): StdMoney.Currency {
|
||||||
return StdMoney.initCurrency(currency)
|
return StdMoney.initCurrency(currency)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,9 +55,9 @@ class MoneyObject(
|
|||||||
else -> this plusWith next
|
else -> this plusWith next
|
||||||
}
|
}
|
||||||
// move the temp value that computed
|
// move the temp value that computed
|
||||||
this.value = temp.getMoneyValue()
|
this.value = temp.getValue()
|
||||||
// move the currency into parent, if changed
|
// 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
|
// move the first operator to previous next operator
|
||||||
// example: first ops = +, then next ops = -, then inside next ops = *, n
|
// example: first ops = +, then next ops = -, then inside next ops = *, n
|
||||||
// return next: first ops = -, then next ops = *, then n
|
// return next: first ops = -, then next ops = *, then n
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package com.cubetiqs.money
|
package com.cubetiqs.money
|
||||||
|
|
||||||
// unary operators
|
// unary operators
|
||||||
operator fun StdMoney.unaryMinus() = (-getMoneyValue())
|
operator fun StdMoney.unaryMinus() = (-getValue())
|
||||||
operator fun StdMoney.unaryPlus() = (+getMoneyValue())
|
operator fun StdMoney.unaryPlus() = (+getValue())
|
||||||
|
|
||||||
// operators
|
// operators
|
||||||
operator fun StdMoney.inc() = Money.from(this).inc()
|
operator fun StdMoney.inc() = Money.from(this).inc()
|
||||||
|
@ -13,14 +13,14 @@ interface StdMoney {
|
|||||||
*
|
*
|
||||||
* @return Double
|
* @return Double
|
||||||
*/
|
*/
|
||||||
fun getMoneyValue(): Double
|
fun getValue(): Double
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get money's currency from current state.
|
* Get money's currency from current state.
|
||||||
*
|
*
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
fun getMoneyCurrency(): Currency
|
fun getCurrency(): Currency
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow for money currency called and implemented
|
* Allow for money currency called and implemented
|
||||||
@ -68,11 +68,11 @@ interface StdMoney {
|
|||||||
|
|
||||||
fun initMoney(initValue: Double, currency: Currency = USD): StdMoney {
|
fun initMoney(initValue: Double, currency: Currency = USD): StdMoney {
|
||||||
return object : StdMoney {
|
return object : StdMoney {
|
||||||
override fun getMoneyCurrency(): Currency {
|
override fun getCurrency(): Currency {
|
||||||
return currency
|
return currency
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoneyValue(): Double {
|
override fun getValue(): Double {
|
||||||
return initValue
|
return initValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ class MoneyTests {
|
|||||||
setDeliSplit(',')
|
setDeliSplit(',')
|
||||||
})
|
})
|
||||||
|
|
||||||
fromJson(MyBatchRates.getJsonRates())
|
fromJsonRates(MyBatchRates.getJsonRates())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,13 +46,13 @@ class MoneyTests {
|
|||||||
val moneyKhr = moneyUsd exchangeTo "khr"
|
val moneyKhr = moneyUsd exchangeTo "khr"
|
||||||
|
|
||||||
// Is correct exchange?
|
// Is correct exchange?
|
||||||
Assert.assertEquals(8000.0, moneyKhr.getMoneyValue(), 0.0)
|
Assert.assertEquals(8000.0, moneyKhr.getValue(), 0.0)
|
||||||
|
|
||||||
// complex operators and exchanging
|
// complex operators and exchanging
|
||||||
val sum =
|
val sum =
|
||||||
((moneyUsd + moneyKhr) * Money.TEN) exchangeTo MoneyCurrency.KHR minusWith (Money.ONE exchangeTo MoneyCurrency.KHR)
|
((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
|
@Test
|
||||||
@ -76,7 +76,7 @@ class MoneyTests {
|
|||||||
|
|
||||||
val result = moneyGen.compute()
|
val result = moneyGen.compute()
|
||||||
|
|
||||||
Assert.assertEquals(2.75, result.getMoneyValue(), 0.0)
|
Assert.assertEquals(2.75, result.getValue(), 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -100,7 +100,7 @@ class MoneyTests {
|
|||||||
|
|
||||||
println(result)
|
println(result)
|
||||||
|
|
||||||
Assert.assertEquals(expected, result.getMoneyValue(), 0.0)
|
Assert.assertEquals(expected, result.getValue(), 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -132,7 +132,7 @@ class MoneyTests {
|
|||||||
.compute()
|
.compute()
|
||||||
val result2 = builder.compute()
|
val result2 = builder.compute()
|
||||||
|
|
||||||
Assert.assertEquals(expected1, result1.getMoneyValue(), 0.0)
|
Assert.assertEquals(expected1, result1.getValue(), 0.0)
|
||||||
Assert.assertEquals(expected2, result2.getMoneyValue(), 0.0)
|
Assert.assertEquals(expected2, result2.getValue(), 0.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user