ISEbd-22. Baygulov A.A. Lab work 08 #9

Closed
tellsense wants to merge 4 commits from lab8 into lab7
4 changed files with 53 additions and 31 deletions
Showing only changes of commit 806b57c7fd - Show all commits

View File

@ -39,7 +39,18 @@ namespace ProjectElectricLocomotive.Generics
}
if (x is DrawingElectricLocomotive && y is DrawingElectricLocomotive)
{
// TODO доделать логику сравнения дополнительных параметров
if ((x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor != (y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor)
{
return false;
}
if ((x.EntityLocomotive as EntityElectricLocomotive).Pantograph != (y.EntityLocomotive as EntityElectricLocomotive).Pantograph)
{
return false;
}
if ((x.EntityLocomotive as EntityElectricLocomotive).Compartment != (y.EntityLocomotive as EntityElectricLocomotive).Compartment)
{
return false;
}
}
return true;
}

View File

@ -94,6 +94,10 @@ namespace ProjectElectricLocomotive
MessageBox.Show(ex.Message);
_logger.LogWarning("Не удалось добавить объект");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ButtonRemoveObject_Click(object sender, EventArgs e)
{
@ -158,19 +162,9 @@ namespace ProjectElectricLocomotive
pictureBoxCollection.Image = obj.ShowLocomotives();
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType());
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
// TODO продумать логику
}
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByColor());
private void CompareLocomotives(IComparer<DrawingLocomotive?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)

View File

@ -8,28 +8,45 @@ using ProjectElectricLocomotive.Entities;
namespace ProjectElectricLocomotive.Generics
{
internal class LocoCompareByColor : IComparer<DrawingLocomotive?>
internal class LocomotiveCompareByColor : IComparer<DrawingLocomotive?>
{
public int Compare(DrawingLocomotive? x, DrawingLocomotive? y)
Review

Требовалось сортировать по критериям: цвет, скорость, вес

Требовалось сортировать по критериям: цвет, скорость, вес
{
if (x == null || x.EntityLocomotive == null)
throw new NotImplementedException(nameof(x));
if (y == null || y.EntityLocomotive == null)
throw new NotImplementedException(nameof(y));
var bodyColor = x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name);
if (bodyColor != 0) return bodyColor;
if (x.EntityLocomotive is EntityElectricLocomotive &&
y.EntityLocomotive is EntityElectricLocomotive)
{
var addcolor = (x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name.CompareTo((y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name);
if (addcolor != 0) return addcolor;
throw new ArgumentNullException(nameof(x));
}
return 1;
if (y == null || y.EntityLocomotive == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityLocomotive.BodyColor.Name != y.EntityLocomotive.BodyColor.Name)
{
return x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is DrawingLocomotive)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawingElectricLocomotive)
{
EntityElectricLocomotive entityX = (EntityElectricLocomotive)x.EntityLocomotive;
EntityElectricLocomotive entityY = (EntityElectricLocomotive)y.EntityLocomotive;
if (entityX.AdditionalColor.Name != entityY.AdditionalColor.Name)
{
return entityX.AdditionalColor.Name.CompareTo(entityY.AdditionalColor.Name);
}
}
var speedCompare =
x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight);
}
}
}

View File

@ -34,7 +34,7 @@ namespace ProjectElectricLocomotive.Generics
{
return -1;
}
return collect._collection.Insert(locomotive);
return collect._collection.Insert(locomotive, new DrawingLocomotiveEqutables());
}
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int pos)
{