Merge branch 'LabWork_1' into LabWork_2

This commit is contained in:
Petek1234 2024-11-22 10:20:41 +04:00
commit e0fa1ea187
5 changed files with 82 additions and 22 deletions

View File

@ -0,0 +1,13 @@
namespace GasStation.Entities.Enums;
[Flags]
public enum SupppliersFuelType
{
None = 0,
Fuel_92 = 1, // 0001
Fuel_95 = 2, // 0010
Fuel_diesel = 4 //
}//

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GasStation.Entities.Enums;
namespace GasStation.Entities;
@ -12,13 +13,17 @@ public class Supplier
public string Brand { get; private set; } = string.Empty;
public static Supplier CreateEntity(int id, string brand)
public SupppliersFuelType Types { get; private set; }
public static Supplier CreateEntity(int id, string brand, SupppliersFuelType types)
{
return new Supplier
{
Id = id,
Brand = brand ?? string.Empty
Brand = brand ?? string.Empty,
Types = types
};
}
}

View File

@ -32,27 +32,29 @@
textBoxBrand = new TextBox();
ButtonSave = new Button();
ButtonCancel = new Button();
checkedListBoxFuelTypes = new CheckedListBox();
label2 = new Label();
SuspendLayout();
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(39, 43);
label1.Location = new Point(39, 40);
label1.Name = "label1";
label1.Size = new Size(116, 15);
label1.Size = new Size(119, 15);
label1.TabIndex = 0;
label1.Text = "Бренд поставщика";
label1.Text = "Бренд поставщика:";
//
// textBoxBrand
//
textBoxBrand.Location = new Point(191, 40);
textBoxBrand.Location = new Point(185, 40);
textBoxBrand.Name = "textBoxBrand";
textBoxBrand.Size = new Size(125, 23);
textBoxBrand.Size = new Size(169, 23);
textBoxBrand.TabIndex = 1;
//
// ButtonSave
//
ButtonSave.Location = new Point(39, 116);
ButtonSave.Location = new Point(39, 257);
ButtonSave.Name = "ButtonSave";
ButtonSave.Size = new Size(116, 30);
ButtonSave.TabIndex = 2;
@ -61,7 +63,7 @@
//
// ButtonCancel
//
ButtonCancel.Location = new Point(191, 116);
ButtonCancel.Location = new Point(187, 257);
ButtonCancel.Name = "ButtonCancel";
ButtonCancel.Size = new Size(125, 30);
ButtonCancel.TabIndex = 3;
@ -69,16 +71,36 @@
ButtonCancel.UseVisualStyleBackColor = true;
ButtonCancel.Click += ButtonCancel_Click;
//
// FormSuppliers
// checkedListBoxFuelTypes
//
checkedListBoxFuelTypes.FormattingEnabled = true;
checkedListBoxFuelTypes.Location = new Point(185, 69);
checkedListBoxFuelTypes.Name = "checkedListBoxFuelTypes";
checkedListBoxFuelTypes.RightToLeft = RightToLeft.No;
checkedListBoxFuelTypes.Size = new Size(169, 94);
checkedListBoxFuelTypes.TabIndex = 4;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(39, 93);
label2.Name = "label2";
label2.Size = new Size(91, 15);
label2.TabIndex = 5;
label2.Text = "Типы топлива:";
//
// FormSupplier
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(366, 189);
ClientSize = new Size(366, 335);
Controls.Add(label2);
Controls.Add(checkedListBoxFuelTypes);
Controls.Add(ButtonCancel);
Controls.Add(ButtonSave);
Controls.Add(textBoxBrand);
Controls.Add(label1);
Name = "FormSuppliers";
Name = "FormSupplier";
StartPosition = FormStartPosition.CenterParent;
Text = "Поставщики";
ResumeLayout(false);
@ -91,5 +113,7 @@
private TextBox textBoxBrand;
private Button ButtonSave;
private Button ButtonCancel;
private CheckedListBox checkedListBoxFuelTypes;
private Label label2;
}
}

View File

@ -9,6 +9,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GasStation.Entities;
using GasStation.Entities.Enums;
using GasStation.Repositories;
using GasStation.Repositories.Implementations;
@ -28,18 +29,20 @@ public partial class FormSupplier : Form
try
{
var supplier = _supplierRepository.ReadSupplierById(value);
if (supplier == null)
{
throw new InvalidDataException(nameof(supplier));
}
foreach (SupppliersFuelType elem in Enum.GetValues(typeof(SupppliersFuelType)))
{
if ((elem & supplier.Types) != 0)
{
checkedListBoxFuelTypes.SetItemChecked(checkedListBoxFuelTypes.Items.IndexOf(elem), true);
}
}
textBoxBrand.Text = supplier.Brand;
_supplierlId = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
@ -52,6 +55,11 @@ public partial class FormSupplier : Form
{
InitializeComponent();
_supplierRepository = supplierRepository ?? throw new ArgumentNullException(nameof(supplierRepository));
foreach (SupppliersFuelType fuelType in Enum.GetValues(typeof(SupppliersFuelType)))
{
if (fuelType == SupppliersFuelType.None) continue;
checkedListBoxFuelTypes.Items.Add(fuelType);
}
}
private void ButtonSave_Click(object sender, EventArgs e)
@ -64,11 +72,11 @@ public partial class FormSupplier : Form
}
if (_supplierlId.HasValue)
{
_supplierRepository.UpdateSupplier(CreateSupplier(_supplierlId.Value));
_supplierRepository.UpdateSupplier(CreateEntity(_supplierlId.Value));
}
else
{
_supplierRepository.CreateSupplier(CreateSupplier(0));
_supplierRepository.CreateSupplier(CreateEntity(0));
}
Close();
}
@ -81,5 +89,14 @@ public partial class FormSupplier : Form
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Supplier CreateSupplier(int id) => Supplier.CreateEntity(id, textBoxBrand.Text);
private Supplier CreateEntity(int id)
{
SupppliersFuelType fuelType = SupppliersFuelType.None;
foreach (var elem in checkedListBoxFuelTypes.CheckedItems)
{
fuelType |= (SupppliersFuelType)elem;
}
return Supplier.CreateEntity(id, textBoxBrand.Text, fuelType);
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GasStation.Entities;
using GasStation.Entities.Enums;
namespace GasStation.Repositories.Implementations;
@ -20,7 +21,7 @@ public class SupplierRepository : ISupplierRepository
public Supplier ReadSupplierById(int id)
{
return Supplier.CreateEntity(0, string.Empty);
return Supplier.CreateEntity(0, string.Empty, SupppliersFuelType.Fuel_diesel);
}
public IEnumerable<Supplier> ReadSuppliers()