Groovy web console

subscribe to the feed Subscribe
to this
site

TailorsoftProductsTest

Published 1 month ago by Christian Giovani Cachaya Bolivar with tags Tailorsoft
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
/**
 * @author Christian Giovani Cachaya Bolivar
 */
import groovy.json.JsonSlurper
import org.apache.groovy.json.internal.LazyMap
import java.math.RoundingMode

def urlJson
// Try catch block to avoid malformed or another url exception
try {
  urlJson = new URL("https://www.tailorsoft.co/sample.json")
} catch(Exception e) {
  e.printStackTrace()
}
//Parse the url to my own object
def Sales sales = new JsonSlurper().parse(urlJson)
//Process the data to obtain a final list with desired information
def List < TableData > dataResult = createDataResult(sales)

// Print the table header
println "+--------------+--------------------------------+-------------------+------------+";
println "| Product      |  Orders placed for the product |  Product Quantity |      Total |";
println "+--------------+--------------------------------+-------------------+------------+";
//Print the result
dataResult.each {
  result ->println("| " + result.product + "\t|\t \t " + result.orders + " \t \t| \t  " + result.quantity + "         |\t " + result.total + "    |")
  println "+--------------+--------------------------------+-------------------+------------+";
}

/**
 * Private function that goes over the products list to get the desired information
 * and build the TableData object
 * @param sales
 * @return
 */
private def List < TableData > createDataResult(Sales sales) {
  def ProcessProductList processProductListImpl = new ProcessProductListImpl()
  List < TableData > results = []
  TableData tableData
  sales.products.each {
    product ->tableData = new TableData()
    def quantity = processProductListImpl.getQuantityByProduct(product, sales.getOrders())
    BigDecimal total = new BigDecimal(quantity * Double.parseDouble(product.price)).setScale(2, RoundingMode.HALF_UP)
    tableData.product = product.name
    tableData.orders = processProductListImpl.getNumberOfOrdersByProduct(product, sales.getOrders())
    tableData.quantity = quantity
    tableData.total = total
    results.add(tableData)
  }
  return results
}

class ProcessProductListImpl implements ProcessProductList {

  @Override
  public int getNumberOfOrdersByProduct(LazyMap product, List < Order > orders) {
    orders.count {
      order ->order.items.count {
        item ->item.productId.equals(product.id)
      }
    }
  }

  @Override
  public int getQuantityByProduct(LazyMap product, List < Order > orders) {
    orders.sum {
      order ->order.items.sum {
        item ->item.productId.equals(product.id) ? item.quantity++:0
      }
    }
  }

}

interface ProcessProductList {

  /**
  * Function to get the orders by product
  * @param product
  * @param orders
  * @return
  */
  def int getNumberOfOrdersByProduct(LazyMap product, List < Order > orders)
  /**
  * Function to get the product quantity
  * @param product
  * @param orders
  * @return
  */
  def int getQuantityByProduct(LazyMap product, List < Order > orders)

}

class TableData {
  String product
  String orders
  String quantity
  double total
}

class Sales {
  List < Product > products
  List < Order > orders
}

class Product {
  String id
  String name
  double price
}

class Item {
  String productId
  int quantity
}

class Order {
  String id
  List < Item > items
}