Codility: task3: e-commerce service/build_simple_aggregator_for_report#1
Codility: task3: e-commerce service/build_simple_aggregator_for_report#1SapozhnikovBogdan wants to merge 1 commit into
Conversation
| public class Solution { | ||
| int top; | ||
|
|
||
| List<Integer> stack; |
There was a problem hiding this comment.
В качестве stack лучше в java использовать https://docs.oracle.com/javase/7/docs/api/java/util/Deque.html
| List<Integer> stack; | ||
| public Solution() { | ||
| // write your code in Java SE 8 | ||
| stack = new ArrayList<>(); |
There was a problem hiding this comment.
реализацию структуры данных лучше сделать отдельным классом
|
|
||
| public static void test() { | ||
| // Define your tests here | ||
| Solution sol = new Solution(); |
There was a problem hiding this comment.
данные тесты лучше оформить как unit тесы. можно использовать junit
| public class EURExchangeService implements ExchangeService { | ||
| @Override | ||
| public Optional<BigDecimal> rate(String currency) { | ||
| Optional<BigDecimal> result = Optional.empty(); |
There was a problem hiding this comment.
можно сразу возвращать результат, не используя для этого доп переменную
|
|
||
| public class EURExchangeService implements ExchangeService { | ||
| @Override | ||
| public Optional<BigDecimal> rate(String currency) { |
There was a problem hiding this comment.
лучше завести спец класс или enum Currency
https://martinfowler.com/eaaCatalog/money.html
|
|
||
| public class EURExchangeService implements ExchangeService { | ||
| @Override | ||
| public Optional<BigDecimal> rate(String currency) { |
There was a problem hiding this comment.
лучше завести спец класс Money
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; |
There was a problem hiding this comment.
кажется нет смысла переопределять эти методы для агрегата
|
|
||
| return products.map(p -> | ||
| new SimpleSoldProduct(p.name, | ||
| p.price.divide(exchangeService.rate(p.currency).filter(r -> r.compareTo(BigDecimal.ZERO)>0).orElse(BigDecimal.ONE), 2, RoundingMode.HALF_EVEN)) |
There was a problem hiding this comment.
думаю эту логику стоит скрыть за интерфейсом ExchangeService
|
|
||
| return products.map(p -> | ||
| new SimpleSoldProduct(p.name, | ||
| p.price.divide(exchangeService.rate(p.currency).filter(r -> r.compareTo(BigDecimal.ZERO)>0).orElse(BigDecimal.ONE), 2, RoundingMode.HALF_EVEN)) |
There was a problem hiding this comment.
Если сделать класс Money, то это будет один из его методов
While working on an e-commerce service, you need to build a simple aggregator for a report, which will hold sold products and summ of their prices converted to EUR currency.
You are given a stream of SoldProduct objects. SoldProduct is defined as follows:
public class SoldProduct {
String name;
BigDecimal price;
String currency;
}
Write a function, which will map the Stream to an inctance of SoldProductsAggregate which is defined as follows:
public class SoldProductsAggregate {
List products;
BigDecimal total;
}
and SimpleSoldProduct:
public class SimpleSoldProduct {
private final String name;
private final BigDecimal price;
}