ISEbd-21 Melnikov I. O. Lab work 08 Base #11
@ -1,6 +1,6 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
internal abstract class AbstractMap
|
||||
internal abstract class AbstractMap : IEquatable<AbstractMap>
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле от интерфейса прорисовки
|
||||
@ -239,5 +239,35 @@
|
||||
/// <param name="i"></param>
|
||||
/// <param name="j"></param>
|
||||
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
||||
/// <summary>
|
||||
/// Реализация сравнения
|
||||
/// </summary>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
public bool Equals(AbstractMap? other)
|
||||
{
|
||||
if (other == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_map.GetLength(0) == other._map.GetLength(0) && _map.GetLength(1) == other._map.GetLength(1))
|
||||
{
|
||||
for (int i = 0; i < _map.GetLength(0); i++)
|
||||
{
|
||||
for (int j = 0; j < _map.GetLength(1); j++)
|
||||
{
|
||||
if (_map[i, j] != other._map[i, j])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
/// <summary>
|
||||
/// Объект от класса отрисовки локомотива
|
||||
/// </summary>
|
||||
private DrawningLocomotive _locomotive = null;
|
||||
public DrawningLocomotive _locomotive { get; set; }
|
||||
public DrawningObjectLocomotive(DrawningLocomotive locomotive)
|
||||
{
|
||||
_locomotive = locomotive;
|
||||
@ -32,5 +32,64 @@
|
||||
}
|
||||
public string GetInfo() => _locomotive?.GetDataForSave();
|
||||
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningLocomotive());
|
||||
/// <summary>
|
||||
/// Реализация проверки на равенство с другим объектом
|
||||
/// </summary>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
public bool Equals(IDrawningObject? other)
|
||||
{
|
||||
//проверка на существование второго объекта
|
||||
if (other == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var otherLocomotive = other as DrawningObjectLocomotive;
|
||||
if (otherLocomotive == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var locomotive = _locomotive.Locomotive;
|
||||
var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive;
|
||||
//проверка характеристик базовой сущности
|
||||
if (locomotive.Speed != otherLocomotiveLocomotive.Speed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive.Weight != otherLocomotiveLocomotive.Weight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive.BodyColor != otherLocomotiveLocomotive.BodyColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//проверка на одинаковость типов первого и второго объекта (не является ли один из них наследником, а другой базовым классом)
|
||||
if (locomotive is EntityWarmlyLocomotive && otherLocomotiveLocomotive is not EntityWarmlyLocomotive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive is not EntityWarmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//если оба объекта являются продвинутыми, сравниваем дополнительные характеристики
|
||||
if (locomotive is EntityWarmlyLocomotive warmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherWarmlyLocomotive)
|
||||
{
|
||||
if (warmlyLocomotive.AdditionalColor != otherWarmlyLocomotive.AdditionalColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (warmlyLocomotive.HasPipe != otherWarmlyLocomotive.HasPipe)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (warmlyLocomotive.HasFuelTank != otherWarmlyLocomotive.HasFuelTank)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,8 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.groupBoxTools = new System.Windows.Forms.GroupBox();
|
||||
this.buttonSortByColor = new System.Windows.Forms.Button();
|
||||
this.buttonSortByType = new System.Windows.Forms.Button();
|
||||
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
|
||||
this.buttonDeleteMap = new System.Windows.Forms.Button();
|
||||
this.listBoxMaps = new System.Windows.Forms.ListBox();
|
||||
@ -59,6 +61,8 @@
|
||||
//
|
||||
// groupBoxTools
|
||||
//
|
||||
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
|
||||
this.groupBoxTools.Controls.Add(this.buttonSortByType);
|
||||
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
|
||||
this.groupBoxTools.Controls.Add(this.buttonUp);
|
||||
this.groupBoxTools.Controls.Add(this.buttonDown);
|
||||
@ -77,6 +81,26 @@
|
||||
this.groupBoxTools.TabStop = false;
|
||||
this.groupBoxTools.Text = "Инструменты";
|
||||
//
|
||||
// buttonSortByColor
|
||||
//
|
||||
this.buttonSortByColor.Location = new System.Drawing.Point(31, 341);
|
||||
this.buttonSortByColor.Name = "buttonSortByColor";
|
||||
this.buttonSortByColor.Size = new System.Drawing.Size(164, 23);
|
||||
this.buttonSortByColor.TabIndex = 3;
|
||||
this.buttonSortByColor.Text = "Сортировать по цвету";
|
||||
this.buttonSortByColor.UseVisualStyleBackColor = true;
|
||||
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
|
||||
//
|
||||
// buttonSortByType
|
||||
//
|
||||
this.buttonSortByType.Location = new System.Drawing.Point(31, 312);
|
||||
this.buttonSortByType.Name = "buttonSortByType";
|
||||
this.buttonSortByType.Size = new System.Drawing.Size(164, 23);
|
||||
this.buttonSortByType.TabIndex = 3;
|
||||
this.buttonSortByType.Text = "Сортировать по типу";
|
||||
this.buttonSortByType.UseVisualStyleBackColor = true;
|
||||
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
|
||||
//
|
||||
// groupBoxMaps
|
||||
//
|
||||
this.groupBoxMaps.Anchor = System.Windows.Forms.AnchorStyles.Right;
|
||||
@ -192,7 +216,7 @@
|
||||
//
|
||||
// buttonShowOnMap
|
||||
//
|
||||
this.buttonShowOnMap.Location = new System.Drawing.Point(31, 426);
|
||||
this.buttonShowOnMap.Location = new System.Drawing.Point(31, 526);
|
||||
this.buttonShowOnMap.Name = "buttonShowOnMap";
|
||||
this.buttonShowOnMap.Size = new System.Drawing.Size(164, 26);
|
||||
this.buttonShowOnMap.TabIndex = 5;
|
||||
@ -202,7 +226,7 @@
|
||||
//
|
||||
// buttonShowStorage
|
||||
//
|
||||
this.buttonShowStorage.Location = new System.Drawing.Point(31, 394);
|
||||
this.buttonShowStorage.Location = new System.Drawing.Point(31, 494);
|
||||
this.buttonShowStorage.Name = "buttonShowStorage";
|
||||
this.buttonShowStorage.Size = new System.Drawing.Size(164, 26);
|
||||
this.buttonShowStorage.TabIndex = 4;
|
||||
@ -212,7 +236,7 @@
|
||||
//
|
||||
// buttonRemoveLocomotive
|
||||
//
|
||||
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 362);
|
||||
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 462);
|
||||
this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
|
||||
this.buttonRemoveLocomotive.Size = new System.Drawing.Size(164, 26);
|
||||
this.buttonRemoveLocomotive.TabIndex = 3;
|
||||
@ -222,7 +246,7 @@
|
||||
//
|
||||
// maskedTextBoxPosition
|
||||
//
|
||||
this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 333);
|
||||
this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 433);
|
||||
this.maskedTextBoxPosition.Mask = "00";
|
||||
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
|
||||
this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23);
|
||||
@ -230,7 +254,7 @@
|
||||
//
|
||||
// buttonAddCar
|
||||
//
|
||||
this.buttonAddCar.Location = new System.Drawing.Point(31, 291);
|
||||
this.buttonAddCar.Location = new System.Drawing.Point(31, 391);
|
||||
this.buttonAddCar.Name = "buttonAddCar";
|
||||
this.buttonAddCar.Size = new System.Drawing.Size(164, 26);
|
||||
this.buttonAddCar.TabIndex = 1;
|
||||
@ -336,5 +360,7 @@
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private Button buttonSortByColor;
|
||||
private Button buttonSortByType;
|
||||
}
|
||||
}
|
@ -135,6 +135,11 @@ namespace Locomotives
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
catch (NotUniqueObjectException ex)
|
||||
{
|
||||
MessageBox.Show($"Ошибка добавления: {ex.Message}");
|
||||
_logger.Warning($"Не удалось добавить объект: {ex.Message}");
|
||||
}
|
||||
catch (StorageOverflowException ex)
|
||||
{
|
||||
MessageBox.Show($"Ошибка добавления: {ex.Message}");
|
||||
@ -296,5 +301,33 @@ namespace Locomotives
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Сортировка по типу
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonSortByType_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByType());
|
||||
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
/// <summary>
|
||||
/// Сортировка по цвету
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonSortByColor_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByColor());
|
||||
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// Интерфейс для отрисовки
|
||||
/// </summary>
|
||||
internal interface IDrawningObject
|
||||
internal interface IDrawningObject : IEquatable<IDrawningObject>
|
||||
{
|
||||
/// <summary>
|
||||
/// Шаг перемещения объекта
|
||||
|
43
Locomotives/Locomotives/LocomotiveCompareByColor.cs
Normal file
43
Locomotives/Locomotives/LocomotiveCompareByColor.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация класса-компаратора для сравнения по цвету
|
||||
/// </summary>
|
||||
internal class LocomotiveCompareByColor : IComparer<IDrawningObject>
|
||||
{
|
||||
public int Compare(IDrawningObject? x, IDrawningObject? y)
|
||||
{
|
||||
//проверяем оба объекта на существование
|
||||
if (x == null && y == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (x == null && y != null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (x != null && y == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
var xLocomotive = x as DrawningObjectLocomotive;
|
||||
var yLocomotive = y as DrawningObjectLocomotive;
|
||||
if (xLocomotive == null && yLocomotive == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (xLocomotive == null && yLocomotive != null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (xLocomotive != null && yLocomotive == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
//сравниваем цвета по названию
|
||||
var xColorName = xLocomotive._locomotive.Locomotive.BodyColor.Name;
|
||||
var yColorName = yLocomotive._locomotive.Locomotive.BodyColor.Name;
|
||||
return xColorName.CompareTo(yColorName);
|
||||
}
|
||||
}
|
||||
}
|
52
Locomotives/Locomotives/LocomotiveCompareByType.cs
Normal file
52
Locomotives/Locomotives/LocomotiveCompareByType.cs
Normal file
@ -0,0 +1,52 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация класса-компаратора для сортировки, сравнение по типу.
|
||||
/// </summary>
|
||||
internal class LocomotiveCompareByType : IComparer<IDrawningObject>
|
||||
{
|
||||
public int Compare(IDrawningObject? x, IDrawningObject? y)
|
||||
{
|
||||
if (x == null && y == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (x == null && y != null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (x != null && y == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
var xLocomotive = x as DrawningObjectLocomotive;
|
||||
var yLocomotive = y as DrawningObjectLocomotive;
|
||||
if (xLocomotive == null && yLocomotive == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (xLocomotive == null && yLocomotive != null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (xLocomotive != null && yLocomotive == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (xLocomotive?._locomotive.GetType().Name != yLocomotive?._locomotive.GetType().Name)
|
||||
{
|
||||
if (xLocomotive?._locomotive.GetType().Name == "DrawningLocomotive")
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
var speedCompare = xLocomotive._locomotive.Locomotive.Speed.CompareTo(yLocomotive._locomotive.Locomotive.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
return xLocomotive._locomotive.Locomotive.Weight.CompareTo(yLocomotive?._locomotive.Locomotive.Weight);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
internal class MapWithSetLocomotivesGeneric<T, U>
|
||||
where T : class, IDrawningObject
|
||||
where T : class, IDrawningObject, IEquatable<T>
|
||||
where U : AbstractMap
|
||||
{
|
||||
/// <summary>
|
||||
@ -201,5 +201,9 @@
|
||||
_setLocomotives.Insert(DrawningObjectLocomotive.Create(record) as T);
|
||||
}
|
||||
}
|
||||
public void Sort(IComparer<T> comparer)
|
||||
{
|
||||
_setLocomotives.SortSet(comparer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
Locomotives/Locomotives/NotUniqueObjectException.cs
Normal file
13
Locomotives/Locomotives/NotUniqueObjectException.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Locomotives
|
||||
{
|
||||
[Serializable]
|
||||
internal class NotUniqueObjectException : ApplicationException
|
||||
{
|
||||
public NotUniqueObjectException() : base("Такой объект уже есть в коллекции") { }
|
||||
public NotUniqueObjectException(string message) : base(message) { }
|
||||
public NotUniqueObjectException(string message, Exception Exception) : base(message, Exception) { }
|
||||
protected NotUniqueObjectException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
internal class SetLocomotivesGeneric<T>
|
||||
where T : class
|
||||
where T : class, IEquatable<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Список объектов, которые храним
|
||||
@ -28,6 +28,10 @@
|
||||
/// <returns></returns>
|
||||
public int Insert(T locomotive)
|
||||
{
|
||||
if (_places.Contains(locomotive))
|
||||
{
|
||||
throw new NotUniqueObjectException();
|
||||
}
|
||||
if (_places.Count == 0)
|
||||
{
|
||||
_places.Add(locomotive);
|
||||
@ -119,5 +123,13 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
public void SortSet(IComparer<T> comparer)
|
||||
{
|
||||
if (comparer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_places.Sort(comparer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user