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.
dsaCalc/src/de/banananetwork/dsa/currencies/CurrencySet.java

41 lines
1009 B
Java

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));
}
}
}