Tsukanova_I.V. LabWork02_hard #7

Closed
Inohara wants to merge 8 commits from LabWork02_hard into LabWork01_hard
23 changed files with 843 additions and 100 deletions
Showing only changes of commit cff88300c0 - Show all commits

View File

@ -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<OrderLogic> logger)
public OrderLogic(IOrderStorage orderStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger<OrderLogic> 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;
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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();
}
}
}
}

View File

@ -0,0 +1,119 @@
namespace IceCreamShopView
{
partial class FormSellIceCream
{
/// <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()
{
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;
}
}

View File

@ -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<FormSellIceCream> 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();
}
}
}

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

@ -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;
}
}

View File

@ -47,6 +47,7 @@ namespace IceCreamShopView
textBoxAddress.Text = view.Adress;
dateTimePicker.Text = view.OpeningDate.ToString();
_shopIceCreams = view.ShopIceCreams ?? new Dictionary<int, (IIceCreamModel, int)>();
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)

View File

@ -63,6 +63,9 @@
<metadata name="ColumnDocumentName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="PriceIceCream.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnCount.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>

View File

@ -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();

View File

@ -54,6 +54,7 @@ namespace IceCreamShop
services.AddTransient<FormShop>();
services.AddTransient<FormShops>();
services.AddTransient<FormShopSupply>();
services.AddTransient<FormSellIceCream>();
}
}
}

View File

@ -17,5 +17,6 @@ namespace IceCreamShopContracts.BindingModels
public DateTime OpeningDate { get; set; }
public Dictionary<int, (IIceCreamModel, int)> ShopIceCreams { get; set; } = new();
public int IceCreamMaxCount { get; set; }
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<int, (IIceCreamModel, int)> ShopIceCreams { get; set; } = new();
}

View File

@ -14,5 +14,6 @@ namespace AbstractIceCreamShopDataModels.Models
String Adress { get; }
DateTime OpeningDate { get; }
Dictionary<int, (IIceCreamModel, int)> ShopIceCreams { get; }
public int IceCreamMaxCount { get; }
}
}

View File

@ -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<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<IceCream> IceCreams { get; private set; }
public List<Shop> 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<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{

View File

@ -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<ShopViewModel> 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<ShopViewModel> 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;
Review

Значение можно получить через LINQ-запрос

Значение можно получить через LINQ-запрос
}
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;
}
}
}

View File

@ -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()));
}
}

View File

@ -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<int, int> IceCreams { get; private set; } = new();
public Dictionary<int, (IIceCreamModel, int)>? _shopIceCreams = null;
public Dictionary<int, (IIceCreamModel, int)> 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()));
}
}

View File

@ -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();
}
}
}

View File

@ -57,5 +57,7 @@ namespace IceCreamShopListImplement.Models
OpeningDate = OpeningDate,
ShopIceCreams = ShopIceCreams
};
public int IceCreamMaxCount => throw new NotImplementedException();
}
}