From ed13752309afaf58fec0359fb75a1412f77d8033 Mon Sep 17 00:00:00 2001 From: Ino Date: Mon, 13 Feb 2023 11:53:17 +0400 Subject: [PATCH 1/7] =?UTF-8?q?2=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractIceCreamShopDataModels.sln | 25 ---- .../AbstractIceCreamShopDataModels.csproj | 9 -- .../IComponentModel.cs | 14 -- .../AbstractIceCreamShopDataModels/IId.cs | 13 -- .../IOrderModel.cs | 19 --- .../IProductModel.cs | 15 --- .../OrderStatus.cs | 11 -- .../BusinessLogics/ComponentLogic.cs | 4 +- IceCreamShop/IceCreamShop.sln | 8 +- .../IceCreamShop/IceCreamShopView.csproj | 1 + IceCreamShop/IceCreamShop/Program.cs | 3 +- .../BusinessLogics/ComponentLogic.cs | 122 ------------------ .../DataFileSingleton.cs | 54 ++++++++ .../IceCreamShopFileImplement.csproj} | 2 +- .../Implements/ComponentStorage.cs | 78 +++++++++++ .../Implements/IceCreamStorage.cs | 84 ++++++++++++ .../Implements/OrderStorage.cs | 86 ++++++++++++ .../Models/Component.cs | 64 +++++++++ .../Models/IceCream.cs | 88 +++++++++++++ .../IceCreamShopFileImplement/Models/Order.cs | 104 +++++++++++++++ 20 files changed, 570 insertions(+), 234 deletions(-) delete mode 100644 IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.sln delete mode 100644 IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.csproj delete mode 100644 IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IComponentModel.cs delete mode 100644 IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IId.cs delete mode 100644 IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IOrderModel.cs delete mode 100644 IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IProductModel.cs delete mode 100644 IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/OrderStatus.cs delete mode 100644 IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ComponentLogic.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs rename IceCreamShop/{IceCreamShopBusinessLogic/1.csproj => IceCreamShopFileImplement/IceCreamShopFileImplement.csproj} (78%) create mode 100644 IceCreamShop/IceCreamShopFileImplement/Implements/ComponentStorage.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Models/Component.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Models/Order.cs diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.sln b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.sln deleted file mode 100644 index 7681ee8..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.3.32922.545 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractIceCreamShopDataModels", "AbstractIceCreamShopDataModels\AbstractIceCreamShopDataModels.csproj", "{28830087-9D9F-4E66-84CC-94C5CF376BF1}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {28830087-9D9F-4E66-84CC-94C5CF376BF1}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {A7BDC253-D956-404D-9013-AD0FE1F32DD1} - EndGlobalSection -EndGlobal diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.csproj b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.csproj deleted file mode 100644 index 132c02c..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - net6.0 - enable - enable - - - diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IComponentModel.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IComponentModel.cs deleted file mode 100644 index 12729fc..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IComponentModel.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AbstractIceCreamShopDataModels.Models -{ - public interface IComponentModel : IId - { - string ComponentName { get; } - double Cost { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IId.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IId.cs deleted file mode 100644 index eb6aca9..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IId.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AbstractIceCreamShopDataModels -{ - public interface IId - { - int Id { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IOrderModel.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IOrderModel.cs deleted file mode 100644 index f41ac85..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IOrderModel.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - - -namespace AbstractIceCreamShopDataModels.Models -{ - public interface IOrderModel : IId - { - int ProductId { get; } - int Count { get; } - double Sum { get; } - OrderStatus Status { get; } - DateTime DateCreate { get; } - DateTime? DateImplement { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IProductModel.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IProductModel.cs deleted file mode 100644 index 19cde0e..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/IProductModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AbstractIceCreamShopDataModels.Models -{ - public interface IProductModel : IId - { - string ProductName { get; } - double Price { get; } - Dictionary ProductComponents { get; } - } -} diff --git a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/OrderStatus.cs b/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/OrderStatus.cs deleted file mode 100644 index fb30825..0000000 --- a/IceCreamShop/AbstractIceCreamShopDataModels/AbstractIceCreamShopDataModels/OrderStatus.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace AbstractIceCreamShopDataModels -{ - public enum OrderStatus - { - Неизвестен = -1, - Принят = 0, - Выполняется = 1, - Готов = 2, - Выдан = 3 - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs index abfb55d..b71e2d2 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -11,7 +11,7 @@ using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; -namespace IceCreamShopBusinessLogic.BusinessLogics +namespace IceCreamBusinessLogic.BusinessLogics { public class ComponentLogic : IComponentLogic { @@ -101,7 +101,7 @@ namespace IceCreamShopBusinessLogic.BusinessLogics throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); } _logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id); - var element = _componentStorage.GetElement(new ComponentSearchModel { ComponentName= model.ComponentName }); + var element = _componentStorage.GetElement(new ComponentSearchModel { ComponentName = model.ComponentName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Компонент с таким названием уже есть"); diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index b6c01d0..d53647f 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopContracts", "Ic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamBusinessLogic", "IceCreamBusinessLogic\IceCreamBusinessLogic.csproj", "{E4C64BCD-22DF-43B5-8324-AE64F9237DFF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{382E618C-D833-4E98-9786-7A831953ABD0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Debug|Any CPU.Build.0 = Debug|Any CPU {EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Release|Any CPU.ActiveCfg = Release|Any CPU {EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Release|Any CPU.Build.0 = Release|Any CPU + {382E618C-D833-4E98-9786-7A831953ABD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {382E618C-D833-4E98-9786-7A831953ABD0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {382E618C-D833-4E98-9786-7A831953ABD0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {382E618C-D833-4E98-9786-7A831953ABD0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShop/IceCreamShopView.csproj b/IceCreamShop/IceCreamShop/IceCreamShopView.csproj index 6a7e820..21ad05e 100644 --- a/IceCreamShop/IceCreamShop/IceCreamShopView.csproj +++ b/IceCreamShop/IceCreamShop/IceCreamShopView.csproj @@ -16,6 +16,7 @@ + diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index 24dcf8a..9e9dcb1 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -1,12 +1,11 @@ using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.StoragesContracts; -using IceCreamShopListImplement.Implements; using IceCreamBusinessLogic.BusinessLogics; using IceCreamShopView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using IceCreamShopBusinessLogic.BusinessLogics; +using IceCreamShopFileImplement.Implements; namespace IceCreamShop { diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ComponentLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ComponentLogic.cs deleted file mode 100644 index d6b3897..0000000 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ComponentLogic.cs +++ /dev/null @@ -1,122 +0,0 @@ -using AbstractShopContracts.BindingModels; -using AbstractShopContracts.BusinessLogicsContracts; -using AbstractShopContracts.SearchModels; -using AbstractShopContracts.StoragesContracts; -using AbstractShopContracts.ViewModels; -using Microsoft.Extensions.Logging; - -using IceCreamShopContracts.BindingModels; - -namespace IceCreamShopBusinessLogic.BusinessLogics -{ - public class ComponentLogic : IComponentLogic - { - private readonly ILogger _logger; - private readonly IComponentStorage _componentStorage; - public ComponentLogic(ILogger logger, IComponentStorage - componentStorage) - { - _logger = logger; - _componentStorage = componentStorage; - } - public List? ReadList(ComponentSearchModel? model) - { - _logger.LogInformation("ReadList. ComponentName:{ComponentName}. - Id:{ Id} - ", model?.ComponentName, model?.Id); - var list = model == null ? _componentStorage.GetFullList() : -_componentStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } - _logger.LogInformation("ReadList. Count:{Count}", list.Count); - return list; - } - public ComponentViewModel? ReadElement(ComponentSearchModel model) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - _logger.LogInformation("ReadElement. ComponentName:{ComponentName}. - Id:{ Id} - ", model.ComponentName, model.Id); - var element = _componentStorage.GetElement(model); - if (element == null) - { - _logger.LogWarning("ReadElement element not found"); - return null; - } - _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); - return element; - } - public bool Create(ComponentBindingModel model) - { - CheckModel(model); - if (_componentStorage.Insert(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - return true; - } - public bool Update(ComponentBindingModel model) - { - CheckModel(model); - if (_componentStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } - public bool Delete(ComponentBindingModel model) - { - CheckModel(model, false); - _logger.LogInformation("Delete. Id:{Id}", model.Id); - if (_componentStorage.Delete(model) == null) - { - _logger.LogWarning("Delete operation failed"); - return false; - } - return true; - } - private void CheckModel(ComponentBindingModel model, bool withParams = - true) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - if (!withParams) - { - return; - } - if (string.IsNullOrEmpty(model.ComponentName)) - { - throw new ArgumentNullException("Нет названия компонента", - nameof(model.ComponentName)); - } - if (model.Cost <= 0) - { - throw new ArgumentNullException("Цена компонента должна быть - больше 0", nameof(model.Cost)); - } - _logger.LogInformation("Component. ComponentName:{ComponentName}. - Cost:{ Cost}. Id: { Id} - ", model.ComponentName, model.Cost, model.Id); - var element = _componentStorage.GetElement(new ComponentSearchModel - { - ComponentName = model.ComponentName - }); - if (element != null && element.Id != model.Id) - { - throw new InvalidOperationException("Компонент с таким названием - уже есть"); - } - } - } -} - diff --git a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..8be6b0f --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs @@ -0,0 +1,54 @@ +using IceCreamShopFileImplement.Models; +using System.Xml.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string IceCreamFileName = "IceCream.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List IceCreams { 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 SaveIceCreams() => SaveData(IceCreams, IceCreamFileName, "IceCreams", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + IceCreams = LoadData(IceCreamFileName, "IceCream", x => IceCream.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/IceCreamShop/IceCreamShopBusinessLogic/1.csproj b/IceCreamShop/IceCreamShopFileImplement/IceCreamShopFileImplement.csproj similarity index 78% rename from IceCreamShop/IceCreamShopBusinessLogic/1.csproj rename to IceCreamShop/IceCreamShopFileImplement/IceCreamShopFileImplement.csproj index 2243cc7..0689277 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/1.csproj +++ b/IceCreamShop/IceCreamShopFileImplement/IceCreamShopFileImplement.csproj @@ -1,7 +1,6 @@ - Exe net6.0 enable enable @@ -9,6 +8,7 @@ + diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..cd34d47 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,78 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs new file mode 100644 index 0000000..c09e963 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs @@ -0,0 +1,84 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics.SymbolStore; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.Implements +{ + public class IceCreamStorage : IIceCreamStorage + { + private readonly DataFileSingleton source; + + public IceCreamStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public IceCreamViewModel? GetElement(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) + { + return null; + } + return source.IceCreams.FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && x.IceCreamName == model.IceCreamName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName)) + { + return new(); + } + return source.IceCreams.Where(x => x.IceCreamName.Contains(model.IceCreamName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.IceCreams.Select(x => x.GetViewModel).ToList(); + } + + public IceCreamViewModel? Insert(IceCreamBindingModel model) + { + model.Id = source.IceCreams.Count > 0 ? source.IceCreams.Max(x => x.Id) + 1 : 1; + var newDoc = IceCream.Create(model); + if (newDoc == null) + { + return null; + } + source.IceCreams.Add(newDoc); + source.SaveIceCreams(); + return newDoc.GetViewModel; + } + + public IceCreamViewModel? Update(IceCreamBindingModel model) + { + var document = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveIceCreams(); + return document.GetViewModel; + } + public IceCreamViewModel? Delete(IceCreamBindingModel model) + { + var document = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveIceCreams(); + return document.GetViewModel; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..e1cf694 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,86 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Orders + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Component.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Component.cs new file mode 100644 index 0000000..01bd975 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Component.cs @@ -0,0 +1,64 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace IceCreamShopFileImplement.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())); + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs new file mode 100644 index 0000000..e72613b --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs @@ -0,0 +1,88 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace IceCreamShopFileImplement.Models +{ + public class IceCream + { + public int Id { get; private set; } + public string IceCreamName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _iceCreamComponents = null; + public Dictionary IceCreamComponents + { + get + { + if (_iceCreamComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _iceCreamComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _iceCreamComponents; + } + } + public static IceCream? Create(IceCreamBindingModel model) + { + if (model == null) + { + return null; + } + return new IceCream() + { + Id = model.Id, + IceCreamName = model.IceCreamName, + Price = model.Price, + Components = model.IceCreamComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static IceCream? Create(XElement element) + { + if (element == null) + { + return null; + } + return new IceCream() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + IceCreamName = element.Element("IceCreamName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("IceCreamComponents")!.Elements("IceCreamComponent") + .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(IceCreamBindingModel model) + { + if (model == null) + { + return; + } + IceCreamName = model.IceCreamName; + Price = model.Price; + Components = model.IceCreamComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _iceCreamComponents = null; + } + public IceCreamViewModel GetViewModel => new() + { + Id = Id, + IceCreamName = IceCreamName, + Price = Price, + IceCreamComponents = IceCreamComponents + }; + public XElement GetXElement => new("IceCream", + new XAttribute("Id", Id), + new XElement("IceCreamName", IceCreamName), + new XElement("Price", Price.ToString()), + new XElement("IceCreamComponents", Components.Select(x => + new XElement("IceCreamComponents", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs new file mode 100644 index 0000000..c70e982 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs @@ -0,0 +1,104 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AbstractIceCreamShopDataModels.Enums; +using System.Xml.Linq; +using System.Reflection.Metadata; + +namespace IceCreamShopFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int IceCreamId { get; private set; } + public string IceCreamName { get; private set; } = string.Empty; + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + Id = model.Id, + IceCreamId = model.IceCreamId, + IceCreamName = model.IceCreamName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + IceCreamId = Convert.ToInt32(element.Element("IceCreamId")!.Value), + IceCreamName = element.Element("IceCreamName")!.Value, + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Id = model.Id; + IceCreamId = model.IceCreamId; + IceCreamName = model.IceCreamName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + IceCreamId = IceCreamId, + IceCreamName = IceCreamName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("IceCreamId", IceCreamId.ToString()), + new XElement("IceCreamName", IceCreamName), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +} -- 2.25.1 From 1bebeeae57cc06e48683345520dbe78472737a24 Mon Sep 17 00:00:00 2001 From: Ino Date: Mon, 13 Feb 2023 15:01:03 +0400 Subject: [PATCH 2/7] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 24 ++++++++++++++++++- IceCreamShop/IceCreamShop/FormMain.cs | 18 -------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index 974925f..be1f742 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -95,8 +95,30 @@ namespace IceCreamBusinessLogic.BusinessLogics _logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. DocumentId: { DocumentId}", model.Id, model.Sum, model.IceCreamId); } - public bool SetNewStatus(OrderBindingModel model, OrderStatus newStatus) + public bool SetNewStatus(OrderBindingModel orderModel, OrderStatus newStatus) { + var viewModel = _orderStorage.GetElement(new OrderSearchModel + { + Id = orderModel.Id + }); + + if (viewModel == null) + { + _logger.LogWarning("Order model not found"); + return false; + } + + OrderBindingModel model = new OrderBindingModel + { + Id = viewModel.Id, + IceCreamId = viewModel.IceCreamId, + IceCreamName = viewModel.IceCreamName, + Status = viewModel.Status, + DateCreate = viewModel.DateCreate, + DateImplement = viewModel.DateImplement, + Count = viewModel.Count, + Sum = viewModel.Sum + }; CheckModel(model); if (model.Status + 1 != newStatus) { diff --git a/IceCreamShop/IceCreamShop/FormMain.cs b/IceCreamShop/IceCreamShop/FormMain.cs index ca6f1bd..8c0bba5 100644 --- a/IceCreamShop/IceCreamShop/FormMain.cs +++ b/IceCreamShop/IceCreamShop/FormMain.cs @@ -90,12 +90,6 @@ namespace IceCreamShopView var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = id, - IceCreamId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["IceCreamId"].Value), - IceCreamName = dataGridView.SelectedRows[0].Cells["IceCreamName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), }); if (!operationResult) { @@ -123,12 +117,6 @@ namespace IceCreamShopView var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id, - IceCreamId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["IceCreamId"].Value), - IceCreamName = dataGridView.SelectedRows[0].Cells["IceCreamName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), }); if (!operationResult) { @@ -155,12 +143,6 @@ namespace IceCreamShopView var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id, - IceCreamId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["IceCreamId"].Value), - IceCreamName = dataGridView.SelectedRows[0].Cells["IceCreamName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), }); if (!operationResult) { -- 2.25.1 From cff88300c0ce7567395b2cdff1255128e1684c68 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sun, 12 Mar 2023 21:19:02 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20?= =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D1=83=D1=81=D0=BB=D0=BE?= =?UTF-8?q?=D0=B6=D0=BD=D0=B5=D0=BD=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 54 +++-- .../BusinessLogics/ShopLogic.cs | 92 ++++++-- .../IceCreamShop/FormMain.Designer.cs | 20 +- IceCreamShop/IceCreamShop/FormMain.cs | 10 + .../IceCreamShop/FormSellIceCream.Designer.cs | 119 +++++++++++ IceCreamShop/IceCreamShop/FormSellIceCream.cs | 90 ++++++++ .../IceCreamShop/FormSellIceCream.resx | 60 ++++++ .../IceCreamShop/FormShop.Designer.cs | 102 +++++---- IceCreamShop/IceCreamShop/FormShop.cs | 5 +- IceCreamShop/IceCreamShop/FormShop.resx | 3 + .../IceCreamShop/FormShopSupply.Designer.cs | 44 ++-- IceCreamShop/IceCreamShop/Program.cs | 1 + .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 2 + .../StoragesContracts/IShopStorage.cs | 4 +- .../ViewModels/ShopViewModel.cs | 2 + .../Models/IShopModel.cs | 1 + .../DataFileSingleton.cs | 9 +- .../Implements/ShopStorage.cs | 197 ++++++++++++++++++ .../Models/IceCream.cs | 2 +- .../IceCreamShopFileImplement/Models/Shop.cs | 115 ++++++++++ .../Implements/ShopStorage.cs | 8 +- .../IceCreamShopListImplement/Models/Shop.cs | 2 + 23 files changed, 843 insertions(+), 100 deletions(-) create mode 100644 IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs create mode 100644 IceCreamShop/IceCreamShop/FormSellIceCream.cs create mode 100644 IceCreamShop/IceCreamShop/FormSellIceCream.resx create mode 100644 IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index be1f742..842b8fd 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,11 +17,15 @@ namespace IceCreamBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopLogic _shopLogic; + private readonly IIceCreamStorage _iceCreamStorage; - public OrderLogic(IOrderStorage orderStorage, ILogger logger) + public OrderLogic(IOrderStorage orderStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) { _orderStorage = orderStorage; _logger = logger; + _shopLogic = shopLogic; + _iceCreamStorage = iceCreamStorage; } public bool CreateOrder(OrderBindingModel model) @@ -104,32 +108,42 @@ namespace IceCreamBusinessLogic.BusinessLogics if (viewModel == null) { - _logger.LogWarning("Order model not found"); - return false; + throw new ArgumentNullException(nameof(orderModel)); } - OrderBindingModel model = new OrderBindingModel - { - Id = viewModel.Id, - IceCreamId = viewModel.IceCreamId, - IceCreamName = viewModel.IceCreamName, - Status = viewModel.Status, - DateCreate = viewModel.DateCreate, - DateImplement = viewModel.DateImplement, - Count = viewModel.Count, - Sum = viewModel.Sum - }; - CheckModel(model); - if (model.Status + 1 != newStatus) + if (viewModel.Status + 1 != newStatus) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } - model.Status = newStatus; - if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) + orderModel.Status = newStatus; + + if (orderModel.Status == OrderStatus.Готов) { - model.Status--; + orderModel.DateImplement = DateTime.Now; + + var iceCream = _iceCreamStorage.GetElement(new() { Id = viewModel.IceCreamId }); + + if (iceCream == null) + { + throw new ArgumentNullException(nameof(iceCream)); + } + + if (!_shopLogic.SupplyIceCreams(iceCream, viewModel.Count)) + { + throw new Exception($"SupplyIceCreams operation failed. Store is full."); + } + } + else + { + orderModel.DateImplement = viewModel.DateImplement; + } + + CheckModel(orderModel); + + if (_orderStorage.Update(orderModel) == null) + { + orderModel.Status--; _logger.LogWarning("Update operation failed"); return false; } diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs index ec00b3b..0700132 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Reflection.Metadata; namespace IceCreamBusinessLogic.BusinessLogics { @@ -72,7 +73,7 @@ namespace IceCreamBusinessLogic.BusinessLogics { throw new ArgumentNullException("Count of icecreams in supply must be more than 0", nameof(count)); } - + _logger.LogInformation("SupplyIceCreams. ShopName:{0}.Id:{ 1}", model.Name, model.Id); var shopElement = _shopStorage.GetElement(model); if (shopElement == null) { @@ -81,29 +82,94 @@ namespace IceCreamBusinessLogic.BusinessLogics } _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name); - if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameDocument)) + var countIceCreams = 0; + foreach (var icecream in shopElement.ShopIceCreams) { - shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameDocument.Item2 + count); - _logger.LogInformation("Same icecream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + countIceCreams += icecream.Value.Item2; } + if (shopElement.IceCreamMaxCount - countIceCreams >= count) + { + if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIceCream)) + { + shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameIceCream.Item2 + count); + _logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + } + else + { + shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count); + _logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + } + _shopStorage.Update(new() + { + Id = shopElement.Id, + Name = shopElement.Name, + Adress = shopElement.Adress, + OpeningDate = shopElement.OpeningDate, + ShopIceCreams = shopElement.ShopIceCreams, + IceCreamMaxCount = shopElement.IceCreamMaxCount + }); + } + else { - shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count); - _logger.LogInformation("New icecream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + _logger.LogWarning("Required shop is overflowed"); + return false; + } + return true; + } + + public bool SupplyIceCreams(IIceCreamModel iceCream, int count) + { + if (iceCream == null) + { + throw new ArgumentNullException(nameof(iceCream)); } - _shopStorage.Update(new() + if (count <= 0) { - Id = shopElement.Id, - Name = shopElement.Name, - Adress = shopElement.Adress, - OpeningDate = shopElement.OpeningDate, - ShopIceCreams = shopElement.ShopIceCreams - }); + throw new ArgumentException("Count of icecreams in supply must be more than 0", nameof(count)); + } + var freePlaces = _shopStorage.GetFullList() + .Select(x => x.IceCreamMaxCount - x.ShopIceCreams + .Select(p => p.Value.Item2).Sum()).Sum() - count; + + if (freePlaces < 0) + { + _logger.LogInformation("SupplyIceCreams. Failed to add icecreams to store. It's full."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + var temp = Math.Min(count, shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum()); + + if (temp <= 0) + { + continue; + } + + if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, temp)) + { + _logger.LogWarning("An error occurred while adding iceCream to stores"); + return false; + } + + count -= temp; + + if (count == 0) + { + return true; + } + } return true; } + public bool SellIceCreams(IIceCreamModel package, int quantity) + { + return _shopStorage.SellIceCreams(package, quantity); + } + public bool Create(ShopBindingModel model) { CheckModel(model); diff --git a/IceCreamShop/IceCreamShop/FormMain.Designer.cs b/IceCreamShop/IceCreamShop/FormMain.Designer.cs index 68195d4..25a564c 100644 --- a/IceCreamShop/IceCreamShop/FormMain.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.мороженоеToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.магазиныToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.buttonSupplyShop = new System.Windows.Forms.Button(); + this.SellIceCreamButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip.SuspendLayout(); this.SuspendLayout(); @@ -135,21 +136,21 @@ // компонентыToolStripMenuItem // this.компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.компонентыToolStripMenuItem.Text = "Компоненты"; this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.компонентыToolStripMenuItem_Click); // // мороженоеToolStripMenuItem // this.мороженоеToolStripMenuItem.Name = "мороженоеToolStripMenuItem"; - this.мороженоеToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.мороженоеToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.мороженоеToolStripMenuItem.Text = "Мороженое"; this.мороженоеToolStripMenuItem.Click += new System.EventHandler(this.мороженоеToolStripMenuItem_Click); // // магазиныToolStripMenuItem // this.магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.магазиныToolStripMenuItem.Text = "Магазины"; this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.магазиныToolStripMenuItem_Click); // @@ -164,11 +165,23 @@ this.buttonSupplyShop.UseVisualStyleBackColor = true; this.buttonSupplyShop.Click += new System.EventHandler(this.buttonSupplyShop_Click); // + // SellIceCreamButton + // + this.SellIceCreamButton.Location = new System.Drawing.Point(878, 231); + this.SellIceCreamButton.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.SellIceCreamButton.Name = "SellIceCreamButton"; + this.SellIceCreamButton.Size = new System.Drawing.Size(170, 37); + this.SellIceCreamButton.TabIndex = 15; + this.SellIceCreamButton.Text = "Продажа мороженого"; + this.SellIceCreamButton.UseVisualStyleBackColor = true; + this.SellIceCreamButton.Click += new System.EventHandler(this.SellIceCreamButton_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1072, 347); + this.Controls.Add(this.SellIceCreamButton); this.Controls.Add(this.buttonSupplyShop); this.Controls.Add(this.buttonUpdate); this.Controls.Add(this.buttonSetToFinish); @@ -202,5 +215,6 @@ private ToolStripMenuItem мороженоеToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; private Button buttonSupplyShop; + private Button SellIceCreamButton; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormMain.cs b/IceCreamShop/IceCreamShop/FormMain.cs index 8dd2ef8..6e91c1d 100644 --- a/IceCreamShop/IceCreamShop/FormMain.cs +++ b/IceCreamShop/IceCreamShop/FormMain.cs @@ -181,5 +181,15 @@ namespace IceCreamShopView form.ShowDialog(); } } + + private void SellIceCreamButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellIceCream)); + + if (service is FormSellIceCream form) + { + form.ShowDialog(); + } + } } } diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs new file mode 100644 index 0000000..5200c73 --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs @@ -0,0 +1,119 @@ +namespace IceCreamShopView +{ + partial class FormSellIceCream + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.ButtonCancel = new System.Windows.Forms.Button(); + this.SaveButton = new System.Windows.Forms.Button(); + this.QuantityTextBox = new System.Windows.Forms.TextBox(); + this.IceCreamСomboBox = new System.Windows.Forms.ComboBox(); + this.QuantityLabel = new System.Windows.Forms.Label(); + this.IceCreamLabel = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // ButtonCancel + // + this.ButtonCancel.Location = new System.Drawing.Point(169, 97); + this.ButtonCancel.Name = "ButtonCancel"; + this.ButtonCancel.Size = new System.Drawing.Size(97, 29); + this.ButtonCancel.TabIndex = 11; + this.ButtonCancel.Text = "Отмена"; + this.ButtonCancel.UseVisualStyleBackColor = true; + this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // SaveButton + // + this.SaveButton.Location = new System.Drawing.Point(66, 97); + this.SaveButton.Name = "SaveButton"; + this.SaveButton.Size = new System.Drawing.Size(97, 29); + this.SaveButton.TabIndex = 10; + this.SaveButton.Text = "Сохранить"; + this.SaveButton.UseVisualStyleBackColor = true; + this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click); + // + // QuantityTextBox + // + this.QuantityTextBox.Location = new System.Drawing.Point(82, 45); + this.QuantityTextBox.Name = "QuantityTextBox"; + this.QuantityTextBox.Size = new System.Drawing.Size(184, 23); + this.QuantityTextBox.TabIndex = 9; + // + // IceCreamСomboBox + // + this.IceCreamСomboBox.FormattingEnabled = true; + this.IceCreamСomboBox.Location = new System.Drawing.Point(82, 12); + this.IceCreamСomboBox.Name = "IceCreamСomboBox"; + this.IceCreamСomboBox.Size = new System.Drawing.Size(184, 23); + this.IceCreamСomboBox.TabIndex = 8; + // + // QuantityLabel + // + this.QuantityLabel.AutoSize = true; + this.QuantityLabel.Location = new System.Drawing.Point(6, 48); + this.QuantityLabel.Name = "QuantityLabel"; + this.QuantityLabel.Size = new System.Drawing.Size(75, 15); + this.QuantityLabel.TabIndex = 7; + this.QuantityLabel.Text = "Количество:"; + // + // IceCreamLabel + // + this.IceCreamLabel.AutoSize = true; + this.IceCreamLabel.Location = new System.Drawing.Point(6, 15); + this.IceCreamLabel.Name = "IceCreamLabel"; + this.IceCreamLabel.Size = new System.Drawing.Size(74, 15); + this.IceCreamLabel.TabIndex = 6; + this.IceCreamLabel.Text = "Мороженое"; + // + // FormSellIceCream + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(278, 135); + this.Controls.Add(this.ButtonCancel); + this.Controls.Add(this.SaveButton); + this.Controls.Add(this.QuantityTextBox); + this.Controls.Add(this.IceCreamСomboBox); + this.Controls.Add(this.QuantityLabel); + this.Controls.Add(this.IceCreamLabel); + this.Name = "FormSellIceCream"; + this.Text = "Продать мороженое"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button ButtonCancel; + private Button SaveButton; + private TextBox QuantityTextBox; + private ComboBox IceCreamСomboBox; + private Label QuantityLabel; + private Label IceCreamLabel; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.cs new file mode 100644 index 0000000..41842a5 --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.cs @@ -0,0 +1,90 @@ +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + + +namespace IceCreamShopView +{ + public partial class FormSellIceCream : Form + { + private readonly ILogger _logger; + private readonly IIceCreamLogic _logicPackage; + private readonly IShopLogic _logicStore; + public FormSellIceCream(ILogger logger, IIceCreamLogic logicIceCream, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicPackage = logicIceCream; + _logicStore = logicShop; + LoadData(); + } + + private void LoadData() + { + _logger.LogInformation("Loading icecream for sale."); + + try + { + var list = _logicPackage.ReadList(null); + if (list != null) + { + IceCreamСomboBox.DisplayMember = "IceCreamName"; + IceCreamСomboBox.ValueMember = "Id"; + IceCreamСomboBox.DataSource = list; + IceCreamСomboBox.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "List loading error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(QuantityTextBox.Text)) + { + MessageBox.Show("Укажите количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (IceCreamСomboBox.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Product sale."); + + try + { + var operationResult = _logicStore.SellIceCreams(_logicPackage.ReadElement(new IceCreamSearchModel() + { + Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue) + })!, Convert.ToInt32(QuantityTextBox.Text)); + + if (!operationResult) + { + throw new Exception("Ошибка при продаже изделия. Дополнительная информация в логах."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Product sale error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.resx b/IceCreamShop/IceCreamShop/FormSellIceCream.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormShop.Designer.cs b/IceCreamShop/IceCreamShop/FormShop.Designer.cs index 356849d..405b100 100644 --- a/IceCreamShop/IceCreamShop/FormShop.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormShop.Designer.cs @@ -31,21 +31,25 @@ this.buttonCancel = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button(); this.dataGridView = new System.Windows.Forms.DataGridView(); - this.ColumnId = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.ColumnDocumentName = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dateTimePicker = new System.Windows.Forms.DateTimePicker(); this.labelOpeningDate = new System.Windows.Forms.Label(); this.textBoxAddress = new System.Windows.Forms.TextBox(); this.textBoxName = new System.Windows.Forms.TextBox(); this.labelAddress = new System.Windows.Forms.Label(); this.labelName = new System.Windows.Forms.Label(); + this.IceCreamMaxCountLable = new System.Windows.Forms.Label(); + this.VolumeNumericUpDown = new System.Windows.Forms.NumericUpDown(); + this.ColumnId = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnDocumentName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.PriceIceCream = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.VolumeNumericUpDown)).BeginInit(); this.SuspendLayout(); // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(197, 338); + this.buttonCancel.Location = new System.Drawing.Point(332, 389); this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(136, 22); @@ -56,7 +60,7 @@ // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(39, 338); + this.buttonSave.Location = new System.Drawing.Point(39, 387); this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSave.Name = "buttonSave"; this.buttonSave.Size = new System.Drawing.Size(136, 22); @@ -71,43 +75,22 @@ this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.ColumnId, this.ColumnDocumentName, + this.PriceIceCream, this.ColumnCount}); - this.dataGridView.Location = new System.Drawing.Point(9, 106); + this.dataGridView.Location = new System.Drawing.Point(9, 155); this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 51; this.dataGridView.RowTemplate.Height = 29; - this.dataGridView.Size = new System.Drawing.Size(351, 225); + this.dataGridView.Size = new System.Drawing.Size(504, 225); this.dataGridView.TabIndex = 19; // - // ColumnId - // - this.ColumnId.HeaderText = "ID"; - this.ColumnId.MinimumWidth = 6; - this.ColumnId.Name = "ColumnId"; - this.ColumnId.Visible = false; - this.ColumnId.Width = 125; - // - // ColumnDocumentName - // - this.ColumnDocumentName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; - this.ColumnDocumentName.HeaderText = "Документ"; - this.ColumnDocumentName.MinimumWidth = 6; - this.ColumnDocumentName.Name = "ColumnDocumentName"; - // - // ColumnCount - // - this.ColumnCount.HeaderText = "Количество"; - this.ColumnCount.MinimumWidth = 6; - this.ColumnCount.Name = "ColumnCount"; - this.ColumnCount.Width = 125; - // // dateTimePicker // - this.dateTimePicker.Location = new System.Drawing.Point(141, 63); + this.dateTimePicker.Location = new System.Drawing.Point(155, 63); this.dateTimePicker.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.dateTimePicker.Name = "dateTimePicker"; - this.dateTimePicker.Size = new System.Drawing.Size(219, 23); + this.dateTimePicker.Size = new System.Drawing.Size(205, 23); this.dateTimePicker.TabIndex = 18; // // labelOpeningDate @@ -153,11 +136,56 @@ this.labelName.TabIndex = 13; this.labelName.Text = "Название:"; // + // IceCreamMaxCountLable + // + this.IceCreamMaxCountLable.AutoSize = true; + this.IceCreamMaxCountLable.Location = new System.Drawing.Point(9, 107); + this.IceCreamMaxCountLable.Name = "IceCreamMaxCountLable"; + this.IceCreamMaxCountLable.Size = new System.Drawing.Size(140, 15); + this.IceCreamMaxCountLable.TabIndex = 22; + this.IceCreamMaxCountLable.Text = "Вместимость магазина: "; + // + // VolumeNumericUpDown + // + this.VolumeNumericUpDown.Location = new System.Drawing.Point(155, 105); + this.VolumeNumericUpDown.Name = "VolumeNumericUpDown"; + this.VolumeNumericUpDown.Size = new System.Drawing.Size(205, 23); + this.VolumeNumericUpDown.TabIndex = 23; + // + // ColumnId + // + this.ColumnId.HeaderText = "ID"; + this.ColumnId.MinimumWidth = 6; + this.ColumnId.Name = "ColumnId"; + this.ColumnId.Visible = false; + this.ColumnId.Width = 125; + // + // ColumnDocumentName + // + this.ColumnDocumentName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.ColumnDocumentName.HeaderText = "Мороженое"; + this.ColumnDocumentName.MinimumWidth = 6; + this.ColumnDocumentName.Name = "ColumnDocumentName"; + // + // PriceIceCream + // + this.PriceIceCream.HeaderText = "Цена"; + this.PriceIceCream.Name = "PriceIceCream"; + // + // ColumnCount + // + this.ColumnCount.HeaderText = "Количество"; + this.ColumnCount.MinimumWidth = 6; + this.ColumnCount.Name = "ColumnCount"; + this.ColumnCount.Width = 125; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(376, 372); + this.ClientSize = new System.Drawing.Size(525, 422); + this.Controls.Add(this.VolumeNumericUpDown); + this.Controls.Add(this.IceCreamMaxCountLable); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.dataGridView); @@ -171,6 +199,7 @@ this.Text = "Магазин"; this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.VolumeNumericUpDown)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -181,14 +210,17 @@ private Button buttonCancel; private Button buttonSave; private DataGridView dataGridView; - private DataGridViewTextBoxColumn ColumnId; - private DataGridViewTextBoxColumn ColumnDocumentName; - private DataGridViewTextBoxColumn ColumnCount; private DateTimePicker dateTimePicker; private Label labelOpeningDate; private TextBox textBoxAddress; private TextBox textBoxName; private Label labelAddress; private Label labelName; + private Label IceCreamMaxCountLable; + private NumericUpDown VolumeNumericUpDown; + private DataGridViewTextBoxColumn ColumnId; + private DataGridViewTextBoxColumn ColumnDocumentName; + private DataGridViewTextBoxColumn PriceIceCream; + private DataGridViewTextBoxColumn ColumnCount; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormShop.cs b/IceCreamShop/IceCreamShop/FormShop.cs index ebca8be..215ff9d 100644 --- a/IceCreamShop/IceCreamShop/FormShop.cs +++ b/IceCreamShop/IceCreamShop/FormShop.cs @@ -47,6 +47,7 @@ namespace IceCreamShopView textBoxAddress.Text = view.Adress; dateTimePicker.Text = view.OpeningDate.ToString(); _shopIceCreams = view.ShopIceCreams ?? new Dictionary(); + VolumeNumericUpDown.Value = view.IceCreamMaxCount; LoadData(); } } @@ -72,6 +73,7 @@ namespace IceCreamShopView { pc.Key, pc.Value.Item1.IceCreamName, + pc.Value.Item1.Price, pc.Value.Item2 } ); @@ -106,7 +108,8 @@ namespace IceCreamShopView Id = _id ?? 0, Name = textBoxName.Text, Adress = textBoxAddress.Text, - OpeningDate = dateTimePicker.Value.Date + OpeningDate = dateTimePicker.Value.Date, + IceCreamMaxCount = (int)VolumeNumericUpDown.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/IceCreamShop/IceCreamShop/FormShop.resx b/IceCreamShop/IceCreamShop/FormShop.resx index 71f1c74..2fe0584 100644 --- a/IceCreamShop/IceCreamShop/FormShop.resx +++ b/IceCreamShop/IceCreamShop/FormShop.resx @@ -63,6 +63,9 @@ True + + True + True diff --git a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs index 5c9ad57..765cc14 100644 --- a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs @@ -40,9 +40,10 @@ // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(288, 179); + this.buttonCancel.Location = new System.Drawing.Point(252, 134); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCancel.Name = "buttonCancel"; - this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.Size = new System.Drawing.Size(82, 22); this.buttonCancel.TabIndex = 17; this.buttonCancel.Text = "Отмена"; this.buttonCancel.UseVisualStyleBackColor = true; @@ -50,9 +51,10 @@ // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(187, 179); + this.buttonSave.Location = new System.Drawing.Point(164, 134); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSave.Name = "buttonSave"; - this.buttonSave.Size = new System.Drawing.Size(94, 29); + this.buttonSave.Size = new System.Drawing.Size(82, 22); this.buttonSave.TabIndex = 16; this.buttonSave.Text = "Сохранить"; this.buttonSave.UseVisualStyleBackColor = true; @@ -60,60 +62,63 @@ // // textBoxCount // - this.textBoxCount.Location = new System.Drawing.Point(131, 119); + this.textBoxCount.Location = new System.Drawing.Point(115, 89); + this.textBoxCount.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.textBoxCount.Name = "textBoxCount"; - this.textBoxCount.Size = new System.Drawing.Size(125, 27); + this.textBoxCount.Size = new System.Drawing.Size(110, 23); this.textBoxCount.TabIndex = 15; // // comboBoxDocument // this.comboBoxDocument.FormattingEnabled = true; - this.comboBoxDocument.Location = new System.Drawing.Point(131, 65); + this.comboBoxDocument.Location = new System.Drawing.Point(115, 49); + this.comboBoxDocument.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.comboBoxDocument.Name = "comboBoxDocument"; - this.comboBoxDocument.Size = new System.Drawing.Size(251, 28); + this.comboBoxDocument.Size = new System.Drawing.Size(220, 23); this.comboBoxDocument.TabIndex = 14; // // comboBoxShop // this.comboBoxShop.FormattingEnabled = true; - this.comboBoxShop.Location = new System.Drawing.Point(131, 15); + this.comboBoxShop.Location = new System.Drawing.Point(115, 11); + this.comboBoxShop.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.comboBoxShop.Name = "comboBoxShop"; - this.comboBoxShop.Size = new System.Drawing.Size(251, 28); + this.comboBoxShop.Size = new System.Drawing.Size(220, 23); this.comboBoxShop.TabIndex = 13; // // labelDocumentCount // this.labelDocumentCount.AutoSize = true; - this.labelDocumentCount.Location = new System.Drawing.Point(9, 119); + this.labelDocumentCount.Location = new System.Drawing.Point(8, 89); this.labelDocumentCount.Name = "labelDocumentCount"; - this.labelDocumentCount.Size = new System.Drawing.Size(93, 20); + this.labelDocumentCount.Size = new System.Drawing.Size(75, 15); this.labelDocumentCount.TabIndex = 12; this.labelDocumentCount.Text = "Количество:"; // // labelDocument // this.labelDocument.AutoSize = true; - this.labelDocument.Location = new System.Drawing.Point(9, 68); + this.labelDocument.Location = new System.Drawing.Point(8, 51); this.labelDocument.Name = "labelDocument"; - this.labelDocument.Size = new System.Drawing.Size(97, 20); + this.labelDocument.Size = new System.Drawing.Size(77, 15); this.labelDocument.TabIndex = 11; this.labelDocument.Text = "Мороженое:"; // // labelShop // this.labelShop.AutoSize = true; - this.labelShop.Location = new System.Drawing.Point(9, 15); + this.labelShop.Location = new System.Drawing.Point(8, 11); this.labelShop.Name = "labelShop"; this.labelShop.RightToLeft = System.Windows.Forms.RightToLeft.No; - this.labelShop.Size = new System.Drawing.Size(72, 20); + this.labelShop.Size = new System.Drawing.Size(57, 15); this.labelShop.TabIndex = 10; this.labelShop.Text = "Магазин:"; // // FormShopSupply // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(405, 220); + this.ClientSize = new System.Drawing.Size(354, 165); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.textBoxCount); @@ -122,9 +127,8 @@ this.Controls.Add(this.labelDocumentCount); this.Controls.Add(this.labelDocument); this.Controls.Add(this.labelShop); - this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormShopSupply"; - this.Text = "FormShopSupply"; + this.Text = "Пополнение магазина"; this.Load += new System.EventHandler(this.FormShopSupply_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index a2443b2..1f3437f 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -54,6 +54,7 @@ namespace IceCreamShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs index 25ccc61..ca3539c 100644 --- a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs @@ -17,5 +17,6 @@ namespace IceCreamShopContracts.BindingModels public DateTime OpeningDate { get; set; } public Dictionary ShopIceCreams { get; set; } = new(); + public int IceCreamMaxCount { get; set; } } } diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index 364f97e..da5b7a5 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,7 @@ namespace IceCreamShopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count); + bool SupplyIceCreams(IIceCreamModel iceCream, int count); + bool SellIceCreams(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs index 48fb1c9..339e31f 100644 --- a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs @@ -1,4 +1,5 @@ -using IceCreamShopContracts.BindingModels; +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.ViewModels; using System; @@ -17,5 +18,6 @@ namespace IceCreamShopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellIceCreams(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs index 0347c63..f49e717 100644 --- a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs @@ -17,6 +17,8 @@ namespace IceCreamShopContracts.ViewModels public string Adress { get; set; } = string.Empty; [DisplayName("Дата открытия")] public DateTime OpeningDate { get; set; } + [DisplayName("Вместимость магазина")] + public int IceCreamMaxCount { get; set; } public Dictionary ShopIceCreams { get; set; } = new(); } diff --git a/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs index 402a3fd..ce884ab 100644 --- a/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs +++ b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs @@ -14,5 +14,6 @@ namespace AbstractIceCreamShopDataModels.Models String Adress { get; } DateTime OpeningDate { get; } Dictionary ShopIceCreams { get; } + public int IceCreamMaxCount { get; } } } diff --git a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs index 8be6b0f..2e662f9 100644 --- a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs +++ b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs @@ -1,10 +1,5 @@ using IceCreamShopFileImplement.Models; using System.Xml.Linq; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace IceCreamShopFileImplement { @@ -14,9 +9,11 @@ namespace IceCreamShopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string IceCreamFileName = "IceCream.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List IceCreams { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -28,11 +25,13 @@ namespace IceCreamShopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveIceCreams() => SaveData(IceCreams, IceCreamFileName, "IceCreams", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; IceCreams = LoadData(IceCreamFileName, "IceCream", x => IceCream.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..055159b --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,197 @@ +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopFileImplement.Models; + +namespace IceCreamShopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + return source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newStore = Shop.Create(model); + if (newStore == null) + { + return null; + } + source.Shops.Add(newStore); + source.SaveShops(); + return newStore.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var store = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (store == null) + { + return null; + } + store.Update(model); + source.SaveShops(); + return store.GetViewModel; + } + + /* public bool SellIceCreams(IIceCreamModel model, int count) + { + + if (source.Shops.Select(x => x.ShopIceCreams.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < count) + { + return false; + } + foreach (var store in source.Shops.Where(x => x.ShopIceCreams.ContainsKey(model.Id))) + { + int QuantityInCurrentShop = store.ShopIceCreams[model.Id].Item2; + if (QuantityInCurrentShop <= count) + { + store.ShopIceCreams.Remove(model.Id); + count -= QuantityInCurrentShop; + } + else + { + store.ShopIceCreams[model.Id] = (store.ShopIceCreams[model.Id].Item1, QuantityInCurrentShop - count); + count = 0; + } + if (count == 0) + { + return true; + } + } + return false; + }*/ + + public bool SellIceCreams(IIceCreamModel model, int count) + { + var icecream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + + var countStore = count; + + if (icecream == null) + { + return false; + } + + foreach (var shop in source.Shops) + { + foreach (var doc in shop.ShopIceCreams) + { + if (doc.Value.Item1.Id == icecream.Id) + { + count -= doc.Value.Item2; + } + if (count <= 0) + { + break; + } + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + + for (int i = 0; i < source.Shops.Count; i++) + { + var shop = source.Shops[i]; + var icecreams = shop.ShopIceCreams; + + for (int j = 1; j < icecreams.Count + 1; j++) + { + if (icecreams[j].Item1.Id == icecream.Id) + { + while (icecreams[j].Item2 > 0 && count > 0) + { + int tempItem2 = icecreams[j].Item2; + tempItem2--; + count--; + icecreams[j] = (icecreams[j].Item1, tempItem2); + } + } + + if (count <= 0) + { + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + IceCreamMaxCount = shop.IceCreamMaxCount, + ShopIceCreams = icecreams + }); + source.SaveShops(); + return true; + + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + IceCreamMaxCount = shop.IceCreamMaxCount, + ShopIceCreams = icecreams + }); + source.SaveShops(); + } + + if (count > 0) + { + return false; + } + return true; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs index e72613b..4d44e16 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs @@ -83,6 +83,6 @@ namespace IceCreamShopFileImplement.Models new XElement("IceCreamName", IceCreamName), new XElement("Price", Price.ToString()), new XElement("IceCreamComponents", Components.Select(x => - new XElement("IceCreamComponents", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + new XElement("IceCreamComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..3aa8d13 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs @@ -0,0 +1,115 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System.Xml.Linq; + + +namespace IceCreamShopFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string Name { get; private set; } = string.Empty; + + public string Adress { get; private set; } = string.Empty; + + public DateTime OpeningDate { get; private set; } + + public int IceCreamMaxCount { get; private set; } + + public Dictionary IceCreams { get; private set; } = new(); + + public Dictionary? _shopIceCreams = null; + + public Dictionary ShopIceCreams + { + get + { + if(_shopIceCreams == null) + { + var source = DataFileSingleton.GetInstance(); + _shopIceCreams = IceCreams.ToDictionary( + x => x.Key, + y => ((source.IceCreams.FirstOrDefault(z => z.Id == y.Key) as IIceCreamModel)!, y.Value)); + } + return _shopIceCreams; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Adress = model.Adress, + IceCreamMaxCount = model.IceCreamMaxCount, + OpeningDate = model.OpeningDate, + IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Adress = element.Element("Adress")!.Value, + IceCreamMaxCount = Convert.ToInt32(element.Element("IceCreamMaxCount")!.Value), + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + IceCreams = element.Element("ShopIceCreams")!.Elements("ShopIceCream").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + IceCreamMaxCount = model.IceCreamMaxCount; + if(model.ShopIceCreams.Count > 0) + { + IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopIceCreams = null; + } + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Adress = Adress, + OpeningDate = OpeningDate, + IceCreamMaxCount = IceCreamMaxCount, + ShopIceCreams = ShopIceCreams + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("Name", Name), + new XElement("Adress", Adress), + new XElement("OpeningDate", OpeningDate), + new XElement("IceCreamMaxCount", IceCreamMaxCount), + new XElement("ShopIceCreams", IceCreams + .Select(x => new XElement("ShopIceCream", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs index c25a24c..7df89b0 100644 --- a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs @@ -1,4 +1,5 @@ -using IceCreamShopContracts.BindingModels; +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; @@ -114,5 +115,10 @@ namespace IceCreamShopListImplement.Implements } return null; } + + public bool SellIceCreams(IIceCreamModel iceCream, int count) + { + throw new NotImplementedException(); + } } } diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs index 78d98e9..5cd4ad1 100644 --- a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs @@ -57,5 +57,7 @@ namespace IceCreamShopListImplement.Models OpeningDate = OpeningDate, ShopIceCreams = ShopIceCreams }; + + public int IceCreamMaxCount => throw new NotImplementedException(); } } -- 2.25.1 From c07a77636f593cff3f6f5578b44a7a2ec10e63c1 Mon Sep 17 00:00:00 2001 From: Ino Date: Mon, 13 Mar 2023 13:25:55 +0400 Subject: [PATCH 4/7] =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 111 ++++++++++++++---- .../BusinessLogics/ShopLogic.cs | 52 +------- .../IceCreamShop/FormIceCreams.Designer.cs | 1 + .../IceCreamShop/FormSellIceCream.Designer.cs | 1 + IceCreamShop/IceCreamShop/FormSellIceCream.cs | 62 +++++----- .../IceCreamShop/FormShopSupply.Designer.cs | 20 ++-- IceCreamShop/IceCreamShop/FormShopSupply.cs | 14 +-- .../BusinessLogicsContracts/IShopLogic.cs | 1 - .../Implements/IceCreamStorage.cs | 8 +- .../Implements/ShopStorage.cs | 77 +++--------- .../IceCreamShopFileImplement/Models/Shop.cs | 5 +- 11 files changed, 170 insertions(+), 182 deletions(-) diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index 842b8fd..0653c4f 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -10,6 +10,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using AbstractIceCreamShopDataModels.Enums; +using AbstractIceCreamShopDataModels.Models; namespace IceCreamBusinessLogic.BusinessLogics { @@ -17,12 +18,14 @@ namespace IceCreamBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; private readonly IShopLogic _shopLogic; private readonly IIceCreamStorage _iceCreamStorage; - public OrderLogic(IOrderStorage orderStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) + public OrderLogic(IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) { _orderStorage = orderStorage; + _shopStorage = shopStorage; _logger = logger; _shopLogic = shopLogic; _iceCreamStorage = iceCreamStorage; @@ -111,43 +114,109 @@ namespace IceCreamBusinessLogic.BusinessLogics throw new ArgumentNullException(nameof(orderModel)); } - if (viewModel.Status + 1 != newStatus) + OrderBindingModel model = new OrderBindingModel + { + Id = viewModel.Id, + IceCreamId = viewModel.IceCreamId, + Status = viewModel.Status, + DateCreate = viewModel.DateCreate, + DateImplement = viewModel.DateImplement, + Count = viewModel.Count, + Sum = viewModel.Sum + }; + + CheckModel(model); + if (model.Status + 1 != newStatus) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } - orderModel.Status = newStatus; - if (orderModel.Status == OrderStatus.Готов) + if (newStatus == OrderStatus.Готов) { - orderModel.DateImplement = DateTime.Now; - - var iceCream = _iceCreamStorage.GetElement(new() { Id = viewModel.IceCreamId }); - - if (iceCream == null) + var icecream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = model.IceCreamId }); + if (icecream == null) { - throw new ArgumentNullException(nameof(iceCream)); + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found."); + return false; } - - if (!_shopLogic.SupplyIceCreams(iceCream, viewModel.Count)) + if (CheckSupply(icecream, model.Count) == false) { - throw new Exception($"SupplyIceCreams operation failed. Store is full."); + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); + return false; } } - else - { - orderModel.DateImplement = viewModel.DateImplement; - } - CheckModel(orderModel); - - if (_orderStorage.Update(orderModel) == null) + model.Status = newStatus; + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + if (_orderStorage.Update(model) == null) { - orderModel.Status--; + model.Status--; _logger.LogWarning("Update operation failed"); return false; } return true; } + + public bool CheckSupply(IIceCreamModel iceCream, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. IceCream count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.IceCreamMaxCount; + foreach (var doc in shop.ShopIceCreams) + { + freeSpace -= doc.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.IceCreamMaxCount; + foreach (var doc in shop.ShopIceCreams) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, freeSpace)) count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } } } diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs index 0700132..0557e56 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection.Metadata; +using System.Xml.Linq; namespace IceCreamBusinessLogic.BusinessLogics { @@ -118,56 +119,9 @@ namespace IceCreamBusinessLogic.BusinessLogics return true; } - public bool SupplyIceCreams(IIceCreamModel iceCream, int count) + public bool SellIceCreams(IIceCreamModel iceCream, int count) { - if (iceCream == null) - { - throw new ArgumentNullException(nameof(iceCream)); - } - - if (count <= 0) - { - throw new ArgumentException("Count of icecreams in supply must be more than 0", nameof(count)); - } - - var freePlaces = _shopStorage.GetFullList() - .Select(x => x.IceCreamMaxCount - x.ShopIceCreams - .Select(p => p.Value.Item2).Sum()).Sum() - count; - - if (freePlaces < 0) - { - _logger.LogInformation("SupplyIceCreams. Failed to add icecreams to store. It's full."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - var temp = Math.Min(count, shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum()); - - if (temp <= 0) - { - continue; - } - - if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, temp)) - { - _logger.LogWarning("An error occurred while adding iceCream to stores"); - return false; - } - - count -= temp; - - if (count == 0) - { - return true; - } - } - return true; - } - - public bool SellIceCreams(IIceCreamModel package, int quantity) - { - return _shopStorage.SellIceCreams(package, quantity); + return _shopStorage.SellIceCreams(iceCream, count); } public bool Create(ShopBindingModel model) diff --git a/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs b/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs index 152124e..5951a2f 100644 --- a/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs @@ -67,6 +67,7 @@ this.buttonEdit.TabIndex = 8; this.buttonEdit.Text = "Изменить"; this.buttonEdit.UseVisualStyleBackColor = true; + this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click); // // buttonAdd // diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs index 5200c73..f1ab985 100644 --- a/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs @@ -102,6 +102,7 @@ this.Controls.Add(this.IceCreamLabel); this.Name = "FormSellIceCream"; this.Text = "Продать мороженое"; + this.Load += new System.EventHandler(this.FormSellIceCream_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.cs index 41842a5..34dfa99 100644 --- a/IceCreamShop/IceCreamShop/FormSellIceCream.cs +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.cs @@ -1,44 +1,27 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.BusinessLogicsContracts; using Microsoft.Extensions.Logging; - +using IceCreamShopContracts.BindingModels; namespace IceCreamShopView { public partial class FormSellIceCream : Form { private readonly ILogger _logger; - private readonly IIceCreamLogic _logicPackage; - private readonly IShopLogic _logicStore; + private readonly IIceCreamLogic _logicI; + private readonly IShopLogic _logicS; public FormSellIceCream(ILogger logger, IIceCreamLogic logicIceCream, IShopLogic logicShop) { InitializeComponent(); _logger = logger; - _logicPackage = logicIceCream; - _logicStore = logicShop; + _logicI = logicIceCream; + _logicS = logicShop; LoadData(); } private void LoadData() { - _logger.LogInformation("Loading icecream for sale."); - - try - { - var list = _logicPackage.ReadList(null); - if (list != null) - { - IceCreamСomboBox.DisplayMember = "IceCreamName"; - IceCreamСomboBox.ValueMember = "Id"; - IceCreamСomboBox.DataSource = list; - IceCreamСomboBox.SelectedItem = null; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "List loading error."); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + } private void SaveButton_Click(object sender, EventArgs e) @@ -59,10 +42,13 @@ namespace IceCreamShopView try { - var operationResult = _logicStore.SellIceCreams(_logicPackage.ReadElement(new IceCreamSearchModel() - { - Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue) - })!, Convert.ToInt32(QuantityTextBox.Text)); + var operationResult = _logicS.SellIceCreams( + new IceCreamBindingModel + { + Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue) + }, + Convert.ToInt32(QuantityTextBox.Text) + ); if (!operationResult) { @@ -86,5 +72,27 @@ namespace IceCreamShopView DialogResult = DialogResult.Cancel; Close(); } + + private void FormSellIceCream_Load(object sender, EventArgs e) + { + _logger.LogInformation("Loading icecream for sale."); + + try + { + var list = _logicI.ReadList(null); + if (list != null) + { + IceCreamСomboBox.DisplayMember = "IceCreamName"; + IceCreamСomboBox.ValueMember = "Id"; + IceCreamСomboBox.DataSource = list; + IceCreamСomboBox.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "List loading error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } diff --git a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs index 765cc14..31580b7 100644 --- a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs @@ -31,7 +31,7 @@ this.buttonCancel = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button(); this.textBoxCount = new System.Windows.Forms.TextBox(); - this.comboBoxDocument = new System.Windows.Forms.ComboBox(); + this.comboBoxIceCream = new System.Windows.Forms.ComboBox(); this.comboBoxShop = new System.Windows.Forms.ComboBox(); this.labelDocumentCount = new System.Windows.Forms.Label(); this.labelDocument = new System.Windows.Forms.Label(); @@ -68,14 +68,14 @@ this.textBoxCount.Size = new System.Drawing.Size(110, 23); this.textBoxCount.TabIndex = 15; // - // comboBoxDocument + // comboBoxIceCream // - this.comboBoxDocument.FormattingEnabled = true; - this.comboBoxDocument.Location = new System.Drawing.Point(115, 49); - this.comboBoxDocument.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.comboBoxDocument.Name = "comboBoxDocument"; - this.comboBoxDocument.Size = new System.Drawing.Size(220, 23); - this.comboBoxDocument.TabIndex = 14; + this.comboBoxIceCream.FormattingEnabled = true; + this.comboBoxIceCream.Location = new System.Drawing.Point(115, 49); + this.comboBoxIceCream.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxIceCream.Name = "comboBoxIceCream"; + this.comboBoxIceCream.Size = new System.Drawing.Size(220, 23); + this.comboBoxIceCream.TabIndex = 14; // // comboBoxShop // @@ -122,7 +122,7 @@ this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.textBoxCount); - this.Controls.Add(this.comboBoxDocument); + this.Controls.Add(this.comboBoxIceCream); this.Controls.Add(this.comboBoxShop); this.Controls.Add(this.labelDocumentCount); this.Controls.Add(this.labelDocument); @@ -140,7 +140,7 @@ private Button buttonCancel; private Button buttonSave; private TextBox textBoxCount; - private ComboBox comboBoxDocument; + private ComboBox comboBoxIceCream; private ComboBox comboBoxShop; private Label labelDocumentCount; private Label labelDocument; diff --git a/IceCreamShop/IceCreamShop/FormShopSupply.cs b/IceCreamShop/IceCreamShop/FormShopSupply.cs index f3d6a4f..889b515 100644 --- a/IceCreamShop/IceCreamShop/FormShopSupply.cs +++ b/IceCreamShop/IceCreamShop/FormShopSupply.cs @@ -33,7 +33,7 @@ namespace IceCreamShopView MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - if (comboBoxDocument.SelectedValue == null) + if (comboBoxIceCream.SelectedValue == null) { MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; @@ -54,8 +54,8 @@ namespace IceCreamShopView }, new IceCreamBindingModel { - Id = Convert.ToInt32(comboBoxDocument.SelectedValue), - IceCreamName = comboBoxDocument.Text + Id = Convert.ToInt32(comboBoxIceCream.SelectedValue), + IceCreamName = comboBoxIceCream.Text }, Convert.ToInt32(textBoxCount.Text) ); @@ -88,10 +88,10 @@ namespace IceCreamShopView var list = _logicI.ReadList(null); if (list != null) { - comboBoxDocument.DisplayMember = "IceCreamName"; - comboBoxDocument.ValueMember = "Id"; - comboBoxDocument.DataSource = list; - comboBoxDocument.SelectedItem = null; + comboBoxIceCream.DisplayMember = "IceCreamName"; + comboBoxIceCream.ValueMember = "Id"; + comboBoxIceCream.DataSource = list; + comboBoxIceCream.SelectedItem = null; } } diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index da5b7a5..bffe5c3 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,7 +18,6 @@ namespace IceCreamShopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count); - bool SupplyIceCreams(IIceCreamModel iceCream, int count); bool SellIceCreams(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs index c09e963..5f3cb16 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs @@ -60,14 +60,14 @@ namespace IceCreamShopFileImplement.Implements public IceCreamViewModel? Update(IceCreamBindingModel model) { - var document = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); - if (document == null) + var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + if (iceCream == null) { return null; } - document.Update(model); + iceCream.Update(model); source.SaveIceCreams(); - return document.GetViewModel; + return iceCream.GetViewModel; } public IceCreamViewModel? Delete(IceCreamBindingModel model) { diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs index 055159b..d7b46e3 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -4,6 +4,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.ViewModels; using AbstractIceCreamShopDataModels.Models; using IceCreamShopFileImplement.Models; +using System.Reflection.Metadata; namespace IceCreamShopFileImplement.Implements { @@ -19,13 +20,13 @@ namespace IceCreamShopFileImplement.Implements public ShopViewModel? Delete(ShopBindingModel model) { var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); - if (element != null) + if (element == null) { - source.Shops.Remove(element); - source.SaveShops(); - return element.GetViewModel; + return null; } - return null; + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; } public ShopViewModel? GetElement(ShopSearchModel model) @@ -79,52 +80,24 @@ namespace IceCreamShopFileImplement.Implements return store.GetViewModel; } - /* public bool SellIceCreams(IIceCreamModel model, int count) - { - - if (source.Shops.Select(x => x.ShopIceCreams.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < count) - { - return false; - } - foreach (var store in source.Shops.Where(x => x.ShopIceCreams.ContainsKey(model.Id))) - { - int QuantityInCurrentShop = store.ShopIceCreams[model.Id].Item2; - if (QuantityInCurrentShop <= count) - { - store.ShopIceCreams.Remove(model.Id); - count -= QuantityInCurrentShop; - } - else - { - store.ShopIceCreams[model.Id] = (store.ShopIceCreams[model.Id].Item1, QuantityInCurrentShop - count); - count = 0; - } - if (count == 0) - { - return true; - } - } - return false; - }*/ - public bool SellIceCreams(IIceCreamModel model, int count) { - var icecream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); var countStore = count; - if (icecream == null) + if (iceCream == null) { return false; } foreach (var shop in source.Shops) { - foreach (var doc in shop.ShopIceCreams) + foreach (var icecream in shop.ShopIceCreams) { - if (doc.Value.Item1.Id == icecream.Id) + if (icecream.Value.Item1.Id == iceCream.Id) { - count -= doc.Value.Item2; + count -= icecream.Value.Item2; } if (count <= 0) { @@ -145,33 +118,15 @@ namespace IceCreamShopFileImplement.Implements var shop = source.Shops[i]; var icecreams = shop.ShopIceCreams; - for (int j = 1; j < icecreams.Count + 1; j++) + foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id)) { - if (icecreams[j].Item1.Id == icecream.Id) - { - while (icecreams[j].Item2 > 0 && count > 0) - { - int tempItem2 = icecreams[j].Item2; - tempItem2--; - count--; - icecreams[j] = (icecreams[j].Item1, tempItem2); - } - } + var min = Math.Min(icecream.Value.Item2, count); + icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min); + count -= min; if (count <= 0) { - shop.Update(new ShopBindingModel - { - Id = shop.Id, - Name = shop.Name, - Adress = shop.Adress, - OpeningDate = shop.OpeningDate, - IceCreamMaxCount = shop.IceCreamMaxCount, - ShopIceCreams = icecreams - }); - source.SaveShops(); - return true; - + break; } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs index 3aa8d13..88d9d0f 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs @@ -2,6 +2,7 @@ using IceCreamShopContracts.ViewModels; using AbstractIceCreamShopDataModels.Models; using System.Xml.Linq; +using System.Reflection.Metadata; namespace IceCreamShopFileImplement.Models @@ -20,7 +21,7 @@ namespace IceCreamShopFileImplement.Models public Dictionary IceCreams { get; private set; } = new(); - public Dictionary? _shopIceCreams = null; + private Dictionary? _shopIceCreams = null; public Dictionary ShopIceCreams { @@ -86,7 +87,7 @@ namespace IceCreamShopFileImplement.Models if(model.ShopIceCreams.Count > 0) { IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); - _shopIceCreams = null; + _shopIceCreams = model.ShopIceCreams; } } -- 2.25.1 From 9923cec6099f5012215b14e46434e48b48f52cac Mon Sep 17 00:00:00 2001 From: Ino Date: Mon, 13 Mar 2023 16:21:48 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShop/FormSellIceCream.cs | 6 ------ .../Implements/OrderStorage.cs | 13 ++++++++++++- .../IceCreamShopFileImplement/Models/IceCream.cs | 2 +- .../IceCreamShopFileImplement/Models/Shop.cs | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.cs index 34dfa99..6a91474 100644 --- a/IceCreamShop/IceCreamShop/FormSellIceCream.cs +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.cs @@ -16,12 +16,6 @@ namespace IceCreamShopView _logger = logger; _logicI = logicIceCream; _logicS = logicShop; - LoadData(); - } - - private void LoadData() - { - } private void SaveButton_Click(object sender, EventArgs e) diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs index e1cf694..9938326 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/OrderStorage.cs @@ -44,7 +44,7 @@ namespace IceCreamShopFileImplement.Implements public List GetFullList() { - return source.Orders.Select(x => x.GetViewModel).ToList(); + return source.Orders.Select(x => GetViewModel(x)).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -82,5 +82,16 @@ namespace IceCreamShopFileImplement.Implements source.SaveOrders(); return order.GetViewModel; } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var document = source.IceCreams.FirstOrDefault(x => x.Id == order.IceCreamId); + if (document != null) + { + viewModel.IceCreamName = document.IceCreamName; + } + return viewModel; + } } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs index 4d44e16..ec7f053 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs @@ -10,7 +10,7 @@ using System.Xml.Linq; namespace IceCreamShopFileImplement.Models { - public class IceCream + public class IceCream : IIceCreamModel { public int Id { get; private set; } public string IceCreamName { get; private set; } = string.Empty; diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs index 88d9d0f..a34c23f 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs @@ -87,7 +87,7 @@ namespace IceCreamShopFileImplement.Models if(model.ShopIceCreams.Count > 0) { IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); - _shopIceCreams = model.ShopIceCreams; + _shopIceCreams = null; } } -- 2.25.1 From f1a8f95f9fb1f7dd689b0da81ae026b991d7af29 Mon Sep 17 00:00:00 2001 From: Inohara Date: Sun, 26 Mar 2023 21:00:37 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 63 +---------- .../BusinessLogics/ShopLogic.cs | 100 +++++++++++++----- .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../Implements/ShopStorage.cs | 34 +----- 4 files changed, 77 insertions(+), 121 deletions(-) diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index 0653c4f..d77fc12 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -140,7 +140,7 @@ namespace IceCreamBusinessLogic.BusinessLogics _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found."); return false; } - if (CheckSupply(icecream, model.Count) == false) + if (!_shopLogic.CheckSupply(icecream, model.Count)) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); return false; @@ -157,66 +157,5 @@ namespace IceCreamBusinessLogic.BusinessLogics } return true; } - - public bool CheckSupply(IIceCreamModel iceCream, int count) - { - if (count <= 0) - { - _logger.LogWarning("Check then supply operation error. IceCream count < 0."); - return false; - } - - int freeSpace = 0; - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace += shop.IceCreamMaxCount; - foreach (var doc in shop.ShopIceCreams) - { - freeSpace -= doc.Value.Item2; - } - } - - if (freeSpace - count < 0) - { - _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace = shop.IceCreamMaxCount; - foreach (var doc in shop.ShopIceCreams) - { - freeSpace -= doc.Value.Item2; - } - if (freeSpace == 0) - { - continue; - } - if (freeSpace - count >= 0) - { - if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) count = 0; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (freeSpace - count < 0) - { - if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, freeSpace)) count -= freeSpace; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (count <= 0) - { - return true; - } - } - return false; - } } } diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs index 0557e56..90498dc 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs @@ -12,6 +12,7 @@ using System.Text; using System.Threading.Tasks; using System.Reflection.Metadata; using System.Xml.Linq; +using System.Reflection; namespace IceCreamBusinessLogic.BusinessLogics { @@ -73,50 +74,95 @@ namespace IceCreamBusinessLogic.BusinessLogics if (count <= 0) { throw new ArgumentNullException("Count of icecreams in supply must be more than 0", nameof(count)); + } _logger.LogInformation("SupplyIceCreams. ShopName:{0}.Id:{ 1}", model.Name, model.Id); - var shopElement = _shopStorage.GetElement(model); - if (shopElement == null) + + var element = _shopStorage.GetElement(model); + if (element == null) { _logger.LogWarning("Required shop element not found in storage"); return false; } - _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name); - var countIceCreams = 0; - foreach (var icecream in shopElement.ShopIceCreams) + _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", element.Id, element.Name); + + if(element.IceCreamMaxCount - element.ShopIceCreams.Select(x => x.Value.Item2).Sum() < count) { - countIceCreams += icecream.Value.Item2; + throw new ArgumentNullException("Shop is full", nameof(count)); } - if (shopElement.IceCreamMaxCount - countIceCreams >= count) + + _logger.LogWarning("SupplyIceCreams find. Id:{Id}", element.Id); + + if(element.ShopIceCreams.TryGetValue(iceCream.Id, out var pair)) { - if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIceCream)) + element.ShopIceCreams[iceCream.Id] = (iceCream, count + pair.Item2); + _logger.LogInformation( + "SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop", + count, iceCream.IceCreamName, element.Name); + } + else + { + element.ShopIceCreams[iceCream.Id] = (iceCream, count); + _logger.LogInformation("SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop", count, iceCream.IceCreamName, element.Name); + } + + _shopStorage.Update(new() + { + Id = element.Id, + Name = element.Name, + Adress = element.Adress, + OpeningDate = element.OpeningDate, + ShopIceCreams = element.ShopIceCreams, + IceCreamMaxCount = element.IceCreamMaxCount + }); + return true; + } + + public bool CheckSupply(IIceCreamModel iceCream, int count) + { + if (iceCream == null) throw new ArgumentNullException(nameof(iceCream)); + + if (count <= 0) + { + throw new ArgumentException("count should be more then 0", nameof(count)); + } + + int freeSpace = _shopStorage.GetFullList().Select(x => x.IceCreamMaxCount - x.ShopIceCreams.Select(p => p.Value.Item2).Sum()).Sum() - count; + + if(freeSpace < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + int countFree = shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum(); + if (countFree < count) { - shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameIceCream.Item2 + count); - _logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, countFree)) + { + _logger.LogWarning("SupplyIceCreams operation failed."); + return false; + } + count -= countFree; } else { - shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count); - _logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) + { + _logger.LogWarning("SupplyIceCreams operation failed."); + return false; + } + count = 0; } - _shopStorage.Update(new() + if (count == 0) { - Id = shopElement.Id, - Name = shopElement.Name, - Adress = shopElement.Adress, - OpeningDate = shopElement.OpeningDate, - ShopIceCreams = shopElement.ShopIceCreams, - IceCreamMaxCount = shopElement.IceCreamMaxCount - }); + return true; + } } - - else - { - _logger.LogWarning("Required shop is overflowed"); - return false; - } - return true; + return false; } public bool SellIceCreams(IIceCreamModel iceCream, int count) diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index bffe5c3..6b2221f 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -19,5 +19,6 @@ namespace IceCreamShopContracts.BusinessLogicsContracts bool Delete(ShopBindingModel model); bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count); bool SellIceCreams(IIceCreamModel iceCream, int count); + bool CheckSupply(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs index d7b46e3..b11c4f6 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -5,6 +5,7 @@ using IceCreamShopContracts.ViewModels; using AbstractIceCreamShopDataModels.Models; using IceCreamShopFileImplement.Models; using System.Reflection.Metadata; +using System.Reflection; namespace IceCreamShopFileImplement.Implements { @@ -82,43 +83,12 @@ namespace IceCreamShopFileImplement.Implements public bool SellIceCreams(IIceCreamModel model, int count) { - var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); - - var countStore = count; - - if (iceCream == null) - { - return false; - } - - foreach (var shop in source.Shops) - { - foreach (var icecream in shop.ShopIceCreams) - { - if (icecream.Value.Item1.Id == iceCream.Id) - { - count -= icecream.Value.Item2; - } - if (count <= 0) - { - break; - } - } - } - - if (count > 0) - { - return false; - } - - count = countStore; - for (int i = 0; i < source.Shops.Count; i++) { var shop = source.Shops[i]; var icecreams = shop.ShopIceCreams; - foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id)) + foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == model.Id)) { var min = Math.Min(icecream.Value.Item2, count); icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min); -- 2.25.1 From 9e2fe8d0ec7bbdfb6c04f935885250d96c5526c1 Mon Sep 17 00:00:00 2001 From: Inohara Date: Tue, 28 Mar 2023 10:02:32 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BD=D0=B0=202=20=D1=83?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 63 ++++++++++- .../BusinessLogics/ShopLogic.cs | 100 +++++------------- .../BusinessLogicsContracts/IShopLogic.cs | 1 - .../Implements/ShopStorage.cs | 34 +++++- 4 files changed, 121 insertions(+), 77 deletions(-) diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index d77fc12..0653c4f 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -140,7 +140,7 @@ namespace IceCreamBusinessLogic.BusinessLogics _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found."); return false; } - if (!_shopLogic.CheckSupply(icecream, model.Count)) + if (CheckSupply(icecream, model.Count) == false) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); return false; @@ -157,5 +157,66 @@ namespace IceCreamBusinessLogic.BusinessLogics } return true; } + + public bool CheckSupply(IIceCreamModel iceCream, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. IceCream count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.IceCreamMaxCount; + foreach (var doc in shop.ShopIceCreams) + { + freeSpace -= doc.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.IceCreamMaxCount; + foreach (var doc in shop.ShopIceCreams) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, freeSpace)) count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } } } diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs index 90498dc..acbc42c 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs @@ -73,96 +73,50 @@ namespace IceCreamBusinessLogic.BusinessLogics if (count <= 0) { - throw new ArgumentNullException("Count of icecreams in supply must be more than 0", nameof(count)); - + throw new ArgumentNullException("Count of iceCreams in supply myst be more than 0", nameof(count)); } - _logger.LogInformation("SupplyIceCreams. ShopName:{0}.Id:{ 1}", model.Name, model.Id); - var element = _shopStorage.GetElement(model); - if (element == null) + var shopElement = _shopStorage.GetElement(model); + if (shopElement == null) { _logger.LogWarning("Required shop element not found in storage"); return false; } + _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name); - _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", element.Id, element.Name); - - if(element.IceCreamMaxCount - element.ShopIceCreams.Select(x => x.Value.Item2).Sum() < count) + var countDocs = 0; + foreach (var doc in shopElement.ShopIceCreams) { - throw new ArgumentNullException("Shop is full", nameof(count)); + countDocs += doc.Value.Item2; } - - _logger.LogWarning("SupplyIceCreams find. Id:{Id}", element.Id); - - if(element.ShopIceCreams.TryGetValue(iceCream.Id, out var pair)) + if (shopElement.IceCreamMaxCount - countDocs >= count) { - element.ShopIceCreams[iceCream.Id] = (iceCream, count + pair.Item2); - _logger.LogInformation( - "SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop", - count, iceCream.IceCreamName, element.Name); - } - else - { - element.ShopIceCreams[iceCream.Id] = (iceCream, count); - _logger.LogInformation("SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop", count, iceCream.IceCreamName, element.Name); - } - - _shopStorage.Update(new() - { - Id = element.Id, - Name = element.Name, - Adress = element.Adress, - OpeningDate = element.OpeningDate, - ShopIceCreams = element.ShopIceCreams, - IceCreamMaxCount = element.IceCreamMaxCount - }); - return true; - } - - public bool CheckSupply(IIceCreamModel iceCream, int count) - { - if (iceCream == null) throw new ArgumentNullException(nameof(iceCream)); - - if (count <= 0) - { - throw new ArgumentException("count should be more then 0", nameof(count)); - } - - int freeSpace = _shopStorage.GetFullList().Select(x => x.IceCreamMaxCount - x.ShopIceCreams.Select(p => p.Value.Item2).Sum()).Sum() - count; - - if(freeSpace < 0) - { - _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - int countFree = shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum(); - if (countFree < count) + if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameDocument)) { - if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, countFree)) - { - _logger.LogWarning("SupplyIceCreams operation failed."); - return false; - } - count -= countFree; + shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameDocument.Item2 + count); + _logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); } else { - if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) - { - _logger.LogWarning("SupplyIceCreams operation failed."); - return false; - } - count = 0; + shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count); + _logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); } - if (count == 0) + _shopStorage.Update(new() { - return true; - } + Id = shopElement.Id, + Name = shopElement.Name, + Adress = shopElement.Adress, + OpeningDate = shopElement.OpeningDate, + ShopIceCreams = shopElement.ShopIceCreams, + IceCreamMaxCount = shopElement.IceCreamMaxCount + }); } - return false; + else + { + _logger.LogWarning("Required shop is overflowed"); + return false; + } + return true; } public bool SellIceCreams(IIceCreamModel iceCream, int count) diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index 6b2221f..bffe5c3 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -19,6 +19,5 @@ namespace IceCreamShopContracts.BusinessLogicsContracts bool Delete(ShopBindingModel model); bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count); bool SellIceCreams(IIceCreamModel iceCream, int count); - bool CheckSupply(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs index b11c4f6..00880d7 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -83,12 +83,43 @@ namespace IceCreamShopFileImplement.Implements public bool SellIceCreams(IIceCreamModel model, int count) { + var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + + var countStore = count; + + if (iceCream == null) + { + return false; + } + + foreach (var shop in source.Shops) + { + foreach (var icecream in shop.ShopIceCreams) + { + if (icecream.Value.Item1.Id == iceCream.Id) + { + count -= icecream.Value.Item2; + } + if (count <= 0) + { + break; + } + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + for (int i = 0; i < source.Shops.Count; i++) { var shop = source.Shops[i]; var icecreams = shop.ShopIceCreams; - foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == model.Id)) + foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id)) { var min = Math.Min(icecream.Value.Item2, count); icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min); @@ -111,7 +142,6 @@ namespace IceCreamShopFileImplement.Implements }); source.SaveShops(); } - if (count > 0) { return false; -- 2.25.1