From f4856c8258e181e04425e8a2c53445030125a3ab Mon Sep 17 00:00:00 2001 From: bogdan Date: Wed, 3 Jul 2019 12:20:13 +0300 Subject: [PATCH] Codility: task3: e-commerce service/build_simple_aggregator_for_report --- pom.xml | 40 +++++++++++++ .../task2_stack_transaction/Solution.java | 57 +++++++++++++++++++ .../java/task2_stack_transaction/main.java | 8 +++ .../task3_streams/EURExchangeService.java | 21 +++++++ .../java/task3_streams/ExchangeService.java | 8 +++ .../java/task3_streams/SimpleSoldProduct.java | 47 +++++++++++++++ src/main/java/task3_streams/SoldProduct.java | 56 ++++++++++++++++++ .../task3_streams/SoldProductsAggregate.java | 48 ++++++++++++++++ .../task3_streams/SoldProductsAggregator.java | 34 +++++++++++ .../SoldProductsAggregatorTest.java | 42 ++++++++++++++ 10 files changed, 361 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/task2_stack_transaction/Solution.java create mode 100644 src/main/java/task2_stack_transaction/main.java create mode 100644 src/main/java/task3_streams/EURExchangeService.java create mode 100644 src/main/java/task3_streams/ExchangeService.java create mode 100644 src/main/java/task3_streams/SimpleSoldProduct.java create mode 100644 src/main/java/task3_streams/SoldProduct.java create mode 100644 src/main/java/task3_streams/SoldProductsAggregate.java create mode 100644 src/main/java/task3_streams/SoldProductsAggregator.java create mode 100644 src/test/java/task3_streams/SoldProductsAggregatorTest.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..db6c67a --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + home.com + codility + 1.0-SNAPSHOT + + + + + + + junit + junit + 4.12 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + + \ No newline at end of file diff --git a/src/main/java/task2_stack_transaction/Solution.java b/src/main/java/task2_stack_transaction/Solution.java new file mode 100644 index 0000000..4560eb5 --- /dev/null +++ b/src/main/java/task2_stack_transaction/Solution.java @@ -0,0 +1,57 @@ +package task2_stack_transaction; + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + int top; + + List stack; + public Solution() { + // write your code in Java SE 8 + stack = new ArrayList<>(); + } + + 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(); + 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(); + } +}; diff --git a/src/main/java/task2_stack_transaction/main.java b/src/main/java/task2_stack_transaction/main.java new file mode 100644 index 0000000..248d576 --- /dev/null +++ b/src/main/java/task2_stack_transaction/main.java @@ -0,0 +1,8 @@ +package task2_stack_transaction; + +public class main { + public static void main(String[] args){ + System.out.println("Test"); + Solution.test(); + } +} diff --git a/src/main/java/task3_streams/EURExchangeService.java b/src/main/java/task3_streams/EURExchangeService.java new file mode 100644 index 0000000..d662a92 --- /dev/null +++ b/src/main/java/task3_streams/EURExchangeService.java @@ -0,0 +1,21 @@ +package task3_streams; + +import java.math.BigDecimal; +import java.util.Optional; + +public class EURExchangeService implements ExchangeService { + @Override + public Optional rate(String currency) { + Optional result = Optional.empty(); + 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; + } +} diff --git a/src/main/java/task3_streams/ExchangeService.java b/src/main/java/task3_streams/ExchangeService.java new file mode 100644 index 0000000..4d64a7f --- /dev/null +++ b/src/main/java/task3_streams/ExchangeService.java @@ -0,0 +1,8 @@ +package task3_streams; + +import java.math.BigDecimal; +import java.util.Optional; + +public interface ExchangeService { + Optional rate(String currency); +} diff --git a/src/main/java/task3_streams/SimpleSoldProduct.java b/src/main/java/task3_streams/SimpleSoldProduct.java new file mode 100644 index 0000000..1289d01 --- /dev/null +++ b/src/main/java/task3_streams/SimpleSoldProduct.java @@ -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; + } +} diff --git a/src/main/java/task3_streams/SoldProduct.java b/src/main/java/task3_streams/SoldProduct.java new file mode 100644 index 0000000..5c28175 --- /dev/null +++ b/src/main/java/task3_streams/SoldProduct.java @@ -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; + } +} diff --git a/src/main/java/task3_streams/SoldProductsAggregate.java b/src/main/java/task3_streams/SoldProductsAggregate.java new file mode 100644 index 0000000..6ce6ab7 --- /dev/null +++ b/src/main/java/task3_streams/SoldProductsAggregate.java @@ -0,0 +1,48 @@ +package task3_streams; + +import java.math.BigDecimal; +import java.util.List; + +public class SoldProductsAggregate { + List products; + BigDecimal total; + + public SoldProductsAggregate(List products, BigDecimal total) { + this.products = products; + this.total = total; + } + + public List 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; + 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; + } +} diff --git a/src/main/java/task3_streams/SoldProductsAggregator.java b/src/main/java/task3_streams/SoldProductsAggregator.java new file mode 100644 index 0000000..3df2638 --- /dev/null +++ b/src/main/java/task3_streams/SoldProductsAggregator.java @@ -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 products) { + + Collector, SoldProductsAggregate> soldProductsCollector = + Collector.of(ArrayList::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)) + ).collect(soldProductsCollector); + } + +} \ No newline at end of file diff --git a/src/test/java/task3_streams/SoldProductsAggregatorTest.java b/src/test/java/task3_streams/SoldProductsAggregatorTest.java new file mode 100644 index 0000000..92eaadf --- /dev/null +++ b/src/test/java/task3_streams/SoldProductsAggregatorTest.java @@ -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 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 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); + } + +} \ No newline at end of file