Updated the demo for some functions

This commit is contained in:
Sambo Chea 2020-08-27 21:15:00 +07:00
parent d80ad060dc
commit 9e7e50a1ba
5 changed files with 90 additions and 20 deletions

View File

@ -1,7 +1,13 @@
package com.example.demo 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.MoneyConfig
import com.example.demo.domain.MoneyDyn 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.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
@ -23,14 +29,22 @@ class DemoApplication : CommandLineRunner {
MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99") MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99")
val value1 = MoneyDyn(value = 2.0, currency = "USD") // val value1 = MoneyDyn(value = 2.0, currency = "USD")
val value2 = MoneyDyn(value = 20000.0, currency = "KHR") // val value2 = MoneyDyn(value = 20000.0, currency = "KHR")
val value3 = MoneyDyn(value = 0.99, currency = "EUR") // val value3 = MoneyDyn(value = 0.99, currency = "EUR")
value1.addMoney(value2) // value1.addMoney(value2)
value1.addMoney(value3) // value1.addMoney(value3)
//
// // val result = value1.exchange("KHR")
// println(value1.exchangeTo("KHR")) // 7
// val result = value1.exchange("KHR") val moneyUsd = Money(2.0)
println(value1.exchangeTo("KHR")) // 7 .addMoney(20000.0, "USD")
.exchange("KHR")
.toStdMoneyFormatable()
.formatMoneyDisplay()
println(moneyUsd)
} }
} }

View File

@ -3,7 +3,7 @@ package com.example.demo.domain
class Money ( class Money (
var value: Double = 0.0, var value: Double = 0.0,
var currency: Currency = Currency.USD var currency: Currency = Currency.USD
) { ) : StdMoneyFormatable {
companion object { companion object {
private const val MONEY_SPLITTER = ':' private const val MONEY_SPLITTER = ':'
fun parse(value: String): Money { 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 { override fun toString(): String {
return "Money(value=$value, currency=$currency)" return "Money(value=${formatMoneyValue()}, 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
} }
} }
enum class Currency (val symbol: Char, val displayName: String) { enum class Currency (val symbol: Char, val displayName: String) {
USD('$', "Dollar"), USD('$', "Dollar"),
KHR('R', "Riel"); KHR('R', "Riel");
companion object { companion object {
fun create(value: String): Currency { fun create(value: String): Currency {
return try { return try {

View File

@ -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
}

View File

@ -0,0 +1,6 @@
package com.example.demo.domain
interface StdMoney {
fun getMoneyValue(): Double
fun getMoneyCurrency(): String
}

View File

@ -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()}"
}
}