Groovy web console

subscribe to the feed Subscribe
to this
site

TailSoft GROOVY Test

Published 3 weeks ago by Edgar Fernando Cuesta Fernandez
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 = "| "

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


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

        /*
         * If the product exists add the quantity
         */
        if (map_products.get(item.productId) != null) {
            BigDecimal total = map_products.get(item.productId)
            total = total.add(BigDecimal.valueOf(item.quantity))
            map_products.put(item.productId, total)
        }
        /*
         * Add the product to the map if it does not exist
         */
        else {
            BigDecimal total = item.quantity
            map_products.put(item.productId, total)
        }
    }
}
/*
 * 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 quantity = map_products.get(productid)
    if(quantity == null)
    {
        quantity = BigDecimal.ZERO
        quantity.setScale(0, RoundingMode.HALF_UP)
    }
    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 quant = quantity.toString()
    int controller = 0
    while(quant.length() < (size_orders_col - 1))
    {
        if(controller % 2 != 0)
        {
            quant = " " + quant
        }
        else
        {
            quant = quant + " "
        }
        controller++
    }
    print(quant)
    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)
}