Groovy web console

subscribe to the feed Subscribe
to this
site

TailorSoftTestDaniel

Published 2 weeks ago by Daniel with tags groovy tailorSoft
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 "+--------------+---------+------------+";

//Get and parse data from url
def infoSample = new JsonSlurper().parseText(url_Json.text);

//Validate is  corect the data
if(infoSample instanceof Map){

    //Define map to count orders place each product
    def mapNumOrderByProduct = [:]

    //Define map to count the quantity for eacht product in all orders
    def mapQualByProduct = [:]
    //Get orders
    def orders= infoSample.orders;

    //Validate if data contains elment orders
    if(orders){
        Integer numOrders = orders.size();

        Integer itemsByOrder = 0
        //loop each orders
        for(def indexOrder=0; indexOrder<numOrders; indexOrder++){
            itemsByOrder = orders[indexOrder].items.size();

            //loop items by each order
            for(def indexItems=0; indexItems<itemsByOrder; indexItems++){
                if(!mapNumOrderByProduct.containsKey(orders[indexOrder].items[indexItems].productId)){
                    mapNumOrderByProduct[orders[indexOrder].items[indexItems].productId]  = (Integer)1;
                    mapQualByProduct [orders[indexOrder].items[indexItems].productId]  = (Integer)orders[indexOrder].items[indexItems].quantity;
                }else{
                    mapNumOrderByProduct[orders[indexOrder].items[indexItems].productId]++  ;
                    mapQualByProduct [orders[indexOrder].items[indexItems].productId] += orders[indexOrder].items[indexItems].quantity;
                }
            }
        }
    }

    def products = infoSample.products;
    //Validate if data contains elmennt product
    if(products){
        Integer numProducts = products.size();

        if(numProducts <= 0){
            println "|              NO PRODUCTS            |";
        }else{
            double dollarsSpend = 0.0;
            Integer ordersByProduct = 0; 
            String nameProduct = '';
            for(def index=0; index<numProducts; index++){
                //Valide if each product is in the maps
                if(mapNumOrderByProduct.containsKey(products[index].id)){
                    dollarsSpend =  (new Double(products[index].price))*mapQualByProduct[products[index].id];
                    ordersByProduct = mapNumOrderByProduct[products[index].id];
                }else{
                    dollarsSpend = 0.0;
                    ordersByProduct  = 0
                }
                if(products[index].name){
                    nameProduct = products[index].name;
                }else{
                    nameProduct = 'NoName';
                }
                println "| ${nameProduct} |  ${ordersByProduct} |  ${dollarsSpend}  |";       
            }
        }
    }else{
            println "|              NO PRODUCTS            |";
    }


}else{
    println "|                ERROR               |";
}

println "+--------------+---------+------------+";