From 1b7c7000bb5686699f31f4576a2865c7c3364217 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Mon, 8 Feb 2021 21:34:17 +0700 Subject: [PATCH] Changed the stdmoney for get fields without money just value and currency for all relates --- src/main/kotlin/com/cubetiqs/money/Money.kt | 14 +++--- .../kotlin/com/cubetiqs/money/MoneyConfig.kt | 2 +- .../com/cubetiqs/money/MoneyExchangeUtils.kt | 4 +- .../com/cubetiqs/money/MoneyExtension.kt | 46 +++++++++---------- .../kotlin/com/cubetiqs/money/MoneyObject.kt | 12 ++--- .../com/cubetiqs/money/MoneyOperator.kt | 4 +- .../kotlin/com/cubetiqs/money/StdMoney.kt | 8 ++-- src/test/kotlin/MoneyTests.kt | 14 +++--- 8 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/main/kotlin/com/cubetiqs/money/Money.kt b/src/main/kotlin/com/cubetiqs/money/Money.kt index 380ff38..3377617 100644 --- a/src/main/kotlin/com/cubetiqs/money/Money.kt +++ b/src/main/kotlin/com/cubetiqs/money/Money.kt @@ -6,23 +6,23 @@ open class Money( ) : StdMoney, StdMoney.Operator, 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()) } } } diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt b/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt index 8b68f19..3266288 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt @@ -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("}") diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt b/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt index fe9f2a5..19a7ceb 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyExchangeUtils.kt @@ -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 { diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt index e82a0e6..32d343f 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt @@ -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 diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt b/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt index 777f362..2f38cdf 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt @@ -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 diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyOperator.kt b/src/main/kotlin/com/cubetiqs/money/MoneyOperator.kt index 9d3b3eb..d5657ed 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyOperator.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyOperator.kt @@ -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() diff --git a/src/main/kotlin/com/cubetiqs/money/StdMoney.kt b/src/main/kotlin/com/cubetiqs/money/StdMoney.kt index 0857fde..260ff26 100644 --- a/src/main/kotlin/com/cubetiqs/money/StdMoney.kt +++ b/src/main/kotlin/com/cubetiqs/money/StdMoney.kt @@ -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 } } diff --git a/src/test/kotlin/MoneyTests.kt b/src/test/kotlin/MoneyTests.kt index 7d78ebd..73c40e5 100644 --- a/src/test/kotlin/MoneyTests.kt +++ b/src/test/kotlin/MoneyTests.kt @@ -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) } } \ No newline at end of file