1
0

docs: add comments to DynamicMemory.LD_24

This commit is contained in:
Rokas Puzonas 2022-03-14 14:44:08 +02:00
parent ba678342e4
commit e7ee870298
9 changed files with 188 additions and 32 deletions

View File

@ -8,8 +8,16 @@ using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Class used for reading and writing to files
/// </summary>
public static class InOutUtils
{
/// <summary>
/// Read lines from a file
/// </summary>
/// <param name="filename">Target filename</param>
/// <returns>IEnumerable of all the lines</returns>
private static IEnumerable<string> ReadLines(string filename)
{
using (StreamReader reader = File.OpenText(filename))
@ -21,6 +29,12 @@ namespace LD_24.Code
}
}
}
/// <summary>
/// Read products from a file
/// </summary>
/// <param name="filename">Target file</param>
/// <returns>A list of products</returns>
public static ProductList ReadProducts(string filename)
{
ProductList products = new ProductList();
@ -35,6 +49,11 @@ namespace LD_24.Code
return products;
}
/// <summary>
/// Read orders from a file
/// </summary>
/// <param name="filename">Target file</param>
/// <returns>A list of orders</returns>
public static OrderList ReadOrders(string filename)
{
OrderList orders = new OrderList();
@ -50,6 +69,12 @@ namespace LD_24.Code
return orders;
}
/// <summary>
/// Print a single table row to file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="cells">Cell data</param>
/// <param name="widths">Cell widths</param>
private static void PrintTableRow(StreamWriter writer, List<string> cells, List<int> widths)
{
for (int i = 0; i < widths.Count; i++)
@ -62,6 +87,14 @@ namespace LD_24.Code
writer.WriteLine("|");
}
/// <summary>
/// Print a table to a file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="header">Header above table</param>
/// <param name="list">Target list</param>
/// <param name="columns">Column names</param>
/// <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)
{
// 0. Collect all the rows
@ -128,6 +161,12 @@ namespace LD_24.Code
writer.WriteLine();
}
/// <summary>
/// Print orders table to file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="orders">List of orders</param>
/// <param name="header">Header above table</param>
public static void PrintOrders(StreamWriter writer, OrderList orders, string header)
{
foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaisas", "-Įtaiso kiekis"))
@ -141,6 +180,13 @@ namespace LD_24.Code
}
}
/// <summary>
/// Print orders with prices table to file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="orders">List of orders</param>
/// <param name="products">List of products</param>
/// <param name="header">Header above table</param>
public static void PrintOrdersWithPrices(StreamWriter writer, OrderList orders, ProductList products, string header)
{
foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaiso kiekis, vnt.", "-Kaina, eur."))
@ -156,6 +202,12 @@ namespace LD_24.Code
}
}
/// <summary>
/// Print a products table to file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="products">List of products</param>
/// <param name="header">Header above table</param>
public static void PrintProducts(StreamWriter writer, ProductList products, string header)
{
foreach (var tuple in PrintTable(writer, header, products, "ID", "Vardas", "-Kaina"))
@ -168,6 +220,13 @@ namespace LD_24.Code
}
}
/// <summary>
/// Print a table of most popular products to file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="orders">List of orders</param>
/// <param name="popularProducts">List of most popular products</param>
/// <param name="header">Header above table</param>
public static void PrintMostPopularProducts(StreamWriter writer, OrderList orders, ProductList popularProducts, string header)
{
foreach (var tuple in PrintTable(writer, header, popularProducts, "ID", "Vardas", "-Įtaisų kiekis, vnt.", "-Įtaisų kaina, eur."))

View File

@ -5,11 +5,26 @@ using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Class used for storing a single order
/// </summary>
public class Order
{
/// <summary>
/// Surname of customer who ordered
/// </summary>
public string CustomerSurname { get; set; }
/// <summary>
/// Name of customer who ordered
/// </summary>
public string CustomerName { get; set; }
/// <summary>
/// ID of ordered product
/// </summary>
public string ProductID { get; set; }
/// <summary>
/// Amount of ordered products
/// </summary>
public int ProductAmount { get; set; }
public Order(string customerSurname, string customerName, string productID, int productAmount)

View File

@ -6,6 +6,9 @@ using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Stores a orders in a linked list
/// </summary>
public class OrderList : IEnumerable<Order>
{
class OrderNode
@ -23,6 +26,10 @@ namespace LD_24.Code
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);
@ -38,6 +45,10 @@ namespace LD_24.Code
}
}
/// <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);
@ -53,6 +64,10 @@ namespace LD_24.Code
}
}
/// <summary>
/// Get the number of values stored in linked list
/// </summary>
/// <returns>A count</returns>
public int Count()
{
int count = 0;
@ -65,6 +80,9 @@ namespace LD_24.Code
return count;
}
/// <summary>
/// Sorts the linked list
/// </summary>
public void Sort()
{
for (OrderNode nodeA = head; nodeA != null; nodeA = nodeA.Next)

View File

@ -23,12 +23,6 @@ namespace LD_24.Code
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// Constructs a new product
/// </summary>
/// <param name="ID"></param>
/// <param name="name"></param>
/// <param name="price"></param>
public Product(string iD, string name, decimal price)
{
ID = iD;

View File

@ -6,6 +6,9 @@ using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Stores multiple products in a linked list
/// </summary>
public class ProductList : IEnumerable<Product>
{
class ProductNode
@ -23,6 +26,10 @@ namespace LD_24.Code
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);
@ -38,6 +45,10 @@ namespace LD_24.Code
}
}
/// <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);
@ -53,6 +64,10 @@ namespace LD_24.Code
}
}
/// <summary>
/// Get the number of values stored in the linked list
/// </summary>
/// <returns>A count</returns>
public int Count()
{
int count = 0;

View File

@ -6,8 +6,16 @@ using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Various functions for operations on data
/// </summary>
public static class TaskUtils
{
/// <summary>
/// Finds the most popular products by the number of sales
/// </summary>
/// <param name="orders">List of orders</param>
/// <returns>List of products ids</returns>
public static List<string> FindMostPopularProducts(OrderList orders)
{
Dictionary<string, int> productSales = new Dictionary<string, int>();
@ -41,6 +49,12 @@ namespace LD_24.Code
return mostPopularProducts;
}
/// <summary>
/// Counts the number of sales of a certain product
/// </summary>
/// <param name="orders">List of products</param>
/// <param name="product">Target product id</param>
/// <returns>Sales</returns>
public static int CountProductSales(OrderList orders, string product)
{
int sales = 0;
@ -54,19 +68,11 @@ namespace LD_24.Code
return sales;
}
public static OrderList FilterByProduct(OrderList orders, string product)
{
OrderList filtered = new OrderList();
foreach (Order order in orders)
{
if (order.ProductID == product)
{
filtered.AddToEnd(order);
}
}
return filtered;
}
/// <summary>
/// Merge orders which have the same customer name, surname and product id into a single order.
/// </summary>
/// <param name="orders">A list of orders</param>
/// <returns>A list of orders where same orders have been merged</returns>
public static OrderList MergeOrders(OrderList orders)
{
Dictionary<Tuple<string, string, string>, Order> ordersByName = new Dictionary<Tuple<string, string, string>, Order>();
@ -90,6 +96,12 @@ namespace LD_24.Code
return mergedOrders;
}
/// <summary>
/// Finds a product by it's id
/// </summary>
/// <param name="products">List of products</param>
/// <param name="id">Target product id</param>
/// <returns>The product</returns>
public static Product FindByID(ProductList products, string id)
{
foreach (Product product in products)
@ -102,6 +114,12 @@ namespace LD_24.Code
return null;
}
/// <summary>
/// Find all products by their ids
/// </summary>
/// <param name="products">List of products</param>
/// <param name="ids">List of product ids</param>
/// <returns>List of products</returns>
public static ProductList FindByID(ProductList products, List<string> ids)
{
ProductList foundProducts = new ProductList();
@ -112,14 +130,22 @@ namespace LD_24.Code
return foundProducts;
}
public static ProductList FilterByQuantitySoldAndPrice(ProductList products, OrderList customers, int minSold, decimal maxPrice)
/// <summary>
/// Filter a list of products by sales and price.
/// </summary>
/// <param name="products">List of products</param>
/// <param name="orders">List of orders</param>
/// <param name="minSold">Minimmum sales amount</param>
/// <param name="maxPrice">Max product price</param>
/// <returns>A list of filtered products</returns>
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(customers, product.ID);
int sold = CountProductSales(orders, product.ID);
if (sold >= minSold)
{
filtered.AddToEnd(product);
@ -129,6 +155,11 @@ namespace LD_24.Code
return filtered;
}
/// <summary>
/// Find all customer which bought only 1 type of product
/// </summary>
/// <param name="orders">List of orders</param>
/// <returns>A list of filtered orders</returns>
public static OrderList FindCustomerWithSingleProduct(OrderList orders)
{
Dictionary<Tuple<string, string>, OrderList> ordersByCusomer = new Dictionary<Tuple<string, string>, OrderList>();
@ -152,6 +183,5 @@ namespace LD_24.Code
}
return finalList;
}
}
}

View File

@ -9,9 +9,11 @@ using LD_24.Code;
namespace LD_24
{
/// <summary>
/// Main form
/// </summary>
public partial class Forma1 : System.Web.UI.Page
{
private const string targetProductID = "1";
private const string inputFileA = "App_Data/U24a.txt";
private const string inputFileB = "App_Data/U24b.txt";
private const string outputFilename = "App_Data/Rezultatai.txt";

View File

@ -11,6 +11,13 @@ namespace LD_24
{
public partial class Forma1 : System.Web.UI.Page
{
/// <summary>
/// Show a list in a table
/// </summary>
/// <param name="table">Target table</param>
/// <param name="list">Target list</param>
/// <param name="columns">Columns names</param>
/// <returns></returns>
private IEnumerable<Tuple<object, TableRow>> ShowTable(Table table, IEnumerable list, params string[] columns)
{
TableRow header = new TableRow();
@ -37,15 +44,11 @@ namespace LD_24
}
}
private IEnumerable<Tuple<object, TableRow>> ShowTable(Table table, string title, IEnumerable list, params string[] columns)
{
TableRow row = new TableRow();
row.Cells.Add(new TableCell { Text = title, ColumnSpan = columns.Length });
table.Rows.Add(row);
return ShowTable(table, list, columns);
}
/// <summary>
/// Show a list of products in a table
/// </summary>
/// <param name="table">Target table</param>
/// <param name="products">Target products</param>
public void ShowProducts(Table table, ProductList products)
{
foreach (var tuple in ShowTable(table, products, "ID", "Vardas", "Kaina, eur."))
@ -58,6 +61,11 @@ namespace LD_24
}
}
/// <summary>
/// Show a list of orders in a table
/// </summary>
/// <param name="table">Target table</param>
/// <param name="orders">Target orders</param>
public void ShowOrders(Table table, OrderList orders)
{
foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisas", "Įtaisų kiekis, vnt."))
@ -71,6 +79,12 @@ namespace LD_24
}
}
/// <summary>
/// Show the most popular products in a table
/// </summary>
/// <param name="table">Target table</param>
/// <param name="orders">Target orders</param>
/// <param name="popularProducts">List of most popular products</param>
public void ShowMostPopularProducts(Table table, OrderList orders, ProductList popularProducts)
{
foreach (var tuple in ShowTable(table, popularProducts, "ID", "Vardas", "Įtaisų kiekis, vnt.", "Įtaisų kaina, eur."))
@ -85,6 +99,12 @@ namespace LD_24
}
}
/// <summary>
/// Show a list of orders with their prices in a table
/// </summary>
/// <param name="table">Target table</param>
/// <param name="orders">Target orders</param>
/// <param name="products">List of products</param>
public void ShowOrdersWithPrices(Table table, OrderList orders, ProductList products)
{
foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisų kiekis, vnt.", "Sumokėta, eur."))

View File

@ -76,6 +76,9 @@
<Content Include="tests\1\inputs\U24a.txt" />
<Content Include="tests\1\inputs\U24b.txt" />
<Content Include="tests\1\outputs\Rezultatai.txt" />
<Content Include="tests\2\inputs\U24a.txt" />
<Content Include="tests\2\inputs\U24b.txt" />
<Content Include="tests\2\outputs\Rezultatai.txt" />
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>