From 97b1187f1bea7a11c3f9e4e7ec8b7de8a99deb70 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Sun, 14 Apr 2024 22:07:50 +0400 Subject: [PATCH 1/2] In process --- .../BusinessLogics/OrderLogic.cs | 93 +++++++++++++- .../BusinessLogics/ShopLogic.cs | 15 +++ .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../SearchModels/SupplySearchModel.cs | 14 ++ .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 2 + AbstractShopDataModels/Models/IShopModel.cs | 1 + Diner/Diner/FormMain.Designer.cs | 27 ++-- Diner/Diner/FormMain.cs | 8 ++ Diner/Diner/FormSell.Designer.cs | 117 +++++++++++++++++ Diner/Diner/FormSell.cs | 117 +++++++++++++++++ Diner/Diner/FormSell.resx | 120 ++++++++++++++++++ Diner/Diner/FormSellSnack.Designer.cs | 119 +++++++++++++++++ Diner/Diner/FormSellSnack.cs | 83 ++++++++++++ Diner/Diner/FormSellSnack.resx | 120 ++++++++++++++++++ Diner/Diner/FormShop.Designer.cs | 65 +++++++--- Diner/Diner/FormShop.cs | 4 +- Diner/Diner/Program.cs | 2 +- 19 files changed, 878 insertions(+), 33 deletions(-) create mode 100644 AbstractShopContracts/SearchModels/SupplySearchModel.cs create mode 100644 Diner/Diner/FormSell.Designer.cs create mode 100644 Diner/Diner/FormSell.cs create mode 100644 Diner/Diner/FormSell.resx create mode 100644 Diner/Diner/FormSellSnack.Designer.cs create mode 100644 Diner/Diner/FormSellSnack.cs create mode 100644 Diner/Diner/FormSellSnack.resx diff --git a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs index 1466f90..005107e 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -3,6 +3,7 @@ using DinerContracts.BusinessLogicsContracts; using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; +using DinerShopDataModels.Models; using DinerDataModels.Enum; using Microsoft.Extensions.Logging; @@ -12,10 +13,18 @@ namespace DinerBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + + private readonly IShopStorage _shopStorage; + + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { - _logger = logger; _orderStorage = orderStorage; + _logger = logger; + _shopStorage = shopStorage; + } public List? ReadList(OrderSearchModel? model) { @@ -29,6 +38,7 @@ namespace DinerBusinessLogic.BusinessLogics _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } + public bool CreateOrder(OrderBindingModel model) { CheckModel(model); @@ -41,6 +51,64 @@ namespace DinerBusinessLogic.BusinessLogics } return true; } + public bool CheckSupply(IDinerModel snack, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. IceCream count < 0."); + return false; + } + + int sumCapacity = 0; + int sumCount = 0; + sumCapacity = _shopStorage.GetFullList().Select(x => x.MaxCapacity).Sum(); + sumCount = _shopStorage.GetFullList().Select(x => x.ShopIceCreams.Select(y => y.Value.Item2).Sum()).Sum(); + int freeSpace = sumCapacity - sumCount; + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCapacity; + foreach (var doc in shop.ShopIceCreams) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, count)) + count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, freeSpace)) + count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } public bool TakeOrderInWork(OrderBindingModel model) { return ChangeStatus(model, OrderStatus.Выполняется); @@ -53,6 +121,27 @@ namespace DinerBusinessLogic.BusinessLogics { return ChangeStatus(model, OrderStatus.Выдан); } + public bool DeliveryOrder(OrderBindingModel model) + { + var order = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id, + }); + if (order == null) + { + throw new ArgumentNullException(nameof(order)); + } + if (!_shopStorage.RestockingShops(new SupplyBindingModel + { + SnackId = order.SnackId, + Count = order.Count + })) + { + throw new ArgumentException("Недостаточно места"); + } + + return ChangeStatus(model, OrderStatus.Выдан); + } private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) diff --git a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs index 6f35a4c..ed9d705 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -157,5 +157,20 @@ namespace DinerBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + public bool Sale(SupplySearchModel model) + { + if (!model.PizzaId.HasValue || !model.Count.HasValue) + { + return false; + } + _logger.LogInformation("Check pizza count in all shops"); + if (_shopStorage.Sale(model)) + { + _logger.LogInformation("Selling sucsess"); + return true; + } + _logger.LogInformation("Selling failed"); + return false; + } } } diff --git a/AbstractShopContracts/BindingModels/ShopBindingModel.cs b/AbstractShopContracts/BindingModels/ShopBindingModel.cs index d4d5088..c347698 100644 --- a/AbstractShopContracts/BindingModels/ShopBindingModel.cs +++ b/AbstractShopContracts/BindingModels/ShopBindingModel.cs @@ -14,5 +14,6 @@ namespace DinerContracts.BindingModels public string Adress { get; set; } = string.Empty; public DateTime OpeningDate { get; set; } = DateTime.Now; public Dictionary ShopSnacks { get; set; } = new(); + public int SnackMaxCount { get; set; } } } \ No newline at end of file diff --git a/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs b/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs index 1d540ad..2adad26 100644 --- a/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -17,5 +17,6 @@ namespace DinerContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool MakeSupply(SupplyBindingModel model); + bool Sale(SupplySearchModel model); } } \ No newline at end of file diff --git a/AbstractShopContracts/SearchModels/SupplySearchModel.cs b/AbstractShopContracts/SearchModels/SupplySearchModel.cs new file mode 100644 index 0000000..67d40b6 --- /dev/null +++ b/AbstractShopContracts/SearchModels/SupplySearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerContracts.SearchModels +{ + public class SupplySearchModel + { + public int? SnackId { get; set; } + public int? Count { get; set; } + } +} diff --git a/AbstractShopContracts/StoragesContracts/IShopStorage.cs b/AbstractShopContracts/StoragesContracts/IShopStorage.cs index 9c62afa..c5d4ae2 100644 --- a/AbstractShopContracts/StoragesContracts/IShopStorage.cs +++ b/AbstractShopContracts/StoragesContracts/IShopStorage.cs @@ -17,5 +17,7 @@ namespace DinerContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool Sale(SupplySearchModel model); + bool RestockingShops(SupplyBindingModel model); } } \ No newline at end of file diff --git a/AbstractShopContracts/ViewModels/ShopViewModel.cs b/AbstractShopContracts/ViewModels/ShopViewModel.cs index 85331fc..7684fe8 100644 --- a/AbstractShopContracts/ViewModels/ShopViewModel.cs +++ b/AbstractShopContracts/ViewModels/ShopViewModel.cs @@ -18,5 +18,7 @@ namespace DinerContracts.ViewModels [DisplayName("Дата открытия")] public DateTime OpeningDate { get; set; } public Dictionary ShopSnacks { get; set; } = new(); + [DisplayName("Вместимость")] + public int SnackMaxCount { get; set; } } } \ No newline at end of file diff --git a/AbstractShopDataModels/Models/IShopModel.cs b/AbstractShopDataModels/Models/IShopModel.cs index e236e00..3e48cc7 100644 --- a/AbstractShopDataModels/Models/IShopModel.cs +++ b/AbstractShopDataModels/Models/IShopModel.cs @@ -14,5 +14,6 @@ namespace DinerDataModels.Models string Adress { get; } DateTime OpeningDate { get; } Dictionary ShopSnacks { get; } + public int SnackMaxCount { get; } } } \ No newline at end of file diff --git a/Diner/Diner/FormMain.Designer.cs b/Diner/Diner/FormMain.Designer.cs index 2f7fe59..8b18c18 100644 --- a/Diner/Diner/FormMain.Designer.cs +++ b/Diner/Diner/FormMain.Designer.cs @@ -38,9 +38,10 @@ toolStripLabel1 = new ToolStripDropDownButton(); componentsToolStripMenuItem = new ToolStripMenuItem(); snacksToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); toolStripDropDownButton1 = new ToolStripDropDownButton(); поставкаToolStripMenuItem = new ToolStripMenuItem(); - магазиныToolStripMenuItem = new ToolStripMenuItem(); + продажаToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); toolStrip1.SuspendLayout(); SuspendLayout(); @@ -126,21 +127,28 @@ // componentsToolStripMenuItem // componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; - componentsToolStripMenuItem.Size = new Size(224, 26); + componentsToolStripMenuItem.Size = new Size(182, 26); componentsToolStripMenuItem.Text = "Компоненты"; componentsToolStripMenuItem.Click += ComponentToolStripMenuItem_Click; // // snacksToolStripMenuItem // snacksToolStripMenuItem.Name = "snacksToolStripMenuItem"; - snacksToolStripMenuItem.Size = new Size(224, 26); + snacksToolStripMenuItem.Size = new Size(182, 26); snacksToolStripMenuItem.Text = "Закуски"; snacksToolStripMenuItem.Click += ProductToolStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(182, 26); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += shopsToolStripMenuItem_Click; + // // toolStripDropDownButton1 // toolStripDropDownButton1.DisplayStyle = ToolStripItemDisplayStyle.Text; - toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem }); + toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, продажаToolStripMenuItem }); toolStripDropDownButton1.ImageTransparentColor = Color.Magenta; toolStripDropDownButton1.Name = "toolStripDropDownButton1"; toolStripDropDownButton1.Size = new Size(95, 24); @@ -153,12 +161,12 @@ поставкаToolStripMenuItem.Text = "Поставка"; поставкаToolStripMenuItem.Click += transactionToolStripMenuItem_Click; // - // магазиныToolStripMenuItem + // продажаToolStripMenuItem // - магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(224, 26); - магазиныToolStripMenuItem.Text = "Магазины"; - магазиныToolStripMenuItem.Click += shopsToolStripMenuItem_Click; + продажаToolStripMenuItem.Name = "продажаToolStripMenuItem"; + продажаToolStripMenuItem.Size = new Size(224, 26); + продажаToolStripMenuItem.Text = "Продажа"; + продажаToolStripMenuItem.Click += SellToolStripMenuItem_Click; // // FormMain // @@ -197,5 +205,6 @@ private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripDropDownButton toolStripDropDownButton1; private ToolStripMenuItem поставкаToolStripMenuItem; + private ToolStripMenuItem продажаToolStripMenuItem; } } \ No newline at end of file diff --git a/Diner/Diner/FormMain.cs b/Diner/Diner/FormMain.cs index b7fe7b9..19c9fc2 100644 --- a/Diner/Diner/FormMain.cs +++ b/Diner/Diner/FormMain.cs @@ -180,5 +180,13 @@ namespace Diner form.ShowDialog(); } } + private void SellToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza)); + if (service is FormSellSnack form) + { + form.ShowDialog(); + } + } } } diff --git a/Diner/Diner/FormSell.Designer.cs b/Diner/Diner/FormSell.Designer.cs new file mode 100644 index 0000000..95509c4 --- /dev/null +++ b/Diner/Diner/FormSell.Designer.cs @@ -0,0 +1,117 @@ +namespace IceCreamShop +{ + partial class SellForm + { + /// + /// 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() + { + IceCreamComboBox = new ComboBox(); + IceCreamLabel = new Label(); + CountLabel = new Label(); + CountTextBox = new TextBox(); + SaveButton = new Button(); + CancelButton = new Button(); + SuspendLayout(); + // + // IceCreamComboBox + // + IceCreamComboBox.FormattingEnabled = true; + IceCreamComboBox.Location = new Point(99, 12); + IceCreamComboBox.Name = "IceCreamComboBox"; + IceCreamComboBox.Size = new Size(121, 23); + IceCreamComboBox.TabIndex = 0; + // + // IceCreamLabel + // + IceCreamLabel.AutoSize = true; + IceCreamLabel.Location = new Point(12, 20); + IceCreamLabel.Name = "IceCreamLabel"; + IceCreamLabel.Size = new Size(81, 15); + IceCreamLabel.TabIndex = 1; + IceCreamLabel.Text = "Мороженное"; + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(21, 49); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(72, 15); + CountLabel.TabIndex = 2; + CountLabel.Text = "Количество"; + // + // CountTextBox + // + CountTextBox.Location = new Point(99, 41); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(121, 23); + CountTextBox.TabIndex = 3; + // + // SaveButton + // + SaveButton.Location = new Point(64, 73); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 4; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(145, 73); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 5; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + // + // SellForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(224, 108); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(CountTextBox); + Controls.Add(CountLabel); + Controls.Add(IceCreamLabel); + Controls.Add(IceCreamComboBox); + Name = "SellForm"; + Text = "Форма продажи"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox IceCreamComboBox; + private Label IceCreamLabel; + private Label CountLabel; + private TextBox CountTextBox; + private Button SaveButton; + private Button CancelButton; + } +} \ No newline at end of file diff --git a/Diner/Diner/FormSell.cs b/Diner/Diner/FormSell.cs new file mode 100644 index 0000000..96143c6 --- /dev/null +++ b/Diner/Diner/FormSell.cs @@ -0,0 +1,117 @@ +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace IceCreamShop +{ + public partial class SellForm : Form + { + private readonly List? _iceCreamList; + IShopLogic _shopLogic; + IIceCreamLogic _iceCreamLogic; + public SellForm(IIceCreamLogic iceCreamLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _iceCreamLogic = iceCreamLogic; + _iceCreamList = iceCreamLogic.ReadList(null); + if (_iceCreamList != null) + { + IceCreamComboBox.DisplayMember = "IceCreamName"; + IceCreamComboBox.ValueMember = "Id"; + IceCreamComboBox.DataSource = _iceCreamList; + IceCreamComboBox.SelectedItem = null; + } + } + public int IceCreamId + { + get + { + return + Convert.ToInt32(IceCreamComboBox.SelectedValue); + } + set + { + IceCreamComboBox.SelectedValue = value; + } + } + + public IIceCreamModel? IceCreamModel + { + get + { + if (_iceCreamList == null) + { + return null; + } + foreach (var elem in _iceCreamList) + { + if (elem.Id == IceCreamId) + { + return elem; + } + } + return null; + } + } + + + public int Count + { + get { return Convert.ToInt32(CountTextBox.Text); } + set + { CountTextBox.Text = value.ToString(); } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (IceCreamComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите мороженное", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + try + { + int count = Convert.ToInt32(CountTextBox.Text); + + bool res = _shopLogic.MakeSell( + _iceCreamLogic.ReadElement(new() { Id = Convert.ToInt32(IceCreamComboBox.SelectedValue) }), + count + ); + + if (!res) + { + throw new Exception("Ошибка при продаже."); + } + + MessageBox.Show("Продажа прошла успешно"); + DialogResult = DialogResult.OK; + Close(); + + } + catch (Exception err) + { + MessageBox.Show("Ошибка продажи"); + return; + } + } + } +} diff --git a/Diner/Diner/FormSell.resx b/Diner/Diner/FormSell.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Diner/Diner/FormSell.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/Diner/Diner/FormSellSnack.Designer.cs b/Diner/Diner/FormSellSnack.Designer.cs new file mode 100644 index 0000000..36fbfeb --- /dev/null +++ b/Diner/Diner/FormSellSnack.Designer.cs @@ -0,0 +1,119 @@ +namespace PizzeriaView +{ + partial class FormSellSnack + { + /// + /// 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() + { + labelSnack = new Label(); + comboBoxSnack = new ComboBox(); + labelCount = new Label(); + textBoxCount = new TextBox(); + buttonSell = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // labelSnack + // + labelSnack.AutoSize = true; + labelSnack.Location = new Point(12, 14); + labelSnack.Name = "labelSnack"; + labelSnack.Size = new Size(68, 20); + labelSnack.TabIndex = 0; + labelSnack.Text = "Закуска: "; + // + // comboBoxSnack + // + comboBoxSnack.FormattingEnabled = true; + comboBoxSnack.Location = new Point(115, 11); + comboBoxSnack.Name = "comboBoxSnack"; + comboBoxSnack.Size = new Size(239, 28); + comboBoxSnack.TabIndex = 1; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(12, 55); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(97, 20); + labelCount.TabIndex = 2; + labelCount.Text = "Количество: "; + // + // textBoxCount + // + textBoxCount.Location = new Point(115, 52); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(239, 27); + textBoxCount.TabIndex = 3; + // + // buttonSell + // + buttonSell.Location = new Point(128, 99); + buttonSell.Name = "buttonSell"; + buttonSell.Size = new Size(94, 29); + buttonSell.TabIndex = 4; + buttonSell.Text = "Продать"; + buttonSell.UseVisualStyleBackColor = true; + buttonSell.Click += ButtonSell_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(242, 99); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 5; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormSellSnack + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(366, 140); + Controls.Add(buttonCancel); + Controls.Add(buttonSell); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxSnack); + Controls.Add(labelSnack); + Name = "FormSellSnack"; + Text = "Продажа пиццы"; + Load += FormSellingSnack_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelSnack; + private ComboBox comboBoxSnack; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Diner/Diner/FormSellSnack.cs b/Diner/Diner/FormSellSnack.cs new file mode 100644 index 0000000..b6070ae --- /dev/null +++ b/Diner/Diner/FormSellSnack.cs @@ -0,0 +1,83 @@ +using Microsoft.Extensions.Logging; +using DinerContracts.BusinessLogicsContracts; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace PizzeriaView +{ + public partial class FormSellSnack : Form + { + private readonly ILogger _logger; + private readonly ISnackLogic _logicP; + private readonly IShopLogic _logicS; + private List _snackList = new List(); + public FormSellSnack(ILogger logger, ISnackLogic logicP, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + private void FormSellingSnack_Load(object sender, EventArgs e) + { + _snackList = _logicP.ReadList(null); + if (_snackList != null) + { + comboBoxSnack.DisplayMember = "PizzaName"; + comboBoxSnack.ValueMember = "Id"; + comboBoxSnack.DataSource = _snackList; + comboBoxSnack.SelectedItem = null; + _logger.LogInformation("Загрузка закуски для продажи"); + } + } + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxSnack.SelectedValue == null) + { + MessageBox.Show("Выберите закуску", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание покупки"); + try + { + bool resout = _logicS.Sale(new SupplySearchModel + { + PizzaId = Convert.ToInt32(comboBoxSnack.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text) + }); + if (resout) + { + _logger.LogInformation("Проверка пройдена, продажа проведена"); + MessageBox.Show("Продажа проведена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + else { + _logger.LogInformation("Проверка не пройдена"); + MessageBox.Show("Продажа не может быть создана.", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + catch(Exception ex) + { + _logger.LogError(ex, "Ошибка создания покупки"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Diner/Diner/FormSellSnack.resx b/Diner/Diner/FormSellSnack.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Diner/Diner/FormSellSnack.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/Diner/Diner/FormShop.Designer.cs b/Diner/Diner/FormShop.Designer.cs index ee461b3..f7a0154 100644 --- a/Diner/Diner/FormShop.Designer.cs +++ b/Diner/Diner/FormShop.Designer.cs @@ -35,12 +35,15 @@ buttonCancel = new Button(); buttonSave = new Button(); dataGridView = new DataGridView(); - label1 = new Label(); - dateTimeOpen = new DateTimePicker(); id = new DataGridViewTextBoxColumn(); DinerName = new DataGridViewTextBoxColumn(); Count = new DataGridViewTextBoxColumn(); + label1 = new Label(); + dateTimeOpen = new DateTimePicker(); + label2 = new Label(); + numericUpSnackMaxCount = new NumericUpDown(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpSnackMaxCount).BeginInit(); SuspendLayout(); // // labelName @@ -102,31 +105,15 @@ dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.Columns.AddRange(new DataGridViewColumn[] { id, DinerName, Count }); - dataGridView.Location = new Point(12, 144); + dataGridView.Location = new Point(12, 197); dataGridView.Name = "dataGridView"; dataGridView.ReadOnly = true; dataGridView.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders; dataGridView.RowTemplate.Height = 29; - dataGridView.Size = new Size(569, 307); + dataGridView.Size = new Size(569, 254); dataGridView.TabIndex = 7; // - // label1 - // - label1.AutoSize = true; - label1.Location = new Point(12, 103); - label1.Name = "label1"; - label1.Size = new Size(110, 20); - label1.TabIndex = 8; - label1.Text = "Дата открытия"; - // - // dateTimeOpen - // - dateTimeOpen.Location = new Point(128, 103); - dateTimeOpen.Name = "dateTimeOpen"; - dateTimeOpen.Size = new Size(401, 27); - dateTimeOpen.TabIndex = 9; - // // id // id.HeaderText = "id"; @@ -149,11 +136,46 @@ Count.Name = "Count"; Count.ReadOnly = true; // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(12, 103); + label1.Name = "label1"; + label1.Size = new Size(110, 20); + label1.TabIndex = 8; + label1.Text = "Дата открытия"; + // + // dateTimeOpen + // + dateTimeOpen.Location = new Point(128, 103); + dateTimeOpen.Name = "dateTimeOpen"; + dateTimeOpen.Size = new Size(401, 27); + dateTimeOpen.TabIndex = 9; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(12, 156); + label2.Name = "label2"; + label2.Size = new Size(107, 20); + label2.TabIndex = 10; + label2.Text = "Вместимость: "; + // + // numericUpSnackMaxCount + // + numericUpSnackMaxCount.Location = new Point(128, 154); + numericUpSnackMaxCount.Maximum = new decimal(new int[] { 10000, 0, 0, 0 }); + numericUpSnackMaxCount.Name = "numericUpSnackMaxCount"; + numericUpSnackMaxCount.Size = new Size(401, 27); + numericUpSnackMaxCount.TabIndex = 12; + // // FormShop // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(593, 513); + Controls.Add(numericUpSnackMaxCount); + Controls.Add(label2); Controls.Add(dateTimeOpen); Controls.Add(label1); Controls.Add(dataGridView); @@ -167,6 +189,7 @@ Text = "Магазин"; Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpSnackMaxCount).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -185,5 +208,7 @@ private DataGridViewTextBoxColumn id; private DataGridViewTextBoxColumn DinerName; private DataGridViewTextBoxColumn Count; + private Label label2; + private NumericUpDown numericUpSnackMaxCount; } } \ No newline at end of file diff --git a/Diner/Diner/FormShop.cs b/Diner/Diner/FormShop.cs index c34f982..2eacec2 100644 --- a/Diner/Diner/FormShop.cs +++ b/Diner/Diner/FormShop.cs @@ -48,6 +48,7 @@ namespace DinerView textBoxName.Text = view.ShopName; textBoxAdress.Text = view.Adress; dateTimeOpen.Value = view.OpeningDate; + numericUpSnackMaxCount.Value = view.SnackMaxCount; _ShopSnacks = view.ShopSnacks ?? new Dictionary(); LoadData(); } @@ -101,7 +102,8 @@ namespace DinerView Id = _id ?? 0, ShopName = textBoxName.Text, Adress = textBoxAdress.Text, - OpeningDate = dateTimeOpen.Value + OpeningDate = dateTimeOpen.Value, + SnackMaxCount = (int)numericUpSnackMaxCount.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/Diner/Diner/Program.cs b/Diner/Diner/Program.cs index 8a61d8f..cbde433 100644 --- a/Diner/Diner/Program.cs +++ b/Diner/Diner/Program.cs @@ -1,6 +1,6 @@ using DinerContracts.BusinessLogicsContracts; using DinerContracts.StoragesContracts; -using DinerListImplement.Implements; +using DinerFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 315ebdf4a5346c0fb06a79732b48cbda34954032 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Mon, 15 Apr 2024 20:46:59 +0400 Subject: [PATCH 2/2] Done --- .../BusinessLogics/OrderLogic.cs | 68 +-------- .../BusinessLogics/ShopLogic.cs | 4 +- .../DinerListImplement.csproj | 2 +- .../Implements/ShopStorage.cs | 9 ++ .../AbstractShopListImplement/Models/Order.cs | 9 +- .../AbstractShopListImplement/Models/Shop.cs | 8 +- Diner/Diner.sln | 6 + Diner/Diner/DinerView.csproj | 6 + Diner/Diner/FormMain.Designer.cs | 16 +- Diner/Diner/FormMain.cs | 2 +- Diner/Diner/FormSell.Designer.cs | 117 -------------- Diner/Diner/FormSell.cs | 117 -------------- Diner/Diner/FormSell.resx | 120 --------------- Diner/Diner/FormSellSnack.Designer.cs | 6 +- Diner/Diner/FormSellSnack.cs | 8 +- Diner/Diner/FormShops.cs | 2 +- Diner/Diner/Program.cs | 1 + Diner/DinerFileImplement/DataFileSingleton.cs | 55 +++++++ .../DinerFileImplement.csproj | 14 ++ .../Implements/ComponentStorage.cs | 74 +++++++++ .../Implements/OrderStorage.cs | 84 ++++++++++ .../Implements/ShopStorage.cs | 144 ++++++++++++++++++ .../Implements/SnackStorage.cs | 74 +++++++++ Diner/DinerFileImplement/Models/Component.cs | 59 +++++++ Diner/DinerFileImplement/Models/Order.cs | 83 ++++++++++ Diner/DinerFileImplement/Models/Shop.cs | 102 +++++++++++++ Diner/DinerFileImplement/Models/Snack.cs | 81 ++++++++++ 27 files changed, 822 insertions(+), 449 deletions(-) delete mode 100644 Diner/Diner/FormSell.Designer.cs delete mode 100644 Diner/Diner/FormSell.cs delete mode 100644 Diner/Diner/FormSell.resx create mode 100644 Diner/DinerFileImplement/DataFileSingleton.cs create mode 100644 Diner/DinerFileImplement/DinerFileImplement.csproj create mode 100644 Diner/DinerFileImplement/Implements/ComponentStorage.cs create mode 100644 Diner/DinerFileImplement/Implements/OrderStorage.cs create mode 100644 Diner/DinerFileImplement/Implements/ShopStorage.cs create mode 100644 Diner/DinerFileImplement/Implements/SnackStorage.cs create mode 100644 Diner/DinerFileImplement/Models/Component.cs create mode 100644 Diner/DinerFileImplement/Models/Order.cs create mode 100644 Diner/DinerFileImplement/Models/Shop.cs create mode 100644 Diner/DinerFileImplement/Models/Snack.cs diff --git a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs index 005107e..22ffbf7 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -3,7 +3,7 @@ using DinerContracts.BusinessLogicsContracts; using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; -using DinerShopDataModels.Models; +using DinerDataModels.Models; using DinerDataModels.Enum; using Microsoft.Extensions.Logging; @@ -16,9 +16,6 @@ namespace DinerBusinessLogic.BusinessLogics private readonly IShopStorage _shopStorage; private readonly IShopLogic _shopLogic; - private readonly IShopStorage _shopStorage; - - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { _orderStorage = orderStorage; @@ -51,64 +48,7 @@ namespace DinerBusinessLogic.BusinessLogics } return true; } - public bool CheckSupply(IDinerModel snack, int count) - { - if (count <= 0) - { - _logger.LogWarning("Check then supply operation error. IceCream count < 0."); - return false; - } - int sumCapacity = 0; - int sumCount = 0; - sumCapacity = _shopStorage.GetFullList().Select(x => x.MaxCapacity).Sum(); - sumCount = _shopStorage.GetFullList().Select(x => x.ShopIceCreams.Select(y => y.Value.Item2).Sum()).Sum(); - int freeSpace = sumCapacity - sumCount; - - if (freeSpace - count < 0) - { - _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace = shop.MaxCapacity; - foreach (var doc in shop.ShopIceCreams) - { - freeSpace -= doc.Value.Item2; - } - if (freeSpace == 0) - { - continue; - } - if (freeSpace - count >= 0) - { - if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, count)) - count = 0; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (freeSpace - count < 0) - { - if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, freeSpace)) - count -= freeSpace; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (count <= 0) - { - return true; - } - } - return false; - } public bool TakeOrderInWork(OrderBindingModel model) { return ChangeStatus(model, OrderStatus.Выполняется); @@ -118,10 +58,6 @@ namespace DinerBusinessLogic.BusinessLogics return ChangeStatus(model, OrderStatus.Готов); } public bool DeliveryOrder(OrderBindingModel model) - { - return ChangeStatus(model, OrderStatus.Выдан); - } - public bool DeliveryOrder(OrderBindingModel model) { var order = _orderStorage.GetElement(new OrderSearchModel { @@ -139,9 +75,9 @@ namespace DinerBusinessLogic.BusinessLogics { throw new ArgumentException("Недостаточно места"); } - return ChangeStatus(model, OrderStatus.Выдан); } + private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) diff --git a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs index ed9d705..75076cd 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -159,11 +159,11 @@ namespace DinerBusinessLogic.BusinessLogics } public bool Sale(SupplySearchModel model) { - if (!model.PizzaId.HasValue || !model.Count.HasValue) + if (!model.SnackId.HasValue || !model.Count.HasValue) { return false; } - _logger.LogInformation("Check pizza count in all shops"); + _logger.LogInformation("Check snack count in all shops"); if (_shopStorage.Sale(model)) { _logger.LogInformation("Selling sucsess"); diff --git a/Diner/AbstractShopListImplement/DinerListImplement.csproj b/Diner/AbstractShopListImplement/DinerListImplement.csproj index 51f4155..f83c53f 100644 --- a/Diner/AbstractShopListImplement/DinerListImplement.csproj +++ b/Diner/AbstractShopListImplement/DinerListImplement.csproj @@ -1,4 +1,4 @@ - + net6.0 diff --git a/Diner/AbstractShopListImplement/Implements/ShopStorage.cs b/Diner/AbstractShopListImplement/Implements/ShopStorage.cs index 7095fc8..d9bee3f 100644 --- a/Diner/AbstractShopListImplement/Implements/ShopStorage.cs +++ b/Diner/AbstractShopListImplement/Implements/ShopStorage.cs @@ -109,5 +109,14 @@ namespace DinerListImplement.Implements } return null; } + public bool Sale(SupplySearchModel model) + { + throw new NotImplementedException(); + } + + public bool RestockingShops(SupplyBindingModel model) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/Diner/AbstractShopListImplement/Models/Order.cs b/Diner/AbstractShopListImplement/Models/Order.cs index 2423aaf..409e3c0 100644 --- a/Diner/AbstractShopListImplement/Models/Order.cs +++ b/Diner/AbstractShopListImplement/Models/Order.cs @@ -39,14 +39,9 @@ namespace DinerListImplement.Models { return; } - Id = model.Id; - SnackID = model.SnackId; - SnackName = model.SnackName; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; - DateImplement = model.DateImplement; + if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement; + if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() { diff --git a/Diner/AbstractShopListImplement/Models/Shop.cs b/Diner/AbstractShopListImplement/Models/Shop.cs index 8a666ee..4fcebf8 100644 --- a/Diner/AbstractShopListImplement/Models/Shop.cs +++ b/Diner/AbstractShopListImplement/Models/Shop.cs @@ -16,6 +16,7 @@ namespace DinerListImplement.Models public string Adress { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary ShopSnacks { get; private set; } = new(); + public int SnackMaxCount { get; private set; } public static Shop? Create(ShopBindingModel? model) { @@ -28,7 +29,8 @@ namespace DinerListImplement.Models Id = model.Id, ShopName = model.ShopName, Adress = model.Adress, - OpeningDate = model.OpeningDate + OpeningDate = model.OpeningDate, + SnackMaxCount = model.SnackMaxCount, }; } @@ -41,6 +43,7 @@ namespace DinerListImplement.Models ShopName = model.ShopName; Adress = model.Adress; OpeningDate = model.OpeningDate; + SnackMaxCount = model.SnackMaxCount; } public ShopViewModel GetViewModel => new() @@ -49,7 +52,8 @@ namespace DinerListImplement.Models ShopName = ShopName, Adress = Adress, OpeningDate = OpeningDate, - ShopSnacks = ShopSnacks + ShopSnacks = ShopSnacks, + SnackMaxCount = SnackMaxCount, }; } } \ No newline at end of file diff --git a/Diner/Diner.sln b/Diner/Diner.sln index be21916..6cb40de 100644 --- a/Diner/Diner.sln +++ b/Diner/Diner.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerBusinessLogic", "..\Ab EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "AbstractShopListImplement\DinerListImplement.csproj", "{A24E7474-4B43-4E81-A6BF-2B7323D5C26A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerFileImplement\DinerFileImplement.csproj", "{13294C1E-C8DF-4E4B-B645-A61900A797EB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Debug|Any CPU.Build.0 = Debug|Any CPU {A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Release|Any CPU.ActiveCfg = Release|Any CPU {A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Release|Any CPU.Build.0 = Release|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Diner/Diner/DinerView.csproj b/Diner/Diner/DinerView.csproj index 9c3757b..a7b8bd0 100644 --- a/Diner/Diner/DinerView.csproj +++ b/Diner/Diner/DinerView.csproj @@ -9,12 +9,18 @@ + + + + + + diff --git a/Diner/Diner/FormMain.Designer.cs b/Diner/Diner/FormMain.Designer.cs index 8b18c18..cd25210 100644 --- a/Diner/Diner/FormMain.Designer.cs +++ b/Diner/Diner/FormMain.Designer.cs @@ -41,7 +41,7 @@ магазиныToolStripMenuItem = new ToolStripMenuItem(); toolStripDropDownButton1 = new ToolStripDropDownButton(); поставкаToolStripMenuItem = new ToolStripMenuItem(); - продажаToolStripMenuItem = new ToolStripMenuItem(); + SellToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); toolStrip1.SuspendLayout(); SuspendLayout(); @@ -148,7 +148,7 @@ // toolStripDropDownButton1 // toolStripDropDownButton1.DisplayStyle = ToolStripItemDisplayStyle.Text; - toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, продажаToolStripMenuItem }); + toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, SellToolStripMenuItem }); toolStripDropDownButton1.ImageTransparentColor = Color.Magenta; toolStripDropDownButton1.Name = "toolStripDropDownButton1"; toolStripDropDownButton1.Size = new Size(95, 24); @@ -161,12 +161,12 @@ поставкаToolStripMenuItem.Text = "Поставка"; поставкаToolStripMenuItem.Click += transactionToolStripMenuItem_Click; // - // продажаToolStripMenuItem + // SellToolStripMenuItem // - продажаToolStripMenuItem.Name = "продажаToolStripMenuItem"; - продажаToolStripMenuItem.Size = new Size(224, 26); - продажаToolStripMenuItem.Text = "Продажа"; - продажаToolStripMenuItem.Click += SellToolStripMenuItem_Click; + SellToolStripMenuItem.Name = "SellToolStripMenuItem"; + SellToolStripMenuItem.Size = new Size(224, 26); + SellToolStripMenuItem.Text = "Продажа"; + SellToolStripMenuItem.Click += SellToolStripMenuItem_Click; // // FormMain // @@ -205,6 +205,6 @@ private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripDropDownButton toolStripDropDownButton1; private ToolStripMenuItem поставкаToolStripMenuItem; - private ToolStripMenuItem продажаToolStripMenuItem; + private ToolStripMenuItem SellToolStripMenuItem; } } \ No newline at end of file diff --git a/Diner/Diner/FormMain.cs b/Diner/Diner/FormMain.cs index 19c9fc2..ade35fa 100644 --- a/Diner/Diner/FormMain.cs +++ b/Diner/Diner/FormMain.cs @@ -182,7 +182,7 @@ namespace Diner } private void SellToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza)); + var service = Program.ServiceProvider?.GetService(typeof(FormSellSnack)); if (service is FormSellSnack form) { form.ShowDialog(); diff --git a/Diner/Diner/FormSell.Designer.cs b/Diner/Diner/FormSell.Designer.cs deleted file mode 100644 index 95509c4..0000000 --- a/Diner/Diner/FormSell.Designer.cs +++ /dev/null @@ -1,117 +0,0 @@ -namespace IceCreamShop -{ - partial class SellForm - { - /// - /// 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() - { - IceCreamComboBox = new ComboBox(); - IceCreamLabel = new Label(); - CountLabel = new Label(); - CountTextBox = new TextBox(); - SaveButton = new Button(); - CancelButton = new Button(); - SuspendLayout(); - // - // IceCreamComboBox - // - IceCreamComboBox.FormattingEnabled = true; - IceCreamComboBox.Location = new Point(99, 12); - IceCreamComboBox.Name = "IceCreamComboBox"; - IceCreamComboBox.Size = new Size(121, 23); - IceCreamComboBox.TabIndex = 0; - // - // IceCreamLabel - // - IceCreamLabel.AutoSize = true; - IceCreamLabel.Location = new Point(12, 20); - IceCreamLabel.Name = "IceCreamLabel"; - IceCreamLabel.Size = new Size(81, 15); - IceCreamLabel.TabIndex = 1; - IceCreamLabel.Text = "Мороженное"; - // - // CountLabel - // - CountLabel.AutoSize = true; - CountLabel.Location = new Point(21, 49); - CountLabel.Name = "CountLabel"; - CountLabel.Size = new Size(72, 15); - CountLabel.TabIndex = 2; - CountLabel.Text = "Количество"; - // - // CountTextBox - // - CountTextBox.Location = new Point(99, 41); - CountTextBox.Name = "CountTextBox"; - CountTextBox.Size = new Size(121, 23); - CountTextBox.TabIndex = 3; - // - // SaveButton - // - SaveButton.Location = new Point(64, 73); - SaveButton.Name = "SaveButton"; - SaveButton.Size = new Size(75, 23); - SaveButton.TabIndex = 4; - SaveButton.Text = "Сохранить"; - SaveButton.UseVisualStyleBackColor = true; - SaveButton.Click += SaveButton_Click; - // - // CancelButton - // - CancelButton.Location = new Point(145, 73); - CancelButton.Name = "CancelButton"; - CancelButton.Size = new Size(75, 23); - CancelButton.TabIndex = 5; - CancelButton.Text = "Отмена"; - CancelButton.UseVisualStyleBackColor = true; - // - // SellForm - // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(224, 108); - Controls.Add(CancelButton); - Controls.Add(SaveButton); - Controls.Add(CountTextBox); - Controls.Add(CountLabel); - Controls.Add(IceCreamLabel); - Controls.Add(IceCreamComboBox); - Name = "SellForm"; - Text = "Форма продажи"; - ResumeLayout(false); - PerformLayout(); - } - - #endregion - - private ComboBox IceCreamComboBox; - private Label IceCreamLabel; - private Label CountLabel; - private TextBox CountTextBox; - private Button SaveButton; - private Button CancelButton; - } -} \ No newline at end of file diff --git a/Diner/Diner/FormSell.cs b/Diner/Diner/FormSell.cs deleted file mode 100644 index 96143c6..0000000 --- a/Diner/Diner/FormSell.cs +++ /dev/null @@ -1,117 +0,0 @@ -using IceCreamShopContracts.BusinessLogicsContracts; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Models; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace IceCreamShop -{ - public partial class SellForm : Form - { - private readonly List? _iceCreamList; - IShopLogic _shopLogic; - IIceCreamLogic _iceCreamLogic; - public SellForm(IIceCreamLogic iceCreamLogic, IShopLogic shopLogic) - { - InitializeComponent(); - _shopLogic = shopLogic; - _iceCreamLogic = iceCreamLogic; - _iceCreamList = iceCreamLogic.ReadList(null); - if (_iceCreamList != null) - { - IceCreamComboBox.DisplayMember = "IceCreamName"; - IceCreamComboBox.ValueMember = "Id"; - IceCreamComboBox.DataSource = _iceCreamList; - IceCreamComboBox.SelectedItem = null; - } - } - public int IceCreamId - { - get - { - return - Convert.ToInt32(IceCreamComboBox.SelectedValue); - } - set - { - IceCreamComboBox.SelectedValue = value; - } - } - - public IIceCreamModel? IceCreamModel - { - get - { - if (_iceCreamList == null) - { - return null; - } - foreach (var elem in _iceCreamList) - { - if (elem.Id == IceCreamId) - { - return elem; - } - } - return null; - } - } - - - public int Count - { - get { return Convert.ToInt32(CountTextBox.Text); } - set - { CountTextBox.Text = value.ToString(); } - } - - private void SaveButton_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(CountTextBox.Text)) - { - MessageBox.Show("Заполните поле Количество", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (IceCreamComboBox.SelectedValue == null) - { - MessageBox.Show("Выберите мороженное", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - try - { - int count = Convert.ToInt32(CountTextBox.Text); - - bool res = _shopLogic.MakeSell( - _iceCreamLogic.ReadElement(new() { Id = Convert.ToInt32(IceCreamComboBox.SelectedValue) }), - count - ); - - if (!res) - { - throw new Exception("Ошибка при продаже."); - } - - MessageBox.Show("Продажа прошла успешно"); - DialogResult = DialogResult.OK; - Close(); - - } - catch (Exception err) - { - MessageBox.Show("Ошибка продажи"); - return; - } - } - } -} diff --git a/Diner/Diner/FormSell.resx b/Diner/Diner/FormSell.resx deleted file mode 100644 index af32865..0000000 --- a/Diner/Diner/FormSell.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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/Diner/Diner/FormSellSnack.Designer.cs b/Diner/Diner/FormSellSnack.Designer.cs index 36fbfeb..8145f17 100644 --- a/Diner/Diner/FormSellSnack.Designer.cs +++ b/Diner/Diner/FormSellSnack.Designer.cs @@ -1,4 +1,4 @@ -namespace PizzeriaView +namespace DinerView { partial class FormSellSnack { @@ -101,8 +101,8 @@ Controls.Add(comboBoxSnack); Controls.Add(labelSnack); Name = "FormSellSnack"; - Text = "Продажа пиццы"; - Load += FormSellingSnack_Load; + Text = "Продажа закуски"; + Load += FormSellingPizza_Load; ResumeLayout(false); PerformLayout(); } diff --git a/Diner/Diner/FormSellSnack.cs b/Diner/Diner/FormSellSnack.cs index b6070ae..d6f96a4 100644 --- a/Diner/Diner/FormSellSnack.cs +++ b/Diner/Diner/FormSellSnack.cs @@ -13,7 +13,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace PizzeriaView +namespace DinerView { public partial class FormSellSnack : Form { @@ -28,12 +28,12 @@ namespace PizzeriaView _logicP = logicP; _logicS = logicS; } - private void FormSellingSnack_Load(object sender, EventArgs e) + private void FormSellingPizza_Load(object sender, EventArgs e) { _snackList = _logicP.ReadList(null); if (_snackList != null) { - comboBoxSnack.DisplayMember = "PizzaName"; + comboBoxSnack.DisplayMember = "SnackName"; comboBoxSnack.ValueMember = "Id"; comboBoxSnack.DataSource = _snackList; comboBoxSnack.SelectedItem = null; @@ -52,7 +52,7 @@ namespace PizzeriaView { bool resout = _logicS.Sale(new SupplySearchModel { - PizzaId = Convert.ToInt32(comboBoxSnack.SelectedValue), + SnackId = Convert.ToInt32(comboBoxSnack.SelectedValue), Count = Convert.ToInt32(textBoxCount.Text) }); if (resout) diff --git a/Diner/Diner/FormShops.cs b/Diner/Diner/FormShops.cs index 11735c1..cacb4a6 100644 --- a/Diner/Diner/FormShops.cs +++ b/Diner/Diner/FormShops.cs @@ -40,7 +40,7 @@ namespace DinerView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ShopDiners"].Visible = false; + dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } diff --git a/Diner/Diner/Program.cs b/Diner/Diner/Program.cs index cbde433..aeca90d 100644 --- a/Diner/Diner/Program.cs +++ b/Diner/Diner/Program.cs @@ -52,6 +52,7 @@ namespace Diner services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/Diner/DinerFileImplement/DataFileSingleton.cs b/Diner/DinerFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..93d9658 --- /dev/null +++ b/Diner/DinerFileImplement/DataFileSingleton.cs @@ -0,0 +1,55 @@ +using DinerFileImplement.Models; +using System.Xml.Linq; + +namespace DinerFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string SnackFileName = "Snack.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Snacks { get; private set; } + public List Shops { get; private set; } + + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveSnacks() => SaveData(Snacks, SnackFileName, "Snacks", 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)!)!; + Snacks = LoadData(SnackFileName, "Snack", x => Snack.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) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + + } +} \ No newline at end of file diff --git a/Diner/DinerFileImplement/DinerFileImplement.csproj b/Diner/DinerFileImplement/DinerFileImplement.csproj new file mode 100644 index 0000000..f83c53f --- /dev/null +++ b/Diner/DinerFileImplement/DinerFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Diner/DinerFileImplement/Implements/ComponentStorage.cs b/Diner/DinerFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..6f610b7 --- /dev/null +++ b/Diner/DinerFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,74 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; + +namespace DinerFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Diner/DinerFileImplement/Implements/OrderStorage.cs b/Diner/DinerFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..a2c60b9 --- /dev/null +++ b/Diner/DinerFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,84 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; + +namespace DinerFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() => source.Orders.Select(x => AttachPizzaName(x.GetViewModel)).ToList(); + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id == model.Id) + .Select(x => AttachPizzaName(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return AttachPizzaName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return AttachPizzaName(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return AttachPizzaName(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order != null) + { + source.Orders.Remove(order); + source.SaveOrders(); + return AttachPizzaName(order.GetViewModel); + } + return null; + } + private OrderViewModel? AttachPizzaName(OrderViewModel? model) + { + if (model == null) + { + return null; + } + var snack = source.Snacks.FirstOrDefault(x => x.Id == model.SnackId); + if (snack != null) + { + model.SnackName = snack.SnackName; + } + return model; + } + } + +} diff --git a/Diner/DinerFileImplement/Implements/ShopStorage.cs b/Diner/DinerFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..3f7f4ba --- /dev/null +++ b/Diner/DinerFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,144 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Shops.Select(x=>x.GetViewModel).ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops.Where(x=>x.ShopName.Contains(model.ShopName)).Select(x=>x.GetViewModel).ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x=>x.Id == model.Id); + if(shop == null) + { + return null; + } + shop.Update(model); + source.SaveShops(); + return shop.GetViewModel; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if(shop != null) + { + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + return null; + } + public bool Sale(SupplySearchModel model) { + if (model == null || !model.SnackId.HasValue || !model.Count.HasValue) + return false; + int remainingSpace = source.Shops.Select(x => x.Snacks.ContainsKey(model.SnackId.Value) ? x.Snacks[model.SnackId.Value] : 0).Sum(); + if(remainingSpace < model.Count) + { + return false; + } + var shops = source.Shops.Where(x => x.Snacks.ContainsKey(model.SnackId.Value)).OrderByDescending(x => x.Snacks[model.SnackId.Value]).ToList(); + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.Snacks[model.SnackId.Value]; + if (residue > 0) + { + shop.Snacks.Remove(model.SnackId.Value); + shop.SnackUpdate(); + model.Count = residue; + } + else + { + if (residue == 0) + { + shop.Snacks.Remove(model.SnackId.Value); + } + else + { + shop.Snacks[model.SnackId.Value] = -residue; + } + shop.SnackUpdate(); + source.SaveShops(); + return true; + } + } + source.SaveShops(); + return false; + } + public bool RestockingShops(SupplyBindingModel model) + { + if (model==null || source.Shops.Select(x => x.SnackMaxCount - x.ShopSnacks.Select(y => y.Value.Item2).Sum()).Sum() < model.Count) + { + return false; + } + foreach (Shop shop in source.Shops) + { + int free_places = shop.SnackMaxCount - shop.ShopSnacks.Select(x => x.Value.Item2).Sum(); + if (free_places <= 0) + continue; + free_places = Math.Min(free_places, model.Count); + model.Count -= free_places; + if (shop.Snacks.ContainsKey(model.SnackId)) + { + shop.Snacks[model.SnackId] += free_places; + } + else + { + shop.Snacks.Add(model.SnackId, free_places); + } + shop.SnackUpdate(); + if (model.Count == 0) + { + source.SaveShops(); + return true; + } + } + return false; + } + } +} diff --git a/Diner/DinerFileImplement/Implements/SnackStorage.cs b/Diner/DinerFileImplement/Implements/SnackStorage.cs new file mode 100644 index 0000000..4720cb5 --- /dev/null +++ b/Diner/DinerFileImplement/Implements/SnackStorage.cs @@ -0,0 +1,74 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; + +namespace DinerFileImplement.Implements +{ + public class SnackStorage : ISnackStorage + { + private readonly DataFileSingleton source; + public SnackStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Snacks.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(SnackSearchModel model) + { + if (string.IsNullOrEmpty(model.SnackName)) + { + return new(); + } + return source.Snacks.Where(x => x.SnackName.Contains(model.SnackName)).Select(x => x.GetViewModel).ToList(); + } + public SnackViewModel? GetElement(SnackSearchModel model) + { + if (string.IsNullOrEmpty(model.SnackName) && !model.Id.HasValue) + { + return null; + } + return source.Snacks.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SnackName) && x.SnackName == model.SnackName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public SnackViewModel? Insert(SnackBindingModel model) + { + model.Id = source.Snacks.Count > 0 ? source.Snacks.Max(x => x.Id) + 1 : 1; + var newSnack = Snack.Create(model); + if (newSnack == null) + { + return null; + } + source.Snacks.Add(newSnack); + source.SaveSnacks(); + return newSnack.GetViewModel; + } + public SnackViewModel? Update(SnackBindingModel model) + { + var Snack = source.Snacks.FirstOrDefault(x => x.Id == model.Id); + if (Snack == null) + { + return null; + } + Snack.Update(model); + source.SaveSnacks(); + return Snack.GetViewModel; + } + public SnackViewModel? Delete(SnackBindingModel model) + { + var Snack = source.Snacks.FirstOrDefault(x => x.Id == model.Id); + if (Snack != null) + { + source.Snacks.Remove(Snack); + source.SaveSnacks(); + return Snack.GetViewModel; + } + return null; + } + } +} diff --git a/Diner/DinerFileImplement/Models/Component.cs b/Diner/DinerFileImplement/Models/Component.cs new file mode 100644 index 0000000..6b59123 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Component.cs @@ -0,0 +1,59 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/Diner/DinerFileImplement/Models/Order.cs b/Diner/DinerFileImplement/Models/Order.cs new file mode 100644 index 0000000..223ce81 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Order.cs @@ -0,0 +1,83 @@ +using DinerDataModels.Enum; +using DinerDataModels.Models; +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int SnackId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SnackId = model.SnackId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + string dateImplement = element.Element("DateImplement")!.Value; + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SnackId = Convert.ToInt32(element.Element("SnackId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = (dateImplement == "" || dateImplement is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateImplement) + }; + + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + SnackId = SnackId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("SnackId", SnackId.ToString()), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } +} diff --git a/Diner/DinerFileImplement/Models/Shop.cs b/Diner/DinerFileImplement/Models/Shop.cs new file mode 100644 index 0000000..a248b24 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Shop.cs @@ -0,0 +1,102 @@ +using DinerDataModels.Models; +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Adress { get; private set; } = string.Empty; + public DateTime OpeningDate { get; private set; } + public Dictionary Snacks { get; private set; } = new(); + private Dictionary? _shopSnacks = null; + public Dictionary ShopSnacks { + get { + if (_shopSnacks == null) + { + var source = DataFileSingleton.GetInstance(); + _shopSnacks = Snacks.ToDictionary(x => x.Key, y => ((source.Snacks.FirstOrDefault(z => z.Id == y.Key) as ISnackModel)!, y.Value)); + } + return _shopSnacks; + } + } + public int SnackMaxCount { get; private set; } + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Snacks = model.ShopSnacks.ToDictionary(x => x.Key, x => x.Value.Item2), + SnackMaxCount = model.SnackMaxCount + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Adress = element.Element("Adress")!.Value, + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Snacks = element.Element("ShopSnacks")!.Elements("ShopSnack")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)), + SnackMaxCount = Convert.ToInt32(element.Element("SnackMaxCount")!.Value) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + SnackMaxCount = model.SnackMaxCount; + Snacks = model.ShopSnacks.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopSnacks = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopSnacks = ShopSnacks, + SnackMaxCount = SnackMaxCount + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName",ShopName), + new XElement("Adress", Adress), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopSnacks", Snacks.Select( + x => new XElement("ShopSnack", new XElement("Key",x.Key),new XElement("Value",x.Value))).ToArray()), + new XElement("SnackMaxCount", SnackMaxCount.ToString()) + ); + + public void SnackUpdate() { + _shopSnacks = null; + } + } +} diff --git a/Diner/DinerFileImplement/Models/Snack.cs b/Diner/DinerFileImplement/Models/Snack.cs new file mode 100644 index 0000000..5fc55e4 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Snack.cs @@ -0,0 +1,81 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Snack : ISnackModel + { + public int Id { get; private set; } + public string SnackName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _snackComponents = null; + public Dictionary SnackComponents + { + get + { + if (_snackComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _snackComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _snackComponents; + } + } + public static Snack? Create(SnackBindingModel model) + { + if (model == null) + { + return null; + } + return new Snack() + { + Id = model.Id, + SnackName = model.SnackName, + Price = model.Price, + Components = model.SnackComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Snack? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Snack() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SnackName = element.Element("SnackName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("SnackComponents")!.Elements("SnackComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(SnackBindingModel model) + { + if (model == null) + { + return; + } + SnackName = model.SnackName; + Price = model.Price; + Components = model.SnackComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _snackComponents = null; + } + public SnackViewModel GetViewModel => new() + { + Id = Id, + SnackName = SnackName, + Price = Price, + SnackComponents = SnackComponents + }; + public XElement GetXElement => new("Snack", + new XAttribute("Id", Id), + new XElement("SnackName", SnackName), + new XElement("Price", Price.ToString()), + new XElement("SnackComponents", Components.Select( + x => new XElement("SnackComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} -- 2.25.1