From 47e2bc50b85f6383b811d1e1630c8c9d9ae1c6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Sat, 9 Mar 2024 14:46:08 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=201=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/Confectionery.sln | 10 +- .../ConfectioneryFileImplement/Component.cs | 60 +++++++++++ .../ConfectioneryFileImplement.csproj | 14 +++ .../DataFileSilgleton.cs | 61 +++++++++++ .../ConfectioneryFileImplement/Pastry.cs | 102 ++++++++++++++++++ .../ConfectioneryListImplement/Component.cs | 2 +- .../ConfectioneryView/FormCreateOrder.cs | 4 +- 7 files changed, 248 insertions(+), 5 deletions(-) create mode 100644 Confectionery/ConfectioneryFileImplement/Component.cs create mode 100644 Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj create mode 100644 Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs create mode 100644 Confectionery/ConfectioneryFileImplement/Pastry.cs diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index a7f5d85..6079332 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryContracts", "ConfectionaryContracts\ConfectioneryContracts.csproj", "{4BC782E2-5108-4C55-9F76-ACE5A56B8735}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{2822ACD8-8B94-4BF2-8C55-478677FDD1FE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{2822ACD8-8B94-4BF2-8C55-478677FDD1FE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{EE30482F-8998-4178-8412-421A02F6A6B4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{EE30482F-8998-4178-8412-421A02F6A6B4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{89C5A160-FB85-4D56-AFD7-506CC6005080}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {EE30482F-8998-4178-8412-421A02F6A6B4}.Debug|Any CPU.Build.0 = Debug|Any CPU {EE30482F-8998-4178-8412-421A02F6A6B4}.Release|Any CPU.ActiveCfg = Release|Any CPU {EE30482F-8998-4178-8412-421A02F6A6B4}.Release|Any CPU.Build.0 = Release|Any CPU + {89C5A160-FB85-4D56-AFD7-506CC6005080}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89C5A160-FB85-4D56-AFD7-506CC6005080}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89C5A160-FB85-4D56-AFD7-506CC6005080}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89C5A160-FB85-4D56-AFD7-506CC6005080}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/ConfectioneryFileImplement/Component.cs b/Confectionery/ConfectioneryFileImplement/Component.cs new file mode 100644 index 0000000..63e6867 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Component.cs @@ -0,0 +1,60 @@ +using System.ComponentModel; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj b/Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj new file mode 100644 index 0000000..54e27de --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs new file mode 100644 index 0000000..06fb502 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs @@ -0,0 +1,61 @@ +using ConfectioneryFileImplement.Models; +using System.Xml.Linq; + +namespace PastryShopFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + + private readonly string ComponentFileName = "Component.xml"; + + private readonly string OrderFileName = "Order.xml"; + + private readonly string PastryFileName = "Pastry.xml"; + + public List Components { get; private set; } + + public List Orders { get; private set; } + + public List Pastrys { get; private set; } + + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + + public void SavePastrys() => SaveData(Pastrys, PastryFileName, "Pastrys", x => x.GetXElement); + + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Pastrys = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/Confectionery/ConfectioneryFileImplement/Pastry.cs b/Confectionery/ConfectioneryFileImplement/Pastry.cs new file mode 100644 index 0000000..20602d5 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Pastry.cs @@ -0,0 +1,102 @@ +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; + + +namespace ConfectioneryFileImplement.Models +{ + public class Pastry : IPastryModel + { + public int Id { get; private set; } + public string PastryName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _PastryComponents = + null; + public Dictionary PastryComponents + { + get + { + if (_PastryComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _PastryComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _PastryComponents; + } + } + public static Pastry? Create(PastryBindingModel model) + { + if (model == null) + { + return null; + } + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Pastry? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pastry() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PastryName = element.Element("PastryName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = +element.Element("PastryComponents")!.Elements("PastryComponent") + .ToDictionary(x => +Convert.ToInt32(x.Element("Key")?.Value), x => +Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PastryBindingModel model) + { + if (model == null) + { + return; + } + PastryName = model.PastryName; + Price = model.Price; + Components = model.PastryComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _PastryComponents = null; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + public XElement GetXElement => new("Pastry", + new XAttribute("Id", Id), + new XElement("PastryName", PastryName), + new XElement("Price", Price.ToString()), + new XElement("PastryComponents", Components.Select(x => + new XElement("PastryComponent", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + + } +} diff --git a/Confectionery/ConfectioneryListImplement/Component.cs b/Confectionery/ConfectioneryListImplement/Component.cs index 9683495..0602b42 100644 --- a/Confectionery/ConfectioneryListImplement/Component.cs +++ b/Confectionery/ConfectioneryListImplement/Component.cs @@ -44,4 +44,4 @@ namespace ConfectioneryListImplement.Models }; } -} +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryView/FormCreateOrder.cs b/Confectionery/ConfectioneryView/FormCreateOrder.cs index 16fe46e..df0fee5 100644 --- a/Confectionery/ConfectioneryView/FormCreateOrder.cs +++ b/Confectionery/ConfectioneryView/FormCreateOrder.cs @@ -55,12 +55,12 @@ namespace ConfectioneryView try { int id = Convert.ToInt32(comboBoxPastry.SelectedValue); - var product = _logicP.ReadElement(new PastrySearchModel + var Pastry = _logicP.ReadElement(new PastrySearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), + textBoxSum.Text = Math.Round(count * (Pastry?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } From 0fda2353bc0074755b4c276550454d4ef4267939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Sat, 9 Mar 2024 14:59:05 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D1=80=D0=B0=D0=B7=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1?= =?UTF-8?q?=D0=B0=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/Confectionery.sln | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index 6079332..5b7648e 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{EE30482F-8998-4178-8412-421A02F6A6B4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{89C5A160-FB85-4D56-AFD7-506CC6005080}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,10 +39,6 @@ Global {EE30482F-8998-4178-8412-421A02F6A6B4}.Debug|Any CPU.Build.0 = Debug|Any CPU {EE30482F-8998-4178-8412-421A02F6A6B4}.Release|Any CPU.ActiveCfg = Release|Any CPU {EE30482F-8998-4178-8412-421A02F6A6B4}.Release|Any CPU.Build.0 = Release|Any CPU - {89C5A160-FB85-4D56-AFD7-506CC6005080}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {89C5A160-FB85-4D56-AFD7-506CC6005080}.Debug|Any CPU.Build.0 = Debug|Any CPU - {89C5A160-FB85-4D56-AFD7-506CC6005080}.Release|Any CPU.ActiveCfg = Release|Any CPU - {89C5A160-FB85-4D56-AFD7-506CC6005080}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE