Groovy web console

subscribe to the feed Subscribe
to this
site

test

Published 2 weeks ago by Pedro Castiblanco with tags tag
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").getText();
def personMap = new JsonSlurper().parseText(url_Json);

personMap.products.id;
//println personMap.orders.items;

int pro1 = 0;
int pro2 = 0;
int pro3 = 0;
int pro4 = 0;
double cost1 = 0;
double cost2 = 0;
double cost3 = 0;
double cost4 = 0;

for(o in personMap.orders.items){
//println o;
int x = o.productId.size();
for(int i=0; i<x;i++){
//println o.productId[i]+"///";
switch(o.productId[i]){  
case "p1":     
pro1++;
cost1 += ((o.quantity[i])*((personMap.products.price[0]) as Double));
break; 
case "p2":     
pro2++;
cost2 += ((o.quantity[i])*((personMap.products.price[1]) as Double));
break;
case "p3":     
pro3++;
cost3 += ((o.quantity[i])*((personMap.products.price[2]) as Double));
break;
case "p4":     
pro4++;
cost4 += ((o.quantity[i])*((personMap.products.price[4]) as Double));
break;
}
}
}

//println (pro1+"---");
//println (pro2+"---");
//println (pro3+"---");
//println (pro4+"---");

//println cost1;

// Print the table header
println "+--------------+---------+------------+";
println "| Name      |  Orders |      Total |";
println "+--------------+---------+------------+";
println "| "+personMap.products.name[0]+"      |  "+pro1+" |  "+cost1+" |";
println "+--------------+---------+------------+";
println "| "+personMap.products.name[1]+"      |  "+pro2+" |  "+cost2+" |";
println "+--------------+---------+------------+";
println "| "+personMap.products.name[2]+"      |  "+pro3+" |  "+cost3+" |";
println "+--------------+---------+------------+";
println "| "+personMap.products.name[3]+"      |  "+pro4+" |  "+cost4+" |";
println "+--------------+---------+------------+";