Add money create for static funcs

This commit is contained in:
Sambo Chea 2020-08-27 10:45:57 +07:00
parent a8841a537d
commit 377809383d
3 changed files with 44 additions and 0 deletions

View File

@ -24,5 +24,27 @@ open class Money(
companion object { companion object {
val ZERO: StdMoney val ZERO: StdMoney
get() = Money(value = 0.0) get() = Money(value = 0.0)
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)
} }
} }

View File

@ -0,0 +1,7 @@
package com.cubetiqs.libra.moneyutils
enum class MoneyCurrency {
USD,
KHR,
EUR
}

View File

@ -1,5 +1,6 @@
import com.cubetiqs.libra.moneyutils.Money import com.cubetiqs.libra.moneyutils.Money
import com.cubetiqs.libra.moneyutils.MoneyConfig import com.cubetiqs.libra.moneyutils.MoneyConfig
import com.cubetiqs.libra.moneyutils.MoneyCurrency
import com.cubetiqs.libra.moneyutils.MoneyExchangeUtils import com.cubetiqs.libra.moneyutils.MoneyExchangeUtils
import com.cubetiqs.libra.moneyutils.plus import com.cubetiqs.libra.moneyutils.plus
import com.cubetiqs.libra.moneyutils.times import com.cubetiqs.libra.moneyutils.times
@ -39,4 +40,18 @@ class MoneyTests {
Assert.assertEquals(8000.0, moneyKhr.getMoneyValue(), 0.0) Assert.assertEquals(8000.0, moneyKhr.getMoneyValue(), 0.0)
} }
@Test
fun money_exchange_config_builder_test() {
MoneyConfig.propertiesBuilder
.setDeliEqual('=')
.setDeliSplit(';')
MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99")
val moneyUsd = Money.ONE
val moneyKhr = Money.create(20000.0, MoneyCurrency.KHR)
val result = moneyUsd
}
} }