Add money config and operator

This commit is contained in:
Sambo Chea 2020-08-26 20:06:06 +07:00
parent 11e25fb2bd
commit 9b7a215555
4 changed files with 71 additions and 11 deletions

View File

@ -5,14 +5,3 @@ data class Money(
var currency: String = "USD" var currency: String = "USD"
) )
operator fun Money.unaryMinus() = (-value)
operator fun Money.unaryPlus() = (+value)
operator fun Money.inc() = Money(value++)
operator fun Money.dec() = Money(value--)
operator fun Money.plus(other: Money) = Money(value + other.value)
operator fun Money.times(other: Money) = Money(value * other.value)
operator fun Money.div(other: Money) = Money(value / other.value)
operator fun Money.timesAssign(other: Money) {
this.value = this.value * other.value
}
operator fun Money.not() = this.value != this.value

View File

@ -0,0 +1,53 @@
package com.cubetiqs.libra.moneyutils
/**
* Default money config in static object.
*
* @author sombochea
* @since 1.0
*/
object MoneyConfig {
private const val SPLIT_ORD_1 = ','
private const val SPLIT_ORD_2 = '='
/**
* Key is the currency
* Value is the rate
*/
private val config = mutableMapOf<String, Double>()
fun parse(config: String): Map<String, Double> {
val result = mutableMapOf<String, Double>()
val rates = config.split(SPLIT_ORD_1)
/**
* USD=1
* KHR=4000
* EUR=0.99
*
* USD -> currency
* 1 -> value
*/
rates.map { i ->
val temp = i.split(SPLIT_ORD_2)
if (temp.size == 2) {
val currency = temp[0]
val value = temp[1].toDouble()
result.put(currency.toUpperCase(), value)
} else {
throw IllegalArgumentException("invalid value!")
}
}
// if you want to use all set currencies without remove from memory, we should remove this line
this.config.clear()
this.config.putAll(result)
return result
}
// all currencies with its rate
fun getConfig() = this.config
fun getRate(currency: String): Double {
return getConfig()[currency.toUpperCase()] ?: throw IllegalArgumentException("currency not found")
}
}

View File

@ -0,0 +1,13 @@
package com.cubetiqs.libra.moneyutils
operator fun Money.unaryMinus() = (-value)
operator fun Money.unaryPlus() = (+value)
operator fun Money.inc() = Money(value++)
operator fun Money.dec() = Money(value--)
operator fun Money.plus(other: Money) = Money(value + other.value)
operator fun Money.times(other: Money) = Money(value * other.value)
operator fun Money.div(other: Money) = Money(value / other.value)
operator fun Money.timesAssign(other: Money) {
this.value = this.value * other.value
}
operator fun Money.not() = this.value != this.value

View File

@ -3,6 +3,9 @@ package com.cubetiqs.libra.moneyutils
/** /**
* Default standard money style * Default standard money style
* We have only value and currency of money * We have only value and currency of money
*
* @author sombochea
* @since 1.0
*/ */
interface StdMoney { interface StdMoney {
/** /**
@ -14,6 +17,8 @@ interface StdMoney {
/** /**
* Get money's currency from current state. * Get money's currency from current state.
*
* @return String
*/ */
fun getCurrency(): String fun getCurrency(): String
} }