-
Notifications
You must be signed in to change notification settings - Fork 0
Codility: task3: e-commerce service/build_simple_aggregator_for_report #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>home.com</groupId> | ||
| <artifactId>codility</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <!-- <properties> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| </properties>--> | ||
|
|
||
| <dependencies> | ||
| <!-- junit --> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.6.1</version> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package task2_stack_transaction; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Solution { | ||
| int top; | ||
|
|
||
| List<Integer> stack; | ||
| public Solution() { | ||
| // write your code in Java SE 8 | ||
| stack = new ArrayList<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. реализацию структуры данных лучше сделать отдельным классом |
||
| } | ||
|
|
||
| public void push(int value) { | ||
| top = value; | ||
| stack.add(top); | ||
| } | ||
|
|
||
| public int top() { | ||
| return top; | ||
| } | ||
|
|
||
| public void pop() { | ||
| if (stack.isEmpty()){ | ||
| return; | ||
| } | ||
| stack.remove(stack.size()-1); | ||
| top = stack.get(stack.size()-1); | ||
| } | ||
|
|
||
| public void begin() { | ||
|
|
||
| } | ||
|
|
||
| public boolean rollback() { | ||
| return false; | ||
| } | ||
|
|
||
| public boolean commit() { | ||
| return false; | ||
| } | ||
|
|
||
| public static void test() { | ||
| // Define your tests here | ||
| Solution sol = new Solution(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. данные тесты лучше оформить как unit тесы. можно использовать junit |
||
| sol.push(5); | ||
| sol.push(2); | ||
| assert sol.top() == 2 : "top() should be 2"; | ||
| sol.pop(); | ||
| assert sol.top() == 5 : "top() should be 5"; | ||
|
|
||
| Solution sol2 = new Solution(); | ||
| assert sol2.top() == 0; | ||
| sol2.pop(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package task2_stack_transaction; | ||
|
|
||
| public class main { | ||
| public static void main(String[] args){ | ||
| System.out.println("Test"); | ||
| Solution.test(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package task3_streams; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Optional; | ||
|
|
||
| public class EURExchangeService implements ExchangeService { | ||
| @Override | ||
| public Optional<BigDecimal> rate(String currency) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше завести спец класс или enum Currency There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше завести спец класс Money |
||
| Optional<BigDecimal> result = Optional.empty(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно сразу возвращать результат, не используя для этого доп переменную |
||
| switch (currency){ | ||
| case "USD": result = Optional.of(new BigDecimal("1.13")); | ||
| break; | ||
| case "UAH": result = Optional.of(new BigDecimal("29.50")); | ||
| break; | ||
| case "RUB": result = Optional.of(new BigDecimal("71.40")); | ||
| break; | ||
| default: break; | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package task3_streams; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Optional; | ||
|
|
||
| public interface ExchangeService { | ||
| Optional<BigDecimal> rate(String currency); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package task3_streams; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public class SimpleSoldProduct { | ||
| private final String name; | ||
| private final BigDecimal price; | ||
|
|
||
| public SimpleSoldProduct(String name, BigDecimal price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "task3_streams.SimpleSoldProduct{" + | ||
| "name='" + name + '\'' + | ||
| ", price=" + price + | ||
| '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| SimpleSoldProduct that = (SimpleSoldProduct) o; | ||
|
|
||
| if (name != null ? !name.equals(that.name) : that.name != null) return false; | ||
| return price != null ? price.equals(that.price) : that.price == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = name != null ? name.hashCode() : 0; | ||
| result = 31 * result + (price != null ? price.hashCode() : 0); | ||
| return result; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package task3_streams; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public class SoldProduct { | ||
| String name; | ||
| BigDecimal price; | ||
| String currency; | ||
|
|
||
| public SoldProduct(String name, BigDecimal price, String currency) { | ||
| this.name = name; | ||
| this.price = price; | ||
| this.currency = currency; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public String getCurrency() { | ||
| return currency; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "task3_streams.SoldProduct{" + | ||
| "name='" + name + '\'' + | ||
| ", price=" + price + | ||
| ", currency='" + currency + '\'' + | ||
| '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| SoldProduct that = (SoldProduct) o; | ||
|
|
||
| if (name != null ? !name.equals(that.name) : that.name != null) return false; | ||
| if (price != null ? !price.equals(that.price) : that.price != null) return false; | ||
| return currency != null ? currency.equals(that.currency) : that.currency == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = name != null ? name.hashCode() : 0; | ||
| result = 31 * result + (price != null ? price.hashCode() : 0); | ||
| result = 31 * result + (currency != null ? currency.hashCode() : 0); | ||
| return result; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package task3_streams; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| public class SoldProductsAggregate { | ||
| List<SimpleSoldProduct> products; | ||
| BigDecimal total; | ||
|
|
||
| public SoldProductsAggregate(List<SimpleSoldProduct> products, BigDecimal total) { | ||
| this.products = products; | ||
| this.total = total; | ||
| } | ||
|
|
||
| public List<SimpleSoldProduct> getProducts() { | ||
| return products; | ||
| } | ||
|
|
||
| public BigDecimal getTotal() { | ||
| return total; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "task3_streams.SoldProductsAggregate{" + | ||
| "products=" + products + | ||
| ", total=" + total + | ||
| '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. кажется нет смысла переопределять эти методы для агрегата |
||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| SoldProductsAggregate that = (SoldProductsAggregate) o; | ||
|
|
||
| if (products != null ? !products.equals(that.products) : that.products != null) return false; | ||
| return total != null ? total.equals(that.total) : that.total == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = products != null ? products.hashCode() : 0; | ||
| result = 31 * result + (total != null ? total.hashCode() : 0); | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package task3_streams; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collector; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class SoldProductsAggregator { | ||
|
|
||
| private final EURExchangeService exchangeService; | ||
|
|
||
| SoldProductsAggregator(EURExchangeService EURExchangeService) { | ||
| this.exchangeService = EURExchangeService; | ||
| } | ||
|
|
||
| SoldProductsAggregate aggregate(Stream<SoldProduct> products) { | ||
|
|
||
| Collector<SimpleSoldProduct, List<SimpleSoldProduct>, SoldProductsAggregate> soldProductsCollector = | ||
| Collector.of(ArrayList<SimpleSoldProduct>::new, | ||
| List::add, | ||
| (l1, l2) -> {l1.addAll(l2); return l1;}, | ||
| (l)->new SoldProductsAggregate(l, | ||
| new BigDecimal(l.stream().mapToDouble(p->p.getPrice().doubleValue()).sum()).setScale(2, RoundingMode.HALF_EVEN)) | ||
| ); | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. думаю эту логику стоит скрыть за интерфейсом ExchangeService There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если сделать класс Money, то это будет один из его методов |
||
| ).collect(soldProductsCollector); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package task3_streams; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import task3_streams.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class SoldProductsAggregatorTest { | ||
|
|
||
| private EURExchangeService exchangeService; | ||
|
|
||
| @Before | ||
| public void init(){ | ||
| exchangeService = new EURExchangeService(); | ||
| } | ||
|
|
||
| @Test | ||
| public void aggregate() throws Exception { | ||
| SoldProductsAggregate soldProductsAggregate; | ||
|
|
||
| List<SimpleSoldProduct> simpleSoldProducts = Arrays.asList( | ||
| new SimpleSoldProduct("Monitor", new BigDecimal("200.53")), | ||
| new SimpleSoldProduct("PC", new BigDecimal(("523.30"))), | ||
| new SimpleSoldProduct("Keyboard", new BigDecimal("15.00")), | ||
| new SimpleSoldProduct("Mouse", new BigDecimal("2.35"))); | ||
|
|
||
| List<SoldProduct> soldProducts = Arrays.asList( | ||
| new SoldProduct("Monitor", new BigDecimal("200.53"), "USD"), | ||
| new SoldProduct("PC", new BigDecimal(("10523.30")), "UAH"), | ||
| new SoldProduct("Keyboard", new BigDecimal("15.00"), "USD"), | ||
| new SoldProduct("Mouse", new BigDecimal("282.35"), "RUB")); | ||
|
|
||
| SoldProductsAggregator soldAgg = new SoldProductsAggregator(exchangeService); | ||
| soldProductsAggregate = soldAgg.aggregate(soldProducts.stream()); | ||
| System.out.println(soldProductsAggregate); | ||
| //assertEquals(2000.55, soldProductsAggregate.getTotal().doubleValue(), 2.00); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
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