Converted CurrencySet from Java to Kotlin

jimFirstDesign
Felix Stupp 6 years ago
parent 2973716c78
commit 6438935f36

@ -1,40 +0,0 @@
package de.banananetwork.dsa.currencies;
import java.util.*;
public class CurrencySet {
private final Currency[] currencies;
private CurrencySet(Currency[] currencies) {
this.currencies = currencies;
}
public Map<Currency, Integer> transferToSet(int baseValue) {
final Map<Currency, Integer> values = new EnumMap<>(Currency.class);
for (Currency currency : currencies) {
int value = currency.transferTo(baseValue);
values.put(currency, value);
baseValue -= currency.transferFrom(value);
}
if (baseValue != 0)
throw new RuntimeException(); // TODO Change to ValueNotRepresentableException
return values;
}
public static class Builder {
private List<Currency> currencies = new LinkedList<>();
public Builder add(Currency currency) {
this.currencies.add(currency);
return this;
}
public CurrencySet create() {
return new CurrencySet(currencies.stream().sorted(Comparator.comparing(Currency::getMultiplier).reversed()).toArray(Currency[]::new));
}
}
}

@ -0,0 +1,37 @@
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())
}
}
}
Loading…
Cancel
Save