fix: Double to BigDecimal
This commit is contained in:
parent
b11607088a
commit
f42dcce74b
15 changed files with 93 additions and 57 deletions
|
@ -45,8 +45,8 @@ public class CurrencyService {
|
|||
}
|
||||
var exchanged = performExchange(from,
|
||||
Optional.ofNullable(user.currencies().get(request.to().toUpperCase())).orElse(create(request.to())),
|
||||
Optional.ofNullable(request.toSell()).orElse(0D),
|
||||
Optional.ofNullable(request.toBuy()).orElse(0D));
|
||||
Optional.ofNullable(request.toSell()).orElse(BigDecimal.ZERO),
|
||||
Optional.ofNullable(request.toBuy()).orElse(BigDecimal.ZERO));
|
||||
user.currencies().putAll(exchanged);
|
||||
return userService.update(user);
|
||||
})
|
||||
|
@ -55,32 +55,36 @@ public class CurrencyService {
|
|||
|
||||
private Currency create(String symbol) {
|
||||
// TODO: check if supported - now limited to PLN <-> USD
|
||||
return new Currency(symbol.toUpperCase(), 0D);
|
||||
return new Currency(symbol.toUpperCase(), BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
private Map<String, Currency> performExchange(Currency from, Currency to, double toSell, double toBuy) {
|
||||
double exchangeRate;
|
||||
double neededFromAmount;
|
||||
double requestedToAmount;
|
||||
private Map<String, Currency> performExchange(Currency from, Currency to, BigDecimal toSell, BigDecimal toBuy) {
|
||||
BigDecimal exchangeRate;
|
||||
BigDecimal neededFromAmount;
|
||||
BigDecimal requestedToAmount;
|
||||
if (from.symbol().equalsIgnoreCase("PLN")) {
|
||||
exchangeRate = nbpService.getSellRate(to.symbol());
|
||||
neededFromAmount = round(toBuy != 0 ? toBuy * exchangeRate : toSell);
|
||||
requestedToAmount = round(toBuy != 0 ? toBuy : toSell / exchangeRate);
|
||||
neededFromAmount = round(toBuy.signum() != 0 ? toBuy.multiply(exchangeRate) : toSell);
|
||||
requestedToAmount = round(toBuy.signum() != 0 ? toBuy : divide(toSell, exchangeRate));
|
||||
} else {
|
||||
exchangeRate = nbpService.getBuyRate(from.symbol());
|
||||
neededFromAmount = round(toBuy != 0 ? toBuy / exchangeRate : toSell);
|
||||
requestedToAmount = round(toBuy != 0 ? toBuy : toSell * exchangeRate);
|
||||
neededFromAmount = round(toBuy.signum() != 0 ? divide(toBuy, exchangeRate) : toSell);
|
||||
requestedToAmount = round(toBuy.signum() != 0 ? toBuy : toSell.multiply(exchangeRate));
|
||||
}
|
||||
if (neededFromAmount > from.amount()) {
|
||||
if (neededFromAmount.compareTo(from.amount()) > 0) {
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
var newFrom = new Currency(from.symbol(), from.amount() - neededFromAmount);
|
||||
var newTo = new Currency(to.symbol(), to.amount() + requestedToAmount);
|
||||
var newFrom = new Currency(from.symbol(), from.amount().subtract(neededFromAmount));
|
||||
var newTo = new Currency(to.symbol(), to.amount().add(requestedToAmount));
|
||||
return Stream.of(newFrom, newTo).collect(Collectors.toMap(Currency::symbol, currency -> currency));
|
||||
}
|
||||
|
||||
private double round(double input) {
|
||||
return BigDecimal.valueOf(input).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
||||
private BigDecimal round(BigDecimal input) {
|
||||
return input.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
private BigDecimal divide(BigDecimal input, BigDecimal division) {
|
||||
return input.setScale(2, RoundingMode.HALF_UP).divide(division, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue