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

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))
var shops = source.Shops.Where(x => x.Computers.ContainsKey(model.Id)); {
foreach (var shop in shops) hasCount += pair.Item2;
{ }
int countInCurrentShop = shop.Computers[model.Id].Item2; });
if (countInCurrentShop <= quantity)
{ if (hasCount < quantity) return false;
shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, 0);
quantity -= countInCurrentShop; source.Shops.ForEach(x =>
} {
else if (x.Computers.TryGetValue(model.Id, out var pair))
{ {
shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, countInCurrentShop - quantity); if (quantity >= pair.Item2)
quantity = 0; {
} quantity -= pair.Item2;
Update(new ShopBindingModel x.Computers[model.Id] = (model, 0);
{ }
Id = shop.Id, else
ShopName = shop.ShopName, {
ShopAddress = shop.ShopAddress, x.Computers[model.Id] = (model, pair.Item2 - quantity);
DateOpening = shop.DateOpening, quantity = 0;
Computers = shop.Computers, }
Capacity = shop.Capacity
}); x.Update(new ShopBindingModel
if (quantity == 0) {
{ Id = x.Id,
return true; ShopAddress = x.ShopAddress,
} Capacity = x.Capacity,
} DateOpening = x.DateOpening,
return false; ShopName = x.ShopName,
} Computers = x.Computers
});
}
});
source.SaveShops();
return true;
}
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))