Compare commits

...

2 Commits

Author SHA1 Message Date
Kirill
a0a018477f Начало 2023-12-27 08:38:38 +04:00
Kirill
55bb9dd042 Конец 7ой лабы2 2023-12-26 21:33:04 +04:00
6 changed files with 151 additions and 13 deletions

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.DrawningObjects;
using ProjectTractor.Entities;
using System.Diagnostics.CodeAnalysis;
namespace ProjectTractor.Generics
{
internal class DrawiningTractorEqutables : IEqualityComparer<DrawningTractor?>
{
public bool Equals(DrawningTractor? x, DrawningTractor? y)
{
if (x == null || x.EntityTractor == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityTractor == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityTractor.Speed != y.EntityTractor.Speed)
{
return false;
}
if (x.EntityTractor.Weight != y.EntityTractor.Weight)
{
return false;
}
if (x.EntityTractor.BodyColor != y.EntityTractor.BodyColor)
{
return false;
}
if (x is DrawningBulldoser && y is DrawningBulldoser)
{
// TODO доделать логику сравнения дополнительных параметров
}
return true;
}
public int GetHashCode([DisallowNull] DrawningTractor obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.Exceptions;
namespace ProjectTractor.Generics
{
@ -31,15 +32,21 @@ namespace ProjectTractor.Generics
_places = new List<T?>(count);
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) =>
_places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="tractor">Добавляемый трактор</param>
/// <returns></returns>
public bool Insert(T tractor)
public bool Insert(T tractor, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
return false;
Insert(tractor, 0);
throw new StorageOverflowException(_maxCount);
Insert(tractor, 0, equal);
return true;
}
/// <summary>
@ -48,24 +55,29 @@ namespace ProjectTractor.Generics
/// <param name="tractor">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T tractor, int position)
public void Insert(T tractor, int position, IEqualityComparer<T>? equal = null)
{
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
return false;
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
throw new Exception("Неверная позиция для вставки");
if (equal != null)
{
if (_places.Contains(tractor, equal))
throw new ArgumentException(nameof(tractor));
}
_places.Insert(position, tractor);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
public void Remove(int position)
{
if (!(position >= 0 && position < Count))
return false;
throw new TractorNotFoundException(position);
_places.RemoveAt(position);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.DrawningObjects;
namespace ProjectTractor.Generics
{
internal class TractorCompareByColor : IComparer<DrawningTractor?>
{
public int Compare(DrawningTractor? x, DrawningTractor? y)
{
if (x == null || x.EntityTractor == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityTractor == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityTractor.BodyColor.Name != y.EntityTractor.BodyColor.Name)
{
return x.EntityTractor.BodyColor.Name.CompareTo(y.EntityTractor.BodyColor.Name);
}
var speedCompare = x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight);
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.DrawningObjects;
namespace ProjectTractor.Generics
{
internal class TractorCompareByType : IComparer<DrawningTractor?>
{
public int Compare(DrawningTractor? x, DrawningTractor? y)
{
if (x == null || x.EntityTractor == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityTractor == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare =
x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight);
}
}
}

View File

@ -55,6 +55,12 @@ namespace ProjectTractor.Generics
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) =>
_collection.SortSet(comparer);
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>

View File

@ -139,12 +139,12 @@ namespace ProjectTractor
string[] strings = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strings == null || strings.Length == 0)
{
throw new Exception("Нет данных для загрузки");
throw new IOException("Нет данных для загрузки");
}
if (!strings[0].StartsWith("TractorStorage"))
{
//если нет такой записи, то это не те данные
throw new Exception("Неверный формат данных");
throw new IOException("Неверный формат данных");
}
_tractorStorages.Clear();
do
@ -164,7 +164,7 @@ namespace ProjectTractor
{
if (!(collection + tractor))
{
throw new Exception("Ошибка добавления в коллекцию");
throw new ArgumentNullException("Ошибка добавления в коллекцию");
}
}
}