using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code
{
///
/// Various functions for operations on data
///
public static class TaskUtils
{
///
/// Finds the most popular products by the number of sales
///
/// List of orders
/// List of products ids
public static List FindMostPopularProducts(OrderList orders)
{
Dictionary productSales = new Dictionary();
foreach (Order order in orders)
{
if (!productSales.ContainsKey(order.ProductID))
{
productSales.Add(order.ProductID, order.ProductAmount);
}
else
{
productSales[order.ProductID] += order.ProductAmount;
}
}
List mostPopularProducts = new List();
int mostPopularCount = 0;
foreach (string product in productSales.Keys)
{
int count = productSales[product];
if (count > mostPopularCount)
{
mostPopularCount = count;
mostPopularProducts = new List { product };
} else if (count == mostPopularCount)
{
mostPopularProducts.Add(product);
}
}
return mostPopularProducts;
}
///
/// Counts the number of sales of a certain product
///
/// List of products
/// Target product id
/// Sales
public static int CountProductSales(OrderList orders, string product)
{
int sales = 0;
foreach (Order order in orders)
{
if (order.ProductID == product)
{
sales += order.ProductAmount;
}
}
return sales;
}
///
/// Merge orders which have the same customer name, surname and product id into a single order.
///
/// A list of orders
/// A list of orders where same orders have been merged
public static OrderList MergeOrders(OrderList orders)
{
Dictionary, Order> ordersByName = new Dictionary, Order>();
foreach (var order in orders)
{
var key = Tuple.Create(order.CustomerSurname, order.CustomerName, order.ProductID);
if (ordersByName.ContainsKey(key))
{
ordersByName[key].ProductAmount += order.ProductAmount;
} else
{
ordersByName.Add(key, new Order(order.CustomerSurname, order.CustomerName, order.ProductID, order.ProductAmount));
}
}
OrderList mergedOrders = new OrderList();
foreach (var order in ordersByName.Values)
{
mergedOrders.AddToEnd(order);
}
return mergedOrders;
}
///
/// Finds a product by it's id
///
/// List of products
/// Target product id
/// The product
public static Product FindByID(ProductList products, string id)
{
foreach (Product product in products)
{
if (product.ID == id)
{
return product;
}
}
return null;
}
///
/// Find all products by their ids
///
/// List of products
/// List of product ids
/// List of products
public static ProductList FindByID(ProductList products, List ids)
{
ProductList foundProducts = new ProductList();
foreach (string id in ids)
{
foundProducts.AddToEnd(FindByID(products, id));
}
return foundProducts;
}
///
/// Filter a list of products by sales and price.
///
/// List of products
/// List of orders
/// Minimmum sales amount
/// Max product price
/// A list of filtered products
public static ProductList FilterByQuantitySoldAndPrice(ProductList products, OrderList orders, int minSold, decimal maxPrice)
{
ProductList filtered = new ProductList();
foreach (Product product in products)
{
if (product.Price < maxPrice)
{
int sold = CountProductSales(orders, product.ID);
if (sold >= minSold)
{
filtered.AddToEnd(product);
}
}
}
return filtered;
}
///
/// Find all customer which bought only 1 type of product
///
/// List of orders
/// A list of filtered orders
public static OrderList FindCustomerWithSingleProduct(OrderList orders)
{
Dictionary, OrderList> ordersByCusomer = new Dictionary, OrderList>();
foreach (var order in TaskUtils.MergeOrders(orders))
{
var key = Tuple.Create(order.CustomerName, order.CustomerSurname);
if (!ordersByCusomer.ContainsKey(key))
{
ordersByCusomer.Add(key, new OrderList());
}
ordersByCusomer[key].AddToEnd(order);
}
OrderList finalList = new OrderList();
foreach (var customerOrders in ordersByCusomer.Values)
{
if (customerOrders.Count() == 1)
{
finalList.AddToEnd(customerOrders.First());
}
}
return finalList;
}
}
}