diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index 842b8fd..0653c4f 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -10,6 +10,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using AbstractIceCreamShopDataModels.Enums; +using AbstractIceCreamShopDataModels.Models; namespace IceCreamBusinessLogic.BusinessLogics { @@ -17,12 +18,14 @@ namespace IceCreamBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; private readonly IShopLogic _shopLogic; private readonly IIceCreamStorage _iceCreamStorage; - public OrderLogic(IOrderStorage orderStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) + public OrderLogic(IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) { _orderStorage = orderStorage; + _shopStorage = shopStorage; _logger = logger; _shopLogic = shopLogic; _iceCreamStorage = iceCreamStorage; @@ -111,43 +114,109 @@ namespace IceCreamBusinessLogic.BusinessLogics throw new ArgumentNullException(nameof(orderModel)); } - if (viewModel.Status + 1 != newStatus) + OrderBindingModel model = new OrderBindingModel + { + Id = viewModel.Id, + IceCreamId = viewModel.IceCreamId, + Status = viewModel.Status, + DateCreate = viewModel.DateCreate, + DateImplement = viewModel.DateImplement, + Count = viewModel.Count, + Sum = viewModel.Sum + }; + + CheckModel(model); + if (model.Status + 1 != newStatus) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } - orderModel.Status = newStatus; - if (orderModel.Status == OrderStatus.Готов) + if (newStatus == OrderStatus.Готов) { - orderModel.DateImplement = DateTime.Now; - - var iceCream = _iceCreamStorage.GetElement(new() { Id = viewModel.IceCreamId }); - - if (iceCream == null) + var icecream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = model.IceCreamId }); + if (icecream == null) { - throw new ArgumentNullException(nameof(iceCream)); + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found."); + return false; } - - if (!_shopLogic.SupplyIceCreams(iceCream, viewModel.Count)) + if (CheckSupply(icecream, model.Count) == false) { - throw new Exception($"SupplyIceCreams operation failed. Store is full."); + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); + return false; } } - else - { - orderModel.DateImplement = viewModel.DateImplement; - } - CheckModel(orderModel); - - if (_orderStorage.Update(orderModel) == null) + model.Status = newStatus; + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + if (_orderStorage.Update(model) == null) { - orderModel.Status--; + model.Status--; _logger.LogWarning("Update operation failed"); return false; } 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 0700132..0557e56 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection.Metadata; +using System.Xml.Linq; namespace IceCreamBusinessLogic.BusinessLogics { @@ -118,56 +119,9 @@ namespace IceCreamBusinessLogic.BusinessLogics return true; } - public bool SupplyIceCreams(IIceCreamModel iceCream, int count) + public bool SellIceCreams(IIceCreamModel iceCream, int count) { - if (iceCream == null) - { - throw new ArgumentNullException(nameof(iceCream)); - } - - if (count <= 0) - { - throw new ArgumentException("Count of icecreams in supply must be more than 0", nameof(count)); - } - - var freePlaces = _shopStorage.GetFullList() - .Select(x => x.IceCreamMaxCount - x.ShopIceCreams - .Select(p => p.Value.Item2).Sum()).Sum() - count; - - if (freePlaces < 0) - { - _logger.LogInformation("SupplyIceCreams. Failed to add icecreams to store. It's full."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - var temp = Math.Min(count, shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum()); - - if (temp <= 0) - { - continue; - } - - if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, temp)) - { - _logger.LogWarning("An error occurred while adding iceCream to stores"); - return false; - } - - count -= temp; - - if (count == 0) - { - return true; - } - } - return true; - } - - public bool SellIceCreams(IIceCreamModel package, int quantity) - { - return _shopStorage.SellIceCreams(package, quantity); + return _shopStorage.SellIceCreams(iceCream, count); } public bool Create(ShopBindingModel model) diff --git a/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs b/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs index 152124e..5951a2f 100644 --- a/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormIceCreams.Designer.cs @@ -67,6 +67,7 @@ this.buttonEdit.TabIndex = 8; this.buttonEdit.Text = "Изменить"; this.buttonEdit.UseVisualStyleBackColor = true; + this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click); // // buttonAdd // diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs index 5200c73..f1ab985 100644 --- a/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs @@ -102,6 +102,7 @@ this.Controls.Add(this.IceCreamLabel); this.Name = "FormSellIceCream"; this.Text = "Продать мороженое"; + this.Load += new System.EventHandler(this.FormSellIceCream_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.cs index 41842a5..34dfa99 100644 --- a/IceCreamShop/IceCreamShop/FormSellIceCream.cs +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.cs @@ -1,44 +1,27 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.BusinessLogicsContracts; using Microsoft.Extensions.Logging; - +using IceCreamShopContracts.BindingModels; namespace IceCreamShopView { public partial class FormSellIceCream : Form { private readonly ILogger _logger; - private readonly IIceCreamLogic _logicPackage; - private readonly IShopLogic _logicStore; + private readonly IIceCreamLogic _logicI; + private readonly IShopLogic _logicS; public FormSellIceCream(ILogger logger, IIceCreamLogic logicIceCream, IShopLogic logicShop) { InitializeComponent(); _logger = logger; - _logicPackage = logicIceCream; - _logicStore = logicShop; + _logicI = logicIceCream; + _logicS = logicShop; LoadData(); } private void LoadData() { - _logger.LogInformation("Loading icecream for sale."); - - try - { - var list = _logicPackage.ReadList(null); - if (list != null) - { - IceCreamСomboBox.DisplayMember = "IceCreamName"; - IceCreamСomboBox.ValueMember = "Id"; - IceCreamСomboBox.DataSource = list; - IceCreamСomboBox.SelectedItem = null; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "List loading error."); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + } private void SaveButton_Click(object sender, EventArgs e) @@ -59,10 +42,13 @@ namespace IceCreamShopView try { - var operationResult = _logicStore.SellIceCreams(_logicPackage.ReadElement(new IceCreamSearchModel() - { - Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue) - })!, Convert.ToInt32(QuantityTextBox.Text)); + var operationResult = _logicS.SellIceCreams( + new IceCreamBindingModel + { + Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue) + }, + Convert.ToInt32(QuantityTextBox.Text) + ); if (!operationResult) { @@ -86,5 +72,27 @@ namespace IceCreamShopView DialogResult = DialogResult.Cancel; Close(); } + + private void FormSellIceCream_Load(object sender, EventArgs e) + { + _logger.LogInformation("Loading icecream for sale."); + + try + { + var list = _logicI.ReadList(null); + if (list != null) + { + IceCreamСomboBox.DisplayMember = "IceCreamName"; + IceCreamСomboBox.ValueMember = "Id"; + IceCreamСomboBox.DataSource = list; + IceCreamСomboBox.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "List loading error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } diff --git a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs index 765cc14..31580b7 100644 --- a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs @@ -31,7 +31,7 @@ this.buttonCancel = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button(); this.textBoxCount = new System.Windows.Forms.TextBox(); - this.comboBoxDocument = new System.Windows.Forms.ComboBox(); + this.comboBoxIceCream = new System.Windows.Forms.ComboBox(); this.comboBoxShop = new System.Windows.Forms.ComboBox(); this.labelDocumentCount = new System.Windows.Forms.Label(); this.labelDocument = new System.Windows.Forms.Label(); @@ -68,14 +68,14 @@ this.textBoxCount.Size = new System.Drawing.Size(110, 23); this.textBoxCount.TabIndex = 15; // - // comboBoxDocument + // comboBoxIceCream // - this.comboBoxDocument.FormattingEnabled = true; - this.comboBoxDocument.Location = new System.Drawing.Point(115, 49); - this.comboBoxDocument.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.comboBoxDocument.Name = "comboBoxDocument"; - this.comboBoxDocument.Size = new System.Drawing.Size(220, 23); - this.comboBoxDocument.TabIndex = 14; + this.comboBoxIceCream.FormattingEnabled = true; + this.comboBoxIceCream.Location = new System.Drawing.Point(115, 49); + this.comboBoxIceCream.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxIceCream.Name = "comboBoxIceCream"; + this.comboBoxIceCream.Size = new System.Drawing.Size(220, 23); + this.comboBoxIceCream.TabIndex = 14; // // comboBoxShop // @@ -122,7 +122,7 @@ this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.textBoxCount); - this.Controls.Add(this.comboBoxDocument); + this.Controls.Add(this.comboBoxIceCream); this.Controls.Add(this.comboBoxShop); this.Controls.Add(this.labelDocumentCount); this.Controls.Add(this.labelDocument); @@ -140,7 +140,7 @@ private Button buttonCancel; private Button buttonSave; private TextBox textBoxCount; - private ComboBox comboBoxDocument; + private ComboBox comboBoxIceCream; private ComboBox comboBoxShop; private Label labelDocumentCount; private Label labelDocument; diff --git a/IceCreamShop/IceCreamShop/FormShopSupply.cs b/IceCreamShop/IceCreamShop/FormShopSupply.cs index f3d6a4f..889b515 100644 --- a/IceCreamShop/IceCreamShop/FormShopSupply.cs +++ b/IceCreamShop/IceCreamShop/FormShopSupply.cs @@ -33,7 +33,7 @@ namespace IceCreamShopView MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - if (comboBoxDocument.SelectedValue == null) + if (comboBoxIceCream.SelectedValue == null) { MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; @@ -54,8 +54,8 @@ namespace IceCreamShopView }, new IceCreamBindingModel { - Id = Convert.ToInt32(comboBoxDocument.SelectedValue), - IceCreamName = comboBoxDocument.Text + Id = Convert.ToInt32(comboBoxIceCream.SelectedValue), + IceCreamName = comboBoxIceCream.Text }, Convert.ToInt32(textBoxCount.Text) ); @@ -88,10 +88,10 @@ namespace IceCreamShopView var list = _logicI.ReadList(null); if (list != null) { - comboBoxDocument.DisplayMember = "IceCreamName"; - comboBoxDocument.ValueMember = "Id"; - comboBoxDocument.DataSource = list; - comboBoxDocument.SelectedItem = null; + comboBoxIceCream.DisplayMember = "IceCreamName"; + comboBoxIceCream.ValueMember = "Id"; + comboBoxIceCream.DataSource = list; + comboBoxIceCream.SelectedItem = null; } } diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index da5b7a5..bffe5c3 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,7 +18,6 @@ namespace IceCreamShopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count); - bool SupplyIceCreams(IIceCreamModel iceCream, int count); bool SellIceCreams(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs index c09e963..5f3cb16 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/IceCreamStorage.cs @@ -60,14 +60,14 @@ namespace IceCreamShopFileImplement.Implements public IceCreamViewModel? Update(IceCreamBindingModel model) { - var document = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); - if (document == null) + var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + if (iceCream == null) { return null; } - document.Update(model); + iceCream.Update(model); source.SaveIceCreams(); - return document.GetViewModel; + return iceCream.GetViewModel; } public IceCreamViewModel? Delete(IceCreamBindingModel model) { diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs index 055159b..d7b46e3 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -4,6 +4,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.ViewModels; using AbstractIceCreamShopDataModels.Models; using IceCreamShopFileImplement.Models; +using System.Reflection.Metadata; namespace IceCreamShopFileImplement.Implements { @@ -19,13 +20,13 @@ namespace IceCreamShopFileImplement.Implements public ShopViewModel? Delete(ShopBindingModel model) { var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); - if (element != null) + if (element == null) { - source.Shops.Remove(element); - source.SaveShops(); - return element.GetViewModel; + return null; } - return null; + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; } public ShopViewModel? GetElement(ShopSearchModel model) @@ -79,52 +80,24 @@ namespace IceCreamShopFileImplement.Implements return store.GetViewModel; } - /* public bool SellIceCreams(IIceCreamModel model, int count) - { - - if (source.Shops.Select(x => x.ShopIceCreams.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < count) - { - return false; - } - foreach (var store in source.Shops.Where(x => x.ShopIceCreams.ContainsKey(model.Id))) - { - int QuantityInCurrentShop = store.ShopIceCreams[model.Id].Item2; - if (QuantityInCurrentShop <= count) - { - store.ShopIceCreams.Remove(model.Id); - count -= QuantityInCurrentShop; - } - else - { - store.ShopIceCreams[model.Id] = (store.ShopIceCreams[model.Id].Item1, QuantityInCurrentShop - count); - count = 0; - } - if (count == 0) - { - return true; - } - } - return false; - }*/ - public bool SellIceCreams(IIceCreamModel model, int count) { - var icecream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); var countStore = count; - if (icecream == null) + if (iceCream == null) { return false; } foreach (var shop in source.Shops) { - foreach (var doc in shop.ShopIceCreams) + foreach (var icecream in shop.ShopIceCreams) { - if (doc.Value.Item1.Id == icecream.Id) + if (icecream.Value.Item1.Id == iceCream.Id) { - count -= doc.Value.Item2; + count -= icecream.Value.Item2; } if (count <= 0) { @@ -145,33 +118,15 @@ namespace IceCreamShopFileImplement.Implements var shop = source.Shops[i]; var icecreams = shop.ShopIceCreams; - for (int j = 1; j < icecreams.Count + 1; j++) + foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id)) { - if (icecreams[j].Item1.Id == icecream.Id) - { - while (icecreams[j].Item2 > 0 && count > 0) - { - int tempItem2 = icecreams[j].Item2; - tempItem2--; - count--; - icecreams[j] = (icecreams[j].Item1, tempItem2); - } - } + var min = Math.Min(icecream.Value.Item2, count); + icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min); + count -= min; if (count <= 0) { - shop.Update(new ShopBindingModel - { - Id = shop.Id, - Name = shop.Name, - Adress = shop.Adress, - OpeningDate = shop.OpeningDate, - IceCreamMaxCount = shop.IceCreamMaxCount, - ShopIceCreams = icecreams - }); - source.SaveShops(); - return true; - + break; } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs index 3aa8d13..88d9d0f 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs @@ -2,6 +2,7 @@ using IceCreamShopContracts.ViewModels; using AbstractIceCreamShopDataModels.Models; using System.Xml.Linq; +using System.Reflection.Metadata; namespace IceCreamShopFileImplement.Models @@ -20,7 +21,7 @@ namespace IceCreamShopFileImplement.Models public Dictionary IceCreams { get; private set; } = new(); - public Dictionary? _shopIceCreams = null; + private Dictionary? _shopIceCreams = null; public Dictionary ShopIceCreams { @@ -86,7 +87,7 @@ namespace IceCreamShopFileImplement.Models if(model.ShopIceCreams.Count > 0) { IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); - _shopIceCreams = null; + _shopIceCreams = model.ShopIceCreams; } }