Groovy web console

subscribe to the feed Subscribe
to this
site

Test_Groovy_EC_Fix

Published 3 weeks ago by Edgar Cuesta
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper
import java.math.BigDecimal
import java.math.RoundingMode

def url_Json = new URL("https://www.tailorsoft.co/sample.json")

JsonSlurper slurper = new JsonSlurper()
def result = slurper.parse(url_Json)

/*
 * Some usefull variables
 */
def size_product_col = 14
def size_orders_col = 9
def size_total_col = 12
def table_space = "+--------------+---------+------------+"
def pipe_sep = "| "
def TOTAL = "total"
def ORDERS = "orders"

// Print the table header
println "+--------------+---------+------------+"
println "| Product      |  Orders |      Total |"
println "+--------------+---------+------------+"


/*
 * Run the orders list to fill the total of orders by products
 */
Map<String,Map<String, BigDecimal>> map_products = new HashMap<String, Map<String, BigDecimal>>()
result.orders.each { order ->
    order.items.each { item ->

        /*
         * If the product exists add the orders + quanitty
         */
        if (map_products.get(item.productId) != null) {
            Map<String, BigDecimal> productData = map_products.get(item.productId)
            BigDecimal total = productData.get(TOTAL)
            total = total.add(BigDecimal.valueOf(item.quantity))
            BigDecimal orders = productData.get(ORDERS)
            orders = orders.add(BigDecimal.ONE)
            productData.put(TOTAL, total)
            productData.put(ORDERS, orders)
            map_products.put(item.productId, productData)
        }
        /*
         * Add the product to the map if it does not exist
         */
        else {
            BigDecimal total = item.quantity
            Map<String, BigDecimal> productData = new HashMap<String, BigDecimal>()
            productData.put(ORDERS, BigDecimal.ONE)
            productData.put(TOTAL, total)
            map_products.put(item.productId, productData)
        }
    }
}
/*
 * Run the products list to print the table
 */
result.products.each { product ->
    print(pipe_sep)
    print(product.name )
    /*
     * Format for the Column Product
     * We add spaces after the product name
     */
    if(product.name.length() < (size_product_col - 1))
    {
        int dif = 13 - product.name.length()
        while(dif > 0)
        {
            print(" ")
            dif--
        }

    }
    print(pipe_sep)
    String productid = product.id
    /*
     * I think is best to work with BigDecimal because I could set the
     * Scale
     */
    BigDecimal orders = BigDecimal.ZERO
    BigDecimal quantity = BigDecimal.ZERO
    Map<String, BigDecimal> productData = map_products.get(productid)
    if(productData != null) {
        orders = productData.get(ORDERS)
        if (orders == null) {
            order = BigDecimal.ZERO
            orders.setScale(0, RoundingMode.HALF_UP)
        }
        quantity = productData.get(TOTAL)
        if (quantity == null) {
            quantity = BigDecimal.ZERO
        }
    }
    BigDecimal price = new BigDecimal(product.price)
    BigDecimal total_sales = price.multiply(quantity).setScale(2,RoundingMode.HALF_UP
    )

    /*
     * For the roders Columns we add an space interchangeble at each size of the
     * String
     */
    String orde = orders.toString()
    int controller = 0
    while(orde.length() < (size_orders_col - 1))
    {
        if(controller % 2 != 0)
        {
            orde = " " + orde
        }
        else
        {
            orde = orde + " "
        }
        controller++
    }
    print(orde)
    print(pipe_sep)
    /*
     * Format for the column Total:
     * We add spaces before the total
     *
     */
    if(total_sales.toString().length() < (size_total_col - 2))
    {
        int dif = 10 - total_sales.toString().length()
        while(dif > 0)
        {
            print(" ")
            dif--
        }
    }
    print(total_sales)
    //We add  space for margin
    print(" ")
    println(pipe_sep)
    println(table_space)
}