лаба 1 точно вся готова

This commit is contained in:
Petek1234 2024-11-22 10:14:33 +04:00
parent 69e9e6b69e
commit a5c5a3a234
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.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using GasStation.Entities.Enums;
namespace GasStation.Entities; namespace GasStation.Entities;
@ -12,13 +13,17 @@ public class Supplier
public string Brand { get; private set; } = string.Empty; 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 return new Supplier
{ {
Id = id, Id = id,
Brand = brand ?? string.Empty Brand = brand ?? string.Empty,
Types = types
}; };
} }
} }

View File

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

View File

@ -9,6 +9,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using GasStation.Entities; using GasStation.Entities;
using GasStation.Entities.Enums;
using GasStation.Repositories; using GasStation.Repositories;
using GasStation.Repositories.Implementations; using GasStation.Repositories.Implementations;
@ -28,18 +29,20 @@ public partial class FormSupplier : Form
try try
{ {
var supplier = _supplierRepository.ReadSupplierById(value); var supplier = _supplierRepository.ReadSupplierById(value);
if (supplier == null) if (supplier == null)
{ {
throw new InvalidDataException(nameof(supplier)); 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; textBoxBrand.Text = supplier.Brand;
_supplierlId = value; _supplierlId = value;
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
@ -52,6 +55,11 @@ public partial class FormSupplier : Form
{ {
InitializeComponent(); InitializeComponent();
_supplierRepository = supplierRepository ?? throw new ArgumentNullException(nameof(supplierRepository)); _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) private void ButtonSave_Click(object sender, EventArgs e)
@ -64,11 +72,11 @@ public partial class FormSupplier : Form
} }
if (_supplierlId.HasValue) if (_supplierlId.HasValue)
{ {
_supplierRepository.UpdateSupplier(CreateSupplier(_supplierlId.Value)); _supplierRepository.UpdateSupplier(CreateEntity(_supplierlId.Value));
} }
else else
{ {
_supplierRepository.CreateSupplier(CreateSupplier(0)); _supplierRepository.CreateSupplier(CreateEntity(0));
} }
Close(); Close();
} }
@ -81,5 +89,14 @@ public partial class FormSupplier : Form
private void ButtonCancel_Click(object sender, EventArgs e) => Close(); 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.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using GasStation.Entities; using GasStation.Entities;
using GasStation.Entities.Enums;
namespace GasStation.Repositories.Implementations; namespace GasStation.Repositories.Implementations;
@ -20,7 +21,7 @@ public class SupplierRepository : ISupplierRepository
public Supplier ReadSupplierById(int id) public Supplier ReadSupplierById(int id)
{ {
return Supplier.CreateEntity(0, string.Empty); return Supplier.CreateEntity(0, string.Empty, SupppliersFuelType.Fuel_diesel);
} }
public IEnumerable<Supplier> ReadSuppliers() public IEnumerable<Supplier> ReadSuppliers()