лаба 2 почти полная

This commit is contained in:
parap 2023-04-20 11:47:09 +04:00
parent dbeb844986
commit 1e9c5a2ae1
13 changed files with 401 additions and 59 deletions

View File

@ -161,7 +161,11 @@ namespace Pizzeria
private void ButtonSell_Click(object sender, EventArgs e) 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) private void ButtonRestore_Click(object sender, EventArgs e)

View File

@ -28,84 +28,83 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.buttonCancel = new System.Windows.Forms.Button(); buttonCancel = new Button();
this.buttonSave = new System.Windows.Forms.Button(); buttonSave = new Button();
this.textBoxCount = new System.Windows.Forms.TextBox(); textBoxCount = new TextBox();
this.label2 = new System.Windows.Forms.Label(); label2 = new Label();
this.label1 = new System.Windows.Forms.Label(); label1 = new Label();
this.comboBoxComponent = new System.Windows.Forms.ComboBox(); comboBoxComponent = new ComboBox();
this.SuspendLayout(); SuspendLayout();
// //
// buttonCancel // buttonCancel
// //
this.buttonCancel.Location = new System.Drawing.Point(249, 101); buttonCancel.Location = new Point(249, 101);
this.buttonCancel.Name = "buttonCancel"; buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(94, 29); buttonCancel.Size = new Size(94, 29);
this.buttonCancel.TabIndex = 11; buttonCancel.TabIndex = 11;
this.buttonCancel.Text = "Отмена"; buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true; buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); buttonCancel.Click += ButtonCancel_Click;
// //
// buttonSave // buttonSave
// //
this.buttonSave.Location = new System.Drawing.Point(135, 101); buttonSave.Location = new Point(135, 101);
this.buttonSave.Name = "buttonSave"; buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(94, 29); buttonSave.Size = new Size(94, 29);
this.buttonSave.TabIndex = 10; buttonSave.TabIndex = 10;
this.buttonSave.Text = "Сохранить"; buttonSave.Text = "Сохранить";
this.buttonSave.UseVisualStyleBackColor = true; buttonSave.UseVisualStyleBackColor = true;
this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); buttonSave.Click += ButtonSave_Click;
// //
// textBoxCount // textBoxCount
// //
this.textBoxCount.Location = new System.Drawing.Point(118, 59); textBoxCount.Location = new Point(118, 59);
this.textBoxCount.Name = "textBoxCount"; textBoxCount.Name = "textBoxCount";
this.textBoxCount.Size = new System.Drawing.Size(225, 27); textBoxCount.Size = new Size(225, 27);
this.textBoxCount.TabIndex = 9; textBoxCount.TabIndex = 9;
// //
// label2 // label2
// //
this.label2.AutoSize = true; label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(23, 66); label2.Location = new Point(23, 66);
this.label2.Name = "label2"; label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(93, 20); label2.Size = new Size(93, 20);
this.label2.TabIndex = 7; label2.TabIndex = 7;
this.label2.Text = "Количество:"; label2.Text = "Количество:";
// //
// label1 // label1
// //
this.label1.AutoSize = true; label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(23, 33); label1.Location = new Point(23, 33);
this.label1.Name = "label1"; label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 20); label1.Size = new Size(91, 20);
this.label1.TabIndex = 6; label1.TabIndex = 6;
this.label1.Text = "Компонент:"; label1.Text = "Компонент:";
// //
// comboBoxComponent // comboBoxComponent
// //
this.comboBoxComponent.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; comboBoxComponent.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBoxComponent.FormattingEnabled = true; comboBoxComponent.FormattingEnabled = true;
this.comboBoxComponent.Location = new System.Drawing.Point(120, 25); comboBoxComponent.Location = new Point(120, 25);
this.comboBoxComponent.Name = "comboBoxComponent"; comboBoxComponent.Name = "comboBoxComponent";
this.comboBoxComponent.Size = new System.Drawing.Size(224, 28); comboBoxComponent.Size = new Size(224, 28);
this.comboBoxComponent.TabIndex = 12; comboBoxComponent.TabIndex = 12;
// //
// FormPizzaComponent // FormPizzaComponent
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(370, 153); ClientSize = new Size(370, 153);
this.Controls.Add(this.comboBoxComponent); Controls.Add(comboBoxComponent);
this.Controls.Add(this.buttonCancel); Controls.Add(buttonCancel);
this.Controls.Add(this.buttonSave); Controls.Add(buttonSave);
this.Controls.Add(this.textBoxCount); Controls.Add(textBoxCount);
this.Controls.Add(this.label2); Controls.Add(label2);
this.Controls.Add(this.label1); Controls.Add(label1);
this.Name = "FormPizzaComponent"; Name = "FormPizzaComponent";
this.Text = "Компонент пиццы"; Text = "Компонент пиццы";
this.ResumeLayout(false); ResumeLayout(false);
this.PerformLayout(); PerformLayout();
} }
#endregion #endregion

View File

@ -0,0 +1,118 @@
namespace Pizzeria
{
partial class FormSellPizza
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

View File

@ -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<PizzaViewModel>? _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<FormSellPizza> 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();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -37,6 +37,7 @@ namespace Pizzeria
textBoxName.Text = view.ShopName; textBoxName.Text = view.ShopName;
textBoxAddres.Text = view.Addres.ToString(); textBoxAddres.Text = view.Addres.ToString();
dateTimePicker.Text = view.OpenTime.ToString(); dateTimePicker.Text = view.OpenTime.ToString();
textBoxCapacity.Text = view.Capacity.ToString();
_shopPizzas = view.ShopPizzas ?? new Dictionary<int, (IPizzaModel, int)>(); _shopPizzas = view.ShopPizzas ?? new Dictionary<int, (IPizzaModel, int)>();
LoadData(); LoadData();
} }

View File

@ -53,6 +53,7 @@ namespace Pizzeria
services.AddTransient<FormShopPizza>(); services.AddTransient<FormShopPizza>();
services.AddTransient<FormShops>(); services.AddTransient<FormShops>();
services.AddTransient<FormPizzaComponent>(); services.AddTransient<FormPizzaComponent>();
services.AddTransient<FormSellPizza>();
} }
} }
} }

View File

@ -121,9 +121,17 @@ namespace PizzeriaBusinessLogic
if(model.ShopPizzas.TryGetValue(pizza.Id, out var pair)) 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); model.ShopPizzas[pizza.Id] = (pair.Item1, pair.Item2 + count);
} else } else
{ {
if (model.Capacity < count)
{
throw new ArgumentException("Количество добавляемого изделия слишком велико");
}
model.ShopPizzas.Add(pizza.Id, (pizza, count)); model.ShopPizzas.Add(pizza.Id, (pizza, count));
} }
@ -131,11 +139,24 @@ namespace PizzeriaBusinessLogic
{ {
Id = model.Id, Id = model.Id,
ShopName = model.ShopName, ShopName = model.ShopName,
OpenTime = model.OpenTime,
Addres = model.Addres, Addres = model.Addres,
ShopPizzas = model.ShopPizzas, ShopPizzas = model.ShopPizzas,
}); });
return true; return true;
} }
public bool sellPizza(IPizzaModel pizza, int count)
{
bool result = _shopStorage.TrySellPizza(pizza, count);
if(!result)
{
throw new ArgumentException("Количество продаваемого изделия слишком велико");
}
return result;
}
} }
} }

View File

@ -10,6 +10,7 @@ namespace PizzeriaContracts.BusinessLogicsContracts
List<ShopViewModel>? ReadList(ShopSearchModel? model); List<ShopViewModel>? ReadList(ShopSearchModel? model);
ShopViewModel? ReadElement(ShopSearchModel model); ShopViewModel? ReadElement(ShopSearchModel model);
bool AddPizza(ShopSearchModel shop, IPizzaModel pizza, int count); bool AddPizza(ShopSearchModel shop, IPizzaModel pizza, int count);
bool sellPizza(IPizzaModel pizza, int count);
bool Create(ShopBindingModel model); bool Create(ShopBindingModel model);
bool Update(ShopBindingModel model); bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model); bool Delete(ShopBindingModel model);

View File

@ -1,6 +1,7 @@
using PizzeriaContracts.BindingModels; using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels; using PizzeriaContracts.SearchModels;
using PizzeriaContracts.ViewModels; using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
namespace PizzeriaContracts.StoragesContracts namespace PizzeriaContracts.StoragesContracts
{ {
@ -9,6 +10,7 @@ namespace PizzeriaContracts.StoragesContracts
List<ShopViewModel> GetFullList(); List<ShopViewModel> GetFullList();
List<ShopViewModel> GetFilteredList(ShopSearchModel model); List<ShopViewModel> GetFilteredList(ShopSearchModel model);
ShopViewModel? GetElement(ShopSearchModel model); ShopViewModel? GetElement(ShopSearchModel model);
bool TrySellPizza(IPizzaModel pizza, int count);
ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model);

View File

@ -10,6 +10,8 @@ namespace PizzeriaListImplement.Models
public string ShopName { get; private set; } = string.Empty; public string ShopName { get; private set; } = string.Empty;
public string Addres { get; private set; } = string.Empty; public string Addres { get; private set; } = string.Empty;
public DateTime OpenTime { get; private set; } = DateTime.Now; public DateTime OpenTime { get; private set; } = DateTime.Now;
public int Capacity { get; private set; }
public Dictionary<int, (IPizzaModel, int)> ShopPizzas public Dictionary<int, (IPizzaModel, int)> ShopPizzas
{ {
@ -29,6 +31,7 @@ namespace PizzeriaListImplement.Models
ShopName = model.ShopName, ShopName = model.ShopName,
Addres = model.Addres, Addres = model.Addres,
OpenTime = model.OpenTime, OpenTime = model.OpenTime,
Capacity = model.Capacity,
ShopPizzas = model.ShopPizzas ShopPizzas = model.ShopPizzas
}; };
} }
@ -49,8 +52,10 @@ namespace PizzeriaListImplement.Models
ShopName = ShopName, ShopName = ShopName,
Addres = Addres, Addres = Addres,
OpenTime = OpenTime, OpenTime = OpenTime,
Capacity = Capacity,
ShopPizzas = ShopPizzas ShopPizzas = ShopPizzas
}; };
} }
} }

View File

@ -2,6 +2,7 @@
using PizzeriaContracts.SearchModels; using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts; using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels; using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
using PizzeriaFileImplement.models; using PizzeriaFileImplement.models;
namespace PizzeriaFileImplement.Implements namespace PizzeriaFileImplement.Implements
@ -41,6 +42,42 @@ namespace PizzeriaFileImplement.Implements
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel; (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) public ShopViewModel? Insert(ShopBindingModel model)
{ {
model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1;

View File

@ -22,7 +22,7 @@ namespace PizzeriaFileImplement.models
{ {
var source = DataFileSingleton.GetInstance(); var source = DataFileSingleton.GetInstance();
_shopPizzas = Pizzas.ToDictionary(x => x.Key, y => _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)); y.Value));
} }
return _shopPizzas; return _shopPizzas;