From c5f4b3caf64fb1912a6fcd908c94a779c3537df8 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sun, 9 Apr 2023 21:16:52 +0400 Subject: [PATCH] =?UTF-8?q?LabWork02=5FHard:=20=D0=A4=D0=B8=D0=BA=D1=81?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogicContracts/IShopLogic.cs | 2 +- .../Implements/ShopStorage.cs | 35 ++++++++----------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs index 782f221..5c4501a 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs @@ -18,6 +18,6 @@ namespace AutomobilePlantContracts.BusinessLogicContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyCars(ShopSearchModel model, ICarModel car, int count); - bool SellCar(ICarModel document, int count); + bool SellCar(ICarModel car, int count); } } diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Implements/ShopStorage.cs b/AutomobilePlant/AutomobilePlantFileImplement/Implements/ShopStorage.cs index a9d0392..c5ce93f 100644 --- a/AutomobilePlant/AutomobilePlantFileImplement/Implements/ShopStorage.cs +++ b/AutomobilePlant/AutomobilePlantFileImplement/Implements/ShopStorage.cs @@ -83,29 +83,24 @@ namespace AutomobilePlantFileImplement.Implements public bool SellCar(ICarModel model, int count) { - // переделать под linq - var car = source.Cars.FirstOrDefault(x => x.Id == model.Id); - var countStore = count; - if (car == null) { return false; } - foreach (var shop in source.Shops) + var countStore = count; + + var shopCars = source.Shops.SelectMany(shop => shop.ShopCars.Where(c => c.Value.Item1.Id == car.Id)); + + foreach (var c in shopCars) { - foreach (var c in shop.ShopCars) + count -= c.Value.Item2; + + if (count <= 0) { - if (c.Value.Item1.Id == car.Id) - { - count -= c.Value.Item2; - } - if (count <= 0) - { - break; - } + break; } } @@ -116,9 +111,8 @@ namespace AutomobilePlantFileImplement.Implements count = countStore; - for (int i = 0; i < source.Shops.Count; i++) + foreach (var shop in source.Shops) { - var shop = source.Shops[i]; var cars = shop.ShopCars; foreach (var c in cars.Where(x => x.Value.Item1.Id == car.Id)) @@ -142,14 +136,13 @@ namespace AutomobilePlantFileImplement.Implements MaxCountCars = shop.MaxCountCars, ShopCars = cars }); + source.SaveShops(); + + if (count <= 0) break; } - if (count > 0) - { - return false; - } - return true; + return count <= 0; } } }