Advertisement
daniel199410

Homework

Dec 17th, 2019
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.13 KB | None | 0 0
  1. import java.util.function.Supplier
  2.  
  3. // ---------------------------
  4. //
  5. // HOMEWORK
  6. //
  7. // You can use either Groovy or Java.
  8. //
  9. // Design a routine that will calculate the average Product price per Group.
  10. //
  11. // The Price of each Product is calculated as:
  12. // Cost * (1 + Margin)
  13. //
  14. // Assume there can be a large number of products.
  15. //
  16. // Plus points:
  17. // - use Groovy and its closures
  18. // - make the category look-up performance effective
  19. // - use method Collection.inject
  20.  
  21. // contains information about [Product, Group, Cost]
  22. def products = [
  23.     ["A", "G1", 20.1],
  24.     ["B", "G2", 98.4],
  25.     ["C", "G1", 49.7],
  26.     ["D", "G3", 35.8],
  27.     ["E", "G3", 105.5],
  28.     ["F", "G1", 55.2],
  29.     ["G", "G1", 12.7],
  30.     ["H", "G3", 88.6],
  31.     ["I", "G1", 5.2],
  32.     ["J", "G2", 72.4]
  33. ]
  34.  
  35. // contains information about Category classification based on product Cost
  36. // [Category, Cost range from (inclusive), Cost range to (exclusive)]
  37. // i.e. if a Product has Cost between 0 and 25, it belongs to category C1
  38. // ranges are mutually exclusive and the last range has a null as uppoer limit.
  39. def category = [
  40.     ["C3", 50, 75],
  41.     ["C4", 75, 100],
  42.     ["C2", 25, 50],
  43.     ["C5", 100, null],
  44.     ["C1", 0, 25]]
  45.  
  46. // contains information about margins for each product Category
  47. // [Category, Margin (either percentage or absolute value)]
  48. def margins = [
  49.     "C1" : "20%",
  50.     "C2" : "30%",
  51.     "C3" : "0.4",
  52.     "C4" : "50%",
  53.     "C5" : "0.6"]
  54.  
  55. // ---------------------------
  56. //
  57. // YOUR CODE GOES BELOW THIS LINE
  58. //
  59. for(margin in margins) {
  60.     if(margin.value.contains("%")) {
  61.         def e = margin.value.replaceAll("%", "")
  62.         margins[margin.key] = Float.parseFloat(e) / 100
  63.     } else {
  64.         margins[margin.key] = Float.parseFloat(margin.value)
  65.     }
  66. }
  67.  
  68. def result = [:]
  69.  
  70. products.stream()
  71.         .map{product ->
  72.             def cat = getCategory(product[2], category)
  73.             product.plus(product[2] * (1 + margins[getCategoryCost(cat)]))
  74.         }
  75.         .collect{product -> product}
  76.         .groupBy {product -> product[1]}
  77.         .each { product ->
  78.             def groupSum = product.value.inject(0){current, value -> current + value[3] }
  79.             def avg = groupSum / product.value.size()
  80.             result[product.key] = roundToNDigits(avg, 1)
  81.         }
  82.  
  83. static def getCategory(cost, category) {
  84.     return category.findResult {cost >= it[1] && cost < Optional.ofNullable(it[2]).orElse(Integer.MAX_VALUE) ? it : null}
  85. }
  86.  
  87. static def getCategoryCost(cat) {
  88.     if(cat == null) {
  89.         throw new Exception("No category found")
  90.     }
  91.     return cat.get(0)
  92. }
  93.  
  94. static def roundToNDigits(double value, int nDigits) {
  95.     return Math.round(value * Math.pow(10, nDigits)) / Math.pow(10, nDigits);
  96. }
  97.  
  98. // ---------------------------
  99. //
  100. // IF YOUR CODE WORKS, YOU SHOULD GET "It works!" WRITTEN IN THE CONSOLE
  101. //
  102. // ---------------------------
  103. assert result == [
  104.     "G1" : 37.5,
  105.     "G2" : 124.5,
  106.     "G3" : 116.1
  107.     ] : "It doesn't work"
  108.  
  109. //a.each {p -> println p[3] }
  110. //println b['G1'][0][3].orElse([])[0]
  111. //println margins[a​[0]​[3]​.orElse([])[0]​]​
  112. //println a
  113. //println result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement