Groovy web console

subscribe to the feed Subscribe
to this
site

GroovyTest

Published 1 month ago by Manuel Fernando with tags 4
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")
def table = new JsonSlurper().parse(url_Json)
def all_items = table.orders.collect{ it.items }.flatten()
 
// Print the table header
println "+--------------+---------+------------------------------------+";
println "| Product      | Orders  | Total Quantity |      Total        |";
println "+--------------+---------+------------------------------------+";
 
table.products.each{ product ->
    def total_quantity = 0
    def orders_amount = all_items.findAll{ it.productId.equals(product.id) }
        .inject(0){t, it -> total_quantity += it.quantity; t + 1}
 
    def total_price = total_quantity * Float.parseFloat(product.price);
 
    printf('|%14s|%9s|%16s|%19s|\n', product.name, orders_amount, total_quantity, total_price)
    println "+--------------+---------+----------------+-------------------+";
}