Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.function.Supplier
- // ---------------------------
- //
- // HOMEWORK
- //
- // You can use either Groovy or Java.
- //
- // Design a routine that will calculate the average Product price per Group.
- //
- // The Price of each Product is calculated as:
- // Cost * (1 + Margin)
- //
- // Assume there can be a large number of products.
- //
- // Plus points:
- // - use Groovy and its closures
- // - make the category look-up performance effective
- // - use method Collection.inject
- // contains information about [Product, Group, Cost]
- def products = [
- ["A", "G1", 20.1],
- ["B", "G2", 98.4],
- ["C", "G1", 49.7],
- ["D", "G3", 35.8],
- ["E", "G3", 105.5],
- ["F", "G1", 55.2],
- ["G", "G1", 12.7],
- ["H", "G3", 88.6],
- ["I", "G1", 5.2],
- ["J", "G2", 72.4]
- ]
- // contains information about Category classification based on product Cost
- // [Category, Cost range from (inclusive), Cost range to (exclusive)]
- // i.e. if a Product has Cost between 0 and 25, it belongs to category C1
- // ranges are mutually exclusive and the last range has a null as uppoer limit.
- def category = [
- ["C3", 50, 75],
- ["C4", 75, 100],
- ["C2", 25, 50],
- ["C5", 100, null],
- ["C1", 0, 25]]
- // contains information about margins for each product Category
- // [Category, Margin (either percentage or absolute value)]
- def margins = [
- "C1" : "20%",
- "C2" : "30%",
- "C3" : "0.4",
- "C4" : "50%",
- "C5" : "0.6"]
- // ---------------------------
- //
- // YOUR CODE GOES BELOW THIS LINE
- //
- for(margin in margins) {
- if(margin.value.contains("%")) {
- def e = margin.value.replaceAll("%", "")
- margins[margin.key] = Float.parseFloat(e) / 100
- } else {
- margins[margin.key] = Float.parseFloat(margin.value)
- }
- }
- def result = [:]
- products.stream()
- .map{product ->
- def cat = getCategory(product[2], category)
- product.plus(product[2] * (1 + margins[getCategoryCost(cat)]))
- }
- .collect{product -> product}
- .groupBy {product -> product[1]}
- .each { product ->
- def groupSum = product.value.inject(0){current, value -> current + value[3] }
- def avg = groupSum / product.value.size()
- result[product.key] = roundToNDigits(avg, 1)
- }
- static def getCategory(cost, category) {
- return category.findResult {cost >= it[1] && cost < Optional.ofNullable(it[2]).orElse(Integer.MAX_VALUE) ? it : null}
- }
- static def getCategoryCost(cat) {
- if(cat == null) {
- throw new Exception("No category found")
- }
- return cat.get(0)
- }
- static def roundToNDigits(double value, int nDigits) {
- return Math.round(value * Math.pow(10, nDigits)) / Math.pow(10, nDigits);
- }
- // ---------------------------
- //
- // IF YOUR CODE WORKS, YOU SHOULD GET "It works!" WRITTEN IN THE CONSOLE
- //
- // ---------------------------
- assert result == [
- "G1" : 37.5,
- "G2" : 124.5,
- "G3" : 116.1
- ] : "It doesn't work"
- //a.each {p -> println p[3] }
- //println b['G1'][0][3].orElse([])[0]
- //println margins[a[0][3].orElse([])[0]]
- //println a
- //println result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement