Skip to content

Codility: task3: e-commerce service/build_simple_aggregator_for_report#1

Open
SapozhnikovBogdan wants to merge 1 commit into
masterfrom
task_3_streams
Open

Codility: task3: e-commerce service/build_simple_aggregator_for_report#1
SapozhnikovBogdan wants to merge 1 commit into
masterfrom
task_3_streams

Conversation

@SapozhnikovBogdan

Copy link
Copy Markdown
Owner

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

public class Solution {
int top;

List<Integer> stack;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В качестве 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<>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

реализацию структуры данных лучше сделать отдельным классом


public static void test() {
// Define your tests here
Solution sol = new Solution();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

данные тесты лучше оформить как unit тесы. можно использовать junit

public class EURExchangeService implements ExchangeService {
@Override
public Optional<BigDecimal> rate(String currency) {
Optional<BigDecimal> result = Optional.empty();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно сразу возвращать результат, не используя для этого доп переменную


public class EURExchangeService implements ExchangeService {
@Override
public Optional<BigDecimal> rate(String currency) {

@mikhail-sokolov mikhail-sokolov Jul 22, 2019

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше завести спец класс или enum Currency
https://martinfowler.com/eaaCatalog/money.html


public class EURExchangeService implements ExchangeService {
@Override
public Optional<BigDecimal> rate(String currency) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше завести спец класс Money


@Override
public boolean equals(Object o) {
if (this == o) return true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кажется нет смысла переопределять эти методы для агрегата


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

думаю эту логику стоит скрыть за интерфейсом 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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если сделать класс Money, то это будет один из его методов

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants