From ba678342e4be2b992f8eea2ced5357f41deac579 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 14 Mar 2022 14:13:41 +0200 Subject: [PATCH] fix: final working code for `DynamicMemory.LD_24` --- DynamicMemory/LD_24/App_Data/Rezultatai.txt | 125 ++++++-------- DynamicMemory/LD_24/App_Data/U24b.txt | 3 + DynamicMemory/LD_24/Code/InOutUtils.cs | 170 ++++++++++++-------- DynamicMemory/LD_24/Code/OrderList.cs | 12 -- DynamicMemory/LD_24/Code/ProductList.cs | 12 -- DynamicMemory/LD_24/Code/TaskUtils.cs | 24 +++ DynamicMemory/LD_24/Forma1.aspx | 6 +- DynamicMemory/LD_24/Forma1.aspx.cs | 36 +---- DynamicMemory/LD_24/Forma1.aspx.designer.cs | 54 +++---- DynamicMemory/LD_24/Forma1Utils.aspx.cs | 102 ++++-------- DynamicMemory/LD_24/LD_24.csproj | 3 + 11 files changed, 249 insertions(+), 298 deletions(-) diff --git a/DynamicMemory/LD_24/App_Data/Rezultatai.txt b/DynamicMemory/LD_24/App_Data/Rezultatai.txt index 570cd9b..8b0f319 100644 --- a/DynamicMemory/LD_24/App_Data/Rezultatai.txt +++ b/DynamicMemory/LD_24/App_Data/Rezultatai.txt @@ -1,80 +1,55 @@ ------------------------------------------- -| Įtaisai | ------------------------------------------- -| ID | Vardas | Kaina | ------------------------------------------- -| 0 | Atsuktuvas | 0.99 | -| 1 | Varztas | 0.05 | -| 2 | Laidas | 2.00 | -| 3 | Plaktukas | 2.99 | ------------------------------------------- +---------------------------- +| Įtaisai | +---------------------------- +| ID | Vardas | Kaina | +---------------------------- +| 0 | Atsuktuvas | 0,99 | +| 1 | Varztas | 0,05 | +| 2 | Laidas | 2,00 | +| 3 | Plaktukas | 2,99 | +---------------------------- ---------------------------------------------------------------- -| Pirkėjai | ---------------------------------------------------------------- -| Pavardė | Vardas | Įtaisas | Įtaiso kiekis | ---------------------------------------------------------------- -| Petraitis | Petras | 0 | 10 | -| Jonaitis | Jonas | 1 | 20 | -| Jonaitis | Jonas | 1 | 20 | -| Jonaitis | Jonas | 1 | 20 | -| Onaite | Ona | 2 | 200 | -| Jonaitis | Brolis | 1 | 100 | -| Jonaitis | Jonas | 1 | 20 | -| Jonaitis | Jonas | 1 | 20 | -| Onaite | Ona | 0 | 20 | ---------------------------------------------------------------- +-------------------------------------------------- +| Pirkėjai | +-------------------------------------------------- +| Pavardė | Vardas | Įtaisas | Įtaiso kiekis | +-------------------------------------------------- +| Petraitis | Petras | 0 | 10 | +| Petraitis | Petras | 1 | 10 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 1 | 20 | +| Onaite | Ona | 2 | 200 | +| Jonaitis | Brolis | 1 | 100 | +| Jonaitis | Brolis | 0 | 100 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 1 | 20 | +| Jonaitis | Jonas | 2 | 20 | +| Onaite | Ona | 0 | 20 | +-------------------------------------------------- ---------------------------------------------------------------------------- -| Populiariausi įtaisai | ---------------------------------------------------------------------------- -| ID | Vardas | Įtaisų kiekis, vnt. | Įtaisų kaina, eur. | ---------------------------------------------------------------------------- -| 1 | Varztas | 200 | 10.00 | -| 2 | Laidas | 200 | 400.00 | ---------------------------------------------------------------------------- +------------------------------------------------------------ +| Populiariausi įtaisai | +------------------------------------------------------------ +| ID | Vardas | Įtaisų kiekis, vnt. | Įtaisų kaina, eur. | +------------------------------------------------------------ +| 2 | Laidas | 220 | 440,00 | +------------------------------------------------------------ -Pirkėjai pagal rūšį: ---------------------------------------------------------------- -| Atsuktuvas | ---------------------------------------------------------------- -| Pavardė | Vardas | Įtaiso kiekis | Kaina | ---------------------------------------------------------------- -| Onaite | Ona | 20 | 19.80 | -| Petraitis | Petras | 10 | 9.90 | ---------------------------------------------------------------- +---------------------------------------------------------- +| Vienos rūšies pirkėjai | +---------------------------------------------------------- +| Pavardė | Vardas | Įtaiso kiekis, vnt. | Kaina, eur. | +---------------------------------------------------------- +| Nėra | +---------------------------------------------------------- ---------------------------------------------------------------- -| Varztas | ---------------------------------------------------------------- -| Pavardė | Vardas | Įtaiso kiekis | Kaina | ---------------------------------------------------------------- -| Jonaitis | Brolis | 100 | 5.00 | -| 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=1.00) | ------------------------------------------- -| ID | Vardas | Kaina | ------------------------------------------- -| 0 | Atsuktuvas | 0.99 | -| 1 | Varztas | 0.05 | ------------------------------------------- +---------------------------------- +| Atrinkti įtaisai (n=1, k=1,00) | +---------------------------------- +| ID | Vardas | Kaina | +---------------------------------- +| 0 | Atsuktuvas | 0,99 | +| 1 | Varztas | 0,05 | +---------------------------------- diff --git a/DynamicMemory/LD_24/App_Data/U24b.txt b/DynamicMemory/LD_24/App_Data/U24b.txt index 8defdf5..22a3639 100644 --- a/DynamicMemory/LD_24/App_Data/U24b.txt +++ b/DynamicMemory/LD_24/App_Data/U24b.txt @@ -1,9 +1,12 @@ Petraitis, Petras, 0, 10 +Petraitis, Petras, 1, 10 Jonaitis, Jonas, 1, 20 Jonaitis, Jonas, 1, 20 Jonaitis, Jonas, 1, 20 Onaite, Ona, 2, 200 Jonaitis, Brolis, 1, 100 +Jonaitis, Brolis, 0, 100 Jonaitis, Jonas, 1, 20 Jonaitis, Jonas, 1, 20 +Jonaitis, Jonas, 2, 20 Onaite, Ona, 0, 20 \ No newline at end of file diff --git a/DynamicMemory/LD_24/Code/InOutUtils.cs b/DynamicMemory/LD_24/Code/InOutUtils.cs index 40ea88e..8f9a4f0 100644 --- a/DynamicMemory/LD_24/Code/InOutUtils.cs +++ b/DynamicMemory/LD_24/Code/InOutUtils.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -49,99 +50,136 @@ namespace LD_24.Code return orders; } - public static void PrintOrders(StreamWriter writer, OrderList orders, string header) + private static void PrintTableRow(StreamWriter writer, List cells, List widths) { - 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 (orders.Count() > 0) + for (int i = 0; i < widths.Count; i++) { - foreach (Order o in orders) + if (widths[i] < 0) + writer.Write("| {0} ", cells[i].PadRight(-widths[i])); + else + writer.Write("| {0} ", cells[i].PadLeft(widths[i])); + } + writer.WriteLine("|"); + } + + private static IEnumerable>> PrintTable(StreamWriter writer, string header, IEnumerable list, params string[] columns) + { + // 0. Collect all the rows + List> rows = new List>(); + foreach (object item in list) + { + List row = new List(); + yield return Tuple.Create(item, row); + rows.Add(row); + } + + // 1. Determine the width of each column + List widths = new List(); + int totalWidth = 3*(columns.Length - 1); + for (int i = 0; i < columns.Length; i++) + { + int width = columns[i].Length; + foreach (var row in rows) { - writer.WriteLine("| {0, -15} | {1, -15} | {2, 7} | {3, 13} |", o.CustomerSurname, o.CustomerName, o.ProductID, o.ProductAmount); + width = Math.Max(row[i].Length, width); + } + widths.Add(width); + totalWidth += width; + } + + // If the header is longer than the body, make the last column wider. + // So the table is a nice rectangle when output to the file + if (header.Length > totalWidth) + { + widths[widths.Count - 1] += (header.Length - totalWidth); + totalWidth = header.Length; + } + + totalWidth += 2 * 2; + + // 2. Adjust widths to account for aligning + for (int i = 0; i < columns.Length; i++) + { + if (columns[i][0] == '-') + { + widths[i] = -widths[i]; + columns[i] = columns[i].Substring(1); } } - else + + // 3. Display the table + writer.WriteLine(new string('-', totalWidth)); + writer.WriteLine("| {0} |", header.PadRight(totalWidth - 4)); + writer.WriteLine(new string('-', totalWidth)); + PrintTableRow(writer, new List(columns), widths); + writer.WriteLine(new string('-', totalWidth)); + if (rows.Count > 0) { - writer.WriteLine("| {0, -59} |", "Nėra"); + foreach (var row in rows) + { + PrintTableRow(writer, row, widths); + } + } else + { + writer.WriteLine("| {0} |", "Nėra".PadRight(totalWidth - 4)); } - writer.WriteLine(new string('-', 63)); + writer.WriteLine(new string('-', totalWidth)); + writer.WriteLine(); } - public static void PrintOrdersWithPrices(StreamWriter writer, OrderList orders, Product product, 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, 13} | {3, 7} |", "Pavardė", "Vardas", "Įtaiso kiekis", "Kaina"); - writer.WriteLine(new string('-', 63)); - - OrderList filtered = TaskUtils.FilterByProduct(orders, product.ID); - filtered = TaskUtils.MergeOrders(filtered); - filtered.Sort(); - - if (filtered.Count() > 0) + foreach (var tuple in PrintTable(writer, header, orders, "Pavardė", "Vardas", "-Įtaisas", "-Įtaiso kiekis")) { - foreach (Order o in filtered) - { - writer.WriteLine("| {0, -15} | {1, -15} | {2, 13} | {3, 7:f2} |", o.CustomerSurname, o.CustomerName, o.ProductAmount, o.ProductAmount*product.Price); - } + Order order = (Order)tuple.Item1; + List row = tuple.Item2; + row.Add(order.CustomerSurname); + row.Add(order.CustomerName); + row.Add(order.ProductID); + row.Add(order.ProductAmount.ToString()); } - else + } + + 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.")) { - writer.WriteLine("| {0, -59} |", "Nėra"); + Order order = (Order)tuple.Item1; + List row = tuple.Item2; + Product product = TaskUtils.FindByID(products, order.ProductID); + + row.Add(order.CustomerSurname); + row.Add(order.CustomerName); + row.Add(order.ProductAmount.ToString()); + row.Add(String.Format("{0:f2}", order.ProductAmount * product.Price)); } - writer.WriteLine(new string('-', 63)); - writer.WriteLine(); } public static void PrintProducts(StreamWriter writer, ProductList products, string header) { - writer.WriteLine(new string('-', 42)); - writer.WriteLine("| {0, -38} |", header); - writer.WriteLine(new string('-', 42)); - writer.WriteLine("| {0, -5} | {1, -20} | {2, 7} |", "ID", "Vardas", "Kaina"); - writer.WriteLine(new string('-', 42)); - if (products.Count() > 0) + foreach (var tuple in PrintTable(writer, header, products, "ID", "Vardas", "-Kaina")) { - foreach (Product c in products) - { - writer.WriteLine("| {0, -5} | {1, -20} | {2, 7} |", c.ID, c.Name, c.Price); - } + Product product = (Product)tuple.Item1; + List row = tuple.Item2; + row.Add(product.ID); + row.Add(product.Name); + row.Add(String.Format("{0:f2}", product.Price)); } - else - { - writer.WriteLine("| {0, -38} |", "Nėra"); - } - 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 (var tuple in PrintTable(writer, header, popularProducts, "ID", "Vardas", "-Įtaisų kiekis, vnt.", "-Įtaisų kaina, eur.")) { - 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); - } + Product product = (Product)tuple.Item1; + List row = tuple.Item2; + int sales = TaskUtils.CountProductSales(orders, product.ID); + row.Add(product.ID); + row.Add(product.Name); + row.Add(sales.ToString()); + row.Add(String.Format("{0:f2}", sales * product.Price)); } - else - { - writer.WriteLine("| {0, -71} |", "Nėra"); - } - writer.WriteLine(new string('-', 75)); - writer.WriteLine(); } } } \ No newline at end of file diff --git a/DynamicMemory/LD_24/Code/OrderList.cs b/DynamicMemory/LD_24/Code/OrderList.cs index 5b45438..488bedd 100644 --- a/DynamicMemory/LD_24/Code/OrderList.cs +++ b/DynamicMemory/LD_24/Code/OrderList.cs @@ -53,18 +53,6 @@ namespace LD_24.Code } } - public Order Get(int index) - { - int i = 0; - OrderNode current = head; - while (i < index && current != null) - { - current = head.Next; - i++; - } - return current.Data; - } - public int Count() { int count = 0; diff --git a/DynamicMemory/LD_24/Code/ProductList.cs b/DynamicMemory/LD_24/Code/ProductList.cs index 85ac4f6..23962eb 100644 --- a/DynamicMemory/LD_24/Code/ProductList.cs +++ b/DynamicMemory/LD_24/Code/ProductList.cs @@ -53,18 +53,6 @@ namespace LD_24.Code } } - public Product Get(int index) - { - int i = 0; - ProductNode current = head; - while (i < index && current != null) - { - current = head.Next; - i++; - } - return current.Data; - } - public int Count() { int count = 0; diff --git a/DynamicMemory/LD_24/Code/TaskUtils.cs b/DynamicMemory/LD_24/Code/TaskUtils.cs index e672b5e..c33d190 100644 --- a/DynamicMemory/LD_24/Code/TaskUtils.cs +++ b/DynamicMemory/LD_24/Code/TaskUtils.cs @@ -129,5 +129,29 @@ namespace LD_24.Code return filtered; } + 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; + } + } } diff --git a/DynamicMemory/LD_24/Forma1.aspx b/DynamicMemory/LD_24/Forma1.aspx index 3c5ae2b..6aefcdf 100644 --- a/DynamicMemory/LD_24/Forma1.aspx +++ b/DynamicMemory/LD_24/Forma1.aspx @@ -35,9 +35,9 @@ - -
-
+ + + diff --git a/DynamicMemory/LD_24/Forma1.aspx.cs b/DynamicMemory/LD_24/Forma1.aspx.cs index cc128b4..9a0b4ad 100644 --- a/DynamicMemory/LD_24/Forma1.aspx.cs +++ b/DynamicMemory/LD_24/Forma1.aspx.cs @@ -34,11 +34,13 @@ namespace LD_24 List mostPopularProductIds = TaskUtils.FindMostPopularProducts(orders); ProductList mostPopularProducts = TaskUtils.FindByID(products, mostPopularProductIds); ProductList filteredProducts = TaskUtils.FilterByQuantitySoldAndPrice(products, orders, n, k); + OrderList customersWithSingleProduct = TaskUtils.FindCustomerWithSingleProduct(orders); + customersWithSingleProduct.Sort(); ShowProducts(Table1, products); ShowOrders(Table2, orders); ShowMostPopularProducts(Table5, orders, mostPopularProducts); - ShowOrdersByProduct(FindControl("OrdersByProductContainer"), orders, products); + ShowOrdersWithPrices(Table3, customersWithSingleProduct, products); ShowProducts(Table4, filteredProducts); using (StreamWriter writer = new StreamWriter(Server.MapPath(outputFilename))) @@ -46,39 +48,9 @@ namespace LD_24 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.PrintOrdersWithPrices(writer, customersWithSingleProduct, products, "Vienos rūšies pirkėjai"); 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) - { - writer.WriteLine("Nėra"); - } - else - { - writer.WriteLine(PopularProduct.Name); - int sales = TaskUtils.CountProductSales(Customers, PopularProduct.ID); - writer.WriteLine($"Pardavimų kiekis: {sales} vnt."); - writer.WriteLine($"Pardavimų kaina: {sales * PopularProduct.Price:f2} eur."); - } - writer.WriteLine(); - - InOutUtils.PrintCustomersWithPrices(writer, CustomersByTargetProduct, TargetProduct, $"Pirkėjai pagal rūšį ({TargetProduct.Name})"); - - InOutUtils.PrintProducts(writer, filteredProducts, $"Atrinkti įtaisai (n={n}, k={k:f2})"); - }*/ } } } \ No newline at end of file diff --git a/DynamicMemory/LD_24/Forma1.aspx.designer.cs b/DynamicMemory/LD_24/Forma1.aspx.designer.cs index a479e24..691f56f 100644 --- a/DynamicMemory/LD_24/Forma1.aspx.designer.cs +++ b/DynamicMemory/LD_24/Forma1.aspx.designer.cs @@ -7,13 +7,11 @@ // //------------------------------------------------------------------------------ -namespace LD_24 -{ - - - public partial class Forma1 - { - +namespace LD_24 { + + + public partial class Forma1 { + /// /// form1 control. /// @@ -22,7 +20,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.HtmlControls.HtmlForm form1; - + /// /// ValidationSummary1 control. /// @@ -31,7 +29,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.ValidationSummary ValidationSummary1; - + /// /// Label5 control. /// @@ -40,7 +38,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Label Label5; - + /// /// TextBox1 control. /// @@ -49,7 +47,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.TextBox TextBox1; - + /// /// RegularExpressionValidator1 control. /// @@ -58,7 +56,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1; - + /// /// Label6 control. /// @@ -67,7 +65,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Label Label6; - + /// /// TextBox2 control. /// @@ -76,7 +74,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.TextBox TextBox2; - + /// /// RegularExpressionValidator2 control. /// @@ -85,7 +83,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2; - + /// /// Button1 control. /// @@ -94,7 +92,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Button Button1; - + /// /// ResultsDiv control. /// @@ -103,7 +101,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.HtmlControls.HtmlGenericControl ResultsDiv; - + /// /// Label1 control. /// @@ -112,7 +110,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Label Label1; - + /// /// Table1 control. /// @@ -121,7 +119,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Table Table1; - + /// /// Label2 control. /// @@ -130,7 +128,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Label Label2; - + /// /// Table2 control. /// @@ -139,7 +137,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Table Table2; - + /// /// Label8 control. /// @@ -148,7 +146,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Label Label8; - + /// /// Table5 control. /// @@ -157,7 +155,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Table Table5; - + /// /// Label4 control. /// @@ -166,16 +164,16 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Label Label4; - + /// - /// OrdersByProductContainer control. + /// Table3 control. /// /// /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// - protected global::System.Web.UI.HtmlControls.HtmlGenericControl OrdersByProductContainer; - + protected global::System.Web.UI.WebControls.Table Table3; + /// /// Label7 control. /// @@ -184,7 +182,7 @@ namespace LD_24 /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.Label Label7; - + /// /// Table4 control. /// diff --git a/DynamicMemory/LD_24/Forma1Utils.aspx.cs b/DynamicMemory/LD_24/Forma1Utils.aspx.cs index 1d17cc6..18b98df 100644 --- a/DynamicMemory/LD_24/Forma1Utils.aspx.cs +++ b/DynamicMemory/LD_24/Forma1Utils.aspx.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; @@ -10,10 +11,8 @@ namespace LD_24 { public partial class Forma1 : System.Web.UI.Page { - private IEnumerable> ShowProdcutsTable(Table table, ProductList products, params string[] columns) + private IEnumerable> ShowTable(Table table, IEnumerable list, params string[] columns) { - table.Rows.Clear(); - TableRow header = new TableRow(); foreach (string column in columns) { @@ -21,14 +20,16 @@ namespace LD_24 } table.Rows.Add(header); - if (products.Count() > 0) { - foreach (Product product in products) - { - TableRow row = new TableRow(); - yield return Tuple.Create(product, row); - table.Rows.Add(row); - } - } else + int n = 0; + foreach (object item in list) + { + TableRow row = new TableRow(); + yield return Tuple.Create(item, row); + table.Rows.Add(row); + n++; + } + + if (n == 0) { TableRow row = new TableRow(); row.Cells.Add(new TableCell { Text = "Nėra", ColumnSpan = columns.Length }); @@ -36,51 +37,20 @@ namespace LD_24 } } - private IEnumerable> ShowOrdersTable(Table table, string title, OrderList orders, params string[] columns) + private IEnumerable> ShowTable(Table table, string title, IEnumerable list, params string[] columns) { - table.Rows.Clear(); + TableRow row = new TableRow(); + row.Cells.Add(new TableCell { Text = title, ColumnSpan = columns.Length }); + table.Rows.Add(row); - 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> ShowOrdersTable(Table table, OrderList orders, params string[] columns) - { - return ShowOrdersTable(table, null, orders, columns); + return ShowTable(table, list, columns); } public void ShowProducts(Table table, ProductList products) { - foreach (var tuple in ShowProdcutsTable(table, products, "ID", "Vardas", "Kaina, eur.")) + foreach (var tuple in ShowTable(table, products, "ID", "Vardas", "Kaina, eur.")) { - Product product = tuple.Item1; + Product product = (Product)tuple.Item1; TableRow row = tuple.Item2; row.Cells.Add(new TableCell { Text = product.ID }); row.Cells.Add(new TableCell { Text = product.Name }); @@ -90,9 +60,9 @@ namespace LD_24 public void ShowOrders(Table table, OrderList orders) { - foreach (var tuple in ShowOrdersTable(table, orders, "Pavardė", "Vardas", "Įtaisas", "Įtaisų kiekis, vnt.")) + foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisas", "Įtaisų kiekis, vnt.")) { - Order order = tuple.Item1; + Order order = (Order)tuple.Item1; TableRow row = tuple.Item2; row.Cells.Add(new TableCell { Text = order.CustomerSurname }); row.Cells.Add(new TableCell { Text = order.CustomerName }); @@ -103,9 +73,9 @@ namespace LD_24 public void ShowMostPopularProducts(Table table, OrderList orders, ProductList popularProducts) { - foreach (var tuple in ShowProdcutsTable(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 = tuple.Item1; + Product product = (Product)tuple.Item1; TableRow row = tuple.Item2; int sales = TaskUtils.CountProductSales(orders, product.ID); row.Cells.Add(new TableCell { Text = product.ID }); @@ -115,26 +85,18 @@ namespace LD_24 } } - public void ShowOrdersByProduct(Control container, OrderList orders, ProductList products) + public void ShowOrdersWithPrices(Table table, OrderList orders, ProductList products) { - container.Controls.Clear(); - foreach (Product product in products) + foreach (var tuple in ShowTable(table, orders, "Pavardė", "Vardas", "Įtaisų kiekis, vnt.", "Sumokėta, eur.")) { - OrderList filtered = TaskUtils.FilterByProduct(orders, product.ID); - filtered = TaskUtils.MergeOrders(filtered); - filtered.Sort(); + Order order = (Order)tuple.Item1; + TableRow row = tuple.Item2; + Product product = TaskUtils.FindByID(products, order.ProductID); - 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) }); - } + 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) }); } } } diff --git a/DynamicMemory/LD_24/LD_24.csproj b/DynamicMemory/LD_24/LD_24.csproj index 755840c..5021c7f 100644 --- a/DynamicMemory/LD_24/LD_24.csproj +++ b/DynamicMemory/LD_24/LD_24.csproj @@ -73,6 +73,9 @@ + + +