From cff88300c0ce7567395b2cdff1255128e1684c68 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sun, 12 Mar 2023 21:19:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D1=83=D1=81=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 54 +++-- .../BusinessLogics/ShopLogic.cs | 92 ++++++-- .../IceCreamShop/FormMain.Designer.cs | 20 +- IceCreamShop/IceCreamShop/FormMain.cs | 10 + .../IceCreamShop/FormSellIceCream.Designer.cs | 119 +++++++++++ IceCreamShop/IceCreamShop/FormSellIceCream.cs | 90 ++++++++ .../IceCreamShop/FormSellIceCream.resx | 60 ++++++ .../IceCreamShop/FormShop.Designer.cs | 102 +++++---- IceCreamShop/IceCreamShop/FormShop.cs | 5 +- IceCreamShop/IceCreamShop/FormShop.resx | 3 + .../IceCreamShop/FormShopSupply.Designer.cs | 44 ++-- IceCreamShop/IceCreamShop/Program.cs | 1 + .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 2 + .../StoragesContracts/IShopStorage.cs | 4 +- .../ViewModels/ShopViewModel.cs | 2 + .../Models/IShopModel.cs | 1 + .../DataFileSingleton.cs | 9 +- .../Implements/ShopStorage.cs | 197 ++++++++++++++++++ .../Models/IceCream.cs | 2 +- .../IceCreamShopFileImplement/Models/Shop.cs | 115 ++++++++++ .../Implements/ShopStorage.cs | 8 +- .../IceCreamShopListImplement/Models/Shop.cs | 2 + 23 files changed, 843 insertions(+), 100 deletions(-) create mode 100644 IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs create mode 100644 IceCreamShop/IceCreamShop/FormSellIceCream.cs create mode 100644 IceCreamShop/IceCreamShop/FormSellIceCream.resx create mode 100644 IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index be1f742..842b8fd 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,11 +17,15 @@ namespace IceCreamBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopLogic _shopLogic; + private readonly IIceCreamStorage _iceCreamStorage; - public OrderLogic(IOrderStorage orderStorage, ILogger logger) + public OrderLogic(IOrderStorage orderStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) { _orderStorage = orderStorage; _logger = logger; + _shopLogic = shopLogic; + _iceCreamStorage = iceCreamStorage; } public bool CreateOrder(OrderBindingModel model) @@ -104,32 +108,42 @@ namespace IceCreamBusinessLogic.BusinessLogics if (viewModel == null) { - _logger.LogWarning("Order model not found"); - return false; + throw new ArgumentNullException(nameof(orderModel)); } - OrderBindingModel model = new OrderBindingModel - { - Id = viewModel.Id, - IceCreamId = viewModel.IceCreamId, - IceCreamName = viewModel.IceCreamName, - Status = viewModel.Status, - DateCreate = viewModel.DateCreate, - DateImplement = viewModel.DateImplement, - Count = viewModel.Count, - Sum = viewModel.Sum - }; - CheckModel(model); - if (model.Status + 1 != newStatus) + if (viewModel.Status + 1 != newStatus) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } - model.Status = newStatus; - if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) + orderModel.Status = newStatus; + + if (orderModel.Status == OrderStatus.Готов) { - model.Status--; + orderModel.DateImplement = DateTime.Now; + + var iceCream = _iceCreamStorage.GetElement(new() { Id = viewModel.IceCreamId }); + + if (iceCream == null) + { + throw new ArgumentNullException(nameof(iceCream)); + } + + if (!_shopLogic.SupplyIceCreams(iceCream, viewModel.Count)) + { + throw new Exception($"SupplyIceCreams operation failed. Store is full."); + } + } + else + { + orderModel.DateImplement = viewModel.DateImplement; + } + + CheckModel(orderModel); + + if (_orderStorage.Update(orderModel) == null) + { + orderModel.Status--; _logger.LogWarning("Update operation failed"); return false; } diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs index ec00b3b..0700132 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/ShopLogic.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Reflection.Metadata; namespace IceCreamBusinessLogic.BusinessLogics { @@ -72,7 +73,7 @@ namespace IceCreamBusinessLogic.BusinessLogics { throw new ArgumentNullException("Count of icecreams in supply must be more than 0", nameof(count)); } - + _logger.LogInformation("SupplyIceCreams. ShopName:{0}.Id:{ 1}", model.Name, model.Id); var shopElement = _shopStorage.GetElement(model); if (shopElement == null) { @@ -81,29 +82,94 @@ namespace IceCreamBusinessLogic.BusinessLogics } _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name); - if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameDocument)) + var countIceCreams = 0; + foreach (var icecream in shopElement.ShopIceCreams) { - shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameDocument.Item2 + count); - _logger.LogInformation("Same icecream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + countIceCreams += icecream.Value.Item2; } + if (shopElement.IceCreamMaxCount - countIceCreams >= count) + { + if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIceCream)) + { + shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameIceCream.Item2 + count); + _logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + } + else + { + shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count); + _logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + } + _shopStorage.Update(new() + { + Id = shopElement.Id, + Name = shopElement.Name, + Adress = shopElement.Adress, + OpeningDate = shopElement.OpeningDate, + ShopIceCreams = shopElement.ShopIceCreams, + IceCreamMaxCount = shopElement.IceCreamMaxCount + }); + } + else { - shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count); - _logger.LogInformation("New icecream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name); + _logger.LogWarning("Required shop is overflowed"); + return false; + } + return true; + } + + public bool SupplyIceCreams(IIceCreamModel iceCream, int count) + { + if (iceCream == null) + { + throw new ArgumentNullException(nameof(iceCream)); } - _shopStorage.Update(new() + if (count <= 0) { - Id = shopElement.Id, - Name = shopElement.Name, - Adress = shopElement.Adress, - OpeningDate = shopElement.OpeningDate, - ShopIceCreams = shopElement.ShopIceCreams - }); + 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); + } + public bool Create(ShopBindingModel model) { CheckModel(model); diff --git a/IceCreamShop/IceCreamShop/FormMain.Designer.cs b/IceCreamShop/IceCreamShop/FormMain.Designer.cs index 68195d4..25a564c 100644 --- a/IceCreamShop/IceCreamShop/FormMain.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.мороженоеToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.магазиныToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.buttonSupplyShop = new System.Windows.Forms.Button(); + this.SellIceCreamButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip.SuspendLayout(); this.SuspendLayout(); @@ -135,21 +136,21 @@ // компонентыToolStripMenuItem // this.компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.компонентыToolStripMenuItem.Text = "Компоненты"; this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.компонентыToolStripMenuItem_Click); // // мороженоеToolStripMenuItem // this.мороженоеToolStripMenuItem.Name = "мороженоеToolStripMenuItem"; - this.мороженоеToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.мороженоеToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.мороженоеToolStripMenuItem.Text = "Мороженое"; this.мороженоеToolStripMenuItem.Click += new System.EventHandler(this.мороженоеToolStripMenuItem_Click); // // магазиныToolStripMenuItem // this.магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.магазиныToolStripMenuItem.Text = "Магазины"; this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.магазиныToolStripMenuItem_Click); // @@ -164,11 +165,23 @@ this.buttonSupplyShop.UseVisualStyleBackColor = true; this.buttonSupplyShop.Click += new System.EventHandler(this.buttonSupplyShop_Click); // + // SellIceCreamButton + // + this.SellIceCreamButton.Location = new System.Drawing.Point(878, 231); + this.SellIceCreamButton.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.SellIceCreamButton.Name = "SellIceCreamButton"; + this.SellIceCreamButton.Size = new System.Drawing.Size(170, 37); + this.SellIceCreamButton.TabIndex = 15; + this.SellIceCreamButton.Text = "Продажа мороженого"; + this.SellIceCreamButton.UseVisualStyleBackColor = true; + this.SellIceCreamButton.Click += new System.EventHandler(this.SellIceCreamButton_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1072, 347); + this.Controls.Add(this.SellIceCreamButton); this.Controls.Add(this.buttonSupplyShop); this.Controls.Add(this.buttonUpdate); this.Controls.Add(this.buttonSetToFinish); @@ -202,5 +215,6 @@ private ToolStripMenuItem мороженоеToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; private Button buttonSupplyShop; + private Button SellIceCreamButton; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormMain.cs b/IceCreamShop/IceCreamShop/FormMain.cs index 8dd2ef8..6e91c1d 100644 --- a/IceCreamShop/IceCreamShop/FormMain.cs +++ b/IceCreamShop/IceCreamShop/FormMain.cs @@ -181,5 +181,15 @@ namespace IceCreamShopView form.ShowDialog(); } } + + private void SellIceCreamButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellIceCream)); + + if (service is FormSellIceCream form) + { + form.ShowDialog(); + } + } } } diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs new file mode 100644 index 0000000..5200c73 --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.Designer.cs @@ -0,0 +1,119 @@ +namespace IceCreamShopView +{ + partial class FormSellIceCream + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.ButtonCancel = new System.Windows.Forms.Button(); + this.SaveButton = new System.Windows.Forms.Button(); + this.QuantityTextBox = new System.Windows.Forms.TextBox(); + this.IceCreamСomboBox = new System.Windows.Forms.ComboBox(); + this.QuantityLabel = new System.Windows.Forms.Label(); + this.IceCreamLabel = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // ButtonCancel + // + this.ButtonCancel.Location = new System.Drawing.Point(169, 97); + this.ButtonCancel.Name = "ButtonCancel"; + this.ButtonCancel.Size = new System.Drawing.Size(97, 29); + this.ButtonCancel.TabIndex = 11; + this.ButtonCancel.Text = "Отмена"; + this.ButtonCancel.UseVisualStyleBackColor = true; + this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // SaveButton + // + this.SaveButton.Location = new System.Drawing.Point(66, 97); + this.SaveButton.Name = "SaveButton"; + this.SaveButton.Size = new System.Drawing.Size(97, 29); + this.SaveButton.TabIndex = 10; + this.SaveButton.Text = "Сохранить"; + this.SaveButton.UseVisualStyleBackColor = true; + this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click); + // + // QuantityTextBox + // + this.QuantityTextBox.Location = new System.Drawing.Point(82, 45); + this.QuantityTextBox.Name = "QuantityTextBox"; + this.QuantityTextBox.Size = new System.Drawing.Size(184, 23); + this.QuantityTextBox.TabIndex = 9; + // + // IceCreamСomboBox + // + this.IceCreamСomboBox.FormattingEnabled = true; + this.IceCreamСomboBox.Location = new System.Drawing.Point(82, 12); + this.IceCreamСomboBox.Name = "IceCreamСomboBox"; + this.IceCreamСomboBox.Size = new System.Drawing.Size(184, 23); + this.IceCreamСomboBox.TabIndex = 8; + // + // QuantityLabel + // + this.QuantityLabel.AutoSize = true; + this.QuantityLabel.Location = new System.Drawing.Point(6, 48); + this.QuantityLabel.Name = "QuantityLabel"; + this.QuantityLabel.Size = new System.Drawing.Size(75, 15); + this.QuantityLabel.TabIndex = 7; + this.QuantityLabel.Text = "Количество:"; + // + // IceCreamLabel + // + this.IceCreamLabel.AutoSize = true; + this.IceCreamLabel.Location = new System.Drawing.Point(6, 15); + this.IceCreamLabel.Name = "IceCreamLabel"; + this.IceCreamLabel.Size = new System.Drawing.Size(74, 15); + this.IceCreamLabel.TabIndex = 6; + this.IceCreamLabel.Text = "Мороженое"; + // + // FormSellIceCream + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(278, 135); + this.Controls.Add(this.ButtonCancel); + this.Controls.Add(this.SaveButton); + this.Controls.Add(this.QuantityTextBox); + this.Controls.Add(this.IceCreamСomboBox); + this.Controls.Add(this.QuantityLabel); + this.Controls.Add(this.IceCreamLabel); + this.Name = "FormSellIceCream"; + this.Text = "Продать мороженое"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button ButtonCancel; + private Button SaveButton; + private TextBox QuantityTextBox; + private ComboBox IceCreamСomboBox; + private Label QuantityLabel; + private Label IceCreamLabel; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.cs b/IceCreamShop/IceCreamShop/FormSellIceCream.cs new file mode 100644 index 0000000..41842a5 --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.cs @@ -0,0 +1,90 @@ +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + + +namespace IceCreamShopView +{ + public partial class FormSellIceCream : Form + { + private readonly ILogger _logger; + private readonly IIceCreamLogic _logicPackage; + private readonly IShopLogic _logicStore; + public FormSellIceCream(ILogger logger, IIceCreamLogic logicIceCream, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicPackage = logicIceCream; + _logicStore = 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) + { + if (string.IsNullOrEmpty(QuantityTextBox.Text)) + { + MessageBox.Show("Укажите количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (IceCreamСomboBox.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Product sale."); + + try + { + var operationResult = _logicStore.SellIceCreams(_logicPackage.ReadElement(new IceCreamSearchModel() + { + Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue) + })!, Convert.ToInt32(QuantityTextBox.Text)); + + if (!operationResult) + { + throw new Exception("Ошибка при продаже изделия. Дополнительная информация в логах."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Product sale error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/IceCreamShop/IceCreamShop/FormSellIceCream.resx b/IceCreamShop/IceCreamShop/FormSellIceCream.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormSellIceCream.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormShop.Designer.cs b/IceCreamShop/IceCreamShop/FormShop.Designer.cs index 356849d..405b100 100644 --- a/IceCreamShop/IceCreamShop/FormShop.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormShop.Designer.cs @@ -31,21 +31,25 @@ this.buttonCancel = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button(); this.dataGridView = new System.Windows.Forms.DataGridView(); - this.ColumnId = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.ColumnDocumentName = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dateTimePicker = new System.Windows.Forms.DateTimePicker(); this.labelOpeningDate = new System.Windows.Forms.Label(); this.textBoxAddress = new System.Windows.Forms.TextBox(); this.textBoxName = new System.Windows.Forms.TextBox(); this.labelAddress = new System.Windows.Forms.Label(); this.labelName = new System.Windows.Forms.Label(); + this.IceCreamMaxCountLable = new System.Windows.Forms.Label(); + this.VolumeNumericUpDown = new System.Windows.Forms.NumericUpDown(); + this.ColumnId = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnDocumentName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.PriceIceCream = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.VolumeNumericUpDown)).BeginInit(); this.SuspendLayout(); // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(197, 338); + this.buttonCancel.Location = new System.Drawing.Point(332, 389); this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(136, 22); @@ -56,7 +60,7 @@ // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(39, 338); + this.buttonSave.Location = new System.Drawing.Point(39, 387); this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSave.Name = "buttonSave"; this.buttonSave.Size = new System.Drawing.Size(136, 22); @@ -71,43 +75,22 @@ this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.ColumnId, this.ColumnDocumentName, + this.PriceIceCream, this.ColumnCount}); - this.dataGridView.Location = new System.Drawing.Point(9, 106); + this.dataGridView.Location = new System.Drawing.Point(9, 155); this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 51; this.dataGridView.RowTemplate.Height = 29; - this.dataGridView.Size = new System.Drawing.Size(351, 225); + this.dataGridView.Size = new System.Drawing.Size(504, 225); this.dataGridView.TabIndex = 19; // - // ColumnId - // - this.ColumnId.HeaderText = "ID"; - this.ColumnId.MinimumWidth = 6; - this.ColumnId.Name = "ColumnId"; - this.ColumnId.Visible = false; - this.ColumnId.Width = 125; - // - // ColumnDocumentName - // - this.ColumnDocumentName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; - this.ColumnDocumentName.HeaderText = "Документ"; - this.ColumnDocumentName.MinimumWidth = 6; - this.ColumnDocumentName.Name = "ColumnDocumentName"; - // - // ColumnCount - // - this.ColumnCount.HeaderText = "Количество"; - this.ColumnCount.MinimumWidth = 6; - this.ColumnCount.Name = "ColumnCount"; - this.ColumnCount.Width = 125; - // // dateTimePicker // - this.dateTimePicker.Location = new System.Drawing.Point(141, 63); + this.dateTimePicker.Location = new System.Drawing.Point(155, 63); this.dateTimePicker.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.dateTimePicker.Name = "dateTimePicker"; - this.dateTimePicker.Size = new System.Drawing.Size(219, 23); + this.dateTimePicker.Size = new System.Drawing.Size(205, 23); this.dateTimePicker.TabIndex = 18; // // labelOpeningDate @@ -153,11 +136,56 @@ this.labelName.TabIndex = 13; this.labelName.Text = "Название:"; // + // IceCreamMaxCountLable + // + this.IceCreamMaxCountLable.AutoSize = true; + this.IceCreamMaxCountLable.Location = new System.Drawing.Point(9, 107); + this.IceCreamMaxCountLable.Name = "IceCreamMaxCountLable"; + this.IceCreamMaxCountLable.Size = new System.Drawing.Size(140, 15); + this.IceCreamMaxCountLable.TabIndex = 22; + this.IceCreamMaxCountLable.Text = "Вместимость магазина: "; + // + // VolumeNumericUpDown + // + this.VolumeNumericUpDown.Location = new System.Drawing.Point(155, 105); + this.VolumeNumericUpDown.Name = "VolumeNumericUpDown"; + this.VolumeNumericUpDown.Size = new System.Drawing.Size(205, 23); + this.VolumeNumericUpDown.TabIndex = 23; + // + // ColumnId + // + this.ColumnId.HeaderText = "ID"; + this.ColumnId.MinimumWidth = 6; + this.ColumnId.Name = "ColumnId"; + this.ColumnId.Visible = false; + this.ColumnId.Width = 125; + // + // ColumnDocumentName + // + this.ColumnDocumentName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.ColumnDocumentName.HeaderText = "Мороженое"; + this.ColumnDocumentName.MinimumWidth = 6; + this.ColumnDocumentName.Name = "ColumnDocumentName"; + // + // PriceIceCream + // + this.PriceIceCream.HeaderText = "Цена"; + this.PriceIceCream.Name = "PriceIceCream"; + // + // ColumnCount + // + this.ColumnCount.HeaderText = "Количество"; + this.ColumnCount.MinimumWidth = 6; + this.ColumnCount.Name = "ColumnCount"; + this.ColumnCount.Width = 125; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(376, 372); + this.ClientSize = new System.Drawing.Size(525, 422); + this.Controls.Add(this.VolumeNumericUpDown); + this.Controls.Add(this.IceCreamMaxCountLable); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.dataGridView); @@ -171,6 +199,7 @@ this.Text = "Магазин"; this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.VolumeNumericUpDown)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -181,14 +210,17 @@ private Button buttonCancel; private Button buttonSave; private DataGridView dataGridView; - private DataGridViewTextBoxColumn ColumnId; - private DataGridViewTextBoxColumn ColumnDocumentName; - private DataGridViewTextBoxColumn ColumnCount; private DateTimePicker dateTimePicker; private Label labelOpeningDate; private TextBox textBoxAddress; private TextBox textBoxName; private Label labelAddress; private Label labelName; + private Label IceCreamMaxCountLable; + private NumericUpDown VolumeNumericUpDown; + private DataGridViewTextBoxColumn ColumnId; + private DataGridViewTextBoxColumn ColumnDocumentName; + private DataGridViewTextBoxColumn PriceIceCream; + private DataGridViewTextBoxColumn ColumnCount; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormShop.cs b/IceCreamShop/IceCreamShop/FormShop.cs index ebca8be..215ff9d 100644 --- a/IceCreamShop/IceCreamShop/FormShop.cs +++ b/IceCreamShop/IceCreamShop/FormShop.cs @@ -47,6 +47,7 @@ namespace IceCreamShopView textBoxAddress.Text = view.Adress; dateTimePicker.Text = view.OpeningDate.ToString(); _shopIceCreams = view.ShopIceCreams ?? new Dictionary(); + VolumeNumericUpDown.Value = view.IceCreamMaxCount; LoadData(); } } @@ -72,6 +73,7 @@ namespace IceCreamShopView { pc.Key, pc.Value.Item1.IceCreamName, + pc.Value.Item1.Price, pc.Value.Item2 } ); @@ -106,7 +108,8 @@ namespace IceCreamShopView Id = _id ?? 0, Name = textBoxName.Text, Adress = textBoxAddress.Text, - OpeningDate = dateTimePicker.Value.Date + OpeningDate = dateTimePicker.Value.Date, + IceCreamMaxCount = (int)VolumeNumericUpDown.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/IceCreamShop/IceCreamShop/FormShop.resx b/IceCreamShop/IceCreamShop/FormShop.resx index 71f1c74..2fe0584 100644 --- a/IceCreamShop/IceCreamShop/FormShop.resx +++ b/IceCreamShop/IceCreamShop/FormShop.resx @@ -63,6 +63,9 @@ True + + True + True diff --git a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs index 5c9ad57..765cc14 100644 --- a/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormShopSupply.Designer.cs @@ -40,9 +40,10 @@ // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(288, 179); + this.buttonCancel.Location = new System.Drawing.Point(252, 134); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCancel.Name = "buttonCancel"; - this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.Size = new System.Drawing.Size(82, 22); this.buttonCancel.TabIndex = 17; this.buttonCancel.Text = "Отмена"; this.buttonCancel.UseVisualStyleBackColor = true; @@ -50,9 +51,10 @@ // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(187, 179); + this.buttonSave.Location = new System.Drawing.Point(164, 134); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSave.Name = "buttonSave"; - this.buttonSave.Size = new System.Drawing.Size(94, 29); + this.buttonSave.Size = new System.Drawing.Size(82, 22); this.buttonSave.TabIndex = 16; this.buttonSave.Text = "Сохранить"; this.buttonSave.UseVisualStyleBackColor = true; @@ -60,60 +62,63 @@ // // textBoxCount // - this.textBoxCount.Location = new System.Drawing.Point(131, 119); + this.textBoxCount.Location = new System.Drawing.Point(115, 89); + this.textBoxCount.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.textBoxCount.Name = "textBoxCount"; - this.textBoxCount.Size = new System.Drawing.Size(125, 27); + this.textBoxCount.Size = new System.Drawing.Size(110, 23); this.textBoxCount.TabIndex = 15; // // comboBoxDocument // this.comboBoxDocument.FormattingEnabled = true; - this.comboBoxDocument.Location = new System.Drawing.Point(131, 65); + 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(251, 28); + this.comboBoxDocument.Size = new System.Drawing.Size(220, 23); this.comboBoxDocument.TabIndex = 14; // // comboBoxShop // this.comboBoxShop.FormattingEnabled = true; - this.comboBoxShop.Location = new System.Drawing.Point(131, 15); + this.comboBoxShop.Location = new System.Drawing.Point(115, 11); + this.comboBoxShop.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.comboBoxShop.Name = "comboBoxShop"; - this.comboBoxShop.Size = new System.Drawing.Size(251, 28); + this.comboBoxShop.Size = new System.Drawing.Size(220, 23); this.comboBoxShop.TabIndex = 13; // // labelDocumentCount // this.labelDocumentCount.AutoSize = true; - this.labelDocumentCount.Location = new System.Drawing.Point(9, 119); + this.labelDocumentCount.Location = new System.Drawing.Point(8, 89); this.labelDocumentCount.Name = "labelDocumentCount"; - this.labelDocumentCount.Size = new System.Drawing.Size(93, 20); + this.labelDocumentCount.Size = new System.Drawing.Size(75, 15); this.labelDocumentCount.TabIndex = 12; this.labelDocumentCount.Text = "Количество:"; // // labelDocument // this.labelDocument.AutoSize = true; - this.labelDocument.Location = new System.Drawing.Point(9, 68); + this.labelDocument.Location = new System.Drawing.Point(8, 51); this.labelDocument.Name = "labelDocument"; - this.labelDocument.Size = new System.Drawing.Size(97, 20); + this.labelDocument.Size = new System.Drawing.Size(77, 15); this.labelDocument.TabIndex = 11; this.labelDocument.Text = "Мороженое:"; // // labelShop // this.labelShop.AutoSize = true; - this.labelShop.Location = new System.Drawing.Point(9, 15); + this.labelShop.Location = new System.Drawing.Point(8, 11); this.labelShop.Name = "labelShop"; this.labelShop.RightToLeft = System.Windows.Forms.RightToLeft.No; - this.labelShop.Size = new System.Drawing.Size(72, 20); + this.labelShop.Size = new System.Drawing.Size(57, 15); this.labelShop.TabIndex = 10; this.labelShop.Text = "Магазин:"; // // FormShopSupply // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(405, 220); + this.ClientSize = new System.Drawing.Size(354, 165); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.textBoxCount); @@ -122,9 +127,8 @@ this.Controls.Add(this.labelDocumentCount); this.Controls.Add(this.labelDocument); this.Controls.Add(this.labelShop); - this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormShopSupply"; - this.Text = "FormShopSupply"; + this.Text = "Пополнение магазина"; this.Load += new System.EventHandler(this.FormShopSupply_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index a2443b2..1f3437f 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -54,6 +54,7 @@ namespace IceCreamShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs index 25ccc61..ca3539c 100644 --- a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs @@ -17,5 +17,6 @@ namespace IceCreamShopContracts.BindingModels public DateTime OpeningDate { get; set; } public Dictionary ShopIceCreams { get; set; } = new(); + public int IceCreamMaxCount { get; set; } } } diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index 364f97e..da5b7a5 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,7 @@ 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/IceCreamShopContracts/StoragesContracts/IShopStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs index 48fb1c9..339e31f 100644 --- a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs @@ -1,4 +1,5 @@ -using IceCreamShopContracts.BindingModels; +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.ViewModels; using System; @@ -17,5 +18,6 @@ namespace IceCreamShopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellIceCreams(IIceCreamModel iceCream, int count); } } diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs index 0347c63..f49e717 100644 --- a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs @@ -17,6 +17,8 @@ namespace IceCreamShopContracts.ViewModels public string Adress { get; set; } = string.Empty; [DisplayName("Дата открытия")] public DateTime OpeningDate { get; set; } + [DisplayName("Вместимость магазина")] + public int IceCreamMaxCount { get; set; } public Dictionary ShopIceCreams { get; set; } = new(); } diff --git a/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs index 402a3fd..ce884ab 100644 --- a/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs +++ b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs @@ -14,5 +14,6 @@ namespace AbstractIceCreamShopDataModels.Models String Adress { get; } DateTime OpeningDate { get; } Dictionary ShopIceCreams { get; } + public int IceCreamMaxCount { get; } } } diff --git a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs index 8be6b0f..2e662f9 100644 --- a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs +++ b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs @@ -1,10 +1,5 @@ using IceCreamShopFileImplement.Models; using System.Xml.Linq; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace IceCreamShopFileImplement { @@ -14,9 +9,11 @@ namespace IceCreamShopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string IceCreamFileName = "IceCream.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List IceCreams { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -28,11 +25,13 @@ namespace IceCreamShopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveIceCreams() => SaveData(IceCreams, IceCreamFileName, "IceCreams", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; IceCreams = LoadData(IceCreamFileName, "IceCream", x => IceCream.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..055159b --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,197 @@ +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopFileImplement.Models; + +namespace IceCreamShopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + return source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newStore = Shop.Create(model); + if (newStore == null) + { + return null; + } + source.Shops.Add(newStore); + source.SaveShops(); + return newStore.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var store = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (store == null) + { + return null; + } + store.Update(model); + source.SaveShops(); + 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 countStore = count; + + if (icecream == null) + { + return false; + } + + foreach (var shop in source.Shops) + { + foreach (var doc in shop.ShopIceCreams) + { + if (doc.Value.Item1.Id == icecream.Id) + { + count -= doc.Value.Item2; + } + if (count <= 0) + { + break; + } + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + + for (int i = 0; i < source.Shops.Count; i++) + { + var shop = source.Shops[i]; + var icecreams = shop.ShopIceCreams; + + for (int j = 1; j < icecreams.Count + 1; j++) + { + 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); + } + } + + 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; + + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + IceCreamMaxCount = shop.IceCreamMaxCount, + ShopIceCreams = icecreams + }); + source.SaveShops(); + } + + if (count > 0) + { + return false; + } + return true; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs index e72613b..4d44e16 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/IceCream.cs @@ -83,6 +83,6 @@ namespace IceCreamShopFileImplement.Models new XElement("IceCreamName", IceCreamName), new XElement("Price", Price.ToString()), new XElement("IceCreamComponents", Components.Select(x => - new XElement("IceCreamComponents", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + new XElement("IceCreamComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..3aa8d13 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs @@ -0,0 +1,115 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; +using System.Xml.Linq; + + +namespace IceCreamShopFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string Name { get; private set; } = string.Empty; + + public string Adress { get; private set; } = string.Empty; + + public DateTime OpeningDate { get; private set; } + + public int IceCreamMaxCount { get; private set; } + + public Dictionary IceCreams { get; private set; } = new(); + + public Dictionary? _shopIceCreams = null; + + public Dictionary ShopIceCreams + { + get + { + if(_shopIceCreams == null) + { + var source = DataFileSingleton.GetInstance(); + _shopIceCreams = IceCreams.ToDictionary( + x => x.Key, + y => ((source.IceCreams.FirstOrDefault(z => z.Id == y.Key) as IIceCreamModel)!, y.Value)); + } + return _shopIceCreams; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Adress = model.Adress, + IceCreamMaxCount = model.IceCreamMaxCount, + OpeningDate = model.OpeningDate, + IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Adress = element.Element("Adress")!.Value, + IceCreamMaxCount = Convert.ToInt32(element.Element("IceCreamMaxCount")!.Value), + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + IceCreams = element.Element("ShopIceCreams")!.Elements("ShopIceCream").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + IceCreamMaxCount = model.IceCreamMaxCount; + if(model.ShopIceCreams.Count > 0) + { + IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopIceCreams = null; + } + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Adress = Adress, + OpeningDate = OpeningDate, + IceCreamMaxCount = IceCreamMaxCount, + ShopIceCreams = ShopIceCreams + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("Name", Name), + new XElement("Adress", Adress), + new XElement("OpeningDate", OpeningDate), + new XElement("IceCreamMaxCount", IceCreamMaxCount), + new XElement("ShopIceCreams", IceCreams + .Select(x => new XElement("ShopIceCream", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs index c25a24c..7df89b0 100644 --- a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs @@ -1,4 +1,5 @@ -using IceCreamShopContracts.BindingModels; +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; @@ -114,5 +115,10 @@ namespace IceCreamShopListImplement.Implements } return null; } + + public bool SellIceCreams(IIceCreamModel iceCream, int count) + { + throw new NotImplementedException(); + } } } diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs index 78d98e9..5cd4ad1 100644 --- a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs @@ -57,5 +57,7 @@ namespace IceCreamShopListImplement.Models OpeningDate = OpeningDate, ShopIceCreams = ShopIceCreams }; + + public int IceCreamMaxCount => throw new NotImplementedException(); } }