From e89900b24c2090ab7d20265fd28ddb5ca9e1441d Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Fri, 24 Mar 2023 00:31:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/ShopLogic.cs | 7 ++++++- .../Implements/ShopStorage.cs | 14 +++++++------- .../Models/Shop.cs | 18 ++++++++++-------- .../Models/Shop.cs | 8 ++++---- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs index 5f7cedb..3cfdc1b 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs @@ -79,13 +79,18 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic _logger.LogInformation("AddManufacture. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id); var shop = _shopStorage.GetElement(model); - if (shop == null || !CheckShopCount(shop, count)) + if (shop == null) { _logger.LogWarning("Add Manufacture operation failed"); return false; } + if (shop.MaxCountManufactures - shop.ShopManufactures.Select(x => x.Value.Item2).Sum() < count) + { + throw new ArgumentNullException("Слишком много изделий для одного магазина", nameof(count)); + } + if (!shop.ShopManufactures.ContainsKey(manufacture.Id)) { shop.ShopManufactures[manufacture.Id] = (manufacture, count); diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs index 65a67cc..5e1b878 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs @@ -98,23 +98,23 @@ namespace BlacksmithWorkshopFileImplement.Implements public bool SellManufactures(IManufactureModel model, int count) { - if (_source.Shops.Select(x => x.Manufactures.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count) + if (_source.Shops.Select(x => x.ShopManufactures.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count) { return false; } - var list = _source.Shops.Where(x => x.Manufactures.ContainsKey(model.Id)); + var list = _source.Shops.Where(x => x.ShopManufactures.ContainsKey(model.Id)); foreach (var shop in list) { - if (shop.Manufactures[model.Id].Item2 < count) + if (shop.ShopManufactures[model.Id].Item2 < count) { - count -= shop.Manufactures[model.Id].Item2; - shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, 0); + count -= shop.ShopManufactures[model.Id].Item2; + shop.ShopManufactures[model.Id] = (shop.ShopManufactures[model.Id].Item1, 0); } else { - shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, shop.Manufactures[model.Id].Item2 - count); + shop.ShopManufactures[model.Id] = (shop.ShopManufactures[model.Id].Item1, shop.ShopManufactures[model.Id].Item2 - count); count -= count; } @@ -124,7 +124,7 @@ namespace BlacksmithWorkshopFileImplement.Implements Address = shop.Address, DateOpen = shop.DateOpen, MaxCountManufactures = shop.MaxCountManufactures, - Manufactures = shop.Manufactures + ShopManufactures = shop.ShopManufactures }); if (count == 0) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs index fa00ef2..eecff32 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs @@ -22,18 +22,18 @@ namespace BlacksmithWorkshopFileImplement.Models public int MaxCountManufactures { get; set; } - public Dictionary countManufacture { get; private set; } = new(); + public Dictionary Manufactures { get; private set; } = new(); public Dictionary _manufactures = null; - public Dictionary Manufactures + public Dictionary ShopManufactures { get { if (_manufactures == null) { var source = DataFileSingleton.GetInstance(); - _manufactures = countManufacture.ToDictionary(x => x.Key, y => ((source.Manufactures.FirstOrDefault(z => z.Id == y.Key) as IManufactureModel)!, y.Value)); + _manufactures = Manufactures.ToDictionary(x => x.Key, y => ((source.Manufactures.FirstOrDefault(z => z.Id == y.Key) as IManufactureModel)!, y.Value)); } return _manufactures; @@ -53,9 +53,10 @@ namespace BlacksmithWorkshopFileImplement.Models Address = model.Address, DateOpen = model.DateOpen, MaxCountManufactures = model.MaxCountManufactures, - countManufacture = model.Manufactures.ToDictionary(x => x.Key, x => x.Value.Item2) + Manufactures = model.ShopManufactures.ToDictionary(x => x.Key, x => x.Value.Item2) }; } + public static Shop? Create(XElement element) { @@ -70,7 +71,7 @@ namespace BlacksmithWorkshopFileImplement.Models Address = element.Element("Address")!.Value, DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value), MaxCountManufactures = Convert.ToInt32(element.Element("MaxCountManufactures")!.Value), - countManufacture = element.Element("Manufactures")!.Elements("Manufactures").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + Manufactures = element.Element("Manufactures")!.Elements("Manufactures").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } @@ -84,7 +85,7 @@ namespace BlacksmithWorkshopFileImplement.Models Address = model.Address; DateOpen = model.DateOpen; MaxCountManufactures = model.MaxCountManufactures; - countManufacture = model.Manufactures.ToDictionary(x => x.Key, x => x.Value.Item2); + Manufactures = model.ShopManufactures.ToDictionary(x => x.Key, x => x.Value.Item2); _manufactures = null; } @@ -95,15 +96,16 @@ namespace BlacksmithWorkshopFileImplement.Models Address = Address, DateOpen = DateOpen, MaxCountManufactures = MaxCountManufactures, - Manufactures = Manufactures + ShopManufactures = ShopManufactures }; + public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("ShopName", ShopName), new XElement("Address", Address), new XElement("DateOpen", DateOpen.ToString()), new XElement("MaxCountManufactures", MaxCountManufactures.ToString()), - new XElement("Manufactures", countManufacture.Select(x => new XElement("Manufactures", + new XElement("Manufactures", Manufactures.Select(x => new XElement("Manufactures", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs index a6221c2..1754ef5 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs @@ -21,7 +21,7 @@ namespace BlacksmithWorkshopListImplement.Models public int MaxCountManufactures { get; set; } - public Dictionary Manufactures { get; private set; } = + public Dictionary ShopManufactures { get; private set; } = new Dictionary(); public static Shop? Create(ShopBindingModel? model) @@ -37,7 +37,7 @@ namespace BlacksmithWorkshopListImplement.Models ShopName = model.ShopName, Address = model.Address, DateOpen = model.DateOpen, - Manufactures = model.Manufactures + ShopManufactures = model.ShopManufactures }; } @@ -51,7 +51,7 @@ namespace BlacksmithWorkshopListImplement.Models ShopName = model.ShopName; Address = model.Address; DateOpen = model.DateOpen; - Manufactures = model.Manufactures; + ShopManufactures = model.ShopManufactures; } public ShopViewModel GetViewModel => new() @@ -60,7 +60,7 @@ namespace BlacksmithWorkshopListImplement.Models ShopName = ShopName, Address = Address, DateOpen = DateOpen, - Manufactures = Manufactures + ShopManufactures = ShopManufactures }; } }