Updated the demo for some functions
This commit is contained in:
parent
d80ad060dc
commit
9e7e50a1ba
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
38
src/main/kotlin/com/example/demo/domain/MoneyExtension.kt
Normal file
38
src/main/kotlin/com/example/demo/domain/MoneyExtension.kt
Normal 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
|
||||
}
|
6
src/main/kotlin/com/example/demo/domain/StdMoney.kt
Normal file
6
src/main/kotlin/com/example/demo/domain/StdMoney.kt
Normal file
@ -0,0 +1,6 @@
|
||||
package com.example.demo.domain
|
||||
|
||||
interface StdMoney {
|
||||
fun getMoneyValue(): Double
|
||||
fun getMoneyCurrency(): String
|
||||
}
|
@ -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()}"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user