All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.serpro69.kfaker.provider.Money.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc.7
Show newest version
package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.FakerService
import io.github.serpro69.kfaker.dictionary.Category
import io.github.serpro69.kfaker.dictionary.YamlCategory
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate

class Money internal constructor(fakerService: FakerService) : AbstractFakeDataProvider(fakerService) {
    override val category = Category.ofName("MONEY")
    override val localUniqueDataProvider = LocalUniqueDataProvider()
    override val unique: Money by UniqueProviderDelegate(localUniqueDataProvider)

    private val currencySymbol by lazy { CurrencySymbol(fakerService).symbol() }

    @JvmOverloads
    fun amount(
        range: IntRange = 0..Int.MAX_VALUE,
        generateDecimals: Boolean = true,
        thousandsSeparator: String = ",",
        decimalSeparator: String = "."
    ): String {
        val randomAmount = fakerService.randomService.nextInt(range).toString()
            .reversed()
            .chunked(3)
            .joinToString(thousandsSeparator)
            .reversed()

        return if (generateDecimals) {
            val randomDecimal = fakerService.randomService.nextInt(0..99).toString().let {
                if (it.length == 1) "0$it" else it
            }
            "$currencySymbol$randomAmount$decimalSeparator$randomDecimal"
        } else "$currencySymbol$randomAmount"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy