From f1a8f95f9fb1f7dd689b0da81ae026b991d7af29 Mon Sep 17 00:00:00 2001 From: Inohara Date: Sun, 26 Mar 2023 21:00:37 +0400 Subject: [PATCH] =?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);