8 лабораторная

This commit is contained in:
sardq 2023-12-28 10:26:08 +04:00
parent b45f27b91d
commit f76b623305
10 changed files with 288 additions and 61 deletions

View File

@ -0,0 +1,49 @@

using HoistingCrane.DrawningObjects;
using HoistingCrane.Entities;
namespace HoistingCrane.Generics
{
internal class CraneCompareByColor : IComparer<DrawingCrane?>
{
public int Compare(DrawingCrane? x, DrawingCrane? y)
{
if (x == null || x.EntityCrane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCrane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityCrane.BodyColor.Name != y.EntityCrane.BodyColor.Name)
{
return x.EntityCrane.BodyColor.Name.CompareTo(y.EntityCrane.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is EntityCrane)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is AdditionalDrawingCrane)
{
AdditionEntityCrane _X = (AdditionEntityCrane)x.EntityCrane;
AdditionEntityCrane _Y = (AdditionEntityCrane)y.EntityCrane;
if (_X.AdditionalColor.Name != _Y.AdditionalColor.Name)
{
return _X.AdditionalColor.Name.CompareTo(_Y.AdditionalColor.Name);
}
}
var speedCompare = x.EntityCrane.Speed.CompareTo(y.EntityCrane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityCrane.Weight.CompareTo(y.EntityCrane.Weight);
}
}
}

View File

@ -0,0 +1,30 @@
using HoistingCrane.DrawningObjects;
namespace HoistingCrane.Generics
{
internal class CraneCompareByType : IComparer<DrawingCrane?>
{
public int Compare(DrawingCrane? x, DrawingCrane? y)
{
if (x == null || x.EntityCrane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCrane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare =
x.EntityCrane.Speed.CompareTo(y.EntityCrane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityCrane.Weight.CompareTo(y.EntityCrane.Weight);
}
}
}

View File

@ -0,0 +1,23 @@
namespace HoistingCrane.Generics
{
internal class CranesCollectionInfo : IEquatable<CranesCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public CranesCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(CranesCollectionInfo? other)
{
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -18,6 +18,7 @@ namespace HoistingCrane.Generics
private readonly int _placeSizeHeight = 150;
/// Набор объектов
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public IEnumerable<T?> GetCranes => _collection.GetCranes();
/// Конструктор
public CranesGenericCollection(int picWidth, int picHeight)
@ -35,7 +36,7 @@ namespace HoistingCrane.Generics
{
return -1;
}
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawningCraneEqutables());
}
/// Перегрузка оператора вычитания
public static bool operator -(CranesGenericCollection<T, U> collect, int pos)

View File

@ -9,9 +9,9 @@ namespace HoistingCrane
internal class CranesGenericStorage
{
/// Словарь (хранилище)
readonly Dictionary<string, CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>> _craneStorages;
Dictionary<CranesCollectionInfo, CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>> _craneStorages;
/// Возвращение списка названий наборов
public List<string> Keys => _craneStorages.Keys.ToList();
public List<CranesCollectionInfo> Keys => _craneStorages.Keys.ToList();
/// Ширина окна отрисовки
private readonly int _pictureWidth;
/// Высота окна отрисовки
@ -29,8 +29,7 @@ namespace HoistingCrane
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>> record in _craneStorages)
foreach (KeyValuePair<CranesCollectionInfo, CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>> record in _craneStorages)
{
StringBuilder records = new();
foreach (DrawingCrane? elem in record.Value.GetCranes)
@ -95,7 +94,7 @@ namespace HoistingCrane
}
}
}
_craneStorages.Add(name, collection);
_craneStorages.Add(new CranesCollectionInfo(name, string.Empty), collection);
}
return true;
}
@ -103,25 +102,30 @@ namespace HoistingCrane
/// Конструктор
public CranesGenericStorage(int pictureWidth, int pictureHeight)
{
_craneStorages = new Dictionary<string, CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>>();
_craneStorages = new Dictionary<CranesCollectionInfo, CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// Добавление набора
public void AddSet(string name)
{
if (_craneStorages.ContainsKey(name))
if (_craneStorages.ContainsKey(new CranesCollectionInfo(name, string.Empty)))
{
return;
MessageBox.Show("Словарь уже содержит набор с таким названием", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
_craneStorages.Add(new CranesCollectionInfo(name, string.Empty), new CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>(_pictureWidth, _pictureHeight));
}
_craneStorages.Add(name, new CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>(_pictureWidth, _pictureHeight));
}
/// Удаление набора
public void DelSet(string name)
{
if (_craneStorages.ContainsKey(name))
CranesGenericCollection<DrawingCrane, DrawningObjectsCrane> crane;
if (_craneStorages.TryGetValue(new CranesCollectionInfo(name, string.Empty), out crane))
{
_craneStorages.Remove(name);
_craneStorages.Remove(new CranesCollectionInfo(name, string.Empty));
}
}
/// Доступ к набору
@ -129,9 +133,10 @@ namespace HoistingCrane
{
get
{
if (_craneStorages.ContainsKey(ind))
return _craneStorages[ind];
CranesCollectionInfo infCrane = new CranesCollectionInfo(ind, string.Empty);
if (_craneStorages.ContainsKey(infCrane)) return _craneStorages[infCrane];
return null;
}
}
}

View File

@ -0,0 +1,58 @@
using HoistingCrane.DrawningObjects;
using HoistingCrane.Entities;
using System.Diagnostics.CodeAnalysis;
namespace HoistingCrane.Generics
{
internal class DrawningCraneEqutables : IEqualityComparer<DrawingCrane?>
{
public bool Equals(DrawingCrane? x, DrawingCrane? y)
{
if (x == null || x.EntityCrane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCrane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityCrane.Speed != y.EntityCrane.Speed)
{
return false;
}
if (x.EntityCrane.Weight != y.EntityCrane.Weight)
{
return false;
}
if (x.EntityCrane.BodyColor != y.EntityCrane.BodyColor)
{
return false;
}
if (x is AdditionalDrawingCrane && y is AdditionalDrawingCrane)
{
AdditionEntityCrane _x = (AdditionEntityCrane)x.EntityCrane;
AdditionEntityCrane _y = (AdditionEntityCrane)y.EntityCrane;
if(_x.AdditionalColor != _y.AdditionalColor)
{
return false;
}
if (_x.Speed != _y.Speed)
{
return false;
}
if (_x.Weight != _y.Weight)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawingCrane obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -38,6 +38,8 @@
this.buttonReload = new System.Windows.Forms.Button();
this.buttonDelete = new System.Windows.Forms.Button();
this.buttonAdd = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@ -58,9 +60,11 @@
this.groupBoxInstruments.Controls.Add(this.buttonReload);
this.groupBoxInstruments.Controls.Add(this.buttonDelete);
this.groupBoxInstruments.Controls.Add(this.buttonAdd);
this.groupBoxInstruments.Location = new System.Drawing.Point(811, 0);
this.groupBoxInstruments.Controls.Add(this.buttonSortByColor);
this.groupBoxInstruments.Controls.Add(this.buttonSortByType);
this.groupBoxInstruments.Location = new System.Drawing.Point(806, 0);
this.groupBoxInstruments.Name = "groupBoxInstruments";
this.groupBoxInstruments.Size = new System.Drawing.Size(185, 464);
this.groupBoxInstruments.Size = new System.Drawing.Size(192, 511);
this.groupBoxInstruments.TabIndex = 0;
this.groupBoxInstruments.TabStop = false;
this.groupBoxInstruments.Text = "Инструменты";
@ -87,9 +91,9 @@
//
// buttonAddSet
//
this.buttonAddSet.Location = new System.Drawing.Point(6, 65);
this.buttonAddSet.Location = new System.Drawing.Point(-6, 65);
this.buttonAddSet.Name = "buttonAddSet";
this.buttonAddSet.Size = new System.Drawing.Size(167, 35);
this.buttonAddSet.Size = new System.Drawing.Size(191, 35);
this.buttonAddSet.TabIndex = 12;
this.buttonAddSet.Text = "Добавить набор";
this.buttonAddSet.UseVisualStyleBackColor = true;
@ -107,9 +111,9 @@
//
// buttonRemoveSet
//
this.buttonRemoveSet.Location = new System.Drawing.Point(6, 216);
this.buttonRemoveSet.Location = new System.Drawing.Point(-1, 216);
this.buttonRemoveSet.Name = "buttonRemoveSet";
this.buttonRemoveSet.Size = new System.Drawing.Size(167, 35);
this.buttonRemoveSet.Size = new System.Drawing.Size(181, 35);
this.buttonRemoveSet.TabIndex = 11;
this.buttonRemoveSet.Text = "Удалить набор";
this.buttonRemoveSet.UseVisualStyleBackColor = true;
@ -117,16 +121,16 @@
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 319);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(29, 398);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31);
this.maskedTextBoxNumber.TabIndex = 9;
//
// buttonReload
//
this.buttonReload.Location = new System.Drawing.Point(12, 397);
this.buttonReload.Location = new System.Drawing.Point(0, 476);
this.buttonReload.Name = "buttonReload";
this.buttonReload.Size = new System.Drawing.Size(167, 35);
this.buttonReload.Size = new System.Drawing.Size(191, 35);
this.buttonReload.TabIndex = 8;
this.buttonReload.Text = "Обновить экран";
this.buttonReload.UseVisualStyleBackColor = true;
@ -134,9 +138,9 @@
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(12, 356);
this.buttonDelete.Location = new System.Drawing.Point(0, 435);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(167, 35);
this.buttonDelete.Size = new System.Drawing.Size(191, 35);
this.buttonDelete.TabIndex = 7;
this.buttonDelete.Text = "Удалить кран";
this.buttonDelete.UseVisualStyleBackColor = true;
@ -144,20 +148,40 @@
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(12, 278);
this.buttonAdd.Location = new System.Drawing.Point(1, 357);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(167, 35);
this.buttonAdd.Size = new System.Drawing.Size(191, 35);
this.buttonAdd.TabIndex = 6;
this.buttonAdd.Text = "Добавить кран";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(-1, 319);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(199, 36);
this.buttonSortByColor.TabIndex = 15;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(0, 278);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(198, 35);
this.buttonSortByType.TabIndex = 14;
this.buttonSortByType.Text = "Сортировка по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// pictureBoxCollection
//
this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(996, 401);
this.pictureBoxCollection.Size = new System.Drawing.Size(998, 478);
this.pictureBoxCollection.TabIndex = 5;
this.pictureBoxCollection.TabStop = false;
//
@ -168,10 +192,10 @@
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 401);
this.menuStrip1.Location = new System.Drawing.Point(0, 478);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;
this.menuStrip1.Size = new System.Drawing.Size(996, 33);
this.menuStrip1.Size = new System.Drawing.Size(998, 33);
this.menuStrip1.TabIndex = 12;
this.menuStrip1.Text = "menuStrip1";
//
@ -211,7 +235,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(996, 434);
this.ClientSize = new System.Drawing.Size(998, 511);
this.Controls.Add(this.groupBoxInstruments);
this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.menuStrip1);
@ -248,5 +272,7 @@
private ToolStripMenuItem ToolStripMenuItemSave;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -10,6 +10,25 @@ namespace HoistingCrane
{
public partial class FormCraneCollection : Form
{
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCranes(new CraneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
CompareCranes(new CraneCompareByColor());
}
private void CompareCranes(IComparer<DrawingCrane?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowCars();
}
private readonly CranesGenericStorage _storage;
private readonly ILogger _logger;
public FormCraneCollection(ILogger<FormCraneCollection> logger)
@ -24,7 +43,7 @@ namespace HoistingCrane
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
listBoxStorages.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
@ -107,6 +126,7 @@ namespace HoistingCrane
}
}
private void ButtonAddCrane_Click(object sender, EventArgs e)
{
{
if (listBoxStorages.SelectedIndex == -1)
{
@ -140,9 +160,16 @@ namespace HoistingCrane
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.Message);
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.Message);
}
});
formCraneConfig.Show();
}
}
private void ButtonRemoveCrane_Click(object sender, EventArgs e)

View File

@ -66,4 +66,7 @@
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>367, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>27</value>
</metadata>
</root>

View File

@ -19,21 +19,26 @@ namespace HoistingCrane.Generics
_maxCount = count;
_places = new List<T?>(count);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// Добавление объекта в набор
public int Insert(T crane)
public int Insert(T crane, IEqualityComparer<T?>? equal = null)
{
if (Count >= _maxCount)
throw new StorageOverflowException(Count);
_places.Insert(0, crane);
Insert(0, crane, equal);
if (_places.Contains(null)) _places.Remove(null);
return 0;
}
public int Insert(T crane, int position)
public int Insert(int position, T crane, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount || Count >= _maxCount)
throw new CraneNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(Count);
if (equal != null && _places.Contains(crane, equal))
{
throw new ArgumentException("Добавляемый объект уже существует в коллекции");
}
_places.Insert(position, crane);
if(_places.Contains(null)) _places.Remove(null);
return position;