PIbd-11.Basalov.A.D.LabWork08.Simple #15

Closed
Tonby73 wants to merge 6 commits from LabWork8 into LabWork7
7 changed files with 152 additions and 14 deletions
Showing only changes of commit 733bc788b6 - Show all commits

View File

@ -62,7 +62,7 @@ public abstract class AbstractCompany
public static int operator +(AbstractCompany company, DrawningLocomotive locomotive)
{
return company._collection.Insert(locomotive);
return company._collection.Insert(locomotive, new DrawiningLocomotiveEqutables()) ?? false;
}
/// <summary>
/// Перегрузка оператора удаления для класса

View File

@ -27,22 +27,23 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj);
/// <summary>
/// Добавление объекта в коллекцию
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <param name="comparer">Cравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Добавление объекта в коллекцию на конкретную позицию
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <param name="position">Позиция</param>
/// <param name="comparer">Cравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, int position);
int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции
/// </summary>
/// <param name="position">Позиция</param>
/// <returns>true - удаление прошло удачно, false - удаление не удалось</returns>
T Remove(int position);
T Remove(int position);
/// <summary>
/// Получение объекта по позиции
/// </summary>
@ -60,4 +61,11 @@ T Remove(int position);
/// </summary>
/// <returns>Поэлементый вывод элементов коллекции</returns>
IEnumerable<T?> GetItems();
/// <summary>
/// Сортировка коллекции
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
void CollectionSort(IComparer<T?> comparer);
}

View File

@ -55,21 +55,25 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects
return _collection[position];
}
public int Insert(T obj)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// выброс ошибки если переполнение
//выброс ошибки если такой объект уже есть в коллекции
if (Count == _maxCount) throw new CollectionOverflowException(Count);
_collection.Add(obj);
return Count;
Review

У списка есть метод Contains для проверки

У списка есть метод Contains для проверки
}
public int Insert(T obj, int position)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// проверка, что не превышено максимальное количество элементов
// проверка позиции
// вставка по позиции
// выброс ошибки, если переполнение
// выброс ошибки если выход за границу
//выброс ошибки если такой объект уже есть в коллекции
if (Count == _maxCount) throw new CollectionOverflowException(Count);
@ -98,5 +102,11 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
throw new NotImplementedException();
}
}
}

View File

@ -56,11 +56,13 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects
if (_collection[position] == null) throw new ObjectNotFoundException(position);
return _collection[position];
}
public int Insert(T obj)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// вставка в свободное место набора
// выброс ошибки, если переполнение
//выброс ошибки, если выход за границы массива
//выброс ошибки если такой объект уже есть в коллекции
for (int i = 0; i < Count; i++)
{
if (_collection[i] == null)
@ -72,7 +74,7 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// проверка позиции
// проверка, что элемент массива по этой позиции пустой, если нет, то
@ -80,6 +82,7 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects
// вставка
//выброс ошибки, если переполнение
//выброс ошибки, если выход за границы массива
//выброс ошибки если такой объект уже есть в коллекции
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
if (_collection[position] == null)
@ -128,5 +131,11 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
{
Array.Sort(_collection, comparer);
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Drawnings;
/// <summary>
/// Реализация сравнения двух объектов класса-прорисовки
/// </summary>
public class DrawiningLocomotiveEqutables : IEqualityComparer<DrawningLocomotive?>
{
public bool Equals(DrawningLocomotive? x, DrawningLocomotive? y)
{
if (x == null || x.EntityLocomotive == null)
{
return false;
}
if (y == null || y.EntityLocomotive == null)
{
return false;
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityLocomotive.Speed != y.EntityLocomotive.Speed)
{
return false;
}
if (x.EntityLocomotive.Weight != y.EntityLocomotive.Weight)
{
return false;
}
if (x.EntityLocomotive.BodyColor != y.EntityLocomotive.BodyColor)
{
return false;
}
if (x is DrawningElectricLocomotive && y is DrawningElectricLocomotive)
{
Review

Зачем неоднократные преобразования, что мешает сделать это один раз?

Зачем неоднократные преобразования, что мешает сделать это один раз?
// TODO доделать логику сравнения дополнительных параметров
}
return true;
}
public int GetHashCode([DisallowNull] DrawningLocomotive obj)
{
return obj.GetHashCode();
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Drawnings
{
/// <summary>
/// Сравнение по цвету, скорости, весу
/// </summary>
public class DrawningCarCompareByColor : IComparer<DrawningLocomotive?>
{
public int Compare(DrawningLocomotive? x, DrawningLocomotive? y)
{
// TODO прописать логику сравения по цветам, скорости, весу
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Drawnings;
/// <summary>
/// Сравнение по типу, скорости, весу
/// </summary>
public class DrawningCarCompareByType : IComparer<DrawningLocomotive?>
{
public int Compare(DrawningLocomotive? x, DrawningLocomotive? y)
{
if (x == null || x.EntityLocomotive == null)
{
return -1;
}
if (y == null || y.EntityLocomotive == null)
{
return 1;
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight);
}
}