Groovy web console

subscribe to the feed Subscribe
to this
site

TailorSoftNewDaniel

Published 1 month ago by Daniel Sarmiento Amaya with tags Grrovy tailorSoft odinds
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")
final LENGHT_COLMN_NAME = 14;

// 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 mapDataOrderByProduct= [:]

    //Get orders
    def orders= infoSample.orders;

    //Validate if data contains elment orders
     if(orders){
        Integer numOrders = orders.size();
        Integer itemsByOrder = 0
        DataOrder dataOrder = null;
        //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(!mapDataOrderByProduct.containsKey(orders[indexOrder].items[indexItems].productId)){
                    dataOrder = new DataOrder(1,orders[indexOrder].items[indexItems].quantity);
                    mapDataOrderByProduct[orders[indexOrder].items[indexItems].productId]  = dataOrder;
                }else{
                    dataOrder = mapDataOrderByProduct[orders[indexOrder].items[indexItems].productId];
                    dataOrder.setOrderByProduct(dataOrder.getOrderByProduct()+1);
                    dataOrder.setQuantityByProduct(dataOrder.getQuantityByProduct()+orders[indexOrder].items[indexItems].quantity);
                    mapDataOrderByProduct[orders[indexOrder].items[indexItems].productId]  = dataOrder;
                }
            }
        }
     }

     def products = infoSample.products;
     //Validate if data contains elment product
     if(products){
        showTableProductos(products,mapDataOrderByProduct);
     }else{
         println "|              NO PRODUCTS            |";
     }
}else{
     println "|                ERROR               |";
}

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


def showTableProductos(products,mapDataOrderByProduct){
    
    Integer numProducts = products.size();

    if(numProducts <= 0){
        println "|              NO PRODUCTS            |";
    }else{
        double dollarsSpend = 0.0;
        Integer ordersByProduct = 0;
        String nameProduct = '';
        Integer sizeFirstColumn  = 0;
        Integer sizeSecondColumn  = 0;

        DataOrder dataOrder = null;
        for(def index=0; index<numProducts; index++){
            sizeFirstColumn  = 0;
            sizeSecondColumn  = 4;
            //Validate if each product is in the map
            if(mapDataOrderByProduct.containsKey(products[index].id)){
                dataOrder = mapDataOrderByProduct[products[index].id];
                ordersByProduct = dataOrder.getOrderByProduct();
                try{
                    dollarsSpend =  (new Double(products[index].price))*dataOrder.getQuantityByProduct();
                }catch(ClassCastException ex) {
                    dollarsSpend = 0.0;
                }
            }else{
               dollarsSpend = 0.0;
               ordersByProduct  = 0
            }
            if(products[index].name){
                nameProduct = products[index].name;
            }else{
                nameProduct = 'NoName';
            }
            
            if(nameProduct.size() > 13){
                sizeFirstColumn = nameProduct.size();            
            }else{
                sizeFirstColumn = 13 - nameProduct.size();
            }

            print "| ${nameProduct} ";
            while(sizeFirstColumn > 0){
                print " ";
                sizeFirstColumn--;
            }

            print "|  ${ordersByProduct} ";

            while(sizeSecondColumn > 0){
                print " ";
                sizeSecondColumn  --;
            }

            println "|  ${dollarsSpend}  |";
        }
    }
}

/**
* Daniel Sarmiento Amaya
*
*/
class DataOrder{
    private Integer orderByProduct;
    private Integer quantityByProduct;

    public DataOrder(Integer orderByProduct,Integer quantityByProduct){
        this.quantityByProduct = quantityByProduct;
        this.orderByProduct = orderByProduct;
    }

    public Integer getOrderByProduct(){
        return this.orderByProduct;
    }

    public void setOrderByProduct(Integer orderByProduct){
        this.orderByProduct = orderByProduct;
    }

    public Integer getQuantityByProduct(){
        return this.quantityByProduct;
    }

    public void setQuantityByProduct(Integer quantityByProduct){
        this.quantityByProduct = quantityByProduct;
    }
}