From 9e2fe8d0ec7bbdfb6c04f935885250d96c5526c1 Mon Sep 17 00:00:00 2001 From: Inohara Date: Tue, 28 Mar 2023 10:02:32 +0400 Subject: [PATCH] =?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;