You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.0 KiB
Kotlin
38 lines
1.0 KiB
Kotlin
package de.banananetwork.dsa.currencies
|
|
|
|
import java.util.*
|
|
import kotlin.collections.ArrayList
|
|
|
|
class CurrencySet private constructor(private val currencies: Array<Currency>) {
|
|
|
|
fun transferToSet(baseValue: Int): Map<Currency, Int> {
|
|
var modBaseValue = baseValue
|
|
val values = EnumMap<Currency, Int>(Currency::class.java)
|
|
for (currency in currencies) {
|
|
val value = currency.transferTo(modBaseValue)
|
|
values[currency] = value
|
|
modBaseValue -= currency.transferFrom(value)
|
|
}
|
|
if (modBaseValue != 0)
|
|
throw RuntimeException() // TODO Change to ValueNotRepresentableException
|
|
return values
|
|
}
|
|
|
|
class Builder {
|
|
|
|
private val currencies = ArrayList<Currency>()
|
|
|
|
fun add(currency: Currency): Builder {
|
|
currencies.add(currency)
|
|
return this
|
|
}
|
|
|
|
fun create(): CurrencySet {
|
|
currencies.sortBy { it.multiplier }
|
|
return CurrencySet(currencies.toTypedArray())
|
|
}
|
|
|
|
}
|
|
|
|
}
|