From d80ad060dc49fdeea9af34c019839bbf84acc3b2 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Wed, 26 Aug 2020 19:51:54 +0700 Subject: [PATCH] Add function for money almost all --- .../com/example/demo/DemoApplication.kt | 18 ++++++--- .../com/example/demo/domain/MoneyConfig.kt | 19 ++++++++- .../com/example/demo/domain/MoneyDyn.kt | 40 +++++++++++++++---- 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/com/example/demo/DemoApplication.kt b/src/main/kotlin/com/example/demo/DemoApplication.kt index caa3388..2b4b095 100644 --- a/src/main/kotlin/com/example/demo/DemoApplication.kt +++ b/src/main/kotlin/com/example/demo/DemoApplication.kt @@ -17,12 +17,20 @@ class DemoApplication : CommandLineRunner { // // println(result) - val config = "USD=1,KHR=4000,EUR=0.99" - val result = MoneyConfig.parse(config) - println(result) + // val config = "USD=1,KHR=4000,EUR=0.99" + // val result = MoneyConfig.parse(config) + // println(result) - val value1 = MoneyDyn() - value1.setConfig(config) + MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99") + + 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 } } diff --git a/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt b/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt index ad889aa..7da321e 100644 --- a/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt +++ b/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt @@ -4,6 +4,12 @@ object MoneyConfig { private const val SPLIT_ORD_1 = ',' private const val SPLIT_ORD_2 = '=' + /** + * Key is the currency + * Value is the rate + */ + private val config = mutableMapOf() + fun parse(config: String): Map { val result = mutableMapOf() val rates = config.split(SPLIT_ORD_1) @@ -20,11 +26,22 @@ object MoneyConfig { if (temp.size == 2) { val currency = temp[0] val value = temp[1].toDouble() - result.put(currency, value) + result.put(currency.toUpperCase(), value) } else { 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 } + + // all currencies with its rate + fun getConfig() = this.config + + fun getRate(currency: String): Double { + return getConfig()[currency.toUpperCase()] ?: throw IllegalArgumentException("currency not found") + } } \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/domain/MoneyDyn.kt b/src/main/kotlin/com/example/demo/domain/MoneyDyn.kt index 794874d..81b8aa5 100644 --- a/src/main/kotlin/com/example/demo/domain/MoneyDyn.kt +++ b/src/main/kotlin/com/example/demo/domain/MoneyDyn.kt @@ -1,17 +1,41 @@ package com.example.demo.domain -class MoneyDyn ( +class MoneyDyn( var value: Double = 0.0, 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 { return "MoneyDyn(value=$value, currency='$currency')" } - - private var config = mapOf() - fun setConfig(config: String) { - this.config = MoneyConfig.parse(config) - } - - } \ No newline at end of file