Add project for money utils
This commit is contained in:
13
src/main/kotlin/com/cubetiqs/libra/moneyutils/Main.kt
Normal file
13
src/main/kotlin/com/cubetiqs/libra/moneyutils/Main.kt
Normal 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)
|
||||
}
|
||||
18
src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt
Normal file
18
src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt
Normal 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
|
||||
19
src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt
Normal file
19
src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user