PIBD-14_Arinina_M.A._Simple_LabWork08 #22

Closed
mara-1 wants to merge 3 commits from LabWork08 into LabWork07
12 changed files with 277 additions and 32 deletions
Showing only changes of commit eafedb0743 - Show all commits

View File

@ -1,5 +1,6 @@
using ProjectBulldozer.Drawnings;
using ProjectBulldozer.Exceptions;
using ProjectElectroTrans.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
@ -63,7 +64,8 @@ public abstract class AbstractCompany
/// <returns></returns>
public static int operator +(AbstractCompany company, DrawningTrackedMachine trackedMachine)
{
return company._collection.Insert(trackedMachine);
return company._collection?.Insert(trackedMachine, new DrawiningMachineEqutables()) ??
throw new DrawingEquitablesException();
}
/// <summary>
@ -77,6 +79,13 @@ public abstract class AbstractCompany
return company._collection.Remove(position);
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
public void Sort(IComparer<DrawningTrackedMachine?> comparer) =>
_collection?.CollectionSort(comparer);
/// <summary>
/// Получение случайного объекта из коллекции
/// </summary>

View File

@ -24,7 +24,7 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj);
int Insert(T obj, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Добавление объекта в коллекцию на конкретную позицию
@ -32,7 +32,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="obj">Добавляемый объект</param>
/// <param name="position">Позиция</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, int position);
int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции
@ -52,10 +52,17 @@ public interface ICollectionGenericObjects<T>
/// Получение типа коллекции
/// </summary>
CollectionType GetCollectionType { get; }
/// <summary>
/// Получение объектов коллекции по одному
/// </summary>
/// <returns>Поэлементый вывод элементов коллекции</returns>
IEnumerable<T?> GetItems();
/// <summary>
/// Сортировка коллекции
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
void CollectionSort(IComparer<T?> comparer);
}

View File

@ -1,4 +1,5 @@
using ProjectBulldozer.Exceptions;
using ProjectElectroTrans.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
@ -48,28 +49,47 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
public T? Get(int position)
{
// TODO проверка позиции
if (position >= Count || position < 0) return null;
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
return _collection[position];
}
public int Insert(T obj)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO вставка в конец набора
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (comparer != null)
{
for (int i = 0; i < Count; i++)
Review

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

У списка есть метод Contains для проверки
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
}
}
if (Count + 1 > _maxCount) return -1;
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO проверка позиции
// TODO вставка по позиции
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
if (comparer != null)
{
for (int i = 0; i < Count; i++)
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
}
}
_collection.Insert(position, obj);
return position;
}
public T? Remove(int position)
{
// TODO проверка позиции
@ -87,4 +107,9 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
}
}

View File

@ -1,4 +1,5 @@
using ProjectBulldozer.Exceptions;
using ProjectElectroTrans.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
@ -6,6 +7,10 @@ using System.Text;
using System.Threading.Tasks;
namespace ProjectBulldozer.CollectionGenericObjects;
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
@ -58,9 +63,21 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
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)
{
// TODO вставка в свободное место набора
if (comparer != null)
{
for (int i = 0; i < Count; i++)
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
}
}
// вставка в свободное место набора
for (int i = 0; i < Count; i++)
{
if (_collection[i] == null)
@ -69,18 +86,26 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return i;
}
}
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// ищется свободное место после этой позиции и идет туда
// если нет после, ищем до
// TODO вставка
// проверка позиции
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position);
if (comparer != null)
{
for (int i = 0; i < Count; i++)
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
}
}
if (_collection[position] != null)
{
bool pushed = false;
@ -113,7 +138,6 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
}
// вставка
_collection[position] = obj;
return position;
}
@ -138,6 +162,15 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
}
public void CollectionSort(IComparer<T?> comparer)
{
List<T?> lst = [.. _collection];
lst.Sort(comparer.Compare);
for (int i = 0; i < _collection.Length; ++i)
{
_collection[i] = lst[i];
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBulldozer.Drawnings;
internal class DrawingMachineCompareByColor : IComparer<DrawningTrackedMachine?>
{
public int Compare(DrawningTrackedMachine? x, DrawningTrackedMachine? y)
{
if (x == null && y == null) return 0;
if (x == null || x.EntityTrackedMachine == null)
{
return 1;
}
if (y == null || y.EntityTrackedMachine == null)
{
return -1;
}
if (ToHex(x.EntityTrackedMachine.BodyColor) != ToHex(y.EntityTrackedMachine.BodyColor))
{
return String.Compare(ToHex(x.EntityTrackedMachine.BodyColor), ToHex(y.EntityTrackedMachine.BodyColor),
StringComparison.Ordinal);
}
var speedCompare = x.EntityTrackedMachine.Speed.CompareTo(y.EntityTrackedMachine.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityTrackedMachine.Weight.CompareTo(y.EntityTrackedMachine.Weight);
}
private static String ToHex(Color c)
=> $"#{c.R:X2}{c.G:X2}{c.B:X2}";
}

View File

@ -0,0 +1,67 @@
using ProjectBulldozer.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBulldozer.Drawnings;
/// <summary>
/// Реализация сравнения двух объектов класса-прорисовки
/// </summary
public class DrawiningMachineEqutables : IEqualityComparer<DrawningTrackedMachine?>
{
public bool Equals(DrawningTrackedMachine? x, DrawningTrackedMachine? y)
{
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityTrackedMachine != null && y.EntityTrackedMachine != null && x.EntityTrackedMachine.Speed != y.EntityTrackedMachine.Speed)
{
return false;
}
if (x.EntityTrackedMachine.Weight != y.EntityTrackedMachine.Weight)
{
return false;
}
if (x.EntityTrackedMachine.BodyColor != y.EntityTrackedMachine.BodyColor)
{
return false;
}
if (x is DrawningBulldozer && y is DrawningBulldozer)
{
// TODO доделать логику сравнения дополнительных параметров
if (((EntityBulldozer)x.EntityTrackedMachine).AdditionalColor !=
Review

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

Зачем неоднократные преобразования, что мешает сделать это один раз?
((EntityBulldozer)y.EntityTrackedMachine).AdditionalColor)
{
return false;
}
if (((EntityBulldozer)x.EntityTrackedMachine).AdditionalOtval !=
((EntityBulldozer)y.EntityTrackedMachine).AdditionalOtval)
{
return false;
}
if (((EntityBulldozer)x.EntityTrackedMachine).AdditionalRihl !=
((EntityBulldozer)y.EntityTrackedMachine).AdditionalRihl)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningTrackedMachine obj)
{
return obj.GetHashCode();
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBulldozer.Drawnings;
internal class DrawningMachineCompareByType : IComparer<DrawningTrackedMachine?>
{
public int Compare(DrawningTrackedMachine? x, DrawningTrackedMachine? y)
{
if (x == null || x.EntityTrackedMachine == null)
{
return -1;
}
if (y == null || y.EntityTrackedMachine == null)
{
return 1;
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityTrackedMachine.Speed.CompareTo(y.EntityTrackedMachine.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityTrackedMachine.Weight.CompareTo(y.EntityTrackedMachine.Weight);
}
}

View File

@ -77,10 +77,7 @@ public class DrawningTrackedMachine
public DrawningTrackedMachine(int speed, double weight, Color bodyColor) : this()
{
EntityTrackedMachine = new EntityTrackedMachine(speed, weight, bodyColor);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}

View File

@ -0,0 +1,13 @@
using System.Runtime.Serialization;
namespace ProjectElectroTrans.Exceptions;
public class CollectionInfoException : Exception
{
public CollectionInfoException() : base() { }
public CollectionInfoException(string message) : base(message) { }
public CollectionInfoException(string message, Exception exception) :
base(message, exception) { }
protected CollectionInfoException(SerializationInfo info, StreamingContext
contex) : base(info, contex) { }
}

View File

@ -0,0 +1,15 @@
using System.Runtime.Serialization;
using ProjectBulldozer.Drawnings;
namespace ProjectElectroTrans.Exceptions;
public class CollectionInsertException : Exception
{
public CollectionInsertException(object obj) : base($"Объект {obj} не удволетворяет уникальности") { }
public CollectionInsertException() : base() { }
public CollectionInsertException(string message) : base(message) { }
public CollectionInsertException(string message, Exception exception) :
base(message, exception) { }
protected CollectionInsertException(SerializationInfo info, StreamingContext
contex) : base(info, contex) { }
}

View File

@ -0,0 +1,13 @@
using System.Runtime.Serialization;
namespace ProjectElectroTrans.Exceptions;
public class DrawingEquitablesException : Exception
{
public DrawingEquitablesException() : base("Объекты прорисовки одинаковые") { }
public DrawingEquitablesException(string message) : base(message) { }
public DrawingEquitablesException(string message, Exception exception) :
base(message, exception) { }
protected DrawingEquitablesException(SerializationInfo info, StreamingContext
contex) : base(info, contex) { }
}

View File

@ -2,13 +2,6 @@
using ProjectBulldozer.CollectionGenericObjects;
using ProjectBulldozer.Drawnings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectBulldozer;