Groovy web console

subscribe to the feed Subscribe
to this
site

Tailorsoft_GROOVY_TEST

Published 3 weeks ago by Sebastian Insignares Tobon
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
  import groovy.json.JsonSlurper
 import java.text.DecimalFormat;
 
 def url_Json = new URL("https://www.tailorsoft.co/sample.json")
 def slurper = new JsonSlurper()
 def info_Json = slurper.parse(url_Json);
 def productsList = info_Json.products;
 def ordersList = info_Json.orders;
 
 /**********************************************************
 * DTO to inventory products
 **********************************************************/
 class ItemsSoldDTO {
     String idProducts;
     String name;
     double price;
     int quantitySold;
     double totalSold(){
         return price * quantitySold;
     }
 }
 
 /*************************************************************************
 * class to handle sales more efficiently
 *************************************************************************/
 class ItemsSoldList {
     ItemsSoldDTO[] items;
     void addSales(String id, int quantity){
         int size = items.size();
         for(int i=0; i<size;i++){
             if(items[i].idProducts == id){
                 items[i].quantitySold += quantity;
                 return;
             }
         }
     }
 }
 
 //----------------------- Begin Printing Metodes ---------------------------
 
 /******************************************************************
 * printMargin:
 * This metode print a margin to separate the rows
 ******************************************************************/
 void printMargin(){
     println "+--------------+---------+------------+";
 }
 
 /******************************************************************
 * fillWithSpace:
 * This metode is used to generate a number of blanks
 *
 * int number  - quantity of blanks to generate
 *    
 * return - String with the number o blanks required
 *******************************************************************/
 String fillWithSpace(int number){
     String toReturn = "";
     for(int i=0; i<number;i++){
         toReturn += " ";
     }
     return toReturn;
 }
 
 /******************************************************************
 * printCell:
 * This Metode is used to enter a string in a cell of specified size
 *
 * String content        - string to enter in the cell.
 * Integer totalLength   - total length of the cell.
 * Integer initPos       - position from which the string is entered
 *                         in the cell. it begin on 0 position.
 * boolean endCell       - Flag to indicate the use of end line
 * boolean leftAlignment - Flag to indicate left alignamen.
 *******************************************************************/
 void printCell(String content, Integer totalLength, Integer initPos,boolean endCell,boolean leftAlignment){
     String toReturn = "|";
     int availableSize = totalLength - initPos;
     int contentLength = content.length();
     String blanksBefore;
     String blanksAfter;
     String toEnter;
     if(availableSize < contentLength){
         toEnter = content.substring(0,availableSize);
     }else{
         toEnter = content;
     }
     if(leftAlignment){
         blanksBefore = fillWithSpace(initPos);
         blanksAfter = fillWithSpace(availableSize - contentLength);
     }else{
         blanksBefore = fillWithSpace(availableSize - contentLength);
         blanksAfter = fillWithSpace(initPos);
     }
     toReturn += blanksBefore + toEnter + blanksAfter;
     if(endCell){
         println toReturn + "|";
     }else{
         print toReturn;
     }
    
 }
 
 /******************************************************************
 * printNewRow:
 * This metode is used print a row of the required table
 *
 * String name      - Name of the product
 * String quantity  - Number of orders placed for that product
 * String total     - Total dollars spent on that product across all
 *                    orders.
 *    
 * return - String with the number o blanks required
 *******************************************************************/
 void printNewRow(String name,String quantity,String total){
     printCell(name,14,1,false,true);
     printCell(quantity,9,1,false,false);
     printCell(total,12,1,true,false);
     printMargin();
 }
 
 
 /******************************************************************
 * printHeader:
 * this method prints the header of the required table
 ******************************************************************/
 void printHeader(){
     // Print the table header
     printMargin();
     printCell("Product",14,1,false,true);
     printCell("Orders",9,1,false,false);
     printCell("Total",12,1,true,false);
     printMargin();
 }
 
 /*****************************************************************
 * processProducts:
 * Method to initialize the list of sold items
 *
 * Object products - is an array of products
 *
 * return the class to handle the sales {@link ItemsSoldList}
 *****************************************************************/
 ItemsSoldList processProducts(Object products){
     int size = products.size();
     ItemsSoldList itemsSoldList = new ItemsSoldList();
     def itemsSold = [];
     for(int i=0; i<size;i++){
         ItemsSoldDTO temp = new ItemsSoldDTO();
         temp.idProducts = products[i].id;
         temp.name = products[i].name;
         temp.price = Double.parseDouble(products[i].price);
         temp.quantitySold = 0;
         itemsSold.add(temp);
     }
     itemsSoldList.items = itemsSold;
     return itemsSoldList;
 }
 
 /***********************************************************************
 * ordersHandler:
 * Metode to handle al the ordes
 *
 * ItemsSoldList itemsSold - class that handle the sales
 * Object ordersList       - list of orders
 *
 * return the class to handle the sales {@link ItemsSoldList} updated
 ***********************************************************************/
 ItemsSoldList ordersHandler(ItemsSoldList itemsSold,Object ordersList){
     int numberOfOrders = ordersList.size();
     for(int i=0; i<numberOfOrders; i++){
         def order = ordersList[i];
         int numberOfItems = order.items.size();
         for(int j=0; j<numberOfItems; j++){
             def item = order.items[j];
             itemsSold.addSales(item.productId,item.quantity);
         }
     }
     return itemsSold;
 }
 
 //--------------------------- logic ------------------------------------
 def itemsSold = processProducts(productsList);
 def iventorySold = ordersHandler(itemsSold,ordersList);
 def items = iventorySold.items;
 int size = items.size();
 DecimalFormat decimalFormat = new DecimalFormat("#.00");
 //------------------------- Print Table --------------------------------
 printHeader();
 for(int i=0; i<size; i++){
     def item = items[i];   
     String total = decimalFormat.format(item.totalSold());
     String quantitySold = String.valueOf(item.quantitySold);
     printNewRow(item.name,quantitySold,total);
 }