Groovy web console

subscribe to the feed Subscribe
to this


Published 1 month ago by Diego Gomez
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts

import groovy.json.*

// I defined 3 classes which work how entities, these classes are used to obtain the objects obtained from the JSON object.

// The class Product is used to obtain the object for each product received.
class Product {
    String id
    String name
    float price

// The class Item is used to obtain an object kind Item that must be used for the next class called Order which have one or more Items.
class Item{
    String productId
    int quantity

//The class Order generate objects which have an id and one or more items.
class Order {
    String id
    List items = []

class Ejemplo{

static void main(String[] args) { 

// card variable is used to get the array in JSON format from the rest api.
    def card = new JsonSlurper().parseText("".toURL().text)

// these Lists are used to contain the objects that will be obtained from the card variable.
List productos = []
List ordenes = []

// this for is used to asign to the List productos the objects obtained from the card variable.
for (int i = 0; i < card.products.size; i++) {
    Product producto2;
    producto2 = new Product(id:card.products[i].id, name: card.products[i].name, price: Float.parseFloat(card.products[i].price));
// this for is used to asign to the List productos the objects obtained from the card variable.

// this for asign to the List ordenes all orders obtained from the card variable.
for(int i = 0; i < card.orders.size; i++){
    Item item5
    List items = []
    for(int j = 0; j < card.orders[i].items.size; j++){
        item5 = new Item(productId: card.orders[i].items[j].productId, quantity: card.orders[i].items[j].quantity.toInteger());
    Order orden = new Order(id: card.orders[i].id, items: items)

//with the productos and ordenes Lists i can to break each object and consult the parameters that i need.

println "| Product      |  Orders |      Total |";
println "+--------------+---------+------------+";

for(int i = 0; i < productos.size; i++){
    int num_ord_prod = 0
    float total_gastado = 0;
    for(int j = 0; j < ordenes.size; j++){
        for(k = 0; k < ordenes[j].items.size; k++){
            if((ordenes[j].items[k].productId).equals(productos[i].id)){    //each time that an id product match with an product in the id order
                num_ord_prod++                                              //I can sum the order numbers for each product
                total_gastado += ordenes[j].items[k].quantity * productos[i].price    //This variable total_gastado makes a multiplication for get the total price in each order and finally get the sum for each product iterated.

    print productos[i].name + "|"                    // print the name product.
    print "ordenes: " + num_ord_prod + "|"           // print the orders quantity for each product.
    println "total gastado: " + total_gastado        // print the total spended for each product for all orders.