Остался вопрос

This commit is contained in:
maxnes3 2023-05-01 17:54:02 +04:00
parent b0b92c6be3
commit 76e2135bf2
2 changed files with 43 additions and 36 deletions

View File

@ -74,41 +74,48 @@ namespace ComputersShopFileImplement.Implements
public bool SellComputers(IComputerModel model, int quantity) public bool SellComputers(IComputerModel model, int quantity)
{ {
int availableQuantity = source.Shops.Select(x => x.Computers.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum(); int hasCount = 0;
if (availableQuantity < quantity)
source.Shops.ForEach(x =>
{ {
return false; if (x.Computers.TryGetValue(model.Id, out var pair))
{
hasCount += pair.Item2;
} }
var shops = source.Shops.Where(x => x.Computers.ContainsKey(model.Id)); });
foreach (var shop in shops)
if (hasCount < quantity) return false;
source.Shops.ForEach(x =>
{ {
int countInCurrentShop = shop.Computers[model.Id].Item2; if (x.Computers.TryGetValue(model.Id, out var pair))
if (countInCurrentShop <= quantity)
{ {
shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, 0); if (quantity >= pair.Item2)
quantity -= countInCurrentShop; {
quantity -= pair.Item2;
x.Computers[model.Id] = (model, 0);
} }
else else
{ {
shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, countInCurrentShop - quantity); x.Computers[model.Id] = (model, pair.Item2 - quantity);
quantity = 0; quantity = 0;
} }
Update(new ShopBindingModel
x.Update(new ShopBindingModel
{ {
Id = shop.Id, Id = x.Id,
ShopName = shop.ShopName, ShopAddress = x.ShopAddress,
ShopAddress = shop.ShopAddress, Capacity = x.Capacity,
DateOpening = shop.DateOpening, DateOpening = x.DateOpening,
Computers = shop.Computers, ShopName = x.ShopName,
Capacity = shop.Capacity Computers = x.Computers
}); });
if (quantity == 0) }
{ });
source.SaveShops();
return true; return true;
} }
}
return false;
}
public ShopViewModel? Update(ShopBindingModel model) public ShopViewModel? Update(ShopBindingModel model)
{ {

View File

@ -39,7 +39,7 @@ namespace ComputersShopView
{ {
if (comboBoxDocuments.SelectedValue == null) if (comboBoxDocuments.SelectedValue == null)
{ {
MessageBox.Show("Выберите корабль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
if (string.IsNullOrEmpty(numericUpDownCount.Text)) if (string.IsNullOrEmpty(numericUpDownCount.Text))