Groovy web console

subscribe to the feed Subscribe
to this
site

Report of Products

Published 1 month ago by Carmen Sofia Hernandez with tags Groovy
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.json.JsonParserType;

def get = new URL("https://www.tailorsoft.co/sample.json").openConnection();
def getRC = get.getResponseCode();
def jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY);
def inventory;
def List<Report> reports = [];

try{    
    if(getRC.equals(200)) {
       inventory  = jsonSlurper.parseText(get.getInputStream().getText()) as Inventory;

      if(inventory !=null){
           reports  = calcReport(inventory);
           
           if(reports == null){
               "It's not possible calculate the report"; 
           }else{
              if( reports.size() > 0)
                  printReport( reports);
               else
                  println "The report doesn't have data";   
           }    
         
      }
       else
          println "It's not possible print the report";  
    }else{
       println "It's not possible print the report because the url it's not accesible"; 
    }

} catch(Exception e1) {
       println("Catching the exception in the Main" + e1);
}
   
 /*
 This method allows to calculate the content of the report
 */  
 List<Report> calcReport(Inventory inventory){
 
 try { 
 
    int numberProducts = inventory.products.size();
    List<Product> products = inventory.products;
    List<Order> orders = inventory.orders;
    List<Report> reports = [];
    def ordersMap = [:]
   
    
    //convert to Map of Items
     for (item in orders) {
        ordersMap.put( item.id, item.items )
    }

       
    for (prod in products) {
       int numberOrders = 0;
       int totalArticles = 0;
       float totalProduct = 0;
            
       numberOrders = calNroOrdersxProduct( prod.id,  ordersMap);

       totalArticles = calcArticlesxProduct( prod.id, ordersMap);
       
       totalProduct =  Float.valueOf( prod.price) * totalArticles;
       
       Report repor = new Report (prod.id, prod.name, numberOrders, totalProduct);
       
       reports << repor
    }
     return reports;
     
    } catch(Exception e1) {
       println("Catching the exception in calcReport" + e1);
    }
}

/*
Calculate the number of orders for each product
*/
int calNroOrdersxProduct(String idProduct,Map  ordersMap ){
    def numberOrders= 0;
   
     ordersMap.each {
            def order = it;
             order.each {
                items = it.value;
                
                 for(int i = 0 ; i < items.productId.size(); i++){
                      def val = items.productId.get(i)
                      if( idProduct == val){
                             numberOrders = numberOrders + 1;
                             break;
                        }
                 }
             }
      }
     
     return  numberOrders;  
}

/*
Calculate number of articles for each product across all orders.
*/
int calcArticlesxProduct(String idProduct, Map  ordersMap ){
    def articles= 0;
       
     ordersMap.each {
            def order = it;           
            order.each {
                items = it.value;
                 for(int i = 0 ; i < items.productId.size(); i++){
                     
                      def val = items.productId.get(i);
                      if( idProduct == val){
                             articles = articles + items.quantity.get(i);
                             break;
                        }
                 }
                
             }
      }
     return  articles;  
}

/*
Print the report
*/
void printReport( List<Report> reports){
   // Print the table header
    println "+--------------+---------+------------+";
    println "| Product      |  Orders |      Total |";
    println "+--------------+---------+------------+";
    
    //Print Details
    def out = new StringBuffer();
    reports.each {
        out << "|" + it.nameProduct.padRight(14)
        out << "|" + it.numberOrders.toString().padLeft(9)
        out << "|" + it.total.toString().padLeft(12)
        out << "|" + '\n'
    }
    print out.toString()
    println "+--------------+---------+------------+";  
   
}
  
class Inventory {
    ArrayList products
    ArrayList orders
}
	
class Product {
    String id
    String name
    float price 
}

class Order {
    String id
    ArrayList items   
}

class Item{
    String productId
    int quantity
}

class Report{
    String idProduct;
    String nameProduct;
    int numberOrders;
    float total;
    
   Report (String idProduct, String nameProduct, int numberOrders, float total){
         this.idProduct =idProduct
         this.nameProduct = nameProduct
         this.numberOrders = numberOrders
         this.total = total
     }  
}