Groovy web console

subscribe to the feed Subscribe
to this
site

Tailorsoft Test

Published 3 weeks ago by Juan Carlos Hernandez H
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 "+----------------------+----------------------+----------------------+";

def data = new JsonSlurper().parse(url_Json);
JsonSlurper slurper = new JsonSlurper();

List<String> names = new ArrayList<String>();
List<Integer> orders = new ArrayList<Integer>();
List<Double> total = new ArrayList<Double>();

def quantity;
def value;


for(product in data.products) {

   quantity = 0;
    value = Double.parseDouble(product.price);

    for(order in data.orders) {

        for(item in order.items) {

            if(product.id == item.productId) {
                quantity += item.quantity;
            }
        }

    }

    names.add(product.name);
    orders.add(quantity);
    total.add(value * quantity);

}




for (i = 0; i < names.size(); i++) {  


    // Print the table header
    println "|"+ names.get(i) + "|" +"                  "+ orders.get(i) + "|" + "                  " + total.get(i) + "|";
    println "+--------------+---------+-------------------------------------------+";
}





/*def printSpaces(value) { 
    def totalSpaces = 24 - value;   
    def spaces = "";

    for (i = 0; i < totalSpaces; i++) {  
        spaces += " ";
    }
    
    return spaces + "==>";
    
}*/