2021-02-08 11:24:14 +07:00
|
|
|
package com.cubetiqs.money
|
2020-08-26 20:02:36 +07:00
|
|
|
|
2020-08-26 21:19:31 +07:00
|
|
|
open class Money(
|
2021-02-08 12:39:52 +07:00
|
|
|
private var value: Double = 0.0,
|
|
|
|
private val currency: StdMoney.Currency = MoneyCurrency.USD,
|
|
|
|
) : StdMoney, StdMoney.Operator<Money>, StdMoney.ExchangeOperator {
|
2020-08-26 21:32:06 +07:00
|
|
|
//////////////////// - PROPERTIES - ////////////////////
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
2020-08-26 21:32:06 +07:00
|
|
|
return this.value
|
2020-08-26 21:19:31 +07:00
|
|
|
}
|
|
|
|
|
2021-02-08 12:39:52 +07:00
|
|
|
// not imply with exchange rate yet
|
|
|
|
override fun StdMoney.getExchangedTo(currency: StdMoney.Currency): Double {
|
2021-02-08 21:34:17 +07:00
|
|
|
return MoneyExchangeUtils.exchange(this, currency).getValue()
|
2021-02-08 12:39:52 +07:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): StdMoney.Currency {
|
2020-08-27 21:42:05 +07:00
|
|
|
return this.currency
|
2020-08-26 21:19:31 +07:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:06 +07:00
|
|
|
//////////////////// - GENERIC - ////////////////////
|
2020-08-26 21:19:31 +07:00
|
|
|
|
|
|
|
override fun toString(): String {
|
2021-02-08 21:34:17 +07:00
|
|
|
return "Money(value=${getValue()}, currency='${getCurrency().getCurrency()}')"
|
2020-08-26 21:19:31 +07:00
|
|
|
}
|
2020-08-26 21:44:33 +07:00
|
|
|
|
2021-02-08 12:39:52 +07:00
|
|
|
override fun inc(): Money = apply {
|
|
|
|
this.value += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun dec(): Money = apply {
|
|
|
|
this.value -= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun plus(other: StdMoney): Money {
|
|
|
|
val temp = this.value + other.getExchangedTo(this.currency)
|
|
|
|
return Money(value = temp, currency = this.currency)
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:08:00 +07:00
|
|
|
override fun minus(other: StdMoney): Money {
|
|
|
|
val temp = this.value - other.getExchangedTo(this.currency)
|
|
|
|
return Money(value = temp, currency = this.currency)
|
|
|
|
}
|
|
|
|
|
2021-02-08 12:39:52 +07:00
|
|
|
override fun divide(other: StdMoney): Money {
|
|
|
|
val temp = this.value / other.getExchangedTo(this.currency)
|
|
|
|
return Money(value = temp, currency = this.currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun multiply(other: StdMoney): Money {
|
2021-02-08 21:34:17 +07:00
|
|
|
val temp = this.value * other.getExchangedTo(this.getCurrency())
|
2021-02-08 12:39:52 +07:00
|
|
|
return Money(value = temp, currency = this.currency)
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:08:00 +07:00
|
|
|
override fun plusAssign(other: StdMoney): Money = apply {
|
2021-02-08 12:39:52 +07:00
|
|
|
this.value = this.value + other.getExchangedTo(this.currency)
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:08:00 +07:00
|
|
|
override fun minusAssign(other: StdMoney): Money = apply {
|
|
|
|
this.value = this.value - other.getExchangedTo(this.currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun divideAssign(other: StdMoney): Money = apply {
|
2021-02-08 12:39:52 +07:00
|
|
|
this.value = this.value / other.getExchangedTo(this.currency)
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:08:00 +07:00
|
|
|
override fun multiplyAssign(other: StdMoney): Money = apply {
|
2021-02-08 21:34:17 +07:00
|
|
|
this.value = this.value * other.getExchangedTo(this.getCurrency())
|
2021-02-08 12:39:52 +07:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:44:33 +07:00
|
|
|
companion object {
|
2020-08-27 10:45:57 +07:00
|
|
|
val ONE: StdMoney
|
2021-02-08 16:08:00 +07:00
|
|
|
get() = StdMoney.initMoney(1.0)
|
2021-02-08 12:39:52 +07:00
|
|
|
|
2020-08-27 10:45:57 +07:00
|
|
|
val TEN: StdMoney
|
2021-02-08 16:08:00 +07:00
|
|
|
get() = StdMoney.initMoney(10.0)
|
2020-08-27 10:45:57 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new money object with custom value
|
|
|
|
*
|
|
|
|
* @param value Double
|
2021-02-08 12:39:52 +07:00
|
|
|
* @param currency MoneyCurrency
|
2020-08-27 10:45:57 +07:00
|
|
|
*/
|
2021-02-08 12:39:52 +07:00
|
|
|
fun create(value: Double, currency: StdMoney.Currency): Money {
|
2020-08-27 10:45:57 +07:00
|
|
|
return Money(value = value, currency = currency)
|
|
|
|
}
|
|
|
|
|
2021-02-08 12:39:52 +07:00
|
|
|
fun from(value: Double, currency: String): Money {
|
2021-02-08 16:08:00 +07:00
|
|
|
return create(value, StdMoney.initCurrency(currency))
|
2021-02-08 12:39:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
fun from(money: StdMoney): Money {
|
2021-02-08 21:34:17 +07:00
|
|
|
return create(value = money.getValue(), currency = money.getCurrency())
|
2021-02-08 12:39:52 +07:00
|
|
|
}
|
2020-08-26 21:44:33 +07:00
|
|
|
}
|
2020-08-26 21:19:31 +07:00
|
|
|
}
|