From 76e2135bf29af8cd5b1dc63c153b712194bde172 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Mon, 1 May 2023 17:54:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=81=D1=82=D0=B0=D0=BB=D1=81=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=BF=D1=80=D0=BE=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 77 ++++++++++--------- .../ComputersShopView/FormSellComputers.cs | 2 +- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs index 88c9961..a499e3a 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs @@ -74,41 +74,48 @@ namespace ComputersShopFileImplement.Implements public bool SellComputers(IComputerModel model, int quantity) { - int availableQuantity = source.Shops.Select(x => x.Computers.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum(); - if (availableQuantity < quantity) - { - return false; - } - var shops = source.Shops.Where(x => x.Computers.ContainsKey(model.Id)); - foreach (var shop in shops) - { - int countInCurrentShop = shop.Computers[model.Id].Item2; - if (countInCurrentShop <= quantity) - { - shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, 0); - quantity -= countInCurrentShop; - } - else - { - shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, countInCurrentShop - quantity); - quantity = 0; - } - Update(new ShopBindingModel - { - Id = shop.Id, - ShopName = shop.ShopName, - ShopAddress = shop.ShopAddress, - DateOpening = shop.DateOpening, - Computers = shop.Computers, - Capacity = shop.Capacity - }); - if (quantity == 0) - { - return true; - } - } - return false; - } + int hasCount = 0; + + source.Shops.ForEach(x => + { + if (x.Computers.TryGetValue(model.Id, out var pair)) + { + hasCount += pair.Item2; + } + }); + + if (hasCount < quantity) return false; + + source.Shops.ForEach(x => + { + if (x.Computers.TryGetValue(model.Id, out var pair)) + { + if (quantity >= pair.Item2) + { + quantity -= pair.Item2; + x.Computers[model.Id] = (model, 0); + } + else + { + x.Computers[model.Id] = (model, pair.Item2 - quantity); + quantity = 0; + } + + x.Update(new ShopBindingModel + { + Id = x.Id, + ShopAddress = x.ShopAddress, + Capacity = x.Capacity, + DateOpening = x.DateOpening, + ShopName = x.ShopName, + Computers = x.Computers + }); + } + }); + + source.SaveShops(); + return true; + } public ShopViewModel? Update(ShopBindingModel model) { diff --git a/ComputersShop/ComputersShopView/FormSellComputers.cs b/ComputersShop/ComputersShopView/FormSellComputers.cs index 323ff81..e301cec 100644 --- a/ComputersShop/ComputersShopView/FormSellComputers.cs +++ b/ComputersShop/ComputersShopView/FormSellComputers.cs @@ -39,7 +39,7 @@ namespace ComputersShopView { if (comboBoxDocuments.SelectedValue == null) { - MessageBox.Show("Выберите корабль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(numericUpDownCount.Text))