1
0

feat: rewrite DynamicMemory.LD_24 so it is correct

This commit is contained in:
Rokas Puzonas 2022-03-14 11:45:13 +02:00
parent 4dbc149e13
commit 3bc94fc52a
16 changed files with 478 additions and 303 deletions

View File

@ -15,17 +15,37 @@
| Pavardė | Vardas | Įtaisas | Įtaiso kiekis |
---------------------------------------------------------------
| Petraitis | Petras | 0 | 10 |
| Onaite | Ona | 2 | 15 |
| Jonaitis | Jonas | 1 | 20 |
| Jonaitis | Jonas | 1 | 20 |
| Jonaitis | Jonas | 1 | 20 |
| Onaite | Ona | 2 | 200 |
| Jonaitis | Brolis | 1 | 100 |
| Jonaitis | Jonas | 1 | 100 |
| Jonaitis | Jonas | 1 | 20 |
| Jonaitis | Jonas | 1 | 20 |
| Onaite | Ona | 0 | 20 |
---------------------------------------------------------------
Populiariausias produktas: Varztas
Pardavimų kiekis: 200 vnt.
Pardavimų kaina: 10.00 eur.
---------------------------------------------------------------------------
| Populiariausi įtaisai |
---------------------------------------------------------------------------
| ID | Vardas | Įtaisų kiekis, vnt. | Įtaisų kaina, eur. |
---------------------------------------------------------------------------
| 1 | Varztas | 200 | 10.00 |
| 2 | Laidas | 200 | 400.00 |
---------------------------------------------------------------------------
Pirkėjai pagal rūšį:
---------------------------------------------------------------
| Atsuktuvas |
---------------------------------------------------------------
| Pavardė | Vardas | Įtaiso kiekis | Kaina |
---------------------------------------------------------------
| Onaite | Ona | 20 | 19.80 |
| Petraitis | Petras | 10 | 9.90 |
---------------------------------------------------------------
---------------------------------------------------------------
| Pirkėjai pagal rūšį (Varztas) |
| Varztas |
---------------------------------------------------------------
| Pavardė | Vardas | Įtaiso kiekis | Kaina |
---------------------------------------------------------------
@ -33,13 +53,28 @@ Pardavimų kaina: 10.00 eur.
| Jonaitis | Jonas | 100 | 5.00 |
---------------------------------------------------------------
---------------------------------------------------------------
| Laidas |
---------------------------------------------------------------
| Pavardė | Vardas | Įtaiso kiekis | Kaina |
---------------------------------------------------------------
| Onaite | Ona | 200 | 400.00 |
---------------------------------------------------------------
---------------------------------------------------------------
| Plaktukas |
---------------------------------------------------------------
| Pavardė | Vardas | Įtaiso kiekis | Kaina |
---------------------------------------------------------------
| Nėra |
---------------------------------------------------------------
------------------------------------------
| Atrinkti įtaisai (n=1, k=10.00) |
| Atrinkti įtaisai (n=1, k=1.00) |
------------------------------------------
| ID | Vardas | Kaina |
------------------------------------------
| 0 | Atsuktuvas | 0.99 |
| 1 | Varztas | 0.05 |
| 2 | Laidas | 2.00 |
------------------------------------------

View File

@ -1,4 +1,9 @@
Petraitis, Petras, 0, 10
Onaite, Ona, 2, 15
Jonaitis, Jonas, 1, 20
Jonaitis, Jonas, 1, 20
Jonaitis, Jonas, 1, 20
Onaite, Ona, 2, 200
Jonaitis, Brolis, 1, 100
Jonaitis, Jonas, 1, 100
Jonaitis, Jonas, 1, 20
Jonaitis, Jonas, 1, 20
Onaite, Ona, 0, 20

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code
{
public class CustomerNode
{
public Customer Data { get; set; }
public CustomerNode Next { get; set; }
public CustomerNode(Customer data = null, CustomerNode next = null)
{
Data = data;
Next = next;
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
@ -27,39 +28,39 @@ namespace LD_24.Code
string[] parts = line.Split(',');
string id = parts[0].Trim();
string name = parts[1].Trim();
decimal price = decimal.Parse(parts[2].Trim());
decimal price = decimal.Parse(parts[2].Trim(), CultureInfo.InvariantCulture);
products.AddToEnd(new Product(id, name, price));
}
return products;
}
public static CustomerList ReadCustomers(string filename)
public static OrderList ReadOrders(string filename)
{
CustomerList customers = new CustomerList();
OrderList orders = new OrderList();
foreach (string line in ReadLines(filename))
{
string[] parts = line.Split(',');
string surname = parts[0].Trim();
string name = parts[1].Trim();
string customerSurname = parts[0].Trim();
string customerName = parts[1].Trim();
string productID = parts[2].Trim();
int productAmount = int.Parse(parts[3].Trim());
customers.AddToEnd(new Customer(surname, name, productID, productAmount));
orders.AddToEnd(new Order(customerSurname, customerName, productID, productAmount));
}
return customers;
return orders;
}
public static void PrintCustomers(StreamWriter writer, CustomerList customers, string header)
public static void PrintOrders(StreamWriter writer, OrderList orders, string header)
{
writer.WriteLine(new string('-', 63));
writer.WriteLine("| {0, -59} |", header);
writer.WriteLine(new string('-', 63));
writer.WriteLine("| {0, -15} | {1, -15} | {2, 7} | {3, 13} |", "Pavardė", "Vardas", "Įtaisas", "Įtaiso kiekis");
writer.WriteLine(new string('-', 63));
if (customers.Count() > 0)
if (orders.Count() > 0)
{
foreach (Customer c in customers)
foreach (Order o in orders)
{
writer.WriteLine("| {0, -15} | {1, -15} | {2, 7} | {3, 13} |", c.Surname, c.Name, c.ProductID, c.ProductAmount);
writer.WriteLine("| {0, -15} | {1, -15} | {2, 7} | {3, 13} |", o.CustomerSurname, o.CustomerName, o.ProductID, o.ProductAmount);
}
}
else
@ -70,18 +71,23 @@ namespace LD_24.Code
writer.WriteLine();
}
public static void PrintCustomersWithPrices(StreamWriter writer, CustomerList customers, Product product, string header)
public static void PrintOrdersWithPrices(StreamWriter writer, OrderList orders, Product product, string header)
{
writer.WriteLine(new string('-', 63));
writer.WriteLine("| {0, -59} |", header);
writer.WriteLine(new string('-', 63));
writer.WriteLine("| {0, -15} | {1, -15} | {2, 13} | {3, 7} |", "Pavardė", "Vardas", "Įtaiso kiekis", "Kaina");
writer.WriteLine(new string('-', 63));
if (customers.Count() > 0)
OrderList filtered = TaskUtils.FilterByProduct(orders, product.ID);
filtered = TaskUtils.MergeOrders(filtered);
filtered.Sort();
if (filtered.Count() > 0)
{
foreach (Customer c in customers)
foreach (Order o in filtered)
{
writer.WriteLine("| {0, -15} | {1, -15} | {2, 13} | {3, 7:f2} |", c.Surname, c.Name, c.ProductAmount, c.ProductAmount *product.Price);
writer.WriteLine("| {0, -15} | {1, -15} | {2, 13} | {3, 7:f2} |", o.CustomerSurname, o.CustomerName, o.ProductAmount, o.ProductAmount*product.Price);
}
}
else
@ -113,5 +119,29 @@ namespace LD_24.Code
writer.WriteLine(new string('-', 42));
writer.WriteLine();
}
public static void PrintMostPopularProducts(StreamWriter writer, OrderList orders, ProductList popularProducts, string header)
{
writer.WriteLine(new string('-', 75));
writer.WriteLine("| {0, -71} |", header);
writer.WriteLine(new string('-', 75));
writer.WriteLine("| {0, -5} | {1, -20} | {2, 7} | {3} |", "ID", "Vardas", "Įtaisų kiekis, vnt.", "Įtaisų kaina, eur.");
writer.WriteLine(new string('-', 75));
if (popularProducts.Count() > 0)
{
foreach (Product p in popularProducts)
{
int sales = TaskUtils.CountProductSales(orders, p.ID);
writer.WriteLine("| {0, -5} | {1, -20} | {2, 19} | {3, 18:f2} |", p.ID, p.Name, sales, sales*p.Price);
}
}
else
{
writer.WriteLine("| {0, -71} |", "Nėra");
}
writer.WriteLine(new string('-', 75));
writer.WriteLine();
}
}
}

View File

@ -5,27 +5,27 @@ using System.Web;
namespace LD_24.Code
{
public class Customer
public class Order
{
public string Surname { get; set; }
public string Name { get; set; }
public string CustomerSurname { get; set; }
public string CustomerName { get; set; }
public string ProductID { get; set; }
public int ProductAmount { get; set; }
public Customer(string surname, string name, string productID, int productAmount)
public Order(string customerSurname, string customerName, string productID, int productAmount)
{
Surname = surname;
Name = name;
CustomerSurname = customerSurname;
CustomerName = customerName;
ProductID = productID;
ProductAmount = productAmount;
}
public override string ToString()
{
return String.Format("Customer{Name = '{0}'}", Name);
return String.Format("Order{Name = '{0}'}", CustomerName);
}
public static bool operator <(Customer a, Customer b)
public static bool operator <(Order a, Order b)
{
if (a.ProductAmount > b.ProductAmount)
{
@ -33,28 +33,28 @@ namespace LD_24.Code
}
else if (a.ProductAmount == b.ProductAmount)
{
int surnameCompare = a.Surname.CompareTo(b.Surname);
int surnameCompare = a.CustomerSurname.CompareTo(b.CustomerSurname);
if (surnameCompare < 0)
{
return true;
}
else if (surnameCompare == 0)
{
return a.Name.CompareTo(b.Name) < 0;
return a.CustomerName.CompareTo(b.CustomerName) < 0;
}
}
return false;
}
public static bool operator >(Customer a, Customer b)
public static bool operator >(Order a, Order b)
{
return !(a < b && a == b);
}
public static bool operator ==(Customer a, Customer b)
public static bool operator ==(Order a, Order b)
{
return a.ProductAmount == b.ProductAmount && a.Name == b.Name && a.Surname == b.Surname;
return a.ProductAmount == b.ProductAmount && a.CustomerName == b.CustomerName && a.CustomerSurname == b.CustomerSurname;
}
public static bool operator !=(Customer a, Customer b)
public static bool operator !=(Order a, Order b)
{
return !(a == b);
}

View File

@ -6,14 +6,26 @@ using System.Web;
namespace LD_24.Code
{
public class CustomerList : IEnumerable<Customer>
public class OrderList : IEnumerable<Order>
{
private CustomerNode head;
private CustomerNode tail;
public void AddToEnd(Customer customer)
class OrderNode
{
CustomerNode node = new CustomerNode(customer);
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;
public void AddToEnd(Order customer)
{
OrderNode node = new OrderNode(customer);
if (tail != null && head != null)
{
tail.Next = node;
@ -26,9 +38,9 @@ namespace LD_24.Code
}
}
public void AddToStart(Customer customer)
public void AddToStart(Order customer)
{
CustomerNode node = new CustomerNode(customer);
OrderNode node = new OrderNode(customer);
if (tail != null && head != null)
{
node.Next = head;
@ -41,10 +53,10 @@ namespace LD_24.Code
}
}
public Customer Get(int index)
public Order Get(int index)
{
int i = 0;
CustomerNode current = head;
OrderNode current = head;
while (i < index && current != null)
{
current = head.Next;
@ -56,7 +68,7 @@ namespace LD_24.Code
public int Count()
{
int count = 0;
CustomerNode current = head;
OrderNode current = head;
while (current != null)
{
current = current.Next;
@ -67,10 +79,10 @@ namespace LD_24.Code
public void Sort()
{
for (CustomerNode nodeA = head; nodeA != null; nodeA = nodeA.Next)
for (OrderNode nodeA = head; nodeA != null; nodeA = nodeA.Next)
{
CustomerNode min = nodeA;
for (CustomerNode nodeB = nodeA.Next; nodeB != null; nodeB = nodeB.Next)
OrderNode min = nodeA;
for (OrderNode nodeB = nodeA.Next; nodeB != null; nodeB = nodeB.Next)
{
if (nodeB.Data < min.Data)
{
@ -78,7 +90,7 @@ namespace LD_24.Code
}
}
Customer tmp = nodeA.Data;
Order tmp = nodeA.Data;
nodeA.Data = min.Data;
min.Data = tmp;
}
@ -86,12 +98,12 @@ namespace LD_24.Code
public override string ToString()
{
return String.Format("CustomerList{ Count = '{0}' }", Count());
return String.Format("OrderList{ Count = '{0}' }", Count());
}
public IEnumerator<Customer> GetEnumerator()
public IEnumerator<Order> GetEnumerator()
{
CustomerNode current = head;
OrderNode current = head;
while (current != null)
{
yield return current.Data;

View File

@ -5,12 +5,30 @@ using System.Web;
namespace LD_24.Code
{
/// <summary>
/// Holds informations about a single product
/// </summary>
public class Product
{
/// <summary>
/// Identification number of product
/// </summary>
public string ID { get; set; }
/// <summary>
/// Name of product
/// </summary>
public string Name { get; set; }
/// <summary>
/// Price of product
/// </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

@ -8,6 +8,18 @@ namespace LD_24.Code
{
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;

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code
{
public class ProductNode
{
public Product Data { get; set; }
public ProductNode Next { get; set; }
public ProductNode(Product data = null, ProductNode next = null)
{
Data = data;
Next = next;
}
}
}

View File

@ -8,62 +8,88 @@ namespace LD_24.Code
{
public static class TaskUtils
{
public static string FindMostPopularProduct(CustomerList customers)
public static List<string> FindMostPopularProducts(OrderList orders)
{
Dictionary<string, int> productCounter = new Dictionary<string, int>();
foreach (Customer customer in customers)
Dictionary<string, int> productSales = new Dictionary<string, int>();
foreach (Order order in orders)
{
if (!productCounter.ContainsKey(customer.ProductID))
if (!productSales.ContainsKey(order.ProductID))
{
productCounter.Add(customer.ProductID, 1);
productSales.Add(order.ProductID, order.ProductAmount);
}
else
{
productCounter[customer.ProductID]++;
productSales[order.ProductID] += order.ProductAmount;
}
}
string mostPopularProduct = null;
List<string> mostPopularProducts = new List<string>();
int mostPopularCount = 0;
foreach (string product in productCounter.Keys)
foreach (string product in productSales.Keys)
{
int count = productCounter[product];
int count = productSales[product];
if (count > mostPopularCount)
{
mostPopularCount = count;
mostPopularProduct = product;
mostPopularProducts = new List<string> { product };
} else if (count == mostPopularCount)
{
mostPopularProducts.Add(product);
}
}
return mostPopularProduct;
return mostPopularProducts;
}
public static int CountProductSales(CustomerList customers, string product)
public static int CountProductSales(OrderList orders, string product)
{
int sales = 0;
foreach (Customer customer in customers)
foreach (Order order in orders)
{
if (customer.ProductID == product)
if (order.ProductID == product)
{
sales += customer.ProductAmount;
sales += order.ProductAmount;
}
}
return sales;
}
public static CustomerList FilterByProduct(CustomerList customers, string product)
public static OrderList FilterByProduct(OrderList orders, string product)
{
CustomerList filtered = new CustomerList();
foreach (Customer customer in customers)
OrderList filtered = new OrderList();
foreach (Order order in orders)
{
if (customer.ProductID == product)
if (order.ProductID == product)
{
filtered.AddToEnd(customer);
filtered.AddToEnd(order);
}
}
return filtered;
}
public static OrderList MergeOrders(OrderList orders)
{
Dictionary<Tuple<string, string, string>, Order> ordersByName = new Dictionary<Tuple<string, string, string>, 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;
}
public static Product FindByID(ProductList products, string id)
{
foreach (Product product in products)
@ -76,28 +102,28 @@ namespace LD_24.Code
return null;
}
public static ProductList FilterByMaxPrice(ProductList products, decimal maxPrice)
public static ProductList FindByID(ProductList products, List<string> ids)
{
ProductList foundProducts = new ProductList();
foreach (string id in ids)
{
foundProducts.AddToEnd(FindByID(products, id));
}
return foundProducts;
}
public static ProductList FilterByQuantitySoldAndPrice(ProductList products, OrderList customers, int minSold, decimal maxPrice)
{
ProductList filtered = new ProductList();
foreach (Product product in products)
{
if (product.Price < maxPrice)
{
filtered.AddToEnd(product);
}
}
return filtered;
}
public static ProductList FilterByMinQuantitySold(ProductList products, CustomerList customers, decimal minSold)
{
ProductList filtered = new ProductList();
foreach (Product product in products)
{
int sold = CountProductSales(customers, product.ID);
if (sold >= minSold)
{
filtered.AddToEnd(product);
int sold = CountProductSales(customers, product.ID);
if (sold >= minSold)
{
filtered.AddToEnd(product);
}
}
}
return filtered;

View File

@ -4,28 +4,12 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="~/Styles/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Įtaisai:"></asp:Label>
<asp:Table ID="Table1" runat="server" BackColor="#FFFFCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" GridLines="Both">
</asp:Table>
<br />
<asp:Label ID="Label2" runat="server" Text="Pirkėjai:"></asp:Label>
<asp:Table ID="Table2" runat="server" BackColor="#FFFFCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" GridLines="Both">
</asp:Table>
<br />
<asp:Label ID="Label3" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label4" runat="server" Text="Pirkėjai pagal rūšį:"></asp:Label>
<asp:Table ID="Table3" runat="server" BackColor="#FFFFCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" GridLines="Both">
</asp:Table>
<br />
<hr />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
<br />
<asp:Label ID="Label5" runat="server" Text="n:"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
@ -38,11 +22,26 @@
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Atrinkti" OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label7" runat="server" Text="Atrinkti įtaisai:"></asp:Label>
<asp:Table ID="Table4" runat="server" BackColor="#FFFFCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" GridLines="Both">
</asp:Table>
<div id="ResultsDiv" runat="server">
<br />
<hr />
<br />
<asp:Label ID="Label1" runat="server" Text="Įtaisai:"></asp:Label>
<asp:Table ID="Table1" runat="server">
</asp:Table>
<asp:Label ID="Label2" runat="server" Text="Pirkėjai:"></asp:Label>
<asp:Table ID="Table2" runat="server">
</asp:Table>
<asp:Label ID="Label8" runat="server" Text="Populiariausi įtaisai:"></asp:Label>
<asp:Table ID="Table5" runat="server">
</asp:Table>
<asp:Label ID="Label4" runat="server" Text="Pirkėjai pagal rūšį:"></asp:Label>
<div id="OrdersByProductContainer" runat="server">
</div>
<asp:Label ID="Label7" runat="server" Text="Atrinkti įtaisai:"></asp:Label>
<asp:Table ID="Table4" runat="server">
</asp:Table>
</div>
</form>
</body>
</html>

View File

@ -16,49 +16,50 @@ namespace LD_24
private const string inputFileB = "App_Data/U24b.txt";
private const string outputFilename = "App_Data/Rezultatai.txt";
private ProductList Products;
private CustomerList Customers;
private Product PopularProduct;
private Product TargetProduct;
private CustomerList CustomersByTargetProduct;
protected void Page_Load(object sender, EventArgs e)
{
Products = InOutUtils.ReadProducts(Server.MapPath(inputFileA));
Customers = InOutUtils.ReadCustomers(Server.MapPath(inputFileB));
ShowProducts(Table1, Products);
ShowCustomers(Table2, Customers);
string popularProductID = TaskUtils.FindMostPopularProduct(Customers);
PopularProduct = null;
if (popularProductID != null)
{
PopularProduct = TaskUtils.FindByID(Products, popularProductID);
}
TargetProduct = TaskUtils.FindByID(Products, targetProductID);
CustomersByTargetProduct = TaskUtils.FilterByProduct(Customers, targetProductID);
CustomersByTargetProduct.Sort();
ShowMostPopularProduct(Label3, Customers, PopularProduct);
Label4.Text = $"Pirkėjai pagal rūšį ({TargetProduct.Name}):";
ShowCustomersByProduct(Table3, CustomersByTargetProduct, TargetProduct);
FindControl("ResultsDiv").Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
int n = int.Parse(TextBox1.Text);
decimal k = decimal.Parse(TextBox2.Text);
ProductList filteredProducts = TaskUtils.FilterByMinQuantitySold(TaskUtils.FilterByMaxPrice(Products, k), Customers, n);
FindControl("ResultsDiv").Visible = true;
ProductList products = InOutUtils.ReadProducts(Server.MapPath(inputFileA));
OrderList orders = InOutUtils.ReadOrders(Server.MapPath(inputFileB));
List<string> mostPopularProductIds = TaskUtils.FindMostPopularProducts(orders);
ProductList mostPopularProducts = TaskUtils.FindByID(products, mostPopularProductIds);
ProductList filteredProducts = TaskUtils.FilterByQuantitySoldAndPrice(products, orders, n, k);
ShowProducts(Table1, products);
ShowOrders(Table2, orders);
ShowMostPopularProducts(Table5, orders, mostPopularProducts);
ShowOrdersByProduct(FindControl("OrdersByProductContainer"), orders, products);
ShowProducts(Table4, filteredProducts);
using (StreamWriter writer = new StreamWriter(Server.MapPath(outputFilename)))
{
InOutUtils.PrintProducts(writer, Products, "Įtaisai");
InOutUtils.PrintCustomers(writer, Customers, "Pirkėjai");
InOutUtils.PrintProducts(writer, products, "Įtaisai");
InOutUtils.PrintOrders(writer, orders, "Pirkėjai");
InOutUtils.PrintMostPopularProducts(writer, orders, mostPopularProducts, "Populiariausi įtaisai");
writer.WriteLine("Pirkėjai pagal rūšį:");
foreach (Product product in products)
{
InOutUtils.PrintOrdersWithPrices(writer, orders, product, product.Name);
}
InOutUtils.PrintProducts(writer, filteredProducts, $"Atrinkti įtaisai (n={n}, k={k:f2})");
}
/*
using (StreamWriter writer = new StreamWriter(Server.MapPath(outputFilename)))
{
writer.Write("Populiariausias produktas: ");
if (PopularProduct == null)
@ -77,7 +78,7 @@ namespace LD_24
InOutUtils.PrintCustomersWithPrices(writer, CustomersByTargetProduct, TargetProduct, $"Pirkėjai pagal rūšį ({TargetProduct.Name})");
InOutUtils.PrintProducts(writer, filteredProducts, $"Atrinkti įtaisai (n={n}, k={k:f2})");
}
}*/
}
}
}

View File

@ -23,69 +23,6 @@ namespace LD_24
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
/// <summary>
/// Label1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label1;
/// <summary>
/// Table1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Table Table1;
/// <summary>
/// Label2 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label2;
/// <summary>
/// Table2 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Table Table2;
/// <summary>
/// Label3 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label3;
/// <summary>
/// Label4 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label4;
/// <summary>
/// Table3 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Table Table3;
/// <summary>
/// ValidationSummary1 control.
/// </summary>
@ -158,6 +95,87 @@ namespace LD_24
/// </remarks>
protected global::System.Web.UI.WebControls.Button Button1;
/// <summary>
/// ResultsDiv control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlGenericControl ResultsDiv;
/// <summary>
/// Label1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label1;
/// <summary>
/// Table1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Table Table1;
/// <summary>
/// Label2 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label2;
/// <summary>
/// Table2 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Table Table2;
/// <summary>
/// Label8 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label8;
/// <summary>
/// Table5 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Table Table5;
/// <summary>
/// Label4 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label4;
/// <summary>
/// OrdersByProductContainer control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlGenericControl OrdersByProductContainer;
/// <summary>
/// Label7 control.
/// </summary>

View File

@ -10,83 +10,131 @@ namespace LD_24
{
public partial class Forma1 : System.Web.UI.Page
{
public void ShowProducts(Table table, ProductList products)
private IEnumerable<Tuple<Product, TableRow>> ShowProdcutsTable(Table table, ProductList products, params string[] columns)
{
table.Rows.Clear();
TableRow header = new TableRow();
header.Cells.Add(new TableCell { Text = "ID" });
header.Cells.Add(new TableCell { Text = "Vardas" });
header.Cells.Add(new TableCell { Text = "Kaina, eur." });
foreach (string column in columns)
{
header.Cells.Add(new TableCell { Text = column });
}
table.Rows.Add(header);
foreach (Product product in products)
if (products.Count() > 0) {
foreach (Product product in products)
{
TableRow row = new TableRow();
yield return Tuple.Create(product, row);
table.Rows.Add(row);
}
} else
{
TableRow row = new TableRow();
row.Cells.Add(new TableCell { Text = "Nėra", ColumnSpan = columns.Length });
table.Rows.Add(row);
}
}
private IEnumerable<Tuple<Order, TableRow>> ShowOrdersTable(Table table, string title, OrderList orders, params string[] columns)
{
table.Rows.Clear();
if (title != null)
{
TableRow row = new TableRow();
row.Cells.Add(new TableCell { Text = title, ColumnSpan = columns.Length });
table.Rows.Add(row);
}
TableRow header = new TableRow();
foreach (string column in columns)
{
header.Cells.Add(new TableCell { Text = column });
}
table.Rows.Add(header);
if (orders.Count() > 0)
{
foreach (Order order in orders)
{
TableRow row = new TableRow();
yield return Tuple.Create(order, row);
table.Rows.Add(row);
}
}
else
{
TableRow row = new TableRow();
row.Cells.Add(new TableCell { Text = "Nėra", ColumnSpan = columns.Length });
table.Rows.Add(row);
}
}
private IEnumerable<Tuple<Order, TableRow>> ShowOrdersTable(Table table, OrderList orders, params string[] columns)
{
return ShowOrdersTable(table, null, orders, columns);
}
public void ShowProducts(Table table, ProductList products)
{
foreach (var tuple in ShowProdcutsTable(table, products, "ID", "Vardas", "Kaina, eur."))
{
Product product = tuple.Item1;
TableRow row = tuple.Item2;
row.Cells.Add(new TableCell { Text = product.ID });
row.Cells.Add(new TableCell { Text = product.Name });
row.Cells.Add(new TableCell { Text = product.Price.ToString() });
table.Rows.Add(row);
}
}
public void ShowCustomers(Table table, CustomerList customers)
public void ShowOrders(Table table, OrderList orders)
{
table.Rows.Clear();
TableRow header = new TableRow();
header.Cells.Add(new TableCell { Text = "Pavardė" });
header.Cells.Add(new TableCell { Text = "Vardas" });
header.Cells.Add(new TableCell { Text = "Įtaisas" });
header.Cells.Add(new TableCell { Text = "Įtaiso kiekis, vnt." });
table.Rows.Add(header);
foreach (Customer customer in customers)
foreach (var tuple in ShowOrdersTable(table, orders, "Pavardė", "Vardas", "Įtaisas", "Įtaisų kiekis, vnt."))
{
TableRow row = new TableRow();
row.Cells.Add(new TableCell { Text = customer.Surname });
row.Cells.Add(new TableCell { Text = customer.Name });
row.Cells.Add(new TableCell { Text = customer.ProductID.ToString() });
row.Cells.Add(new TableCell { Text = customer.ProductAmount.ToString() });
table.Rows.Add(row);
Order order = tuple.Item1;
TableRow row = tuple.Item2;
row.Cells.Add(new TableCell { Text = order.CustomerSurname });
row.Cells.Add(new TableCell { Text = order.CustomerName });
row.Cells.Add(new TableCell { Text = order.ProductID.ToString() });
row.Cells.Add(new TableCell { Text = order.ProductAmount.ToString() });
}
}
public void ShowMostPopularProduct(Label label, CustomerList customers, Product product)
public void ShowMostPopularProducts(Table table, OrderList orders, ProductList popularProducts)
{
label.Text = "Populiariausias produktas: ";
if (product == null)
foreach (var tuple in ShowProdcutsTable(table, popularProducts, "ID", "Vardas", "Įtaisų kiekis, vnt.", "Įtaisų kaina, eur."))
{
label.Text = "Nėra";
return;
Product product = tuple.Item1;
TableRow row = tuple.Item2;
int sales = TaskUtils.CountProductSales(orders, product.ID);
row.Cells.Add(new TableCell { Text = product.ID });
row.Cells.Add(new TableCell { Text = product.Name });
row.Cells.Add(new TableCell { Text = sales.ToString() });
row.Cells.Add(new TableCell { Text = String.Format("{0:f2}", sales * product.Price) });
}
label.Text += $"{product.Name}<br />";
int sales = TaskUtils.CountProductSales(customers, product.ID);
label.Text += $"Pardavimų kiekis: {sales} vnt.<br />";
label.Text += $"Pardavimų kaina: {sales*product.Price:f2} eur.";
}
public void ShowCustomersByProduct(Table table, CustomerList customers, Product product)
public void ShowOrdersByProduct(Control container, OrderList orders, ProductList products)
{
table.Rows.Clear();
TableRow header = new TableRow();
header.Cells.Add(new TableCell { Text = "Pavardė" });
header.Cells.Add(new TableCell { Text = "Vardas" });
header.Cells.Add(new TableCell { Text = "Įtaisas kiekis, vnt." });
header.Cells.Add(new TableCell { Text = "Kaina, eur." });
table.Rows.Add(header);
foreach (Customer customer in customers)
container.Controls.Clear();
foreach (Product product in products)
{
TableRow row = new TableRow();
row.Cells.Add(new TableCell { Text = customer.Surname });
row.Cells.Add(new TableCell { Text = customer.Name });
row.Cells.Add(new TableCell { Text = customer.ProductAmount.ToString() });
row.Cells.Add(new TableCell { Text = (Math.Round(customer.ProductAmount*product.Price, 2)).ToString() });
table.Rows.Add(row);
OrderList filtered = TaskUtils.FilterByProduct(orders, product.ID);
filtered = TaskUtils.MergeOrders(filtered);
filtered.Sort();
Table table = new Table();
container.Controls.Add(table);
foreach (var tuple in ShowOrdersTable(table, product.Name, filtered, "Pavardė", "Vardas", "Įtaisų kiekis, vnt.", "Sumokėta, eur."))
{
Order order = tuple.Item1;
TableRow row = tuple.Item2;
row.Cells.Add(new TableCell { Text = order.CustomerSurname });
row.Cells.Add(new TableCell { Text = order.CustomerName });
row.Cells.Add(new TableCell { Text = order.ProductAmount.ToString() });
row.Cells.Add(new TableCell { Text = String.Format("{0:f2}", order.ProductAmount * product.Price, 2) });
}
}
}
}

View File

@ -72,16 +72,15 @@
<Content Include="App_Data\U24b.txt" />
<Content Include="App_Data\U24a.txt" />
<Content Include="Forma1.aspx" />
<Content Include="Styles\main.css" />
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Code\Customer.cs" />
<Compile Include="Code\CustomerList.cs" />
<Compile Include="Code\CustomerNode.cs" />
<Compile Include="Code\Order.cs" />
<Compile Include="Code\OrderList.cs" />
<Compile Include="Code\InOutUtils.cs" />
<Compile Include="Code\Product.cs" />
<Compile Include="Code\ProductList.cs" />
<Compile Include="Code\ProductNode.cs" />
<Compile Include="Code\TaskUtils.cs" />
<Compile Include="Forma1.aspx.cs">
<DependentUpon>Forma1.aspx</DependentUpon>

View File

@ -0,0 +1,10 @@
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
table {
margin-bottom: 1em;
background-color: #FFFFCC;
}