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

51 lines
1.4 KiB
Kotlin
Raw Normal View History

package com.cubetiqs.money
2020-08-26 20:02:36 +07:00
2020-08-26 21:19:31 +07:00
open class Money(
2020-08-26 21:32:06 +07:00
var value: Double,
@SpecialString(trim = true, upperCase = true) private var currency: String = "USD"
2020-08-26 21:19:31 +07:00
) : StdMoney {
2020-08-26 20:02:36 +07:00
2020-08-26 21:32:06 +07:00
//////////////////// - PROPERTIES - ////////////////////
override fun getMoneyValue(): Double {
return this.value
2020-08-26 21:19:31 +07:00
}
2020-08-26 21:32:06 +07:00
override fun getMoneyCurrency(): String {
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 {
2020-08-26 21:32:06 +07:00
return "Money(value=${getMoneyValue()}, currency='${getMoneyCurrency()}')"
2020-08-26 21:19:31 +07:00
}
companion object {
val ZERO: StdMoney
get() = Money(value = 0.0)
2020-08-27 10:45:57 +07:00
val ONE: StdMoney
get() = Money(value = 1.0)
val TEN: StdMoney
get() = Money(value = 10.0)
/**
* Create a new money object with custom value
*
* @param value Double
* @param currency String
*/
fun create(value: Double, currency: String = MoneyCurrency.USD.name): StdMoney {
return Money(value = value, currency = currency)
}
/**
* Create a new money object with custom value
*
* @param value Double
* @param currency MoneyCurrency
*/
fun create(value: Double, currency: MoneyCurrency = MoneyCurrency.USD): StdMoney = create(value, currency.name)
}
2020-08-26 21:19:31 +07:00
}