2021-02-08 11:24:14 +07:00
|
|
|
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 {
|
|
|
|
/**
|
|
|
|
* Get money's value from current state.
|
|
|
|
*
|
|
|
|
* @return Double
|
|
|
|
*/
|
2021-02-08 21:34:17 +07:00
|
|
|
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
|
|
|
*/
|
2021-02-08 21:34:17 +07:00
|
|
|
fun getCurrency(): Currency
|
2021-02-08 12:39:52 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow for money currency called and implemented
|
|
|
|
*/
|
|
|
|
interface Currency {
|
|
|
|
fun getCurrency(): String
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ExchangeOperator {
|
|
|
|
fun StdMoney.getExchangedTo(currency: Currency): Double
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Operator<T : StdMoney> {
|
|
|
|
fun plus(other: StdMoney): T
|
2021-02-08 16:08:00 +07:00
|
|
|
fun minus(other: StdMoney): T
|
2021-02-08 12:39:52 +07:00
|
|
|
fun divide(other: StdMoney): T
|
|
|
|
|
|
|
|
fun inc(): T
|
|
|
|
fun dec(): T
|
|
|
|
fun multiply(other: StdMoney): T
|
|
|
|
|
|
|
|
// assign operators
|
2021-02-08 16:08:00 +07:00
|
|
|
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 {
|
|
|
|
return currency?.toUpperCase()?.trim() ?: "USD"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val USD
|
|
|
|
get() = initCurrency("USD")
|
|
|
|
val KHR
|
|
|
|
get() = initCurrency("KHR")
|
|
|
|
|
|
|
|
fun initMoney(initValue: Double, currency: Currency = USD): StdMoney {
|
|
|
|
return object : StdMoney {
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): Currency {
|
2021-02-08 16:08:00 +07:00
|
|
|
return currency
|
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
2021-02-08 16:08:00 +07:00
|
|
|
return initValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val ZERO
|
|
|
|
get() = initMoney(0.0, USD)
|
2021-02-08 12:39:52 +07:00
|
|
|
}
|
2020-08-26 20:02:36 +07:00
|
|
|
}
|