Groovy web console

subscribe to the feed Subscribe
to this
site

ProductBilling

Published 1 month ago by Mateo Arroyave Ortiz with tags Groovy Test Interview
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")      
def response = new JsonSlurper().parseText(url_Json.text)          
def products= response.products
def orders= response.orders

for(int i = 0; i < products.size; i++){
    def totalOrders = 0
    def totalSpent = 0

     for(int j = 0; j < orders.size; j++){
        def items = orders[j].items

        for(int k = 0; k < items.size; k++){

            if(items[k].productId.equals(products[i].id)){
                totalOrders++
                totalSpent = totalSpent + 
                                (Double.valueOf(items[k].quantity) 
                                    * Double.valueOf(products[i].price))
            }
        }
     }   
    println "| Product: "+products[i].name
    println "| Orders: "+totalOrders
    println "| Total Spent: "+'$'+totalSpent+"\n"
}