Groovy web console

subscribe to the feed Subscribe
to this
site

SalesItem

Published 1 month ago by Luis Alfonso Rocha with tags Rocha
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper
def url_Json = new URL("https://www.tailorsoft.co/sample.json")

// Print the table header
println "+--------------+---------+------------+";
println "| Product      |  Orders |      Total |";
println "+--------------+---------+------------+";


def salesItem = new JsonSlurper().parseText(url_Json.text);
def ordersDispatched = salesItem.orders.items.flatten();

def sumaryOfSales = salesItem.products.collect {
    products ->
    def noOrders = ordersDispatched.findAll { it.productId == products.id}.sum { it.quantity } ?:0
    [ name: products.name, orders: noOrders, total: new BigDecimal(products.price) * noOrders ]
}



sumaryOfSales.each{

    println "|${it.name.padRight(14)}| ${it.orders.toString().center(8)}|${it.total.toString().padLeft(11)} |"

}