ISEbd-21 Melnikov I. O. Lab work 08 Base #11

Closed
Igor-Melnikov wants to merge 7 commits from LabWork08 into LabWork07
3 changed files with 27 additions and 14 deletions
Showing only changes of commit a28fb7a3d0 - Show all commits

View File

@ -1,17 +1,14 @@
namespace Locomotives
{
/// <summary>
/// Класс-наследник от интерфейса (реализация)
/// </summary>
/// <summary>
/// Класс-наследник от интерфейса (реализация)
/// </summary>
internal class DrawningObjectLocomotive : IDrawningObject
{
/// <summary>
/// Объект от класса отрисовки локомотива
/// </summary>
private DrawningLocomotive _locomotive = null;
public DrawningLocomotive GetLocomotive => _locomotive;
public DrawningLocomotive _locomotive { get; set; }
public DrawningObjectLocomotive(DrawningLocomotive locomotive)
{
_locomotive = locomotive;
@ -35,9 +32,14 @@
}
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;
@ -49,6 +51,7 @@
}
var locomotive = _locomotive.Locomotive;
var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive;
//проверка характеристик базовой сущности
if (locomotive.Speed != otherLocomotiveLocomotive.Speed)
{
return false;
@ -61,6 +64,7 @@
{
return false;
}
//проверка на одинаковость типов первого и второго объекта (не является ли один из них наследником, а другой базовым классом)
if (locomotive is EntityWarmlyLocomotive && otherLocomotiveLocomotive is not EntityWarmlyLocomotive)
{
return false;
@ -69,6 +73,7 @@
{
return false;
}
//если оба объекта являются продвинутыми, сравниваем дополнительные характеристики
if (locomotive is EntityWarmlyLocomotive warmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherWarmlyLocomotive)
{
if (warmlyLocomotive.AdditionalColor != otherWarmlyLocomotive.AdditionalColor)

View File

@ -1,9 +1,13 @@
namespace Locomotives
{
/// <summary>
/// Реализация класса-компаратора для сравнения по цвету
/// </summary>
internal class LocomotiveCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
//проверяем оба объекта на существование
if (x == null && y == null)
{
return 0;
@ -30,8 +34,9 @@
{
return -1;
}
var xColorName = xLocomotive.GetLocomotive.Locomotive.BodyColor.Name;
var yColorName = yLocomotive.GetLocomotive.Locomotive.BodyColor.Name;
//сравниваем цвета по названию
var xColorName = xLocomotive._locomotive.Locomotive.BodyColor.Name;
var yColorName = yLocomotive._locomotive.Locomotive.BodyColor.Name;
return xColorName.CompareTo(yColorName);
}
}

View File

@ -1,5 +1,8 @@
namespace Locomotives
{
/// <summary>
/// Реализация класса-компаратора для сортировки, сравнение по типу.
/// </summary>
internal class LocomotiveCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
@ -30,20 +33,20 @@
{
return -1;
}
if (xLocomotive?.GetLocomotive.GetType().Name != yLocomotive?.GetLocomotive.GetType().Name)
if (xLocomotive?._locomotive.GetType().Name != yLocomotive?._locomotive.GetType().Name)
{
if (xLocomotive.GetLocomotive.GetType().Name == "DrawningLocomotive")
if (xLocomotive?._locomotive.GetType().Name == "DrawningLocomotive")
{
return -1;
}
return 1;
}
var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed);
var speedCompare = xLocomotive._locomotive.Locomotive.Speed.CompareTo(yLocomotive._locomotive.Locomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight);
return xLocomotive._locomotive.Locomotive.Weight.CompareTo(yLocomotive?._locomotive.Locomotive.Weight);
}
}
}