Groovy web console

subscribe to the feed Subscribe
to this
site

tailordoft proof

Published 1 month ago by Edisson Guerrero
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").getText();
def storeMap = new JsonSlurper().parseText(url_Json)
def newStore= new Store(storeMap)

for(item in newStore.products){
if(newStore.getOrdersplaced().get(item.id)!=null||newStore.getDollarSpend().get(item.id)!=null){
println "+--------------+---------+------------+";
println "| Product "+item.name+" |  Orders "+newStore.getOrdersplaced().get(item.id)+" | Total " +newStore.getDollarSpend().get(item.id)+" |";
println "+--------------+---------+------------+";
}
else{
println "+--------------+---------+------------+";
println "| Product "+item.name+" |  Orders "+0+" | Total " +0+" |";
println "+--------------+---------+------------+";
}}


public class Store{
private List<Product> products;    
private List<Order> orders;

public Map<String,Integer> getOrdersplaced(){
Map<String,Integer> ordersplaced= new HashMap<String,Integer>();
for(order in orders){
int amount=0;
for(item in order.items){
for(product in products){
if(product.id.equals(item.productId)){
amount++;
if(ordersplaced.containsKey(product.id)){
amount+=ordersplaced.get(product.id);
}
ordersplaced.put(product.id,amount);
}}}}
return ordersplaced;
}

public Map<String,Integer> getDollarSpend(){
Map<String,Integer> dollarSpend= new HashMap<String,Integer>();
for(order in orders){
for(item in order.items){
for(product in products){
if(product.id.equals(item.productId)){
double total = item.quantity*Double.parseDouble(""+product.price);
dollarSpend.put(product.id,total);
}}}}
return dollarSpend;
}}

public class Product{
private String id;
private String name;
private double price;
}

public class Order{
private List<Item> items; 
private String id; 
}

public class Item{
private String productId; 
private int quantity;
}