Compare commits
3 Commits
LabWork07
...
LabWork_08
| Author | SHA1 | Date | |
|---|---|---|---|
| 55d4fe2ed9 | |||
| a4c7b2d9a3 | |||
| 587bc65760 |
@@ -59,7 +59,7 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
/// <returns></returns>
|
||||
public static bool operator +(AbstractCompany company, DrawningBoat boat)
|
||||
{
|
||||
return company._collection?.Insert(boat) ?? false;
|
||||
return company._collection?.Insert(boat, new DrawningBoatEqutables()) ?? false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,6 +103,12 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка
|
||||
/// </summary>
|
||||
/// <param name="comparer">Сравнитель объектов</param>
|
||||
public void Sort(IComparer<DrawningBoat?> comparer) => _collection?.CollectionSort(comparer);
|
||||
|
||||
/// <summary>
|
||||
/// Вывод заднего фона
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
namespace MotorBoat.CollectionGenericObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, хранящиий информацию по коллекции
|
||||
/// </summary>
|
||||
public class CollectionInfo : IEquatable<CollectionInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// Название
|
||||
/// </summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Тип
|
||||
/// </summary>
|
||||
public CollectionType CollectionType { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Описание
|
||||
/// </summary>
|
||||
public string Description { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly string _separator = "-";
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="name">Название</param>
|
||||
/// <param name="collectionType">Тип</param>
|
||||
/// <param name="description">Описание</param>
|
||||
public CollectionInfo(string name, CollectionType collectionType, string description)
|
||||
{
|
||||
Name = name;
|
||||
CollectionType = collectionType;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="data">Строка</param>
|
||||
/// <returns>Объект или null</returns>
|
||||
public static CollectionInfo? GetCollectionInfo(string data)
|
||||
{
|
||||
string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (strs.Length < 1 || strs.Length > 3)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CollectionInfo(strs[0], (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name + _separator + CollectionType + _separator + Description;
|
||||
}
|
||||
|
||||
public bool Equals(CollectionInfo? other)
|
||||
{
|
||||
return Name == other?.Name;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return Equals(obj as CollectionInfo);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Name.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,16 +21,18 @@
|
||||
/// Добавление объекта в коллекцию
|
||||
/// </summary>
|
||||
/// <param name="obj">Добавляемый объект</param>
|
||||
/// <param name="comparer">Cравнение двух объектов</param>
|
||||
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
|
||||
bool Insert(T obj);
|
||||
bool 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>
|
||||
bool Insert(T obj, int position);
|
||||
bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
|
||||
|
||||
/// <summary>
|
||||
/// Удаление объекта из коллекции с конкретной позиции
|
||||
@@ -56,5 +58,11 @@
|
||||
/// </summary>
|
||||
/// <returns>Поэлементый вывод элементов коллекции</returns>
|
||||
IEnumerable<T?> GetItems();
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка коллекции
|
||||
/// </summary>
|
||||
/// <param name="comparer">Сравнитель объектов</param>
|
||||
void CollectionSort(IComparer<T?> comparer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,44 +49,50 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
{
|
||||
|
||||
if (position < 0 || position >= _maxCount)
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки если выход за границы массива--------------------------//
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
{
|
||||
throw new PositionOutOfCollectionException(position);
|
||||
}
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
public bool Insert(T obj)
|
||||
public bool Insert(T obj, IEqualityComparer<T?>? comparer = null)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab08 - выборс ошибки, если такой объект есть в коллекции---------------//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
if (Count == _maxCount)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки при переполнении--------------------------//
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
throw new CollectionOverflowException(_maxCount);
|
||||
}
|
||||
if (_collection.Contains(obj, comparer))
|
||||
{
|
||||
throw new AlreadyExistException();
|
||||
}
|
||||
_collection.Add(obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Insert(T obj, int position)
|
||||
public bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab08 - выборс ошибки, если такой объект есть в коллекции---------------//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
if (position < 0 || position >= _maxCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (Count == _maxCount)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки при переполнении--------------------------//
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
throw new CollectionOverflowException(_maxCount);
|
||||
}
|
||||
if (_collection.Contains(obj, comparer))
|
||||
{
|
||||
throw new AlreadyExistException();
|
||||
}
|
||||
_collection.Insert(position, obj);
|
||||
return true;
|
||||
}
|
||||
@@ -95,10 +101,6 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
{
|
||||
if (/*_collection.Count == 0 || position < 0 || */position > _collection.Count)
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки если выход за границы массива--------------------------//
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
throw new PositionOutOfCollectionException(position);
|
||||
}
|
||||
_collection.RemoveAt(position);
|
||||
@@ -112,5 +114,12 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
yield return _collection[i];
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
//---------------Lab08 - добавить код---------------//
|
||||
/////////////////////////////////////////////////////
|
||||
///как и проверка на равенство идентично, у листа встроенный метод сорт точка, у массива класс аррэй точка
|
||||
///туда передаем компэир который говорит как мы хотим сравнивать и внутри будет вызываться метод компэир
|
||||
public void CollectionSort(IComparer<T?> comparer) => _collection.Sort(comparer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,17 +52,13 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
public T? Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= _collection.Length)
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки если выход за границы массива--------------------------//
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
{
|
||||
throw new PositionOutOfCollectionException(position);
|
||||
}
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
public bool Insert(T obj)
|
||||
public bool Insert(T obj, IEqualityComparer<T?>? comparer = null)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
@@ -71,12 +67,17 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
int result = _collection.Count(s => s == null);
|
||||
if (result == 0)
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки при переполнении--------------------------//
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
throw new CollectionOverflowException(Count);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab08 - выборс ошибки, если такой объект есть в коллекции---------------//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
if (_collection.Contains(obj, comparer))
|
||||
{
|
||||
throw new AlreadyExistException();
|
||||
}
|
||||
for (int i = 0; i < _collection.Length; i++)
|
||||
{
|
||||
if (_collection[i] == null)
|
||||
@@ -88,47 +89,7 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
return false;
|
||||
}
|
||||
|
||||
//public bool Insert(T obj)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// if (obj == null)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// int result = _collection.Count(s => s == null);
|
||||
|
||||
// if (result == 0)
|
||||
// {
|
||||
// ////////////////////////////////////////////////////////////////////////////////////
|
||||
// //---------------Lab07 - Выброс ошибки при переполнении--------------------------//
|
||||
// //////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// throw new CollectionOverflowException(Count);
|
||||
// }
|
||||
|
||||
// for (int i = 0; i < _collection.Length; i++)
|
||||
// {
|
||||
// if (_collection[i] == null)
|
||||
// {
|
||||
// _collection[i] = obj;
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return false;
|
||||
// }
|
||||
// catch (CollectionOverflowException ex)
|
||||
// {
|
||||
// // Обработка исключения при переполнении коллекции
|
||||
// Console.WriteLine($"Ошибка: {ex.Message}");
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public bool Insert(T obj, int position)
|
||||
public bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
@@ -139,6 +100,15 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
throw new PositionOutOfCollectionException(position);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab08 - выборс ошибки, если такой объект есть в коллекции---------------//
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
if (_collection.Contains(obj, comparer))
|
||||
{
|
||||
throw new AlreadyExistException();
|
||||
}
|
||||
|
||||
int result = _collection.Count(s => s == null);
|
||||
if (result == 0)
|
||||
{
|
||||
@@ -173,18 +143,10 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
{
|
||||
if (position < 0 || position >= _collection.Length)
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки если выход за границы массива--------------------------//
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
throw new PositionOutOfCollectionException(position);
|
||||
}
|
||||
if (_collection[position] == null)
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab07 - Выброс ошибки если объект пустой--------------------------//
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
throw new ObjectNotFoundException(position);
|
||||
}
|
||||
_collection[position] = null;
|
||||
@@ -198,5 +160,13 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
yield return _collection[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void CollectionSort(IComparer<T?> comparer)
|
||||
{
|
||||
if (_collection != null && _collection.Length > 0)
|
||||
{
|
||||
Array.Sort(_collection, comparer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using MotorBoat.Drawnings;
|
||||
using MotorBoat.Exceptions;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MotorBoat.CollectionGenericObjects
|
||||
@@ -14,12 +16,12 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
/// <summary>
|
||||
/// Словарь (хранилище) с коллекциями
|
||||
/// </summary>
|
||||
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
|
||||
readonly Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
|
||||
|
||||
/// <summary>
|
||||
/// Возвращение списка названий коллекций
|
||||
/// </summary>
|
||||
public List<string> Keys => _storages.Keys.ToList();
|
||||
public List<CollectionInfo> Keys => _storages.Keys.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Ключевое слово, с которого должен начинаться файл
|
||||
@@ -41,7 +43,7 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
/// </summary>
|
||||
public StorageCollection()
|
||||
{
|
||||
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
|
||||
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -51,20 +53,23 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
/// <param name="collectionType">тип коллекции</param>
|
||||
public void AddCollection(string name, CollectionType collectionType)
|
||||
{
|
||||
if (name == null || Keys.Contains(name))
|
||||
CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
|
||||
|
||||
if (name == null || Keys.Contains(collectionInfo))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (collectionType)
|
||||
{
|
||||
case CollectionType.Massive:
|
||||
_storages.Add(name, new MassiveGenericObjects<T>());
|
||||
_storages.Add(collectionInfo, new MassiveGenericObjects<T>());
|
||||
break;
|
||||
case CollectionType.List:
|
||||
_storages.Add(name, new ListGenericObjects<T>());
|
||||
break;
|
||||
case CollectionType.None:
|
||||
_storages.Add(collectionInfo, new ListGenericObjects<T>());
|
||||
break;
|
||||
default:
|
||||
return; // Обработка других типов, если необходимо
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,11 +79,12 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
/// <param name="name">Название коллекции</param>
|
||||
public void DelCollection(string name)
|
||||
{
|
||||
if (name == null || !Keys.Contains(name))
|
||||
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
|
||||
if (name == null || !Keys.Contains(collectionInfo))
|
||||
{
|
||||
return;
|
||||
}
|
||||
_storages.Remove(name);
|
||||
_storages.Remove(collectionInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -90,10 +96,12 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
{
|
||||
get
|
||||
{
|
||||
return _storages.GetValueOrDefault(name, null);
|
||||
return _storages.GetValueOrDefault(new CollectionInfo(name, CollectionType.Massive, string.Empty), null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Сохранение информации по автомобилям в хранилище в файл
|
||||
/// </summary>
|
||||
@@ -113,7 +121,7 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
StringBuilder sb = new();
|
||||
|
||||
sb.Append(_collectionKey);
|
||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
|
||||
{
|
||||
sb.Append(Environment.NewLine);
|
||||
// не сохраняем пустые коллекции
|
||||
@@ -121,11 +129,8 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
sb.Append(value.Key);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
sb.Append(value.Value.GetCollectionType);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
sb.Append(value.Value.MaxCount);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
|
||||
@@ -177,7 +182,6 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
|
||||
if (!strs[0].Equals(_collectionKey))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
throw new Exception("В файле неверные данные");
|
||||
}
|
||||
|
||||
@@ -185,17 +189,18 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
foreach (string data in strs)
|
||||
{
|
||||
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 4)
|
||||
if (record.Length != 3)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType) ??
|
||||
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
|
||||
throw new Exception("Не удалось определить информацию коллекции:" + record[0]);
|
||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
|
||||
throw new Exception("Не удалось определить тип коллекции:" + record[1]);
|
||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||
collection.MaxCount = Convert.ToInt32(record[1]);
|
||||
|
||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
if (elem?.CreateDrawningBoat() is T boat)
|
||||
@@ -214,7 +219,7 @@ namespace MotorBoat.CollectionGenericObjects
|
||||
}
|
||||
}
|
||||
|
||||
_storages.Add(record[0], collection);
|
||||
_storages.Add(collectionInfo, collection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
34
MotorBoat/MotorBoat/Drawnings/DrawningBoatCompareByColor.cs
Normal file
34
MotorBoat/MotorBoat/Drawnings/DrawningBoatCompareByColor.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace MotorBoat.Drawnings
|
||||
{
|
||||
/// <summary>
|
||||
/// Сравнение по цвету, скорости, весу
|
||||
/// </summary>
|
||||
public class DrawningBoatCompareByColor : IComparer<DrawningBoat?>
|
||||
{
|
||||
public int Compare(DrawningBoat? x, DrawningBoat? y)
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab08 - прописать логику сравения по цветам, скорости, весу---------------//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
if (x == null || x.EntityBoat == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (y == null || y.EntityBoat == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (x.EntityBoat.BodyColor.Name != y.EntityBoat.BodyColor.Name)
|
||||
{
|
||||
return x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name);
|
||||
}
|
||||
var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
MotorBoat/MotorBoat/Drawnings/DrawningBoatCompareByType.cs
Normal file
34
MotorBoat/MotorBoat/Drawnings/DrawningBoatCompareByType.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace MotorBoat.Drawnings
|
||||
{
|
||||
/// <summary>
|
||||
/// Сравнение по типу, скорости, весу
|
||||
/// </summary>
|
||||
public class DrawningBoatCompareByType : IComparer<DrawningBoat?>
|
||||
{
|
||||
public int Compare(DrawningBoat? x, DrawningBoat? y)
|
||||
{
|
||||
if (x == null || x.EntityBoat == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (y == null || y.EntityBoat == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return x.GetType().Name.CompareTo(y.GetType().Name);
|
||||
}
|
||||
|
||||
var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
|
||||
return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight);
|
||||
}
|
||||
}
|
||||
}
|
||||
78
MotorBoat/MotorBoat/Drawnings/DrawningBoatEqutables.cs
Normal file
78
MotorBoat/MotorBoat/Drawnings/DrawningBoatEqutables.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using MotorBoat.Entities;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace MotorBoat.Drawnings
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация сравнения двух объектов класса-прорисовки
|
||||
/// </summary>
|
||||
public class DrawningBoatEqutables : IEqualityComparer<DrawningBoat?>
|
||||
{
|
||||
public bool Equals(DrawningBoat? x, DrawningBoat? y)
|
||||
{
|
||||
if (x == null || x.EntityBoat == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (y == null || y.EntityBoat == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x.EntityBoat.Speed != y.EntityBoat.Speed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x.EntityBoat.Weight != y.EntityBoat.Weight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x is DrawningMotorBoat && y is DrawningMotorBoat)
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//---------------Lab08 - доделать логику сравнения дополнительных параметров---------------//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
EntityMotorBoat entityMotorBoatX = (EntityMotorBoat)x.EntityBoat;
|
||||
EntityMotorBoat entityMotorBoatY = (EntityMotorBoat)y.EntityBoat;
|
||||
if (entityMotorBoatX.AdditionalColor != entityMotorBoatY.AdditionalColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (entityMotorBoatX.AddTwoMotors != entityMotorBoatY.AddTwoMotors)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (entityMotorBoatX.Sofa != entityMotorBoatY.Sofa)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (entityMotorBoatX.SportLines != entityMotorBoatY.SportLines)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int GetHashCode([DisallowNull] DrawningBoat obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
16
MotorBoat/MotorBoat/Exceptions/AlreadyExistException.cs
Normal file
16
MotorBoat/MotorBoat/Exceptions/AlreadyExistException.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace MotorBoat.Exceptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Объект уже есть в коллекции
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
internal class AlreadyExistException: ApplicationException
|
||||
{
|
||||
public AlreadyExistException() : base("Объект уже есть в коллекции") { }
|
||||
public AlreadyExistException(string message) : base(message) { }
|
||||
public AlreadyExistException(string message, Exception exception) : base(message, exception) { }
|
||||
protected AlreadyExistException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
|
||||
}
|
||||
}
|
||||
44
MotorBoat/MotorBoat/FormBoatCollection.Designer.cs
generated
44
MotorBoat/MotorBoat/FormBoatCollection.Designer.cs
generated
@@ -52,6 +52,8 @@
|
||||
loadToolStripMenuItem = new ToolStripMenuItem();
|
||||
saveFileDialog = new SaveFileDialog();
|
||||
openFileDialog = new OpenFileDialog();
|
||||
buttonSortByColor = new Button();
|
||||
buttonSortByType = new Button();
|
||||
groupBoxTools.SuspendLayout();
|
||||
panelCompanyTools.SuspendLayout();
|
||||
panelStorage.SuspendLayout();
|
||||
@@ -75,15 +77,17 @@
|
||||
//
|
||||
// panelCompanyTools
|
||||
//
|
||||
panelCompanyTools.Controls.Add(buttonSortByColor);
|
||||
panelCompanyTools.Controls.Add(buttonSortByType);
|
||||
panelCompanyTools.Controls.Add(buttonAddBoat);
|
||||
panelCompanyTools.Controls.Add(buttonRefresh);
|
||||
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
|
||||
panelCompanyTools.Controls.Add(buttonGoToCheck);
|
||||
panelCompanyTools.Controls.Add(buttonRemoveBoat);
|
||||
panelCompanyTools.Dock = DockStyle.Bottom;
|
||||
panelCompanyTools.Location = new Point(3, 362);
|
||||
panelCompanyTools.Location = new Point(3, 301);
|
||||
panelCompanyTools.Name = "panelCompanyTools";
|
||||
panelCompanyTools.Size = new Size(194, 143);
|
||||
panelCompanyTools.Size = new Size(194, 204);
|
||||
panelCompanyTools.TabIndex = 9;
|
||||
//
|
||||
// buttonAddBoat
|
||||
@@ -121,7 +125,7 @@
|
||||
// buttonGoToCheck
|
||||
//
|
||||
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonGoToCheck.Location = new Point(12, 89);
|
||||
buttonGoToCheck.Location = new Point(14, 89);
|
||||
buttonGoToCheck.Name = "buttonGoToCheck";
|
||||
buttonGoToCheck.Size = new Size(167, 23);
|
||||
buttonGoToCheck.TabIndex = 5;
|
||||
@@ -142,7 +146,7 @@
|
||||
//
|
||||
// buttonCreateCompany
|
||||
//
|
||||
buttonCreateCompany.Location = new Point(15, 303);
|
||||
buttonCreateCompany.Location = new Point(15, 272);
|
||||
buttonCreateCompany.Name = "buttonCreateCompany";
|
||||
buttonCreateCompany.Size = new Size(173, 23);
|
||||
buttonCreateCompany.TabIndex = 8;
|
||||
@@ -162,12 +166,12 @@
|
||||
panelStorage.Dock = DockStyle.Top;
|
||||
panelStorage.Location = new Point(3, 19);
|
||||
panelStorage.Name = "panelStorage";
|
||||
panelStorage.Size = new Size(194, 249);
|
||||
panelStorage.Size = new Size(194, 218);
|
||||
panelStorage.TabIndex = 7;
|
||||
//
|
||||
// buttonCollectionDel
|
||||
//
|
||||
buttonCollectionDel.Location = new Point(12, 219);
|
||||
buttonCollectionDel.Location = new Point(12, 189);
|
||||
buttonCollectionDel.Name = "buttonCollectionDel";
|
||||
buttonCollectionDel.Size = new Size(173, 23);
|
||||
buttonCollectionDel.TabIndex = 6;
|
||||
@@ -181,7 +185,7 @@
|
||||
listBoxCollection.ItemHeight = 15;
|
||||
listBoxCollection.Location = new Point(12, 119);
|
||||
listBoxCollection.Name = "listBoxCollection";
|
||||
listBoxCollection.Size = new Size(173, 94);
|
||||
listBoxCollection.Size = new Size(173, 64);
|
||||
listBoxCollection.TabIndex = 5;
|
||||
//
|
||||
// buttonCollectionAdd
|
||||
@@ -238,7 +242,7 @@
|
||||
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxSelectorCompany.FormattingEnabled = true;
|
||||
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
|
||||
comboBoxSelectorCompany.Location = new Point(15, 274);
|
||||
comboBoxSelectorCompany.Location = new Point(15, 243);
|
||||
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
|
||||
comboBoxSelectorCompany.Size = new Size(173, 23);
|
||||
comboBoxSelectorCompany.TabIndex = 0;
|
||||
@@ -294,6 +298,28 @@
|
||||
//
|
||||
openFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// buttonSortByColor
|
||||
//
|
||||
buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonSortByColor.Location = new Point(14, 174);
|
||||
buttonSortByColor.Name = "buttonSortByColor";
|
||||
buttonSortByColor.Size = new Size(167, 21);
|
||||
buttonSortByColor.TabIndex = 8;
|
||||
buttonSortByColor.Text = "Сортировка по цвету";
|
||||
buttonSortByColor.UseVisualStyleBackColor = true;
|
||||
buttonSortByColor.Click += ButtonSortByColor_Click;
|
||||
//
|
||||
// buttonSortByType
|
||||
//
|
||||
buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonSortByType.Location = new Point(14, 145);
|
||||
buttonSortByType.Name = "buttonSortByType";
|
||||
buttonSortByType.Size = new Size(167, 23);
|
||||
buttonSortByType.TabIndex = 7;
|
||||
buttonSortByType.Text = "Сортировка по типу";
|
||||
buttonSortByType.UseVisualStyleBackColor = true;
|
||||
buttonSortByType.Click += ButtonSortByType_Click;
|
||||
//
|
||||
// FormBoatCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
@@ -343,5 +369,7 @@
|
||||
private ToolStripMenuItem loadToolStripMenuItem;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private Button buttonSortByColor;
|
||||
private Button buttonSortByType;
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,7 @@ namespace MotorBoat
|
||||
pictureBox.Image = _company.Show();
|
||||
_logger.LogInformation("Добавлен объект: {boat}", saveFileDialog.FileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||
@@ -110,6 +110,11 @@ namespace MotorBoat
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Передача на тесты
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonGoToCheck_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_company == null)
|
||||
@@ -141,6 +146,11 @@ namespace MotorBoat
|
||||
form.ShowDialog();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновление
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonRefresh_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_company == null)
|
||||
@@ -161,6 +171,7 @@ namespace MotorBoat
|
||||
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
|
||||
{
|
||||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogError("Ошибка: Не все данные заполнены");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -176,6 +187,7 @@ namespace MotorBoat
|
||||
|
||||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||||
RerfreshListBoxItems();
|
||||
_logger.LogInformation("Добавлена коллекция: {textBoxCollectionName.Text}", saveFileDialog.FileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -206,7 +218,7 @@ namespace MotorBoat
|
||||
listBoxCollection.Items.Clear();
|
||||
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
||||
{
|
||||
string? colName = _storageCollection.Keys?[i];
|
||||
string? colName = _storageCollection.Keys?[i].Name;
|
||||
if (!string.IsNullOrEmpty(colName))
|
||||
{
|
||||
listBoxCollection.Items.Add(colName);
|
||||
@@ -282,5 +294,40 @@ namespace MotorBoat
|
||||
}
|
||||
RerfreshListBoxItems();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка по типу
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonSortByType_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
CompareBoats(new DrawningBoatCompareByType());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка по цвету
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonSortByColor_Click(object sender, EventArgs e)
|
||||
{
|
||||
CompareBoats(new DrawningBoatCompareByColor());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка по сравнителю
|
||||
/// </summary>
|
||||
/// <param name="comparer">Сравнитель объектов</param>
|
||||
private void CompareBoats(IComparer<DrawningBoat?> comparer)
|
||||
{
|
||||
if (_company == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_company.Sort(comparer);
|
||||
pictureBox.Image = _company.Show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user