From 1e9c5a2ae12fbc8b01f7ed05753dc08a9ccc3c19 Mon Sep 17 00:00:00 2001 From: parap Date: Thu, 20 Apr 2023 11:47:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=202=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=87=D1=82=D0=B8=20=D0=BF=D0=BE=D0=BB=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pizzeria/Pizzeria/FormMain.cs | 6 +- .../Pizzeria/FormPizzaComponent.Designer.cs | 113 +++++++++-------- Pizzeria/Pizzeria/FormSellPizza.Designer.cs | 118 ++++++++++++++++++ Pizzeria/Pizzeria/FormSellPizza.cs | 93 ++++++++++++++ Pizzeria/Pizzeria/FormSellPizza.resx | 60 +++++++++ Pizzeria/Pizzeria/FormShop.cs | 1 + Pizzeria/Pizzeria/Program.cs | 1 + Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs | 21 ++++ .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../StoragesContracts/IShopStorage.cs | 2 + Pizzeria/PizzeriaListImplement/Models/Shop.cs | 5 + .../Implements/ShopStorage.cs | 37 ++++++ .../PizzeriaShopFileImplement/models/Shop.cs | 2 +- 13 files changed, 401 insertions(+), 59 deletions(-) create mode 100644 Pizzeria/Pizzeria/FormSellPizza.Designer.cs create mode 100644 Pizzeria/Pizzeria/FormSellPizza.cs create mode 100644 Pizzeria/Pizzeria/FormSellPizza.resx diff --git a/Pizzeria/Pizzeria/FormMain.cs b/Pizzeria/Pizzeria/FormMain.cs index f48dbbb..686b13a 100644 --- a/Pizzeria/Pizzeria/FormMain.cs +++ b/Pizzeria/Pizzeria/FormMain.cs @@ -161,7 +161,11 @@ namespace Pizzeria private void ButtonSell_Click(object sender, EventArgs e) { - LoadData(); + var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza)); + if (service is FormSellPizza form) + { + form.ShowDialog(); + } } private void ButtonRestore_Click(object sender, EventArgs e) diff --git a/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs b/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs index 9d5ab54..f30f5a8 100644 --- a/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs +++ b/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs @@ -28,84 +28,83 @@ /// private void InitializeComponent() { - this.buttonCancel = new System.Windows.Forms.Button(); - this.buttonSave = new System.Windows.Forms.Button(); - this.textBoxCount = new System.Windows.Forms.TextBox(); - this.label2 = new System.Windows.Forms.Label(); - this.label1 = new System.Windows.Forms.Label(); - this.comboBoxComponent = new System.Windows.Forms.ComboBox(); - this.SuspendLayout(); + buttonCancel = new Button(); + buttonSave = new Button(); + textBoxCount = new TextBox(); + label2 = new Label(); + label1 = new Label(); + comboBoxComponent = new ComboBox(); + SuspendLayout(); // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(249, 101); - this.buttonCancel.Name = "buttonCancel"; - this.buttonCancel.Size = new System.Drawing.Size(94, 29); - this.buttonCancel.TabIndex = 11; - this.buttonCancel.Text = "Отмена"; - this.buttonCancel.UseVisualStyleBackColor = true; - this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + buttonCancel.Location = new Point(249, 101); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 11; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(135, 101); - this.buttonSave.Name = "buttonSave"; - this.buttonSave.Size = new System.Drawing.Size(94, 29); - this.buttonSave.TabIndex = 10; - this.buttonSave.Text = "Сохранить"; - this.buttonSave.UseVisualStyleBackColor = true; - this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + buttonSave.Location = new Point(135, 101); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(94, 29); + buttonSave.TabIndex = 10; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; // // textBoxCount // - this.textBoxCount.Location = new System.Drawing.Point(118, 59); - this.textBoxCount.Name = "textBoxCount"; - this.textBoxCount.Size = new System.Drawing.Size(225, 27); - this.textBoxCount.TabIndex = 9; + textBoxCount.Location = new Point(118, 59); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(225, 27); + textBoxCount.TabIndex = 9; // // label2 // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(23, 66); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(93, 20); - this.label2.TabIndex = 7; - this.label2.Text = "Количество:"; + label2.AutoSize = true; + label2.Location = new Point(23, 66); + label2.Name = "label2"; + label2.Size = new Size(93, 20); + label2.TabIndex = 7; + label2.Text = "Количество:"; // // label1 // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(23, 33); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(91, 20); - this.label1.TabIndex = 6; - this.label1.Text = "Компонент:"; + label1.AutoSize = true; + label1.Location = new Point(23, 33); + label1.Name = "label1"; + label1.Size = new Size(91, 20); + label1.TabIndex = 6; + label1.Text = "Компонент:"; // // comboBoxComponent // - this.comboBoxComponent.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxComponent.FormattingEnabled = true; - this.comboBoxComponent.Location = new System.Drawing.Point(120, 25); - this.comboBoxComponent.Name = "comboBoxComponent"; - this.comboBoxComponent.Size = new System.Drawing.Size(224, 28); - this.comboBoxComponent.TabIndex = 12; + comboBoxComponent.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxComponent.FormattingEnabled = true; + comboBoxComponent.Location = new Point(120, 25); + comboBoxComponent.Name = "comboBoxComponent"; + comboBoxComponent.Size = new Size(224, 28); + comboBoxComponent.TabIndex = 12; // // FormPizzaComponent // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(370, 153); - this.Controls.Add(this.comboBoxComponent); - this.Controls.Add(this.buttonCancel); - this.Controls.Add(this.buttonSave); - this.Controls.Add(this.textBoxCount); - this.Controls.Add(this.label2); - this.Controls.Add(this.label1); - this.Name = "FormPizzaComponent"; - this.Text = "Компонент пиццы"; - this.ResumeLayout(false); - this.PerformLayout(); - + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(370, 153); + Controls.Add(comboBoxComponent); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxCount); + Controls.Add(label2); + Controls.Add(label1); + Name = "FormPizzaComponent"; + Text = "Компонент пиццы"; + ResumeLayout(false); + PerformLayout(); } #endregion diff --git a/Pizzeria/Pizzeria/FormSellPizza.Designer.cs b/Pizzeria/Pizzeria/FormSellPizza.Designer.cs new file mode 100644 index 0000000..53a2aa2 --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.Designer.cs @@ -0,0 +1,118 @@ +namespace Pizzeria +{ + partial class FormSellPizza + { + /// + /// 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() + { + comboBoxPizza = new ComboBox(); + buttonCancel = new Button(); + buttonSell = new Button(); + textBoxCount = new TextBox(); + label2 = new Label(); + label1 = new Label(); + SuspendLayout(); + // + // comboBoxPizza + // + comboBoxPizza.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxPizza.FormattingEnabled = true; + comboBoxPizza.Location = new Point(113, 25); + comboBoxPizza.Name = "comboBoxPizza"; + comboBoxPizza.Size = new Size(224, 28); + comboBoxPizza.TabIndex = 18; + // + // buttonCancel + // + buttonCancel.Location = new Point(242, 101); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 17; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + // + // buttonSell + // + buttonSell.Location = new Point(128, 101); + buttonSell.Name = "buttonSell"; + buttonSell.Size = new Size(94, 29); + buttonSell.TabIndex = 16; + buttonSell.Text = "Продать"; + buttonSell.UseVisualStyleBackColor = true; + buttonSell.Click += buttonSell_Click; + // + // textBoxCount + // + textBoxCount.Location = new Point(111, 59); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(225, 27); + textBoxCount.TabIndex = 15; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(16, 66); + label2.Name = "label2"; + label2.Size = new Size(93, 20); + label2.TabIndex = 14; + label2.Text = "Количество:"; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(16, 33); + label1.Name = "label1"; + label1.Size = new Size(55, 20); + label1.TabIndex = 13; + label1.Text = "Пицца"; + // + // FormSellPizza + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(352, 145); + Controls.Add(comboBoxPizza); + Controls.Add(buttonCancel); + Controls.Add(buttonSell); + Controls.Add(textBoxCount); + Controls.Add(label2); + Controls.Add(label1); + Name = "FormSellPizza"; + Text = "Продажа"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox comboBoxPizza; + private Button buttonCancel; + private Button buttonSell; + private TextBox textBoxCount; + private Label label2; + private Label label1; + } +} \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormSellPizza.cs b/Pizzeria/Pizzeria/FormSellPizza.cs new file mode 100644 index 0000000..ed79b48 --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.cs @@ -0,0 +1,93 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; + +namespace Pizzeria +{ + public partial class FormSellPizza : Form + { + private readonly List? _list; + private readonly IShopLogic _shopLogic; + private readonly ILogger _logger; + + public int Id + { + get + { + return Convert.ToInt32(comboBoxPizza.SelectedValue); + } + set + { + comboBoxPizza.SelectedValue = value; + } + } + public IPizzaModel? PizzaModel + { + get + { + if (_list == null) + { + return null; + } + foreach (var elem in _list) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + public FormSellPizza(ILogger logger, IPizzaLogic pizzaLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _logger = logger; + _list = pizzaLogic.ReadList(null); + if (_list != null) + { + comboBoxPizza.DisplayMember = "PizzaName"; + comboBoxPizza.ValueMember = "Id"; + comboBoxPizza.DataSource = _list; + comboBoxPizza.SelectedItem = null; + } + _shopLogic = shopLogic; + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxPizza.SelectedValue == null) + { + MessageBox.Show("Выберите пиццу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + try + { + _shopLogic.sellPizza(PizzaModel, Convert.ToInt32(textBoxCount.Text)); + MessageBox.Show("Продано успешно!"); + } + catch (ArgumentException ex) + { + MessageBox.Show("Ошибка продажи. Слишком большое количество"); + _logger.LogWarning(ex, "Ошибка продажи"); + return; + } catch(Exception err) + { + MessageBox.Show("Ошибка продажи."); + _logger.LogError(err, "Ошибка продажи"); + return; + } + + DialogResult = DialogResult.OK; + Close(); + } + } +} diff --git a/Pizzeria/Pizzeria/FormSellPizza.resx b/Pizzeria/Pizzeria/FormSellPizza.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.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/Pizzeria/Pizzeria/FormShop.cs b/Pizzeria/Pizzeria/FormShop.cs index 9d0875f..e12d2ed 100644 --- a/Pizzeria/Pizzeria/FormShop.cs +++ b/Pizzeria/Pizzeria/FormShop.cs @@ -37,6 +37,7 @@ namespace Pizzeria textBoxName.Text = view.ShopName; textBoxAddres.Text = view.Addres.ToString(); dateTimePicker.Text = view.OpenTime.ToString(); + textBoxCapacity.Text = view.Capacity.ToString(); _shopPizzas = view.ShopPizzas ?? new Dictionary(); LoadData(); } diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index 7c53e58..36064ec 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -53,6 +53,7 @@ namespace Pizzeria services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs index 33acfb6..52c74e0 100644 --- a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs @@ -121,9 +121,17 @@ namespace PizzeriaBusinessLogic if(model.ShopPizzas.TryGetValue(pizza.Id, out var pair)) { + if(model.Capacity - pair.Item2 < count) + { + throw new ArgumentException("Количество добавляемого изделия слишком велико"); + } model.ShopPizzas[pizza.Id] = (pair.Item1, pair.Item2 + count); } else { + if (model.Capacity < count) + { + throw new ArgumentException("Количество добавляемого изделия слишком велико"); + } model.ShopPizzas.Add(pizza.Id, (pizza, count)); } @@ -131,11 +139,24 @@ namespace PizzeriaBusinessLogic { Id = model.Id, ShopName = model.ShopName, + OpenTime = model.OpenTime, Addres = model.Addres, ShopPizzas = model.ShopPizzas, }); return true; } + + public bool sellPizza(IPizzaModel pizza, int count) + { + bool result = _shopStorage.TrySellPizza(pizza, count); + + if(!result) + { + throw new ArgumentException("Количество продаваемого изделия слишком велико"); + } + + return result; + } } } diff --git a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs index d26bdf8..d3d4ae5 100644 --- a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs @@ -10,6 +10,7 @@ namespace PizzeriaContracts.BusinessLogicsContracts List? ReadList(ShopSearchModel? model); ShopViewModel? ReadElement(ShopSearchModel model); bool AddPizza(ShopSearchModel shop, IPizzaModel pizza, int count); + bool sellPizza(IPizzaModel pizza, int count); bool Create(ShopBindingModel model); bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs index 99cdc9c..ee44e2d 100644 --- a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using PizzeriaContracts.BindingModels; using PizzeriaContracts.SearchModels; using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; namespace PizzeriaContracts.StoragesContracts { @@ -9,6 +10,7 @@ namespace PizzeriaContracts.StoragesContracts List GetFullList(); List GetFilteredList(ShopSearchModel model); ShopViewModel? GetElement(ShopSearchModel model); + bool TrySellPizza(IPizzaModel pizza, int count); ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); diff --git a/Pizzeria/PizzeriaListImplement/Models/Shop.cs b/Pizzeria/PizzeriaListImplement/Models/Shop.cs index 1195166..178b228 100644 --- a/Pizzeria/PizzeriaListImplement/Models/Shop.cs +++ b/Pizzeria/PizzeriaListImplement/Models/Shop.cs @@ -10,6 +10,8 @@ namespace PizzeriaListImplement.Models public string ShopName { get; private set; } = string.Empty; public string Addres { get; private set; } = string.Empty; public DateTime OpenTime { get; private set; } = DateTime.Now; + public int Capacity { get; private set; } + public Dictionary ShopPizzas { @@ -29,6 +31,7 @@ namespace PizzeriaListImplement.Models ShopName = model.ShopName, Addres = model.Addres, OpenTime = model.OpenTime, + Capacity = model.Capacity, ShopPizzas = model.ShopPizzas }; } @@ -49,8 +52,10 @@ namespace PizzeriaListImplement.Models ShopName = ShopName, Addres = Addres, OpenTime = OpenTime, + Capacity = Capacity, ShopPizzas = ShopPizzas }; + } } diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs index 04f9a85..d5ab7d3 100644 --- a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using PizzeriaContracts.SearchModels; using PizzeriaContracts.StoragesContracts; using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; using PizzeriaFileImplement.models; namespace PizzeriaFileImplement.Implements @@ -41,6 +42,42 @@ namespace PizzeriaFileImplement.Implements (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } + + public bool TrySellPizza(IPizzaModel pizza, int count) + { + int hasCount = 0; + + source.Shops.ForEach(x => + { + if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair)) + { + hasCount += pair.Item2; + } + }); + + if (hasCount < count) return false; + + source.Shops.ForEach(x => + { + if (count < 0) return; + if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair)) + { + if(count >= pair.Item2) + { + count -= pair.Item2; + x.ShopPizzas[pizza.Id] = (pizza, 0); + } else + { + x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 - count); + count = 0; + } + } + }); + + source.SaveShops(); + return true; + } + public ShopViewModel? Insert(ShopBindingModel model) { model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs index 9af508f..fb23761 100644 --- a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs +++ b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs @@ -22,7 +22,7 @@ namespace PizzeriaFileImplement.models { var source = DataFileSingleton.GetInstance(); _shopPizzas = Pizzas.ToDictionary(x => x.Key, y => - ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!, + ((source.Pizzas.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!, y.Value)); } return _shopPizzas;