From 9e7e50a1ba137216d3e9cce014e2e521ad8b7ccf Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 27 Aug 2020 21:15:00 +0700 Subject: [PATCH] Updated the demo for some functions --- .../com/example/demo/DemoApplication.kt | 28 ++++++++++---- .../kotlin/com/example/demo/domain/Money.kt | 20 ++++------ .../com/example/demo/domain/MoneyExtension.kt | 38 +++++++++++++++++++ .../com/example/demo/domain/StdMoney.kt | 6 +++ .../example/demo/domain/StdMoneyFormatable.kt | 18 +++++++++ 5 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/com/example/demo/domain/MoneyExtension.kt create mode 100644 src/main/kotlin/com/example/demo/domain/StdMoney.kt create mode 100644 src/main/kotlin/com/example/demo/domain/StdMoneyFormatable.kt diff --git a/src/main/kotlin/com/example/demo/DemoApplication.kt b/src/main/kotlin/com/example/demo/DemoApplication.kt index 2b4b095..697056e 100644 --- a/src/main/kotlin/com/example/demo/DemoApplication.kt +++ b/src/main/kotlin/com/example/demo/DemoApplication.kt @@ -1,7 +1,13 @@ package com.example.demo +import com.example.demo.domain.Currency +import com.example.demo.domain.Money import com.example.demo.domain.MoneyConfig import com.example.demo.domain.MoneyDyn +import com.example.demo.domain.addMoney +import com.example.demo.domain.exchange +import com.example.demo.domain.plus +import com.example.demo.domain.toStdMoneyFormatable import org.springframework.boot.CommandLineRunner import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @@ -23,14 +29,22 @@ class DemoApplication : CommandLineRunner { 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 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 - // val result = value1.exchange("KHR") - println(value1.exchangeTo("KHR")) // 7 + val moneyUsd = Money(2.0) + .addMoney(20000.0, "USD") + .exchange("KHR") + .toStdMoneyFormatable() + .formatMoneyDisplay() + + println(moneyUsd) } } diff --git a/src/main/kotlin/com/example/demo/domain/Money.kt b/src/main/kotlin/com/example/demo/domain/Money.kt index 70ffb6f..5da27d3 100644 --- a/src/main/kotlin/com/example/demo/domain/Money.kt +++ b/src/main/kotlin/com/example/demo/domain/Money.kt @@ -3,7 +3,7 @@ package com.example.demo.domain class Money ( var value: Double = 0.0, var currency: Currency = Currency.USD -) { +) : StdMoneyFormatable { companion object { private const val MONEY_SPLITTER = ':' fun parse(value: String): Money { @@ -16,25 +16,19 @@ class Money ( } } + override fun getMoneyValue() = value + + override fun getMoneyCurrency() = this.currency.name + override fun toString(): String { - return "Money(value=$value, currency=$currency)" - } - - fun addMoney(value: Money): Money { - if (value.currency == this.currency) { - this.value = this.value.plus(value.value) - } else { - throw IllegalArgumentException("currency ${value.currency.name} is not matched!") - } - - return this + return "Money(value=${formatMoneyValue()}, currency=$currency)" } } enum class Currency (val symbol: Char, val displayName: String) { USD('$', "Dollar"), KHR('R', "Riel"); - + companion object { fun create(value: String): Currency { return try { diff --git a/src/main/kotlin/com/example/demo/domain/MoneyExtension.kt b/src/main/kotlin/com/example/demo/domain/MoneyExtension.kt new file mode 100644 index 0000000..b9b8624 --- /dev/null +++ b/src/main/kotlin/com/example/demo/domain/MoneyExtension.kt @@ -0,0 +1,38 @@ +package com.example.demo.domain + +operator fun StdMoney.plus(other: StdMoney): StdMoney { + if (!this.getMoneyCurrency().equals(other.getMoneyCurrency(), ignoreCase = true)) { + throw IllegalStateException("money currency is not valid") + } + return Money(this.getMoneyValue().plus(other.getMoneyValue()), currency = Currency.create(this.getMoneyCurrency())) +} + +fun StdMoney.addMoney(other: StdMoney): StdMoney { + if (this.getMoneyCurrency() == other.getMoneyCurrency()) { + return this + other + } + + // this = USD + // other = KHR + + // other (20000) -> this (5) + + // exchange + val otherExchange = other.exchange(this.getMoneyCurrency()) + return this + otherExchange +} + +fun StdMoney.addMoney(value: Double, currency: String): StdMoney { + return this.addMoney(Money(value = value, currency = Currency.create(currency))) +} + +fun StdMoney.exchange(toCurrency: String): StdMoney { + val rateFrom = MoneyConfig.getRate(this.getMoneyCurrency()) + val rateTo = MoneyConfig.getRate(toCurrency) + val resultAmount = this.getMoneyValue() * ((1 / rateFrom) / (1 / rateTo)) + return Money(value = resultAmount, currency = Currency.create(toCurrency)) +} + +fun StdMoney.toStdMoneyFormatable(): StdMoneyFormatable { + return this as StdMoneyFormatable +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/domain/StdMoney.kt b/src/main/kotlin/com/example/demo/domain/StdMoney.kt new file mode 100644 index 0000000..31fc9c3 --- /dev/null +++ b/src/main/kotlin/com/example/demo/domain/StdMoney.kt @@ -0,0 +1,6 @@ +package com.example.demo.domain + +interface StdMoney { + fun getMoneyValue(): Double + fun getMoneyCurrency(): String +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/domain/StdMoneyFormatable.kt b/src/main/kotlin/com/example/demo/domain/StdMoneyFormatable.kt new file mode 100644 index 0000000..dff7e64 --- /dev/null +++ b/src/main/kotlin/com/example/demo/domain/StdMoneyFormatable.kt @@ -0,0 +1,18 @@ +package com.example.demo.domain + +import java.text.DecimalFormat + +interface StdMoneyFormatable : StdMoney { + companion object { + const val MONEY_FORMAT = "#,##0.##" + } + + fun formatMoneyValue(): String { + val df = DecimalFormat(MONEY_FORMAT) + return df.format(getMoneyValue()) + } + + fun formatMoneyDisplay(): String { + return "${formatMoneyValue()} ${getMoneyCurrency()}" + } +} \ No newline at end of file