Add project for money utils

This commit is contained in:
2020-08-26 20:02:36 +07:00
commit 11e25fb2bd
18 changed files with 641 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package com.cubetiqs.libra.moneyutils
class Main {
fun run() = print("Just mind!")
}
fun main() {
Main().run()
val money = Money(10.0)
val money2 = Money(20.0)
money *= money
println((money + money2) * money2)
}

View File

@@ -0,0 +1,18 @@
package com.cubetiqs.libra.moneyutils
data class Money(
var value: Double,
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,19 @@
package com.cubetiqs.libra.moneyutils
/**
* Default standard money style
* We have only value and currency of money
*/
interface StdMoney {
/**
* Get money's value from current state.
*
* @return Double
*/
fun getValue(): Double
/**
* Get money's currency from current state.
*/
fun getCurrency(): String
}