money-module/src/main/kotlin/com/cubetiqs/money/StdMoney.kt

88 lines
2.1 KiB
Kotlin
Raw Normal View History

package com.cubetiqs.money
2020-08-26 20:02:36 +07:00
/**
* Default standard money style
* We have only value and currency of money
2020-08-26 20:06:06 +07:00
*
* @author sombochea
* @since 1.0
2020-08-26 20:02:36 +07:00
*/
interface StdMoney : MoneyMixin {
2020-08-26 20:02:36 +07:00
/**
* Get money's value from current state.
*
* @return Double
*/
fun getValue(): Double
2020-08-26 20:02:36 +07:00
/**
* Get money's currency from current state.
2020-08-26 20:06:06 +07:00
*
* @return String
2020-08-26 20:02:36 +07:00
*/
fun getCurrency(): Currency
/**
* Allow for money currency called and implemented
*/
interface Currency {
fun getCurrency(): String
fun isEquals(other: Currency): Boolean {
return this.getCurrency().equals(other.getCurrency(), ignoreCase = true)
}
}
interface ExchangeOperator {
fun StdMoney.getExchangedTo(currency: Currency): Double
}
interface Operator<T : StdMoney> {
fun plus(other: StdMoney): T
fun minus(other: StdMoney): T
fun divide(other: StdMoney): T
fun inc(): T
fun dec(): T
fun multiply(other: StdMoney): T
// assign operators
fun plusAssign(other: StdMoney): T
fun minusAssign(other: StdMoney): T
fun divideAssign(other: StdMoney): T
fun multiplyAssign(other: StdMoney): T
// none-of-return
fun nor() {}
}
companion object {
fun initCurrency(currency: String?): Currency {
return object : Currency {
override fun getCurrency(): String {
2022-12-16 20:49:57 +07:00
return currency?.uppercase()?.trim() ?: "USD"
}
}
}
val USD
get() = initCurrency("USD")
val KHR
get() = initCurrency("KHR")
fun initMoney(initValue: Double, currency: Currency = USD): StdMoney {
return object : StdMoney {
override fun getCurrency(): Currency {
return currency
}
override fun getValue(): Double {
return initValue
}
}
}
val ZERO
get() = initMoney(0.0, USD)
}
2020-08-26 20:02:36 +07:00
}