Implemented CurrencySet
parent
764d8dcd74
commit
2973716c78
@ -0,0 +1,40 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue