feat: Currency symbols enum
This commit is contained in:
parent
f42dcce74b
commit
11d6e41c98
22 changed files with 248 additions and 129 deletions
|
@ -4,6 +4,7 @@ import eu.ztsh.wymiana.exception.ExchangeFailedException;
|
|||
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
||||
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
||||
import eu.ztsh.wymiana.model.Currency;
|
||||
import eu.ztsh.wymiana.model.Symbol;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||
|
@ -28,23 +29,17 @@ public class CurrencyService {
|
|||
public User exchange(CurrencyExchangeRequest request) {
|
||||
validator.validate(request);
|
||||
return userService.get(request.pesel()).map(user -> {
|
||||
if (!request.from().equalsIgnoreCase("PLN") && !request.to().equalsIgnoreCase("PLN")) {
|
||||
if (!request.from().equals(Symbol.PLN) && !request.to().equals(Symbol.PLN)) {
|
||||
throw new ExchangeFailedException("Either 'from' or 'to' has to be PLN");
|
||||
}
|
||||
// As we support only USD now, we need to limit second parameter too
|
||||
// Begin: unlock other currencies
|
||||
if (!request.from().equalsIgnoreCase("USD") && !request.to().equalsIgnoreCase("USD")) {
|
||||
throw new ExchangeFailedException("Either 'from' or 'to' has to be USD");
|
||||
}
|
||||
// End: unlock other currencies
|
||||
|
||||
var from = user.currencies().get(request.from().toUpperCase());
|
||||
var from = user.currencies().get(request.from());
|
||||
if (from == null) {
|
||||
// There is no currency 'from' opened so no need to check if user has funds to exchange
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
var exchanged = performExchange(from,
|
||||
Optional.ofNullable(user.currencies().get(request.to().toUpperCase())).orElse(create(request.to())),
|
||||
Optional.ofNullable(user.currencies().get(request.to())).orElse(create(request.to())),
|
||||
Optional.ofNullable(request.toSell()).orElse(BigDecimal.ZERO),
|
||||
Optional.ofNullable(request.toBuy()).orElse(BigDecimal.ZERO));
|
||||
user.currencies().putAll(exchanged);
|
||||
|
@ -53,16 +48,15 @@ public class CurrencyService {
|
|||
.orElseThrow(() -> new UserNotFoundException(request));
|
||||
}
|
||||
|
||||
private Currency create(String symbol) {
|
||||
// TODO: check if supported - now limited to PLN <-> USD
|
||||
return new Currency(symbol.toUpperCase(), BigDecimal.ZERO);
|
||||
private Currency create(Symbol symbol) {
|
||||
return new Currency(symbol, BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
private Map<String, Currency> performExchange(Currency from, Currency to, BigDecimal toSell, BigDecimal toBuy) {
|
||||
private Map<Symbol, Currency> performExchange(Currency from, Currency to, BigDecimal toSell, BigDecimal toBuy) {
|
||||
BigDecimal exchangeRate;
|
||||
BigDecimal neededFromAmount;
|
||||
BigDecimal requestedToAmount;
|
||||
if (from.symbol().equalsIgnoreCase("PLN")) {
|
||||
if (from.symbol().equals(Symbol.PLN)) {
|
||||
exchangeRate = nbpService.getSellRate(to.symbol());
|
||||
neededFromAmount = round(toBuy.signum() != 0 ? toBuy.multiply(exchangeRate) : toSell);
|
||||
requestedToAmount = round(toBuy.signum() != 0 ? toBuy : divide(toSell, exchangeRate));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue