Лабораторная работа №8
This commit is contained in:
parent
ba1621ee9f
commit
ae2a71ad80
@ -1,5 +1,4 @@
|
|||||||
using ProjectLiner.Drawnings;
|
using ProjectLiner.Drawnings;
|
||||||
using ProjectLiner.Exceptions;
|
|
||||||
|
|
||||||
namespace ProjectLiner.CollectionGenericObjects;
|
namespace ProjectLiner.CollectionGenericObjects;
|
||||||
|
|
||||||
@ -64,7 +63,7 @@ public abstract class AbstractCompany
|
|||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return company._collection.Insert(ship);
|
return company._collection.Insert(ship, new DrawiningShipEqutables());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -106,8 +105,8 @@ public abstract class AbstractCompany
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DrawningShip? obj = _collection?.Get(i);
|
DrawningShip? obj = _collection?.Get(i);
|
||||||
obj?.DrawTransport(graphics);
|
obj?.DrawTransport(graphics);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
@ -117,6 +116,12 @@ public abstract class AbstractCompany
|
|||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Сортировка
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="comparer">Сравнитель объектов</param>
|
||||||
|
public void Sort(IComparer<DrawningShip?> comparer) => _collection?.CollectionSort(comparer);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вывод заднего фона
|
/// Вывод заднего фона
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
namespace ProjectLiner.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();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
using ProjectLiner.Drawnings;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Сравнение по типу, скорости, весу
|
||||||
|
/// </summary>
|
||||||
|
public class DrawningShipCompareByType : IComparer<DrawningShip?>
|
||||||
|
{
|
||||||
|
public int Compare(DrawningShip? x, DrawningShip? y)
|
||||||
|
{
|
||||||
|
if (x == null && y == null)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (x == null || x.EntityShip == null)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y == null || y.EntityShip == null)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (x.GetType().Name != y.GetType().Name)
|
||||||
|
{
|
||||||
|
return x.GetType().Name.CompareTo(y.GetType().Name);
|
||||||
|
}
|
||||||
|
var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed);
|
||||||
|
if (speedCompare != 0)
|
||||||
|
{
|
||||||
|
return speedCompare;
|
||||||
|
}
|
||||||
|
return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight);
|
||||||
|
}
|
||||||
|
}
|
@ -21,16 +21,18 @@ public interface ICollectionGenericObjects<T>
|
|||||||
/// Добавление объекта в коллекцию
|
/// Добавление объекта в коллекцию
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj">Добавляемый объект</param>
|
/// <param name="obj">Добавляемый объект</param>
|
||||||
|
/// <param name="comparer">Cравнение двух объектов</param>
|
||||||
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
|
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
|
||||||
int Insert(T obj);
|
int Insert(T obj, IEqualityComparer<T?>? comparer = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в коллекцию на конкретную позицию
|
/// Добавление объекта в коллекцию на конкретную позицию
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj">Добавляемый объект</param>
|
/// <param name="obj">Добавляемый объект</param>
|
||||||
/// <param name="position">Позиция</param>
|
/// <param name="position">Позиция</param>
|
||||||
|
/// <param name="comparer">Cравнение двух объектов</param>
|
||||||
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
|
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
|
||||||
int Insert(T obj, int position);
|
int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление объекта из коллекции с конкретной позиции
|
/// Удаление объекта из коллекции с конкретной позиции
|
||||||
@ -56,4 +58,10 @@ public interface ICollectionGenericObjects<T>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Поэлементый вывод элементов коллекции</returns>
|
/// <returns>Поэлементый вывод элементов коллекции</returns>
|
||||||
IEnumerable<T?> GetItems();
|
IEnumerable<T?> GetItems();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Сортировка коллекции
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="comparer">Сравнитель объектов</param>
|
||||||
|
void CollectionSort(IComparer<T?> comparer);
|
||||||
}
|
}
|
@ -21,8 +21,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
|
|
||||||
public int Count => _collection.Count;
|
public int Count => _collection.Count;
|
||||||
|
|
||||||
public int MaxCount
|
public int MaxCount {
|
||||||
{
|
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return _maxCount;
|
return _maxCount;
|
||||||
@ -57,32 +56,32 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T obj)
|
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
|
||||||
{
|
{
|
||||||
return Insert(obj, Count);
|
return Insert(obj, Count, comparer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T obj, int position)
|
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
|
||||||
{
|
{
|
||||||
|
if (_collection.Contains(obj, comparer))
|
||||||
|
{
|
||||||
|
throw new ObjectNotUniqueException();
|
||||||
|
}
|
||||||
if (Count >= _maxCount)
|
if (Count >= _maxCount)
|
||||||
{
|
{
|
||||||
throw new CollectionOverflowException(Count);
|
throw new CollectionOverflowException(Count);
|
||||||
}
|
}
|
||||||
if (position > _collection.Count || position < 0)
|
|
||||||
{
|
|
||||||
throw new PositionOutOfCollectionException();
|
|
||||||
}
|
|
||||||
_collection.Insert(position, obj);
|
_collection.Insert(position, obj);
|
||||||
return Count;
|
return Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? Remove(int position)
|
public T? Remove(int position)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
T obj = _collection[position];
|
T obj = _collection[position];
|
||||||
_collection.RemoveAt(position);
|
_collection.RemoveAt(position);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
catch (IndexOutOfRangeException)
|
catch (IndexOutOfRangeException)
|
||||||
{
|
{
|
||||||
@ -97,4 +96,9 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
yield return _collection[i];
|
yield return _collection[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CollectionSort(IComparer<T?> comparer)
|
||||||
|
{
|
||||||
|
_collection.Sort(comparer);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using ProjectLiner.Exceptions;
|
using ProjectLiner.Exceptions;
|
||||||
|
using ProjectLiner.Drawnings;
|
||||||
|
|
||||||
namespace ProjectLiner.CollectionGenericObjects;
|
namespace ProjectLiner.CollectionGenericObjects;
|
||||||
|
|
||||||
@ -52,10 +53,6 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (_collection[position] == null)
|
|
||||||
{
|
|
||||||
throw new ObjectNotFoundException();
|
|
||||||
}
|
|
||||||
return _collection[position];
|
return _collection[position];
|
||||||
}
|
}
|
||||||
catch (IndexOutOfRangeException)
|
catch (IndexOutOfRangeException)
|
||||||
@ -64,17 +61,25 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T obj)
|
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
|
||||||
{
|
{
|
||||||
return Insert(obj, 0);
|
return Insert(obj, 0, comparer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T obj, int position)
|
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
|
||||||
{
|
{
|
||||||
if (position < 0 || position > _collection.Length - 1)
|
if (position < 0 || position > _collection.Length - 1)
|
||||||
{
|
{
|
||||||
throw new PositionOutOfCollectionException();
|
throw new PositionOutOfCollectionException();
|
||||||
}
|
}
|
||||||
|
if (comparer != null)
|
||||||
|
{
|
||||||
|
foreach (T? item in _collection)
|
||||||
|
{
|
||||||
|
if ((comparer as IEqualityComparer<DrawningShip>).Equals(obj as DrawningShip, item as DrawningShip))
|
||||||
|
throw new ObjectNotUniqueException();
|
||||||
|
}
|
||||||
|
}
|
||||||
for (int i = position; i < Count; i++)
|
for (int i = position; i < Count; i++)
|
||||||
{
|
{
|
||||||
if (_collection[i] == null)
|
if (_collection[i] == null)
|
||||||
@ -119,4 +124,10 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
yield return _collection[i];
|
yield return _collection[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CollectionSort(IComparer<T?> comparer)
|
||||||
|
{
|
||||||
|
Array.Sort(_collection, comparer);
|
||||||
|
Array.Reverse(_collection);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using ProjectLiner.Drawnings;
|
using ProjectLiner.Drawnings;
|
||||||
using ProjectLiner.Exceptions;
|
using ProjectLiner.Exceptions;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace ProjectLiner.CollectionGenericObjects;
|
namespace ProjectLiner.CollectionGenericObjects;
|
||||||
|
|
||||||
@ -29,19 +30,19 @@ public class StorageCollection<T>
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Словарь (хранилище) с коллекциями
|
/// Словарь (хранилище) с коллекциями
|
||||||
/// </summary>
|
/// </summary>
|
||||||
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
|
readonly Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Возвращение списка названий коллекций
|
/// Возвращение списка названий коллекций
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<string> Keys => _storages.Keys.ToList();
|
public List<CollectionInfo> Keys => _storages.Keys.ToList();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StorageCollection()
|
public StorageCollection()
|
||||||
{
|
{
|
||||||
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
|
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -51,21 +52,20 @@ public class StorageCollection<T>
|
|||||||
/// <param name="collectionType">тип коллекции</param>
|
/// <param name="collectionType">тип коллекции</param>
|
||||||
public void AddCollection(string name, CollectionType collectionType)
|
public void AddCollection(string name, CollectionType collectionType)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(name) || _storages.ContainsKey(name))
|
CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
|
||||||
{
|
if (collectionInfo.Name == null || _storages.ContainsKey(collectionInfo))
|
||||||
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (collectionType == CollectionType.None)
|
switch (collectionType){
|
||||||
{
|
case CollectionType.None:
|
||||||
return;
|
return;
|
||||||
}
|
case CollectionType.Massive:
|
||||||
if (collectionType == CollectionType.Massive)
|
_storages.Add(collectionInfo, new MassiveGenericObjects<T> { });
|
||||||
{
|
return;
|
||||||
_storages[name] = new MassiveGenericObjects<T>();
|
case CollectionType.List:
|
||||||
}
|
_storages.Add(collectionInfo, new ListGenericObjects<T> { });
|
||||||
else if (collectionType == CollectionType.List)
|
return;
|
||||||
{
|
|
||||||
_storages[name] = new ListGenericObjects<T>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,11 +75,12 @@ public class StorageCollection<T>
|
|||||||
/// <param name="name">Название коллекции</param>
|
/// <param name="name">Название коллекции</param>
|
||||||
public void DelCollection(string name)
|
public void DelCollection(string name)
|
||||||
{
|
{
|
||||||
if (name == null || !_storages.ContainsKey(name))
|
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
|
||||||
|
if (collectionInfo.Name == null || !_storages.ContainsKey(collectionInfo))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_storages.Remove(name);
|
_storages.Remove(collectionInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -91,14 +92,12 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_storages.ContainsKey(name))
|
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
|
||||||
{
|
if (collectionInfo == null || !_storages.ContainsKey(collectionInfo))
|
||||||
return _storages[name];
|
{
|
||||||
}
|
return null;
|
||||||
else
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return _storages[collectionInfo];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,16 +119,17 @@ public class StorageCollection<T>
|
|||||||
using (StreamWriter writer = new StreamWriter(filename))
|
using (StreamWriter writer = new StreamWriter(filename))
|
||||||
{
|
{
|
||||||
writer.WriteLine(_collectionKey);
|
writer.WriteLine(_collectionKey);
|
||||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
|
||||||
{
|
{
|
||||||
writer.Write($"{value.Key}{_separatorForKeyValue}{value.Value.GetCollectionType}{_separatorForKeyValue}{value.Value.MaxCount}{_separatorForKeyValue}");
|
writer.Write($"{value.Key}{_separatorForKeyValue}{value.Value.MaxCount}{_separatorForKeyValue}");
|
||||||
|
|
||||||
foreach (T? item in value.Value.GetItems())
|
foreach (T? item in value.Value.GetItems())
|
||||||
{
|
{
|
||||||
string data = item?.GetDataForSave() ?? string.Empty;
|
string data = item?.GetDataForSave() ?? string.Empty;
|
||||||
if (!string.IsNullOrEmpty(data))
|
if (!string.IsNullOrEmpty(data))
|
||||||
{
|
{
|
||||||
writer.WriteLine(data);
|
writer.Write(data);
|
||||||
|
writer.Write(_separatorItems);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -165,22 +165,18 @@ public class StorageCollection<T>
|
|||||||
while (line != null)
|
while (line != null)
|
||||||
{
|
{
|
||||||
string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (record.Length != 4)
|
if (record.Length != 3)
|
||||||
{
|
{
|
||||||
line = reader.ReadLine();
|
line = reader.ReadLine();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
|
||||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
throw new Exception("Не удалось определить информацию коллекции: " + record[0]);
|
||||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
|
||||||
if (collection == null)
|
|
||||||
{
|
|
||||||
throw new InvalidCastException("Не удалось создать коллекцию");
|
throw new InvalidCastException("Не удалось создать коллекцию");
|
||||||
}
|
collection.MaxCount = Convert.ToInt32(record[1]);
|
||||||
|
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
|
|
||||||
foreach (string elem in set)
|
foreach (string elem in set)
|
||||||
{
|
{
|
||||||
@ -197,7 +193,7 @@ public class StorageCollection<T>
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_storages.Add(record[0], collection);
|
_storages.Add(collectionInfo, collection);
|
||||||
line = reader.ReadLine();
|
line = reader.ReadLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
using ProjectLiner.Entities;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
namespace ProjectLiner.Drawnings;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Реализация сравнения двух объектов класса-прорисовки
|
||||||
|
/// </summary>
|
||||||
|
public class DrawiningShipEqutables : IEqualityComparer<DrawningShip?>
|
||||||
|
{
|
||||||
|
public bool Equals(DrawningShip? x, DrawningShip? y)
|
||||||
|
{
|
||||||
|
if (x == null || x.EntityShip == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (y == null || y.EntityShip == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (x.GetType().Name != y.GetType().Name)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (x.EntityShip.Speed != y.EntityShip.Speed)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (x.EntityShip.Weight != y.EntityShip.Weight)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (x.EntityShip.BodyColor != y.EntityShip.BodyColor)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (x is DrawningLiner && y is DrawningLiner)
|
||||||
|
{
|
||||||
|
EntityLiner _x = (EntityLiner)x.EntityShip;
|
||||||
|
EntityLiner _y = (EntityLiner)x.EntityShip;
|
||||||
|
|
||||||
|
if (_x.AdditionalColor != _y.AdditionalColor)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_x.SecondDeck != _y.SecondDeck)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_x.Pool != _y.Pool)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetHashCode([DisallowNull] DrawningShip obj)
|
||||||
|
{
|
||||||
|
return obj.GetHashCode();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
namespace ProjectLiner.Drawnings;
|
||||||
|
/// <summary>
|
||||||
|
/// Сравнение по цвету, скорости, весу
|
||||||
|
/// </summary>
|
||||||
|
public class DrawningShipCompareByColor : IComparer<DrawningShip?>
|
||||||
|
{
|
||||||
|
public int Compare(DrawningShip? x, DrawningShip? y)
|
||||||
|
{
|
||||||
|
if (x == null && y == null)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (x == null || x.EntityShip == null)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y == null || y.EntityShip == null)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var bodycolorCompare = y.EntityShip.BodyColor.Name.CompareTo(x.EntityShip.BodyColor.Name);
|
||||||
|
if (bodycolorCompare != 0)
|
||||||
|
{
|
||||||
|
return bodycolorCompare;
|
||||||
|
}
|
||||||
|
|
||||||
|
var speedCompare = y.EntityShip.Speed.CompareTo(x.EntityShip.Speed);
|
||||||
|
if (speedCompare != 0)
|
||||||
|
{
|
||||||
|
return speedCompare;
|
||||||
|
}
|
||||||
|
|
||||||
|
return y.EntityShip.Weight.CompareTo(x.EntityShip.Weight);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectLiner.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку наличия такого же объекта в коллекции
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class ObjectNotUniqueException : ApplicationException
|
||||||
|
{
|
||||||
|
public ObjectNotUniqueException() : base("Такой объект уже присутствует в колекции") { }
|
||||||
|
public ObjectNotUniqueException(string message) : base(message) { }
|
||||||
|
public ObjectNotUniqueException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
protected ObjectNotUniqueException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
@ -34,6 +34,8 @@ namespace ProjectLiner
|
|||||||
{
|
{
|
||||||
groupBoxTools = new GroupBox();
|
groupBoxTools = new GroupBox();
|
||||||
panelCompanyTools = new Panel();
|
panelCompanyTools = new Panel();
|
||||||
|
buttonSortByColor = new Button();
|
||||||
|
buttonSortByType = new Button();
|
||||||
buttonAddShip = new Button();
|
buttonAddShip = new Button();
|
||||||
buttonRefresh = new Button();
|
buttonRefresh = new Button();
|
||||||
maskedTextBoxPosition = new MaskedTextBox();
|
maskedTextBoxPosition = new MaskedTextBox();
|
||||||
@ -72,13 +74,15 @@ namespace ProjectLiner
|
|||||||
groupBoxTools.Dock = DockStyle.Right;
|
groupBoxTools.Dock = DockStyle.Right;
|
||||||
groupBoxTools.Location = new Point(793, 24);
|
groupBoxTools.Location = new Point(793, 24);
|
||||||
groupBoxTools.Name = "groupBoxTools";
|
groupBoxTools.Name = "groupBoxTools";
|
||||||
groupBoxTools.Size = new Size(144, 547);
|
groupBoxTools.Size = new Size(144, 588);
|
||||||
groupBoxTools.TabIndex = 0;
|
groupBoxTools.TabIndex = 0;
|
||||||
groupBoxTools.TabStop = false;
|
groupBoxTools.TabStop = false;
|
||||||
groupBoxTools.Text = "Инструменты";
|
groupBoxTools.Text = "Инструменты";
|
||||||
//
|
//
|
||||||
// panelCompanyTools
|
// panelCompanyTools
|
||||||
//
|
//
|
||||||
|
panelCompanyTools.Controls.Add(buttonSortByColor);
|
||||||
|
panelCompanyTools.Controls.Add(buttonSortByType);
|
||||||
panelCompanyTools.Controls.Add(buttonAddShip);
|
panelCompanyTools.Controls.Add(buttonAddShip);
|
||||||
panelCompanyTools.Controls.Add(buttonRefresh);
|
panelCompanyTools.Controls.Add(buttonRefresh);
|
||||||
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
|
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
|
||||||
@ -88,9 +92,29 @@ namespace ProjectLiner
|
|||||||
panelCompanyTools.Enabled = false;
|
panelCompanyTools.Enabled = false;
|
||||||
panelCompanyTools.Location = new Point(3, 321);
|
panelCompanyTools.Location = new Point(3, 321);
|
||||||
panelCompanyTools.Name = "panelCompanyTools";
|
panelCompanyTools.Name = "panelCompanyTools";
|
||||||
panelCompanyTools.Size = new Size(138, 223);
|
panelCompanyTools.Size = new Size(138, 264);
|
||||||
panelCompanyTools.TabIndex = 10;
|
panelCompanyTools.TabIndex = 10;
|
||||||
//
|
//
|
||||||
|
// buttonSortByColor
|
||||||
|
//
|
||||||
|
buttonSortByColor.Location = new Point(4, 218);
|
||||||
|
buttonSortByColor.Name = "buttonSortByColor";
|
||||||
|
buttonSortByColor.Size = new Size(126, 43);
|
||||||
|
buttonSortByColor.TabIndex = 8;
|
||||||
|
buttonSortByColor.Text = "Сортировка по цвету";
|
||||||
|
buttonSortByColor.UseVisualStyleBackColor = true;
|
||||||
|
buttonSortByColor.Click += ButtonSortByColor_Click;
|
||||||
|
//
|
||||||
|
// buttonSortByType
|
||||||
|
//
|
||||||
|
buttonSortByType.Location = new Point(4, 182);
|
||||||
|
buttonSortByType.Name = "buttonSortByType";
|
||||||
|
buttonSortByType.Size = new Size(126, 30);
|
||||||
|
buttonSortByType.TabIndex = 7;
|
||||||
|
buttonSortByType.Text = "Сортировка по типу";
|
||||||
|
buttonSortByType.UseVisualStyleBackColor = true;
|
||||||
|
buttonSortByType.Click += ButtonSortByType_Click;
|
||||||
|
//
|
||||||
// buttonAddShip
|
// buttonAddShip
|
||||||
//
|
//
|
||||||
buttonAddShip.Location = new Point(4, 3);
|
buttonAddShip.Location = new Point(4, 3);
|
||||||
@ -103,9 +127,9 @@ namespace ProjectLiner
|
|||||||
//
|
//
|
||||||
// buttonRefresh
|
// buttonRefresh
|
||||||
//
|
//
|
||||||
buttonRefresh.Location = new Point(4, 172);
|
buttonRefresh.Location = new Point(3, 153);
|
||||||
buttonRefresh.Name = "buttonRefresh";
|
buttonRefresh.Name = "buttonRefresh";
|
||||||
buttonRefresh.Size = new Size(126, 42);
|
buttonRefresh.Size = new Size(126, 26);
|
||||||
buttonRefresh.TabIndex = 6;
|
buttonRefresh.TabIndex = 6;
|
||||||
buttonRefresh.Text = "Обновить";
|
buttonRefresh.Text = "Обновить";
|
||||||
buttonRefresh.UseVisualStyleBackColor = true;
|
buttonRefresh.UseVisualStyleBackColor = true;
|
||||||
@ -113,7 +137,7 @@ namespace ProjectLiner
|
|||||||
//
|
//
|
||||||
// maskedTextBoxPosition
|
// maskedTextBoxPosition
|
||||||
//
|
//
|
||||||
maskedTextBoxPosition.Location = new Point(4, 71);
|
maskedTextBoxPosition.Location = new Point(3, 52);
|
||||||
maskedTextBoxPosition.Mask = "00";
|
maskedTextBoxPosition.Mask = "00";
|
||||||
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
|
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
|
||||||
maskedTextBoxPosition.Size = new Size(126, 23);
|
maskedTextBoxPosition.Size = new Size(126, 23);
|
||||||
@ -122,7 +146,7 @@ namespace ProjectLiner
|
|||||||
//
|
//
|
||||||
// buttonGoToCheck
|
// buttonGoToCheck
|
||||||
//
|
//
|
||||||
buttonGoToCheck.Location = new Point(4, 136);
|
buttonGoToCheck.Location = new Point(3, 117);
|
||||||
buttonGoToCheck.Name = "buttonGoToCheck";
|
buttonGoToCheck.Name = "buttonGoToCheck";
|
||||||
buttonGoToCheck.Size = new Size(126, 30);
|
buttonGoToCheck.Size = new Size(126, 30);
|
||||||
buttonGoToCheck.TabIndex = 5;
|
buttonGoToCheck.TabIndex = 5;
|
||||||
@ -132,7 +156,7 @@ namespace ProjectLiner
|
|||||||
//
|
//
|
||||||
// buttonRemoveShip
|
// buttonRemoveShip
|
||||||
//
|
//
|
||||||
buttonRemoveShip.Location = new Point(4, 100);
|
buttonRemoveShip.Location = new Point(3, 81);
|
||||||
buttonRemoveShip.Name = "buttonRemoveShip";
|
buttonRemoveShip.Name = "buttonRemoveShip";
|
||||||
buttonRemoveShip.Size = new Size(126, 30);
|
buttonRemoveShip.Size = new Size(126, 30);
|
||||||
buttonRemoveShip.TabIndex = 4;
|
buttonRemoveShip.TabIndex = 4;
|
||||||
@ -251,7 +275,7 @@ namespace ProjectLiner
|
|||||||
pictureBox.Dock = DockStyle.Fill;
|
pictureBox.Dock = DockStyle.Fill;
|
||||||
pictureBox.Location = new Point(0, 24);
|
pictureBox.Location = new Point(0, 24);
|
||||||
pictureBox.Name = "pictureBox";
|
pictureBox.Name = "pictureBox";
|
||||||
pictureBox.Size = new Size(793, 547);
|
pictureBox.Size = new Size(793, 588);
|
||||||
pictureBox.TabIndex = 1;
|
pictureBox.TabIndex = 1;
|
||||||
pictureBox.TabStop = false;
|
pictureBox.TabStop = false;
|
||||||
//
|
//
|
||||||
@ -299,7 +323,7 @@ namespace ProjectLiner
|
|||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(937, 571);
|
ClientSize = new Size(937, 612);
|
||||||
Controls.Add(pictureBox);
|
Controls.Add(pictureBox);
|
||||||
Controls.Add(groupBoxTools);
|
Controls.Add(groupBoxTools);
|
||||||
Controls.Add(menuStrip);
|
Controls.Add(menuStrip);
|
||||||
@ -344,5 +368,7 @@ namespace ProjectLiner
|
|||||||
private ToolStripMenuItem loadToolStripMenuItem;
|
private ToolStripMenuItem loadToolStripMenuItem;
|
||||||
private SaveFileDialog saveFileDialog;
|
private SaveFileDialog saveFileDialog;
|
||||||
private OpenFileDialog openFileDialog;
|
private OpenFileDialog openFileDialog;
|
||||||
|
private Button buttonSortByColor;
|
||||||
|
private Button buttonSortByType;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -215,7 +215,7 @@ public partial class FormShipCollection : Form
|
|||||||
listBoxCollection.Items.Clear();
|
listBoxCollection.Items.Clear();
|
||||||
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
||||||
{
|
{
|
||||||
string? colName = _storageCollection.Keys?[i];
|
string? colName = _storageCollection.Keys?[i].Name;
|
||||||
if (!string.IsNullOrEmpty(colName))
|
if (!string.IsNullOrEmpty(colName))
|
||||||
{
|
{
|
||||||
listBoxCollection.Items.Add(colName);
|
listBoxCollection.Items.Add(colName);
|
||||||
@ -302,4 +302,38 @@ public partial class FormShipCollection : Form
|
|||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Сортировка по типу
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonSortByType_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
CompareShips(new DrawningShipCompareByType());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Сортировка по цвету
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonSortByColor_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
CompareShips(new DrawningShipCompareByColor());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Сортировка по сравнителю
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="comparer">Сравнитель объектов</param>
|
||||||
|
private void CompareShips(IComparer<DrawningShip?> comparer)
|
||||||
|
{
|
||||||
|
if (_company == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_company.Sort(comparer);
|
||||||
|
pictureBox.Image = _company.Show();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user