Groovy web console

subscribe to the feed Subscribe
to this
site

Tailorsoft Coding Test

Published 1 month ago by Anderson Omar Quintero Morales
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper
 
def connection = new URL( "https://www.tailorsoft.co/sample.json")
.openConnection() as HttpURLConnection
 
connection.setRequestProperty( 'Accept', 'application/json' )
// get the response code - automatically sends the request
def card = connection.inputStream.text
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(card)
 
// Print the table header
println "+---------------+--------+---------------+";
println "| Product       | Orders |     Total     |";
println "+---------------+--------+---------------+";
 
def prod = parsedJson["products"];
def ord = parsedJson["orders"];
 
def countOrd
Float countTot
for (products in prod)
{
    countOrd = 0
    countTot = 0.0
    for (orders in ord)
    {       
        def itemss = orders.items
        for (items in itemss)
        {
            if (items.productId == products.id)
            {
            countOrd+=items.quantity
            countTot+= ((products.price as Float) * items.quantity).trunc(2)
            }
        }
    }
    println "| " + products.name+"\t|   " + countOrd + " \t |    " + countTot +"\t |";
    println "+---------------+--------+---------------+";          
}