Лабораторная работа №8
This commit is contained in:
parent
d4ee137ca9
commit
b3effcc182
@ -60,7 +60,18 @@ public abstract class AbstractCompany
|
||||
/// <returns></returns>
|
||||
public static int operator +(AbstractCompany company, DrawningWarship warship)
|
||||
{
|
||||
return company._collection.Insert(warship, 0);
|
||||
try
|
||||
{
|
||||
return company._collection.Insert(warship, 0, new DrawningWarshipEqutables());
|
||||
}
|
||||
catch (ObjectAlreadyInCollectionException)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
catch (CollectionOverflowException)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -118,6 +129,12 @@ public abstract class AbstractCompany
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка
|
||||
/// </summary>
|
||||
/// <param name="comparer">Сравнитель объектов</param>
|
||||
public void Sort(IComparer<DrawningWarship?> comparer) => _collection?.CollectionSort(comparer);
|
||||
|
||||
/// <summary>
|
||||
/// Вывод заднего фона
|
||||
/// </summary>
|
||||
|
@ -0,0 +1,77 @@
|
||||
namespace ProjectAircraftCarrier_.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();
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
namespace ProjectAircraftCarrier_.CollectionGenericObjects;
|
||||
using ProjectAircraftCarrier_.Drawnings;
|
||||
|
||||
namespace ProjectAircraftCarrier_.CollectionGenericObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Интерфейс описания действий для набора хранимых объектов
|
||||
@ -17,21 +19,22 @@ public interface ICollectionGenericObjects<T>
|
||||
/// </summary>
|
||||
int MaxCount { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Добавление объекта в коллекцию
|
||||
/// </summary>
|
||||
/// <param name="obj">Добавляемый объект</param>
|
||||
/// <param name="comparer">Сравнение двух объектов</param>
|
||||
/// <returns>индекс добавленного элемента - вставка прошла удачно, -1 - вставка не удалась</returns>
|
||||
int Insert(T obj);
|
||||
int Insert(T obj, IEqualityComparer<DrawningWarship?>? comparer = null);
|
||||
|
||||
/// <summary>
|
||||
/// Добавление объекта в коллекцию на конкретную позицию
|
||||
/// </summary>
|
||||
/// <param name="obj">Добавляемый объект</param>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <param name="comparer">Сравнение двух объектов</param>
|
||||
/// <returns>индекс добавленного элемента - вставка прошла удачно, -1 - вставка не удалась</returns>
|
||||
int Insert(T obj, int position);
|
||||
int Insert(T obj, int position, IEqualityComparer<DrawningWarship?>? comparer = null);
|
||||
|
||||
/// <summary>
|
||||
/// Удаление объекта из коллекции с конкретной позиции
|
||||
@ -57,5 +60,10 @@ public interface ICollectionGenericObjects<T>
|
||||
/// </summary>
|
||||
/// <returns>Поэлементый вывод элементов коллекции</returns>
|
||||
IEnumerable<T?> GetItems();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка коллекции
|
||||
/// </summary>
|
||||
/// <param name="comparer">Сравнитель объектов</param>
|
||||
void CollectionSort(IComparer<T?> comparer);
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using ProjectAircraftCarrier_.Exceptions;
|
||||
using ProjectAircraftCarrier_.Drawnings;
|
||||
using ProjectAircraftCarrier_.Exceptions;
|
||||
|
||||
namespace ProjectAircraftCarrier_.CollectionGenericObjects;
|
||||
|
||||
@ -44,31 +45,50 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
public int Insert(T obj)
|
||||
public int Insert(T obj, IEqualityComparer<DrawningWarship?>? comparer = null)
|
||||
{
|
||||
// TODO выброс ошибки, если переполнение
|
||||
// TODO выброс ошибки, если такой объект есть в коллекции
|
||||
if (Count == _maxCount)
|
||||
{
|
||||
throw new CollectionOverflowException(Count);
|
||||
}
|
||||
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
if (comparer.Equals((_collection[i] as DrawningWarship), (obj as DrawningWarship)))
|
||||
{
|
||||
throw new ObjectAlreadyInCollectionException(i);
|
||||
}
|
||||
}
|
||||
|
||||
_collection.Add(obj);
|
||||
return _collection.Count;
|
||||
}
|
||||
|
||||
public int Insert(T obj, int position)
|
||||
public int Insert(T obj, int position, IEqualityComparer<DrawningWarship?>? comparer = null)
|
||||
{
|
||||
// TODO выброс ошибки, если выход за границы списка
|
||||
// TODO выброс ошибки, если переполнение
|
||||
// TODO выброс ошибки, если такой объект есть в коллекции
|
||||
if (position < 0 || position > Count)
|
||||
{
|
||||
throw new PositionOutOfCollectionException(position);
|
||||
}
|
||||
|
||||
if (Count == _maxCount)
|
||||
{
|
||||
throw new CollectionOverflowException(Count);
|
||||
}
|
||||
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
if (comparer.Equals((_collection[i] as DrawningWarship), (obj as DrawningWarship)))
|
||||
{
|
||||
throw new ObjectAlreadyInCollectionException(i);
|
||||
}
|
||||
}
|
||||
|
||||
_collection.Insert(position, obj);
|
||||
return position;
|
||||
}
|
||||
@ -93,4 +113,10 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
yield return _collection[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void CollectionSort(IComparer<T?> comparer)
|
||||
{
|
||||
// TODO
|
||||
_collection.Sort(comparer);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using ProjectAircraftCarrier_.Exceptions;
|
||||
using ProjectAircraftCarrier_.Drawnings;
|
||||
using ProjectAircraftCarrier_.Exceptions;
|
||||
|
||||
namespace ProjectAircraftCarrier_.CollectionGenericObjects;
|
||||
|
||||
@ -64,9 +65,18 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
public int Insert(T obj)
|
||||
public int Insert(T obj, IEqualityComparer<DrawningWarship?>? comparer = null)
|
||||
{
|
||||
// TODO выброс ошибки, если такой объект есть в коллекции
|
||||
// TODO выброс ошибки, если переполнение
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
if (comparer.Equals((_collection[i] as DrawningWarship), (obj as DrawningWarship)))
|
||||
{
|
||||
throw new ObjectAlreadyInCollectionException(i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
if (_collection[i] == null)
|
||||
@ -79,15 +89,24 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
throw new CollectionOverflowException(Count);
|
||||
}
|
||||
|
||||
public int Insert(T obj, int position)
|
||||
public int Insert(T obj, int position, IEqualityComparer<DrawningWarship?>? comparer = null)
|
||||
{
|
||||
// TODO выброс ошибки, если выход за границы массива
|
||||
// TODO выброс ошибки, если такой объект есть в коллекции
|
||||
// TODO выброс ошибки, если переполнение
|
||||
if (position < 0 || position >= Count)
|
||||
{
|
||||
throw new PositionOutOfCollectionException(position);
|
||||
}
|
||||
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
if (comparer.Equals((_collection[i] as DrawningWarship), (obj as DrawningWarship)))
|
||||
{
|
||||
throw new ObjectAlreadyInCollectionException(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (_collection[position] == null)
|
||||
{
|
||||
_collection[position] = obj;
|
||||
@ -140,5 +159,10 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
yield return _collection[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CollectionSort(IComparer<T?> comparer)
|
||||
{
|
||||
// TODO
|
||||
Array.Sort(_collection, comparer);
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
using ProjectAircraftCarrier_.Drawnings;
|
||||
using ProjectAircraftCarrier_.Exceptions;
|
||||
using System.Text;
|
||||
|
||||
namespace ProjectAircraftCarrier_.CollectionGenericObjects;
|
||||
|
||||
@ -14,12 +13,12 @@ public class StorageCollection<T>
|
||||
/// <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>
|
||||
/// Ключевое слово, с которого должен начинаться файл
|
||||
@ -39,29 +38,29 @@ public class StorageCollection<T>
|
||||
/// </summary>
|
||||
public StorageCollection()
|
||||
{
|
||||
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
|
||||
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавление коллекции в хранилище
|
||||
/// </summary>
|
||||
/// <param name="name">Название коллекции</param>
|
||||
/// <param name="info">Название коллекции</param>
|
||||
/// <param name="collectionType">Тип коллекции</param>
|
||||
public void AddCollection(string name, CollectionType collectionType)
|
||||
public void AddCollection(CollectionInfo info)
|
||||
{
|
||||
if (name == null || _storages.ContainsKey(name))
|
||||
if (info == null || _storages.ContainsKey(info))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (collectionType == CollectionType.Massive)
|
||||
if (info.CollectionType == CollectionType.Massive)
|
||||
{
|
||||
_storages.Add(name, new MassiveGenericObjects<T>());
|
||||
_storages.Add(info, new MassiveGenericObjects<T>());
|
||||
}
|
||||
|
||||
if (collectionType == CollectionType.List)
|
||||
if (info.CollectionType == CollectionType.List)
|
||||
{
|
||||
_storages.Add(name, new ListGenericObjects<T>());
|
||||
_storages.Add(info, new ListGenericObjects<T>());
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,14 +68,14 @@ public class StorageCollection<T>
|
||||
/// Удаление коллекции
|
||||
/// </summary>
|
||||
/// <param name="name">Название коллекции</param>
|
||||
public void DelCollection(string name)
|
||||
public void DelCollection(CollectionInfo info)
|
||||
{
|
||||
if (name == null || !_storages.ContainsKey(name))
|
||||
if (info == null || !_storages.ContainsKey(info))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_storages.Remove(name);
|
||||
_storages.Remove(info);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -84,13 +83,13 @@ public class StorageCollection<T>
|
||||
/// </summary>
|
||||
/// <param name="name">Название коллекции</param>
|
||||
/// <returns></returns>
|
||||
public ICollectionGenericObjects<T>? this[string name]
|
||||
public ICollectionGenericObjects<T>? this[CollectionInfo info]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_storages.ContainsKey(name))
|
||||
if (_storages.ContainsKey(info))
|
||||
{
|
||||
return _storages[name];
|
||||
return _storages[info];
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -117,7 +116,7 @@ public class StorageCollection<T>
|
||||
{
|
||||
sw.Write(_collectionKey);
|
||||
|
||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
|
||||
{
|
||||
sw.Write(Environment.NewLine);
|
||||
// не сохраняем пустые коллекции
|
||||
@ -128,8 +127,6 @@ public class StorageCollection<T>
|
||||
|
||||
sw.Write(value.Key);
|
||||
sw.Write(_separatorForKeyValue);
|
||||
sw.Write(value.Value.GetCollectionType);
|
||||
sw.Write(_separatorForKeyValue);
|
||||
sw.Write(value.Value.MaxCount);
|
||||
sw.Write(_separatorForKeyValue);
|
||||
|
||||
@ -176,27 +173,25 @@ public class StorageCollection<T>
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
string[] record = line.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);
|
||||
if (collection == null)
|
||||
{
|
||||
throw new InvalidOperationException("Не удалось создать коллекцию");
|
||||
}
|
||||
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
|
||||
throw new Exception("Не удалось определить информацию коллекции: " + record[0]);
|
||||
ICollectionGenericObjects<T> ? collection = StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
|
||||
throw new Exception("Не удалось создать коллекцию");
|
||||
collection.MaxCount = Convert.ToInt32(record[1]);
|
||||
|
||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
if (elem?.CreateDrawningWarship() is T warship)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (collection.Insert(warship) == -1)
|
||||
if (collection.Insert(warship, new DrawningWarshipEqutables()) == -1)
|
||||
{
|
||||
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
|
||||
}
|
||||
@ -205,9 +200,13 @@ public class StorageCollection<T>
|
||||
{
|
||||
throw new OverflowException("Коллекция переполнена", ex);
|
||||
}
|
||||
catch (ObjectAlreadyInCollectionException ex)
|
||||
{
|
||||
throw new InvalidOperationException("Объект уже присутствует в коллекции", ex);
|
||||
}
|
||||
}
|
||||
_storages.Add(record[0], collection);
|
||||
}
|
||||
_storages.Add(collectionInfo, collection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
using ProjectAircraftCarrier_.Entities;
|
||||
|
||||
namespace ProjectAircraftCarrier_.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Сравнение по цвету, скорости, весу
|
||||
/// </summary>
|
||||
public class DrawningWarshipCompareByColor : IComparer<DrawningWarship?>
|
||||
{
|
||||
public int Compare(DrawningWarship? x, DrawningWarship? y)
|
||||
{
|
||||
// TODO прописать логику сравения по цветам, скорости, весу
|
||||
if (x == null || x.EntityWarship == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (y == null || y.EntityWarship == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
var bodyColorCompare = y.EntityWarship.BodyColor.Name.CompareTo(x.EntityWarship.BodyColor.Name);
|
||||
if (bodyColorCompare != 0)
|
||||
{
|
||||
return bodyColorCompare;
|
||||
}
|
||||
if (x is DrawningAircraftCarrier && y is DrawningAircraftCarrier)
|
||||
{
|
||||
var additionalColorCompare = (y.EntityWarship as EntityAircraftCarrier).AdditionalColor.Name.CompareTo(
|
||||
(x.EntityWarship as EntityAircraftCarrier).AdditionalColor.Name);
|
||||
if (additionalColorCompare != 0)
|
||||
{
|
||||
return additionalColorCompare;
|
||||
}
|
||||
}
|
||||
var speedCompare = y.EntityWarship.Speed.CompareTo(x.EntityWarship.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
return y.EntityWarship.Weight.CompareTo(x.EntityWarship.Weight);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
namespace ProjectAircraftCarrier_.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Сравнение по типу, скорости, весу
|
||||
/// </summary>
|
||||
public class DrawningWarshipCompareByType : IComparer<DrawningWarship?>
|
||||
{
|
||||
public int Compare(DrawningWarship? x, DrawningWarship? y)
|
||||
{
|
||||
if (x == null || x.EntityWarship == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (y == null || y.EntityWarship == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return y.GetType().Name.CompareTo(x.GetType().Name);
|
||||
}
|
||||
var speedCompare = y.EntityWarship.Speed.CompareTo(x.EntityWarship.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
return y.EntityWarship.Weight.CompareTo(x.EntityWarship.Weight);
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
using ProjectAircraftCarrier_.Entities;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace ProjectAircraftCarrier_.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Реализация сравнения двух объектов класса-прорисовки
|
||||
/// </summary>
|
||||
public class DrawningWarshipEqutables : IEqualityComparer<DrawningWarship?>
|
||||
{
|
||||
public bool Equals(DrawningWarship? x, DrawningWarship? y)
|
||||
{
|
||||
if (x == null || x.EntityWarship == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (y == null || y.EntityWarship == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityWarship.Speed != y.EntityWarship.Speed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityWarship.Weight != y.EntityWarship.Weight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityWarship.BodyColor != y.EntityWarship.BodyColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x is DrawningAircraftCarrier && y is DrawningAircraftCarrier)
|
||||
{
|
||||
// TODO доделать логику сравнения дополнительных параметров
|
||||
if ((x.EntityWarship as EntityAircraftCarrier)?.AdditionalColor !=
|
||||
(y.EntityWarship as EntityAircraftCarrier)?.AdditionalColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((x.EntityWarship as EntityAircraftCarrier)?.DeckForAircraftTakeOff !=
|
||||
(y.EntityWarship as EntityAircraftCarrier)?.DeckForAircraftTakeOff)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((x.EntityWarship as EntityAircraftCarrier)?.ControlCabin !=
|
||||
(y.EntityWarship as EntityAircraftCarrier)?.ControlCabin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((x.EntityWarship as EntityAircraftCarrier)?.FighterJet !=
|
||||
(y.EntityWarship as EntityAircraftCarrier)?.FighterJet)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int GetHashCode([DisallowNull] DrawningWarship? obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ProjectAircraftCarrier_.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, описывающий ошибку переполнения коллекции
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
internal class ObjectAlreadyInCollectionException : ApplicationException
|
||||
{
|
||||
public ObjectAlreadyInCollectionException(int index) : base("Такой объект уже присутствует в коллекции. Позиция " + index) { }
|
||||
|
||||
public ObjectAlreadyInCollectionException() : base() { }
|
||||
|
||||
public ObjectAlreadyInCollectionException(string message) : base(message) { }
|
||||
|
||||
public ObjectAlreadyInCollectionException(string message, Exception exception) : base(message, exception) { }
|
||||
|
||||
protected ObjectAlreadyInCollectionException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
|
||||
}
|
@ -30,6 +30,8 @@
|
||||
{
|
||||
groupBoxTools = new GroupBox();
|
||||
panelCompanyTools = new Panel();
|
||||
buttonSortByColor = new Button();
|
||||
buttonSortByType = new Button();
|
||||
buttonAddWarship = new Button();
|
||||
maskedTextBoxPosition = new MaskedTextBox();
|
||||
buttonRefresh = new Button();
|
||||
@ -66,15 +68,17 @@
|
||||
groupBoxTools.Controls.Add(panelStorage);
|
||||
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
|
||||
groupBoxTools.Dock = DockStyle.Right;
|
||||
groupBoxTools.Location = new Point(1187, 38);
|
||||
groupBoxTools.Location = new Point(1114, 38);
|
||||
groupBoxTools.Name = "groupBoxTools";
|
||||
groupBoxTools.Size = new Size(326, 904);
|
||||
groupBoxTools.Size = new Size(326, 963);
|
||||
groupBoxTools.TabIndex = 0;
|
||||
groupBoxTools.TabStop = false;
|
||||
groupBoxTools.Text = "Инструменты";
|
||||
//
|
||||
// panelCompanyTools
|
||||
//
|
||||
panelCompanyTools.Controls.Add(buttonSortByColor);
|
||||
panelCompanyTools.Controls.Add(buttonSortByType);
|
||||
panelCompanyTools.Controls.Add(buttonAddWarship);
|
||||
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
|
||||
panelCompanyTools.Controls.Add(buttonRefresh);
|
||||
@ -82,17 +86,39 @@
|
||||
panelCompanyTools.Controls.Add(buttonGoToCheck);
|
||||
panelCompanyTools.Dock = DockStyle.Bottom;
|
||||
panelCompanyTools.Enabled = false;
|
||||
panelCompanyTools.Location = new Point(3, 518);
|
||||
panelCompanyTools.Location = new Point(3, 509);
|
||||
panelCompanyTools.Name = "panelCompanyTools";
|
||||
panelCompanyTools.Size = new Size(320, 383);
|
||||
panelCompanyTools.Size = new Size(320, 451);
|
||||
panelCompanyTools.TabIndex = 9;
|
||||
//
|
||||
// buttonSortByColor
|
||||
//
|
||||
buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonSortByColor.Location = new Point(3, 386);
|
||||
buttonSortByColor.Name = "buttonSortByColor";
|
||||
buttonSortByColor.Size = new Size(314, 60);
|
||||
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(3, 320);
|
||||
buttonSortByType.Name = "buttonSortByType";
|
||||
buttonSortByType.Size = new Size(314, 60);
|
||||
buttonSortByType.TabIndex = 7;
|
||||
buttonSortByType.Text = "Сортировка по типу";
|
||||
buttonSortByType.UseVisualStyleBackColor = true;
|
||||
buttonSortByType.Click += ButtonSortByType_Click;
|
||||
//
|
||||
// buttonAddWarship
|
||||
//
|
||||
buttonAddWarship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonAddWarship.Location = new Point(3, 3);
|
||||
buttonAddWarship.Name = "buttonAddWarship";
|
||||
buttonAddWarship.Size = new Size(314, 72);
|
||||
buttonAddWarship.Size = new Size(314, 73);
|
||||
buttonAddWarship.TabIndex = 1;
|
||||
buttonAddWarship.Text = "Добавление военного корабля";
|
||||
buttonAddWarship.UseVisualStyleBackColor = true;
|
||||
@ -110,9 +136,9 @@
|
||||
// buttonRefresh
|
||||
//
|
||||
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonRefresh.Location = new Point(3, 304);
|
||||
buttonRefresh.Location = new Point(3, 254);
|
||||
buttonRefresh.Name = "buttonRefresh";
|
||||
buttonRefresh.Size = new Size(314, 70);
|
||||
buttonRefresh.Size = new Size(314, 60);
|
||||
buttonRefresh.TabIndex = 6;
|
||||
buttonRefresh.Text = "Обновить";
|
||||
buttonRefresh.UseVisualStyleBackColor = true;
|
||||
@ -121,9 +147,9 @@
|
||||
// buttonRemoveWarship
|
||||
//
|
||||
buttonRemoveWarship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonRemoveWarship.Location = new Point(3, 152);
|
||||
buttonRemoveWarship.Location = new Point(3, 122);
|
||||
buttonRemoveWarship.Name = "buttonRemoveWarship";
|
||||
buttonRemoveWarship.Size = new Size(314, 70);
|
||||
buttonRemoveWarship.Size = new Size(314, 60);
|
||||
buttonRemoveWarship.TabIndex = 4;
|
||||
buttonRemoveWarship.Text = "Удаление военного корабля";
|
||||
buttonRemoveWarship.UseVisualStyleBackColor = true;
|
||||
@ -132,9 +158,9 @@
|
||||
// buttonGoToCheck
|
||||
//
|
||||
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
buttonGoToCheck.Location = new Point(3, 228);
|
||||
buttonGoToCheck.Location = new Point(3, 188);
|
||||
buttonGoToCheck.Name = "buttonGoToCheck";
|
||||
buttonGoToCheck.Size = new Size(314, 70);
|
||||
buttonGoToCheck.Size = new Size(314, 60);
|
||||
buttonGoToCheck.TabIndex = 5;
|
||||
buttonGoToCheck.Text = "Передать на тесты";
|
||||
buttonGoToCheck.UseVisualStyleBackColor = true;
|
||||
@ -142,7 +168,7 @@
|
||||
//
|
||||
// buttonCreateCompany
|
||||
//
|
||||
buttonCreateCompany.Location = new Point(6, 472);
|
||||
buttonCreateCompany.Location = new Point(6, 463);
|
||||
buttonCreateCompany.Name = "buttonCreateCompany";
|
||||
buttonCreateCompany.Size = new Size(314, 40);
|
||||
buttonCreateCompany.TabIndex = 8;
|
||||
@ -238,7 +264,7 @@
|
||||
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxSelectorCompany.FormattingEnabled = true;
|
||||
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
|
||||
comboBoxSelectorCompany.Location = new Point(6, 428);
|
||||
comboBoxSelectorCompany.Location = new Point(6, 419);
|
||||
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
|
||||
comboBoxSelectorCompany.Size = new Size(314, 38);
|
||||
comboBoxSelectorCompany.TabIndex = 0;
|
||||
@ -249,7 +275,7 @@
|
||||
pictureBox.Dock = DockStyle.Fill;
|
||||
pictureBox.Location = new Point(0, 38);
|
||||
pictureBox.Name = "pictureBox";
|
||||
pictureBox.Size = new Size(1187, 904);
|
||||
pictureBox.Size = new Size(1114, 963);
|
||||
pictureBox.TabIndex = 1;
|
||||
pictureBox.TabStop = false;
|
||||
//
|
||||
@ -259,7 +285,7 @@
|
||||
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
|
||||
menuStrip.Location = new Point(0, 0);
|
||||
menuStrip.Name = "menuStrip";
|
||||
menuStrip.Size = new Size(1513, 38);
|
||||
menuStrip.Size = new Size(1440, 38);
|
||||
menuStrip.TabIndex = 2;
|
||||
menuStrip.Text = "menuStrip";
|
||||
//
|
||||
@ -298,7 +324,7 @@
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(12F, 30F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(1513, 942);
|
||||
ClientSize = new Size(1440, 1001);
|
||||
Controls.Add(pictureBox);
|
||||
Controls.Add(groupBoxTools);
|
||||
Controls.Add(menuStrip);
|
||||
@ -343,5 +369,7 @@
|
||||
private ToolStripMenuItem loadToolStripMenuItem;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private Button buttonSortByColor;
|
||||
private Button buttonSortByType;
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
using ProjectAircraftCarrier_.CollectionGenericObjects;
|
||||
using ProjectAircraftCarrier_.Drawnings;
|
||||
using ProjectAircraftCarrier_.Exceptions;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ProjectAircraftCarrier_;
|
||||
|
||||
@ -63,26 +62,34 @@ public partial class FormWarshipCollection : Form
|
||||
/// </summary>
|
||||
/// <param name="warship"></param>
|
||||
private void SetWarship(DrawningWarship? warship)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_company == null || warship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (_company + warship != -1)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
_logger.LogInformation($"Добавлен объект {warship.GetDataForSave()}");
|
||||
pictureBox.Image = _company.Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
catch (CollectionOverflowException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"Ошибка: {ex.Message}");
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"Ошибка: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -199,8 +206,9 @@ public partial class FormWarshipCollection : Form
|
||||
{
|
||||
collectionType = CollectionType.List;
|
||||
}
|
||||
CollectionInfo collectionInfo = new CollectionInfo(textBoxCollectionName.Text, collectionType, string.Empty);
|
||||
|
||||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||||
_storageCollection.AddCollection(collectionInfo);
|
||||
_logger.LogInformation($"Добавлена коллекция: {textBoxCollectionName.Text} типа: {collectionType}");
|
||||
RerfreshListBoxItems();
|
||||
}
|
||||
@ -222,7 +230,9 @@ public partial class FormWarshipCollection : Form
|
||||
{
|
||||
return;
|
||||
}
|
||||
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||||
|
||||
CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty);
|
||||
_storageCollection.DelCollection(collectionInfo);
|
||||
_logger.LogInformation($"Удалена коллекция: {listBoxCollection.SelectedItem.ToString()}");
|
||||
RerfreshListBoxItems();
|
||||
}
|
||||
@ -235,7 +245,7 @@ public partial class FormWarshipCollection : Form
|
||||
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);
|
||||
@ -256,7 +266,8 @@ public partial class FormWarshipCollection : Form
|
||||
return;
|
||||
}
|
||||
|
||||
ICollectionGenericObjects<DrawningWarship>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
|
||||
CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty);
|
||||
ICollectionGenericObjects<DrawningWarship>? collection = _storageCollection[collectionInfo];
|
||||
if (collection == null)
|
||||
{
|
||||
MessageBox.Show("Коллекция не проинициализирована");
|
||||
@ -288,7 +299,7 @@ public partial class FormWarshipCollection : Form
|
||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
|
||||
}
|
||||
catch(Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||
@ -312,11 +323,45 @@ public partial class FormWarshipCollection : Form
|
||||
RerfreshListBoxItems();
|
||||
_logger.LogInformation("Загрузка из фала: {filename}", openFileDialog.FileName);
|
||||
}
|
||||
catch(Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка по типу
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonSortByType_Click(object sender, EventArgs e)
|
||||
{
|
||||
CompareWarships(new DrawningWarshipCompareByType());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка по цвету
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonSortByColor_Click(object sender, EventArgs e)
|
||||
{
|
||||
CompareWarships(new DrawningWarshipCompareByColor());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка по сравнителю
|
||||
/// </summary>
|
||||
/// <param name="comparer">Сравнитель объектов</param>
|
||||
private void CompareWarships(IComparer<DrawningWarship?> comparer)
|
||||
{
|
||||
if (_company == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_company.Sort(comparer);
|
||||
pictureBox.Image = _company.Show();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user