1
0

refactor: rewrite LinkedList using generic in L3.LD_24

This commit is contained in:
Rokas Puzonas 2022-03-21 16:26:03 +02:00
parent 720bb2957f
commit 8371ae885b
11 changed files with 204 additions and 312 deletions

View File

@ -3,10 +3,10 @@
---------------------------- ----------------------------
| ID | Vardas | Kaina | | ID | Vardas | Kaina |
---------------------------- ----------------------------
| 0 | Atsuktuvas | 0,99 | | 0 | Atsuktuvas | 0.99 |
| 1 | Varztas | 0,05 | | 1 | Varztas | 0.05 |
| 2 | Laidas | 2,00 | | 2 | Laidas | 2.00 |
| 3 | Plaktukas | 2,99 | | 3 | Plaktukas | 2.99 |
---------------------------- ----------------------------
-------------------------------------------------- --------------------------------------------------
@ -33,7 +33,7 @@
------------------------------------------------------------ ------------------------------------------------------------
| ID | Vardas | Įtaisų kiekis, vnt. | Įtaisų kaina, eur. | | ID | Vardas | Įtaisų kiekis, vnt. | Įtaisų kaina, eur. |
------------------------------------------------------------ ------------------------------------------------------------
| 2 | Laidas | 220 | 440,00 | | 2 | Laidas | 220 | 440.00 |
------------------------------------------------------------ ------------------------------------------------------------
---------------------------------------------------------- ----------------------------------------------------------
@ -45,11 +45,11 @@
---------------------------------------------------------- ----------------------------------------------------------
---------------------------------- ----------------------------------
| Atrinkti įtaisai (n=1, k=1,00) | | Atrinkti įtaisai (n=1, k=1.00) |
---------------------------------- ----------------------------------
| ID | Vardas | Kaina | | ID | Vardas | Kaina |
---------------------------------- ----------------------------------
| 0 | Atsuktuvas | 0,99 | | 0 | Atsuktuvas | 0.99 |
| 1 | Varztas | 0,05 | | 1 | Varztas | 0.05 |
---------------------------------- ----------------------------------

View File

@ -35,16 +35,16 @@ namespace LD_24.Code
/// </summary> /// </summary>
/// <param name="filename">Target file</param> /// <param name="filename">Target file</param>
/// <returns>A list of products</returns> /// <returns>A list of products</returns>
public static ProductList ReadProducts(string filename) public static LinkedList<Product> ReadProducts(string filename)
{ {
ProductList products = new ProductList(); LinkedList<Product> products = new LinkedList<Product>();
foreach (string line in ReadLines(filename)) foreach (string line in ReadLines(filename))
{ {
string[] parts = line.Split(','); string[] parts = line.Split(',');
string id = parts[0].Trim(); string id = parts[0].Trim();
string name = parts[1].Trim(); string name = parts[1].Trim();
decimal price = decimal.Parse(parts[2].Trim(), CultureInfo.InvariantCulture); decimal price = decimal.Parse(parts[2].Trim(), CultureInfo.InvariantCulture);
products.AddToEnd(new Product(id, name, price)); products.Add(new Product(id, name, price));
} }
return products; return products;
} }
@ -54,9 +54,9 @@ namespace LD_24.Code
/// </summary> /// </summary>
/// <param name="filename">Target file</param> /// <param name="filename">Target file</param>
/// <returns>A list of orders</returns> /// <returns>A list of orders</returns>
public static OrderList ReadOrders(string filename) public static LinkedList<Order> ReadOrders(string filename)
{ {
OrderList orders = new OrderList(); LinkedList<Order> orders = new LinkedList<Order>();
foreach (string line in ReadLines(filename)) foreach (string line in ReadLines(filename))
{ {
string[] parts = line.Split(','); string[] parts = line.Split(',');
@ -64,7 +64,7 @@ namespace LD_24.Code
string customerName = parts[1].Trim(); string customerName = parts[1].Trim();
string productID = parts[2].Trim(); string productID = parts[2].Trim();
int productAmount = int.Parse(parts[3].Trim()); int productAmount = int.Parse(parts[3].Trim());
orders.AddToEnd(new Order(customerSurname, customerName, productID, productAmount)); orders.Add(new Order(customerSurname, customerName, productID, productAmount));
} }
return orders; return orders;
} }
@ -95,11 +95,11 @@ namespace LD_24.Code
/// <param name="list">Target list</param> /// <param name="list">Target list</param>
/// <param name="columns">Column names</param> /// <param name="columns">Column names</param>
/// <returns>A IEnumerable for inserting values for each row</returns> /// <returns>A IEnumerable for inserting values for each row</returns>
private static IEnumerable<Tuple<object, List<String>>> PrintTable(StreamWriter writer, string header, IEnumerable list, params string[] columns) private static IEnumerable<Tuple<T, List<string>>> PrintTable<T>(StreamWriter writer, string header, IEnumerable<T> list, params string[] columns)
{ {
// 0. Collect all the rows // 0. Collect all the rows
List<List<string>> rows = new List<List<string>>(); List<List<string>> rows = new List<List<string>>();
foreach (object item in list) foreach (T item in list)
{ {
List<string> row = new List<string>(); List<string> row = new List<string>();
yield return Tuple.Create(item, row); yield return Tuple.Create(item, row);
@ -167,11 +167,11 @@ namespace LD_24.Code
/// <param name="writer">Target file</param> /// <param name="writer">Target file</param>
/// <param name="orders">List of orders</param> /// <param name="orders">List of orders</param>
/// <param name="header">Header above table</param> /// <param name="header">Header above table</param>
public static void PrintOrders(StreamWriter writer, OrderList orders, string header) public static void PrintOrders(StreamWriter writer, IEnumerable<Order> orders, string header)
{ {
foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaisas", "-Įtaiso kiekis")) foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaisas", "-Įtaiso kiekis"))
{ {
Order order = (Order)tuple.Item1; Order order = tuple.Item1;
List<string> row = tuple.Item2; List<string> row = tuple.Item2;
row.Add(order.CustomerSurname); row.Add(order.CustomerSurname);
row.Add(order.CustomerName); row.Add(order.CustomerName);
@ -187,18 +187,18 @@ namespace LD_24.Code
/// <param name="orders">List of orders</param> /// <param name="orders">List of orders</param>
/// <param name="products">List of products</param> /// <param name="products">List of products</param>
/// <param name="header">Header above table</param> /// <param name="header">Header above table</param>
public static void PrintOrdersWithPrices(StreamWriter writer, OrderList orders, ProductList products, string header) public static void PrintOrdersWithPrices(StreamWriter writer, IEnumerable<Order> orders, IEnumerable<Product> products, string header)
{ {
foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaiso kiekis, vnt.", "-Kaina, eur.")) foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaiso kiekis, vnt.", "-Kaina, eur."))
{ {
Order order = (Order)tuple.Item1; Order order = tuple.Item1;
List<string> row = tuple.Item2; List<string> row = tuple.Item2;
Product product = TaskUtils.FindByID(products, order.ProductID); Product product = TaskUtils.FindByID(products, order.ProductID);
row.Add(order.CustomerSurname); row.Add(order.CustomerSurname);
row.Add(order.CustomerName); row.Add(order.CustomerName);
row.Add(order.ProductAmount.ToString()); row.Add(order.ProductAmount.ToString());
row.Add(String.Format("{0:f2}", order.ProductAmount * product.Price)); row.Add(string.Format("{0:f2}", order.ProductAmount * product.Price));
} }
} }
@ -208,15 +208,15 @@ namespace LD_24.Code
/// <param name="writer">Target file</param> /// <param name="writer">Target file</param>
/// <param name="products">List of products</param> /// <param name="products">List of products</param>
/// <param name="header">Header above table</param> /// <param name="header">Header above table</param>
public static void PrintProducts(StreamWriter writer, ProductList products, string header) public static void PrintProducts(StreamWriter writer, IEnumerable<Product> products, string header)
{ {
foreach (var tuple in PrintTable(writer, header, products, "ID", "Vardas", "-Kaina")) foreach (var tuple in PrintTable(writer, header, products, "ID", "Vardas", "-Kaina"))
{ {
Product product = (Product)tuple.Item1; Product product = tuple.Item1;
List<string> row = tuple.Item2; List<string> row = tuple.Item2;
row.Add(product.ID); row.Add(product.ID);
row.Add(product.Name); row.Add(product.Name);
row.Add(String.Format("{0:f2}", product.Price)); row.Add(string.Format("{0:f2}", product.Price));
} }
} }
@ -227,17 +227,17 @@ namespace LD_24.Code
/// <param name="orders">List of orders</param> /// <param name="orders">List of orders</param>
/// <param name="popularProducts">List of most popular products</param> /// <param name="popularProducts">List of most popular products</param>
/// <param name="header">Header above table</param> /// <param name="header">Header above table</param>
public static void PrintMostPopularProducts(StreamWriter writer, OrderList orders, ProductList popularProducts, string header) public static void PrintMostPopularProducts(StreamWriter writer, IEnumerable<Order> orders, IEnumerable<Product> popularProducts, string header)
{ {
foreach (var tuple in PrintTable(writer, header, popularProducts, "ID", "Vardas", "-Įtaisų kiekis, vnt.", "-Įtaisų kaina, eur.")) foreach (var tuple in PrintTable(writer, header, popularProducts, "ID", "Vardas", "-Įtaisų kiekis, vnt.", "-Įtaisų kaina, eur."))
{ {
Product product = (Product)tuple.Item1; Product product = tuple.Item1;
List<string> row = tuple.Item2; List<string> row = tuple.Item2;
int sales = TaskUtils.CountProductSales(orders, product.ID); int sales = TaskUtils.CountProductSales(orders, product.ID);
row.Add(product.ID); row.Add(product.ID);
row.Add(product.Name); row.Add(product.Name);
row.Add(sales.ToString()); row.Add(sales.ToString());
row.Add(String.Format("{0:f2}", sales * product.Price)); row.Add(string.Format("{0:f2}", sales * product.Price));
} }
} }
} }

View File

@ -0,0 +1,94 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code
{
public class LinkedList<T> : IEnumerable<T>
where T: IComparable<T>, IEquatable<T>
{
class Node
{
public T Data { get; set; }
public Node Next { get; set; }
public Node(T data, Node next = null)
{
Data = data;
Next = next;
}
}
private Node head;
private Node tail;
public void Add(T customer)
{
Node node = new Node(customer);
if (tail != null && head != null)
{
tail.Next = node;
tail = node;
}
else
{
tail = node;
head = node;
}
}
public int Count()
{
int count = 0;
Node current = head;
while (current != null)
{
current = current.Next;
count++;
}
return count;
}
public bool IsEmpty()
{
return head == null;
}
public void Sort()
{
for (Node nodeA = head; nodeA != null; nodeA = nodeA.Next)
{
Node min = nodeA;
for (Node nodeB = nodeA.Next; nodeB != null; nodeB = nodeB.Next)
{
if (nodeB.Data.CompareTo(min.Data) > 0)
{
min = nodeB;
}
}
T tmp = nodeA.Data;
nodeA.Data = min.Data;
min.Data = tmp;
}
}
public override string ToString()
{
return String.Format("LinkedList<{0}>{ Count = '{0}' }", Count());
}
public IEnumerator<T> GetEnumerator()
{
for (Node d = head; d != null; d = d.Next)
yield return d.Data;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@ -8,7 +8,7 @@ namespace LD_24.Code
/// <summary> /// <summary>
/// Class used for storing a single order /// Class used for storing a single order
/// </summary> /// </summary>
public class Order public class Order: IEquatable<Order>, IComparable<Order>
{ {
/// <summary> /// <summary>
/// Surname of customer who ordered /// Surname of customer who ordered
@ -40,38 +40,44 @@ namespace LD_24.Code
return String.Format("Order{Name = '{0}'}", CustomerName); return String.Format("Order{Name = '{0}'}", CustomerName);
} }
public static bool operator <(Order a, Order b) public int CompareTo(Order other)
{ {
if (a.ProductAmount > b.ProductAmount) if (ProductAmount > other.ProductAmount)
{ {
return true; return 1;
} }
else if (a.ProductAmount == b.ProductAmount) else if (ProductAmount == other.ProductAmount)
{ {
int surnameCompare = a.CustomerSurname.CompareTo(b.CustomerSurname); int surnameCompare = CustomerSurname.CompareTo(other.CustomerSurname);
if (surnameCompare < 0) if (surnameCompare < 0)
{ {
return true; return 1;
} }
else if (surnameCompare == 0) else if (surnameCompare == 0 && CustomerName.CompareTo(other.CustomerName) < 0)
{ {
return a.CustomerName.CompareTo(b.CustomerName) < 0; return 1;
} }
} }
return false; return Equals(other) ? 0 : -1;
} }
public static bool operator >(Order a, Order b)
public bool Equals(Order other)
{ {
return !(a < b && a == b); return CustomerSurname == other.CustomerSurname &&
CustomerName == other.CustomerName &&
ProductID == other.ProductID &&
ProductAmount == other.ProductAmount;
} }
public static bool operator ==(Order a, Order b)
public override int GetHashCode()
{ {
return a.ProductAmount == b.ProductAmount && a.CustomerName == b.CustomerName && a.CustomerSurname == b.CustomerSurname; int hashCode = -273364163;
} hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(CustomerSurname);
public static bool operator !=(Order a, Order b) hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(CustomerName);
{ hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ProductID);
return !(a == b); hashCode = hashCode * -1521134295 + ProductAmount.GetHashCode();
return hashCode;
} }
} }
} }

View File

@ -1,125 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Stores a orders in a linked list
/// </summary>
public class OrderList : IEnumerable<Order>
{
class OrderNode
{
public Order Data { get; set; }
public OrderNode Next { get; set; }
public OrderNode(Order data = null, OrderNode next = null)
{
Data = data;
Next = next;
}
}
private OrderNode head;
private OrderNode tail;
/// <summary>
/// Append a value to the end of the linked list
/// </summary>
/// <param name="customer"></param>
public void AddToEnd(Order customer)
{
OrderNode node = new OrderNode(customer);
if (tail != null && head != null)
{
tail.Next = node;
tail = node;
}
else
{
tail = node;
head = node;
}
}
/// <summary>
/// Insert a value to the start of the linked list
/// </summary>
/// <param name="customer"></param>
public void AddToStart(Order customer)
{
OrderNode node = new OrderNode(customer);
if (tail != null && head != null)
{
node.Next = head;
head = node;
}
else
{
tail = node;
head = node;
}
}
/// <summary>
/// Get the number of values stored in linked list
/// </summary>
/// <returns>A count</returns>
public int Count()
{
int count = 0;
OrderNode current = head;
while (current != null)
{
current = current.Next;
count++;
}
return count;
}
/// <summary>
/// Sorts the linked list
/// </summary>
public void Sort()
{
for (OrderNode nodeA = head; nodeA != null; nodeA = nodeA.Next)
{
OrderNode min = nodeA;
for (OrderNode nodeB = nodeA.Next; nodeB != null; nodeB = nodeB.Next)
{
if (nodeB.Data < min.Data)
{
min = nodeB;
}
}
Order tmp = nodeA.Data;
nodeA.Data = min.Data;
min.Data = tmp;
}
}
public override string ToString()
{
return String.Format("OrderList{ Count = '{0}' }", Count());
}
public IEnumerator<Order> GetEnumerator()
{
OrderNode current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@ -8,7 +8,7 @@ namespace LD_24.Code
/// <summary> /// <summary>
/// Holds informations about a single product /// Holds informations about a single product
/// </summary> /// </summary>
public class Product public class Product: IEquatable<Product>, IComparable<Product>
{ {
/// <summary> /// <summary>
/// Identification number of product /// Identification number of product
@ -34,5 +34,26 @@ namespace LD_24.Code
{ {
return String.Format("Product{ID = '{0}'}", ID); return String.Format("Product{ID = '{0}'}", ID);
} }
public bool Equals(Product other)
{
return ID == other.ID &&
Name == other.Name &&
Price == other.Price;
}
public override int GetHashCode()
{
int hashCode = 560300832;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ID);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
hashCode = hashCode * -1521134295 + Price.GetHashCode();
return hashCode;
}
public int CompareTo(Product other)
{
return ID.CompareTo(other.ID);
}
} }
} }

View File

@ -1,103 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Stores multiple products in a linked list
/// </summary>
public class ProductList : IEnumerable<Product>
{
class ProductNode
{
public Product Data { get; set; }
public ProductNode Next { get; set; }
public ProductNode(Product data = null, ProductNode next = null)
{
Data = data;
Next = next;
}
}
private ProductNode head;
private ProductNode tail;
/// <summary>
/// Append a value to the end of the linked list
/// </summary>
/// <param name="product"></param>
public void AddToEnd(Product product)
{
ProductNode node = new ProductNode(product);
if (tail != null && head != null)
{
tail.Next = node;
tail = node;
}
else
{
tail = node;
head = node;
}
}
/// <summary>
/// Inserts a value to the start of the linked list
/// </summary>
/// <param name="product"></param>
public void AddToStart(Product product)
{
ProductNode node = new ProductNode(product);
if (tail != null && head != null)
{
node.Next = head;
head = node;
}
else
{
tail = node;
head = node;
}
}
/// <summary>
/// Get the number of values stored in the linked list
/// </summary>
/// <returns>A count</returns>
public int Count()
{
int count = 0;
ProductNode current = head;
while (current != null)
{
current = current.Next;
count++;
}
return count;
}
public override string ToString()
{
return String.Format("ProductList{ Count = '{0}' }", Count());
}
public IEnumerator<Product> GetEnumerator()
{
ProductNode current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@ -16,7 +16,7 @@ namespace LD_24.Code
/// </summary> /// </summary>
/// <param name="orders">List of orders</param> /// <param name="orders">List of orders</param>
/// <returns>List of products ids</returns> /// <returns>List of products ids</returns>
public static List<string> FindMostPopularProducts(OrderList orders) public static List<string> FindMostPopularProducts(IEnumerable<Order> orders)
{ {
Dictionary<string, int> productSales = new Dictionary<string, int>(); Dictionary<string, int> productSales = new Dictionary<string, int>();
foreach (Order order in orders) foreach (Order order in orders)
@ -55,7 +55,7 @@ namespace LD_24.Code
/// <param name="orders">List of products</param> /// <param name="orders">List of products</param>
/// <param name="product">Target product id</param> /// <param name="product">Target product id</param>
/// <returns>Sales</returns> /// <returns>Sales</returns>
public static int CountProductSales(OrderList orders, string product) public static int CountProductSales(IEnumerable<Order> orders, string product)
{ {
int sales = 0; int sales = 0;
foreach (Order order in orders) foreach (Order order in orders)
@ -73,7 +73,7 @@ namespace LD_24.Code
/// </summary> /// </summary>
/// <param name="orders">A list of orders</param> /// <param name="orders">A list of orders</param>
/// <returns>A list of orders where same orders have been merged</returns> /// <returns>A list of orders where same orders have been merged</returns>
public static OrderList MergeOrders(OrderList orders) public static LinkedList<Order> MergeOrders(IEnumerable<Order> orders)
{ {
Dictionary<Tuple<string, string, string>, Order> ordersByName = new Dictionary<Tuple<string, string, string>, Order>(); Dictionary<Tuple<string, string, string>, Order> ordersByName = new Dictionary<Tuple<string, string, string>, Order>();
foreach (var order in orders) foreach (var order in orders)
@ -88,10 +88,10 @@ namespace LD_24.Code
} }
} }
OrderList mergedOrders = new OrderList(); LinkedList<Order> mergedOrders = new LinkedList<Order>();
foreach (var order in ordersByName.Values) foreach (var order in ordersByName.Values)
{ {
mergedOrders.AddToEnd(order); mergedOrders.Add(order);
} }
return mergedOrders; return mergedOrders;
} }
@ -102,7 +102,7 @@ namespace LD_24.Code
/// <param name="products">List of products</param> /// <param name="products">List of products</param>
/// <param name="id">Target product id</param> /// <param name="id">Target product id</param>
/// <returns>The product</returns> /// <returns>The product</returns>
public static Product FindByID(ProductList products, string id) public static Product FindByID(IEnumerable<Product> products, string id)
{ {
foreach (Product product in products) foreach (Product product in products)
{ {
@ -120,12 +120,12 @@ namespace LD_24.Code
/// <param name="products">List of products</param> /// <param name="products">List of products</param>
/// <param name="ids">List of product ids</param> /// <param name="ids">List of product ids</param>
/// <returns>List of products</returns> /// <returns>List of products</returns>
public static ProductList FindByID(ProductList products, List<string> ids) public static LinkedList<Product> FindByID(IEnumerable<Product> products, List<string> ids)
{ {
ProductList foundProducts = new ProductList(); LinkedList<Product> foundProducts = new LinkedList<Product>();
foreach (string id in ids) foreach (string id in ids)
{ {
foundProducts.AddToEnd(FindByID(products, id)); foundProducts.Add(FindByID(products, id));
} }
return foundProducts; return foundProducts;
} }
@ -138,9 +138,9 @@ namespace LD_24.Code
/// <param name="minSold">Minimmum sales amount</param> /// <param name="minSold">Minimmum sales amount</param>
/// <param name="maxPrice">Max product price</param> /// <param name="maxPrice">Max product price</param>
/// <returns>A list of filtered products</returns> /// <returns>A list of filtered products</returns>
public static ProductList FilterByQuantitySoldAndPrice(ProductList products, OrderList orders, int minSold, decimal maxPrice) public static LinkedList<Product> FilterByQuantitySoldAndPrice(IEnumerable<Product> products, IEnumerable<Order> orders, int minSold, decimal maxPrice)
{ {
ProductList filtered = new ProductList(); LinkedList<Product> filtered = new LinkedList<Product>();
foreach (Product product in products) foreach (Product product in products)
{ {
if (product.Price < maxPrice) if (product.Price < maxPrice)
@ -148,7 +148,7 @@ namespace LD_24.Code
int sold = CountProductSales(orders, product.ID); int sold = CountProductSales(orders, product.ID);
if (sold >= minSold) if (sold >= minSold)
{ {
filtered.AddToEnd(product); filtered.Add(product);
} }
} }
} }
@ -160,25 +160,25 @@ namespace LD_24.Code
/// </summary> /// </summary>
/// <param name="orders">List of orders</param> /// <param name="orders">List of orders</param>
/// <returns>A list of filtered orders</returns> /// <returns>A list of filtered orders</returns>
public static OrderList FindCustomerWithSingleProduct(OrderList orders) public static LinkedList<Order> FindCustomerWithSingleProduct(IEnumerable<Order> orders)
{ {
Dictionary<Tuple<string, string>, OrderList> ordersByCusomer = new Dictionary<Tuple<string, string>, OrderList>(); var ordersByCusomer = new Dictionary<Tuple<string, string>, LinkedList<Order>>();
foreach (var order in TaskUtils.MergeOrders(orders)) foreach (var order in MergeOrders(orders))
{ {
var key = Tuple.Create(order.CustomerName, order.CustomerSurname); var key = Tuple.Create(order.CustomerName, order.CustomerSurname);
if (!ordersByCusomer.ContainsKey(key)) if (!ordersByCusomer.ContainsKey(key))
{ {
ordersByCusomer.Add(key, new OrderList()); ordersByCusomer.Add(key, new LinkedList<Order>());
} }
ordersByCusomer[key].AddToEnd(order); ordersByCusomer[key].Add(order);
} }
OrderList finalList = new OrderList(); LinkedList<Order> finalList = new LinkedList<Order>();
foreach (var customerOrders in ordersByCusomer.Values) foreach (var customerOrders in ordersByCusomer.Values)
{ {
if (customerOrders.Count() == 1) if (customerOrders.Count() == 1)
{ {
finalList.AddToEnd(customerOrders.First()); finalList.Add(customerOrders.First());
} }
} }
return finalList; return finalList;

View File

@ -30,13 +30,13 @@ namespace LD_24
FindControl("ResultsDiv").Visible = true; FindControl("ResultsDiv").Visible = true;
ProductList products = InOutUtils.ReadProducts(Server.MapPath(inputFileA)); var products = InOutUtils.ReadProducts(Server.MapPath(inputFileA));
OrderList orders = InOutUtils.ReadOrders(Server.MapPath(inputFileB)); var orders = InOutUtils.ReadOrders(Server.MapPath(inputFileB));
List<string> mostPopularProductIds = TaskUtils.FindMostPopularProducts(orders); List<string> mostPopularProductIds = TaskUtils.FindMostPopularProducts(orders);
ProductList mostPopularProducts = TaskUtils.FindByID(products, mostPopularProductIds); var mostPopularProducts = TaskUtils.FindByID(products, mostPopularProductIds);
ProductList filteredProducts = TaskUtils.FilterByQuantitySoldAndPrice(products, orders, n, k); var filteredProducts = TaskUtils.FilterByQuantitySoldAndPrice(products, orders, n, k);
OrderList customersWithSingleProduct = TaskUtils.FindCustomerWithSingleProduct(orders); var customersWithSingleProduct = TaskUtils.FindCustomerWithSingleProduct(orders);
customersWithSingleProduct.Sort(); customersWithSingleProduct.Sort();
ShowProducts(Table1, products); ShowProducts(Table1, products);

View File

@ -18,7 +18,7 @@ namespace LD_24
/// <param name="list">Target list</param> /// <param name="list">Target list</param>
/// <param name="columns">Columns names</param> /// <param name="columns">Columns names</param>
/// <returns></returns> /// <returns></returns>
private IEnumerable<Tuple<object, TableRow>> ShowTable(Table table, IEnumerable list, params string[] columns) private IEnumerable<Tuple<T, TableRow>> ShowTable<T>(Table table, IEnumerable<T> list, params string[] columns)
{ {
TableRow header = new TableRow(); TableRow header = new TableRow();
foreach (string column in columns) foreach (string column in columns)
@ -28,7 +28,7 @@ namespace LD_24
table.Rows.Add(header); table.Rows.Add(header);
int n = 0; int n = 0;
foreach (object item in list) foreach (T item in list)
{ {
TableRow row = new TableRow(); TableRow row = new TableRow();
yield return Tuple.Create(item, row); yield return Tuple.Create(item, row);
@ -49,11 +49,11 @@ namespace LD_24
/// </summary> /// </summary>
/// <param name="table">Target table</param> /// <param name="table">Target table</param>
/// <param name="products">Target products</param> /// <param name="products">Target products</param>
public void ShowProducts(Table table, ProductList products) public void ShowProducts(Table table, IEnumerable<Product> products)
{ {
foreach (var tuple in ShowTable(table, products, "ID", "Vardas", "Kaina, eur.")) foreach (var tuple in ShowTable(table, products, "ID", "Vardas", "Kaina, eur."))
{ {
Product product = (Product)tuple.Item1; Product product = tuple.Item1;
TableRow row = tuple.Item2; TableRow row = tuple.Item2;
row.Cells.Add(new TableCell { Text = product.ID }); row.Cells.Add(new TableCell { Text = product.ID });
row.Cells.Add(new TableCell { Text = product.Name }); row.Cells.Add(new TableCell { Text = product.Name });
@ -66,11 +66,11 @@ namespace LD_24
/// </summary> /// </summary>
/// <param name="table">Target table</param> /// <param name="table">Target table</param>
/// <param name="orders">Target orders</param> /// <param name="orders">Target orders</param>
public void ShowOrders(Table table, OrderList orders) public void ShowOrders(Table table, IEnumerable<Order> orders)
{ {
foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisas", "Įtaisų kiekis, vnt.")) foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisas", "Įtaisų kiekis, vnt."))
{ {
Order order = (Order)tuple.Item1; Order order = tuple.Item1;
TableRow row = tuple.Item2; TableRow row = tuple.Item2;
row.Cells.Add(new TableCell { Text = order.CustomerSurname }); row.Cells.Add(new TableCell { Text = order.CustomerSurname });
row.Cells.Add(new TableCell { Text = order.CustomerName }); row.Cells.Add(new TableCell { Text = order.CustomerName });
@ -85,17 +85,17 @@ namespace LD_24
/// <param name="table">Target table</param> /// <param name="table">Target table</param>
/// <param name="orders">Target orders</param> /// <param name="orders">Target orders</param>
/// <param name="popularProducts">List of most popular products</param> /// <param name="popularProducts">List of most popular products</param>
public void ShowMostPopularProducts(Table table, OrderList orders, ProductList popularProducts) public void ShowMostPopularProducts(Table table, IEnumerable<Order> orders, IEnumerable<Product> popularProducts)
{ {
foreach (var tuple in ShowTable(table, popularProducts, "ID", "Vardas", "Įtaisų kiekis, vnt.", "Įtaisų kaina, eur.")) foreach (var tuple in ShowTable(table, popularProducts, "ID", "Vardas", "Įtaisų kiekis, vnt.", "Įtaisų kaina, eur."))
{ {
Product product = (Product)tuple.Item1; Product product = tuple.Item1;
TableRow row = tuple.Item2; TableRow row = tuple.Item2;
int sales = TaskUtils.CountProductSales(orders, product.ID); int sales = TaskUtils.CountProductSales(orders, product.ID);
row.Cells.Add(new TableCell { Text = product.ID }); row.Cells.Add(new TableCell { Text = product.ID });
row.Cells.Add(new TableCell { Text = product.Name }); row.Cells.Add(new TableCell { Text = product.Name });
row.Cells.Add(new TableCell { Text = sales.ToString() }); row.Cells.Add(new TableCell { Text = sales.ToString() });
row.Cells.Add(new TableCell { Text = String.Format("{0:f2}", sales * product.Price) }); row.Cells.Add(new TableCell { Text = string.Format("{0:f2}", sales * product.Price) });
} }
} }
@ -105,18 +105,18 @@ namespace LD_24
/// <param name="table">Target table</param> /// <param name="table">Target table</param>
/// <param name="orders">Target orders</param> /// <param name="orders">Target orders</param>
/// <param name="products">List of products</param> /// <param name="products">List of products</param>
public void ShowOrdersWithPrices(Table table, OrderList orders, ProductList products) public void ShowOrdersWithPrices(Table table, IEnumerable<Order> orders, IEnumerable<Product> products)
{ {
foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisų kiekis, vnt.", "Sumokėta, eur.")) foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisų kiekis, vnt.", "Sumokėta, eur."))
{ {
Order order = (Order)tuple.Item1; Order order = tuple.Item1;
TableRow row = tuple.Item2; TableRow row = tuple.Item2;
Product product = TaskUtils.FindByID(products, order.ProductID); Product product = TaskUtils.FindByID(products, order.ProductID);
row.Cells.Add(new TableCell { Text = order.CustomerSurname }); row.Cells.Add(new TableCell { Text = order.CustomerSurname });
row.Cells.Add(new TableCell { Text = order.CustomerName }); row.Cells.Add(new TableCell { Text = order.CustomerName });
row.Cells.Add(new TableCell { Text = order.ProductAmount.ToString() }); row.Cells.Add(new TableCell { Text = order.ProductAmount.ToString() });
row.Cells.Add(new TableCell { Text = String.Format("{0:f2}", order.ProductAmount * product.Price, 2) }); row.Cells.Add(new TableCell { Text = string.Format("{0:f2}", order.ProductAmount * product.Price, 2) });
} }
} }
} }

View File

@ -106,10 +106,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Code\InOutUtils.cs" /> <Compile Include="Code\InOutUtils.cs" />
<Compile Include="Code\LinkedList.cs" />
<Compile Include="Code\Order.cs" /> <Compile Include="Code\Order.cs" />
<Compile Include="Code\OrderList.cs" />
<Compile Include="Code\Product.cs" /> <Compile Include="Code\Product.cs" />
<Compile Include="Code\ProductList.cs" />
<Compile Include="Code\TaskUtils.cs" /> <Compile Include="Code\TaskUtils.cs" />
<Compile Include="Forma1.aspx.cs"> <Compile Include="Forma1.aspx.cs">
<DependentUpon>Forma1.aspx</DependentUpon> <DependentUpon>Forma1.aspx</DependentUpon>