доработка 8 лаб

This commit is contained in:
Андрей Байгулов 2023-12-24 17:27:07 +04:00
parent e23ed22c72
commit 806b57c7fd
4 changed files with 53 additions and 31 deletions

View File

@ -39,7 +39,18 @@ namespace ProjectElectricLocomotive.Generics
} }
if (x is DrawingElectricLocomotive && y is DrawingElectricLocomotive) 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; return true;
} }

View File

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

View File

@ -8,28 +8,45 @@ using ProjectElectricLocomotive.Entities;
namespace ProjectElectricLocomotive.Generics namespace ProjectElectricLocomotive.Generics
{ {
internal class LocoCompareByColor : IComparer<DrawingLocomotive?> internal class LocomotiveCompareByColor : IComparer<DrawingLocomotive?>
{ {
public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) public int Compare(DrawingLocomotive? x, DrawingLocomotive? y)
{ {
if (x == null || x.EntityLocomotive == null) 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); throw new ArgumentNullException(nameof(x));
if (addcolor != 0) return addcolor;
} }
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 -1;
} }
return collect._collection.Insert(locomotive); return collect._collection.Insert(locomotive, new DrawingLocomotiveEqutables());
} }
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int pos) public static T? operator -(LocomotivesGenericCollection<T, U> collect, int pos)
{ {