From 377809383d616ee669d71c7634c7399e58ab8c0b Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 27 Aug 2020 10:45:57 +0700 Subject: [PATCH] Add money create for static funcs --- .../com/cubetiqs/libra/moneyutils/Money.kt | 22 +++++++++++++++++++ .../libra/moneyutils/MoneyCurrency.kt | 7 ++++++ src/test/kotlin/MoneyTests.kt | 15 +++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyCurrency.kt diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt index f7a1a1f..a452b8e 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt @@ -24,5 +24,27 @@ open class Money( companion object { val ZERO: StdMoney 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) } } diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyCurrency.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyCurrency.kt new file mode 100644 index 0000000..fbe0247 --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyCurrency.kt @@ -0,0 +1,7 @@ +package com.cubetiqs.libra.moneyutils + +enum class MoneyCurrency { + USD, + KHR, + EUR +} \ No newline at end of file diff --git a/src/test/kotlin/MoneyTests.kt b/src/test/kotlin/MoneyTests.kt index 87e292f..f9991c4 100644 --- a/src/test/kotlin/MoneyTests.kt +++ b/src/test/kotlin/MoneyTests.kt @@ -1,5 +1,6 @@ import com.cubetiqs.libra.moneyutils.Money import com.cubetiqs.libra.moneyutils.MoneyConfig +import com.cubetiqs.libra.moneyutils.MoneyCurrency import com.cubetiqs.libra.moneyutils.MoneyExchangeUtils import com.cubetiqs.libra.moneyutils.plus import com.cubetiqs.libra.moneyutils.times @@ -39,4 +40,18 @@ class MoneyTests { 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 + } } \ No newline at end of file