Groovy web console

subscribe to the feed Subscribe
to this
site

TailorSoft_Prueba

Published 1 month ago by SebastianGiraldo
Actions Execute script  ▶ 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 connection = url_Json.openConnection()
connection.requestMethod = 'GET'
def jsonSlurper = new JsonSlurper()

if(connection.responseCode == 200){
    def responseTxt = connection.content.text
    def list = jsonSlurper.parseText(responseTxt)
    list.each {println it}

    def products = list.products
    def orders = list.orders


    products.each {it.nOrders = 0;
                   it.total = 0}


    def pId, qt
    for (Map order: orders){
        for(Map items: order.items){
            pId = items.productId
            qt = items.quantity
            products.find{it.id.equals(pId)}.nOrders += qt
        }
    }



    products.each{it.total = new java.text.DecimalFormat("#0.00").format(Double.parseDouble(it.price) * it.nOrders)}

    def lProduct = 15, lOrders=15, lTotal=" Total ".size();


// Print the table header

    println "+"+"".padRight(lProduct, '-') + "+"+"".padRight(lOrders, '-') + "+"+"".padRight(lTotal, '-')+'+';
    println "|"+"Product".padRight(lProduct, )+ "|"+ "Orders".padRight(lOrders) + "|" + "Total".padRight(lTotal) + "|";
    println "+"+"".padRight(lProduct, '-') + "+"+"".padRight(lOrders, '-') + "+"+"".padRight(lTotal, '-')+'+';
    for (Map product: products) {
        println "|" + product.name.padRight(lProduct, )+ "|"+ product.nOrders.toString().padRight(lOrders) + "|"+ product.total.toString().padRight(lTotal) + "|";
        println "+"+"".padRight(lProduct, '-') + "+"+"".padRight(lOrders, '-') + "+"+"".padRight(lTotal, '-')+'+';
    }

}