 |
Subscribe to this site |
|
Result_OF_Script
package com.hello
import groovy.json.JsonSlurper
def url_Json = new URL("https://www.tailorsoft.co/sample.json")
// Print the table header
println "+--------------+---------+-------------+";
println "| Products | Orders | Totals |";
println "+--------------+---------+-------------+";
def salesItem = new JsonSlurper().parseText(url_Json.text);
def ordersDispatched = salesItem.orders.items.flatten();
def sumOfSales = salesItem.products.collect {
products ->
def quantityOfSales = ordersDispatched.findAll {
it.productId == products.id}.sum { it.quantity } ?:0
def quantityOfOrders = ordersDispatched.findAll {
it.productId == products.id}.count {it.productId} ?:0
[ name: products.name,
orders: quantityOfOrders,
total: new BigDecimal(products.price) * quantityOfSales
]
}
//Sums and Results
sumOfSales.each{
println "|${it.name.padRight(14)}| ${it.orders.toString().center(8)}|${it.total.toString().padLeft(11)} |"
}