Add function for money almost all

This commit is contained in:
Sambo Chea 2020-08-26 19:51:54 +07:00
parent 7d1f0ba1d3
commit d80ad060dc
3 changed files with 63 additions and 14 deletions

View File

@ -17,12 +17,20 @@ class DemoApplication : CommandLineRunner {
// //
// println(result) // println(result)
val config = "USD=1,KHR=4000,EUR=0.99" // val config = "USD=1,KHR=4000,EUR=0.99"
val result = MoneyConfig.parse(config) // val result = MoneyConfig.parse(config)
println(result) // println(result)
val value1 = MoneyDyn() MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99")
value1.setConfig(config)
val value1 = MoneyDyn(value = 2.0, currency = "USD")
val value2 = MoneyDyn(value = 20000.0, currency = "KHR")
val value3 = MoneyDyn(value = 0.99, currency = "EUR")
value1.addMoney(value2)
value1.addMoney(value3)
// val result = value1.exchange("KHR")
println(value1.exchangeTo("KHR")) // 7
} }
} }

View File

@ -4,6 +4,12 @@ object MoneyConfig {
private const val SPLIT_ORD_1 = ',' private const val SPLIT_ORD_1 = ','
private const val SPLIT_ORD_2 = '=' private const val SPLIT_ORD_2 = '='
/**
* Key is the currency
* Value is the rate
*/
private val config = mutableMapOf<String, Double>()
fun parse(config: String): Map<String, Double> { fun parse(config: String): Map<String, Double> {
val result = mutableMapOf<String, Double>() val result = mutableMapOf<String, Double>()
val rates = config.split(SPLIT_ORD_1) val rates = config.split(SPLIT_ORD_1)
@ -20,11 +26,22 @@ object MoneyConfig {
if (temp.size == 2) { if (temp.size == 2) {
val currency = temp[0] val currency = temp[0]
val value = temp[1].toDouble() val value = temp[1].toDouble()
result.put(currency, value) result.put(currency.toUpperCase(), value)
} else { } else {
throw IllegalArgumentException("invalid value!") throw IllegalArgumentException("invalid value!")
} }
} }
// if you want to use all set currencies without remove from memory, we should remove this line
this.config.clear()
this.config.putAll(result)
return result return result
} }
// all currencies with its rate
fun getConfig() = this.config
fun getRate(currency: String): Double {
return getConfig()[currency.toUpperCase()] ?: throw IllegalArgumentException("currency not found")
}
} }

View File

@ -4,14 +4,38 @@ class MoneyDyn (
var value: Double = 0.0, var value: Double = 0.0,
var currency: String = "USD" var currency: String = "USD"
) { ) {
fun addMoney(other: MoneyDyn): MoneyDyn {
this.value = this.value.plus(exchange(other, this.currency))
return this
}
fun exchange(fromMoney: MoneyDyn, exchangeCurrency: String): Double {
if (!fromMoney.currency.equals(exchangeCurrency, ignoreCase = true)) {
val rateFrom = MoneyConfig.getRate(fromMoney.currency)
val rateTo = MoneyConfig.getRate(exchangeCurrency)
return computeRate(amountFrom = fromMoney.value, rateFrom = rateFrom, rateTo = rateTo)
}
return fromMoney.value
}
fun exchangeTo(toCurrency: String): MoneyDyn {
return MoneyDyn(value = exchange(this, toCurrency))
}
private fun computeRate(
amountFrom: Double = 1.0,
baseRate: Double = 1.0,
rateFrom: Double,
rateTo: Double
): Double {
return amountFrom * ((baseRate / rateFrom) / (baseRate / rateTo))
}
private fun isEqualCurrency(other: MoneyDyn) =
this.currency.equals(other.currency, ignoreCase = true)
override fun toString(): String { override fun toString(): String {
return "MoneyDyn(value=$value, currency='$currency')" return "MoneyDyn(value=$value, currency='$currency')"
} }
private var config = mapOf<String, Double>()
fun setConfig(config: String) {
this.config = MoneyConfig.parse(config)
}
} }