почти done

This commit is contained in:
Олег Кудринский 2024-05-20 01:50:09 +04:00
parent 2507c11893
commit 3f1795c430
12 changed files with 474 additions and 130 deletions

View File

@ -59,7 +59,7 @@ namespace ProjectContainerShip.CollectionGenericObjects;
/// <returns></returns>
public static int operator +(AbstractCompany company, DrawningShip ship)
{
return company._collection?.Insert(ship) ?? -1;
return company._collection?.Insert(ship, new DrawiningShipEqutables()) ?? -1;
}
/// <summary>
@ -104,6 +104,12 @@ namespace ProjectContainerShip.CollectionGenericObjects;
}
return bitmap;
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
public void Sort(IComparer<DrawningShip?> comparer) => _collection?.CollectionSort(comparer);
/// <summary>
/// Вывод заднего фона

View File

@ -0,0 +1,74 @@
namespace ProjectContainerShip.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></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();
}
}

View File

@ -1,4 +1,6 @@
namespace ProjectContainerShip.CollectionGenericObjects
using ProjectContainerShip.Drawings;
namespace ProjectContainerShip.CollectionGenericObjects
{
public interface ICollectionGenericObjects<T>
where T : class
@ -17,16 +19,18 @@
/// Добавление объекта в коллекцию
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// /// <param name="comparer">Сравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj);
int Insert(T obj, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Добавление объекта в коллекцию на конкретную позицию
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <param name="position">Позиция</param>
/// <param name="comparer">Сравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, int position);
int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции
@ -52,5 +56,11 @@
/// </summary>
/// <returns>Поэлементный вывод элементов коллекции</returns>
IEnumerable<T?> GetItems();
/// <summary>
/// Сортировка коллекции
/// </summary>
/// <param name="comparer"></param>
void CollectionSort(IComparer<T?> comparer);
}
}

View File

@ -1,5 +1,7 @@
using ProjectContainerShip.CollectionGenericObjects;
using ProjectContainerShip.Drawings;
using ProjectContainerShip.Exceptions;
using System.Linq;
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
@ -29,7 +31,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
}
}
public CollectionType GetColectionType => CollectionType.Massive;
public CollectionType GetColectionType => CollectionType.List;
/// <summary>
/// Конструктор
@ -44,19 +46,28 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
return _collection[position];
}
public int Insert(T obj)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO вставка в конец набора
if (comparer != null)
{
if (_collection.Contains(obj, comparer))
{
throw new ObjectIsEqualException();
}
}
if (Count == _maxCount) throw new CollectionOverflowException(Count);
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO проверка, что не превышено максимальное количество элементов
// TODO проверка позиции
// TODO вставка по позиции
if (comparer != null)
{
if (_collection.Contains(obj, comparer))
{
throw new ObjectIsEqualException();
}
}
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
_collection.Insert(position, obj);
@ -79,4 +90,9 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i];
}
}
void ICollectionGenericObjects<T>.CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
}
}

View File

@ -1,4 +1,5 @@

using ProjectContainerShip.Drawings;
using ProjectContainerShip.Exceptions;
namespace ProjectContainerShip.CollectionGenericObjects
@ -51,9 +52,16 @@ namespace ProjectContainerShip.CollectionGenericObjects
if (_collection[position] == null) throw new ObjectNotFoundException(position);
return _collection[position];
}
public int Insert(T obj)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// TODO вставка в свободное место набора
if (comparer != null)
{
foreach (T? item in _collection)
{
if ((comparer as IEqualityComparer<DrawningShip>).Equals(obj as DrawningShip, item as DrawningShip))
throw new ObjectIsEqualException();
}
}
int index = 0;
while (index < _collection.Length)
{
@ -66,13 +74,16 @@ namespace ProjectContainerShip.CollectionGenericObjects
}
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// ищется свободное место после этой позиции и идет вставка туда
// если нет после, ищем до
// TODO вставка
if (comparer != null)
{
foreach (T? item in _collection)
{
if ((comparer as IEqualityComparer<DrawningShip>).Equals(obj as DrawningShip, item as DrawningShip))
throw new ObjectIsEqualException();
}
}
if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position);
if (_collection[position] == null)
@ -122,5 +133,10 @@ namespace ProjectContainerShip.CollectionGenericObjects
yield return _collection[i];
}
}
void ICollectionGenericObjects<T>.CollectionSort(IComparer<T?> comparer)
{
Array.Sort(_collection, comparer);
}
}
}

View File

@ -15,12 +15,12 @@ where T : DrawningShip
/// <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>
/// Ключевое слово, с которого должен начинаться файл
@ -42,7 +42,7 @@ where T : DrawningShip
/// </summary>
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
}
/// <summary>
@ -52,19 +52,13 @@ where T : DrawningShip
/// <param name="collectionType">тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType)
{
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
// TODO Прописать логику для добавления
if (!(collectionType == CollectionType.None) && !_storages.ContainsKey(name))
{
if (collectionType == CollectionType.List)
{
_storages.Add(name, new ListGenericObjects<T>());
}
else if (collectionType == CollectionType.Massive)
{
_storages.Add(name, new MassiveGenericObjects<T>());
}
}
CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
if (_storages.ContainsKey(collectionInfo)) return;
if (collectionType == CollectionType.None) return;
else if (collectionType == CollectionType.Massive)
_storages[collectionInfo] = new MassiveGenericObjects<T>();
else if (collectionType == CollectionType.List)
_storages[collectionInfo] = new ListGenericObjects<T>();
}
/// <summary>
@ -73,8 +67,9 @@ where T : DrawningShip
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
// TODO Прописать логику для удаления коллекции
if (_storages.ContainsKey(name)) { _storages.Remove(name); }
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
if (_storages.ContainsKey(collectionInfo))
_storages.Remove(collectionInfo);
}
/// <summary>
/// Доступ к коллекции
@ -85,11 +80,9 @@ where T : DrawningShip
{
get
{
// TODO Продумать логику получения объекта
if (_storages.ContainsKey(name))
{
return _storages[name];
}
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
if (_storages.ContainsKey(collectionInfo))
return _storages[collectionInfo];
return null;
}
}
@ -111,7 +104,7 @@ where T : DrawningShip
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
{
StringBuilder sb = new();
sb.Append(Environment.NewLine);
@ -122,8 +115,6 @@ where T : DrawningShip
}
sb.Append(value.Key);
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.GetColectionType);
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.MaxCount);
sb.Append(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
@ -145,53 +136,61 @@ where T : DrawningShip
/// Загрузка информации по кораблям в хранилище из файла
/// </summary>
/// <param name="filename">Путь и имя файла</param>
public bool LoadData(string filename)
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
throw new Exception("Файл не существует");
}
using (StreamReader reader = File.OpenText(filename))
using (StreamReader fs = File.OpenText(filename))
{
string str = reader.ReadLine();
string str = fs.ReadLine();
if (str == null || str.Length == 0)
{
return false;
throw new Exception("В файле нет данных");
}
if (!str.StartsWith(_collectionKey))
{
return false;
throw new Exception("В файле неверные данные");
}
_storages.Clear();
string strs = "";
while ((strs = reader.ReadLine()) != null)
while ((strs = fs.ReadLine()) != null)
{
string[] record = strs.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("Не удалось создать коллекцию");
if (collection == null)
{
return false;
throw new Exception("Не удалось создать коллекцию");
}
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
collection.MaxCount = Convert.ToInt32(record[1]);
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
if (elem?.CreateDrawningShip() is T ship)
{
if (collection.Insert(ship) == -1)
try
{
return false;
if (collection.Insert(ship) == -1)
{
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
}
}
catch (CollectionOverflowException ex)
{
throw new Exception("Коллекция переполнена", ex);
}
}
}
_storages.Add(record[0], collection);
_storages.Add(collectionInfo, collection);
}
return true;
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectContainerShip.Drawings;
/// <summary>
/// Сравнение по цвету, скорости, весу
/// </summary>
public class DrawningShipCompareByColor : IComparer<DrawningShip?>
{
public int Compare(DrawningShip? x, DrawningShip? y)
{
if (x == null || x.EntityShip == null)
{
return 1;
}
if (y == null || y.EntityShip == null)
{
return -1;
}
var bodycolorCompare = x.EntityShip.BodyColor.Name.CompareTo(y.EntityShip.BodyColor.Name);
if (bodycolorCompare != 0)
{
return bodycolorCompare;
}
var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight);
}
}

View File

@ -0,0 +1,32 @@
namespace ProjectContainerShip.Drawings;
/// <summary>
/// Сравнение по типу, скорости, весу
/// </summary>
internal class DrawningShipCompareByType : IComparer<DrawningShip?>
{
public int Compare(DrawningShip? x, DrawningShip? y)
{
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);
}
}

View File

@ -0,0 +1,71 @@
using ProjectContainerShip.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectContainerShip.Drawings;
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 DrawningContainerShip && y is DrawningContainerShip)
{
EntityContainerShip _x = (EntityContainerShip)x.EntityShip;
EntityContainerShip _y = (EntityContainerShip)x.EntityShip;
if (_x.AdditionalColor != _y.AdditionalColor)
{
return false;
}
if (_x.Crane != _y.Crane)
{
return false;
}
if (_x.Container != _y.Container)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningShip obj)
{
return obj.GetHashCode();
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ProjectContainerShip.Exceptions;
/// <summary>
/// Класс, описывающий ошибку переполнения коллекции
/// </summary>
[Serializable]
public class ObjectIsEqualException : ApplicationException
{
public ObjectIsEqualException(int count) : base("В коллекции содержится равный элемент: " + count) { }
public ObjectIsEqualException() : base() { }
public ObjectIsEqualException(string message) : base(message) { }
public ObjectIsEqualException(string message, Exception exception) : base(message, exception) { }
protected ObjectIsEqualException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}

View File

@ -52,6 +52,8 @@
loadToolStripMenuItem = new ToolStripMenuItem();
saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog();
buttonSortByType = new Button();
buttonSortByColor = new Button();
groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout();
panelStorage.SuspendLayout();
@ -67,17 +69,19 @@
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.ForeColor = Color.Black;
groupBoxTools.Location = new Point(722, 24);
groupBoxTools.Margin = new Padding(2, 1, 2, 1);
groupBoxTools.Location = new Point(1341, 40);
groupBoxTools.Margin = new Padding(4, 2, 4, 2);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Padding = new Padding(2, 1, 2, 1);
groupBoxTools.Size = new Size(209, 497);
groupBoxTools.Padding = new Padding(4, 2, 4, 2);
groupBoxTools.Size = new Size(388, 1189);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonSortByType);
panelCompanyTools.Controls.Add(buttonSortByColor);
panelCompanyTools.Controls.Add(buttonAddShip);
panelCompanyTools.Controls.Add(maskedTextBox);
panelCompanyTools.Controls.Add(buttonRemoveShip);
@ -85,19 +89,19 @@
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(2, 275);
panelCompanyTools.Margin = new Padding(2, 1, 2, 1);
panelCompanyTools.Location = new Point(4, 608);
panelCompanyTools.Margin = new Padding(4, 2, 4, 2);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(205, 221);
panelCompanyTools.Size = new Size(380, 579);
panelCompanyTools.TabIndex = 10;
//
// buttonAddShip
//
buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddShip.Location = new Point(2, 33);
buttonAddShip.Margin = new Padding(2, 1, 2, 1);
buttonAddShip.Location = new Point(4, 17);
buttonAddShip.Margin = new Padding(4, 2, 4, 2);
buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Size = new Size(198, 36);
buttonAddShip.Size = new Size(367, 77);
buttonAddShip.TabIndex = 1;
buttonAddShip.Text = "Добавление корабля";
buttonAddShip.UseVisualStyleBackColor = true;
@ -105,21 +109,21 @@
//
// maskedTextBox
//
maskedTextBox.Location = new Point(2, 94);
maskedTextBox.Margin = new Padding(2, 1, 2, 1);
maskedTextBox.Location = new Point(4, 98);
maskedTextBox.Margin = new Padding(4, 2, 4, 2);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(201, 23);
maskedTextBox.Size = new Size(370, 39);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
//
// buttonRemoveShip
//
buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveShip.Location = new Point(2, 115);
buttonRemoveShip.Margin = new Padding(2, 1, 2, 1);
buttonRemoveShip.Location = new Point(4, 142);
buttonRemoveShip.Margin = new Padding(4, 2, 4, 2);
buttonRemoveShip.Name = "buttonRemoveShip";
buttonRemoveShip.Size = new Size(198, 36);
buttonRemoveShip.Size = new Size(367, 77);
buttonRemoveShip.TabIndex = 4;
buttonRemoveShip.Text = "Удалить корабль";
buttonRemoveShip.UseVisualStyleBackColor = true;
@ -128,10 +132,10 @@
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(2, 154);
buttonGoToCheck.Margin = new Padding(2, 1, 2, 1);
buttonGoToCheck.Location = new Point(4, 226);
buttonGoToCheck.Margin = new Padding(4, 2, 4, 2);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(198, 36);
buttonGoToCheck.Size = new Size(367, 77);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
@ -140,10 +144,10 @@
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(2, 193);
buttonRefresh.Margin = new Padding(2, 1, 2, 1);
buttonRefresh.Location = new Point(4, 309);
buttonRefresh.Margin = new Padding(4, 2, 4, 2);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(198, 36);
buttonRefresh.Size = new Size(367, 77);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
@ -151,10 +155,10 @@
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(3, 256);
buttonCreateCompany.Margin = new Padding(2, 1, 2, 1);
buttonCreateCompany.Location = new Point(6, 546);
buttonCreateCompany.Margin = new Padding(4, 2, 4, 2);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(199, 22);
buttonCreateCompany.Size = new Size(370, 47);
buttonCreateCompany.TabIndex = 9;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
@ -170,19 +174,19 @@
panelStorage.Controls.Add(textBoxCollectionName);
panelStorage.Controls.Add(labelCollectionName);
panelStorage.Dock = DockStyle.Top;
panelStorage.Location = new Point(2, 17);
panelStorage.Margin = new Padding(2, 1, 2, 1);
panelStorage.Location = new Point(4, 34);
panelStorage.Margin = new Padding(4, 2, 4, 2);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(205, 215);
panelStorage.Size = new Size(380, 459);
panelStorage.TabIndex = 8;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(21, 43);
radioButtonMassive.Margin = new Padding(2, 1, 2, 1);
radioButtonMassive.Location = new Point(39, 92);
radioButtonMassive.Margin = new Padding(4, 2, 4, 2);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(67, 19);
radioButtonMassive.Size = new Size(128, 36);
radioButtonMassive.TabIndex = 7;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
@ -190,10 +194,10 @@
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(2, 181);
buttonCollectionDel.Margin = new Padding(2, 1, 2, 1);
buttonCollectionDel.Location = new Point(4, 386);
buttonCollectionDel.Margin = new Padding(4, 2, 4, 2);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(199, 22);
buttonCollectionDel.Size = new Size(370, 47);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
@ -202,19 +206,18 @@
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(2, 87);
listBoxCollection.Margin = new Padding(2, 1, 2, 1);
listBoxCollection.Location = new Point(4, 186);
listBoxCollection.Margin = new Padding(4, 2, 4, 2);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(201, 94);
listBoxCollection.Size = new Size(370, 196);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(2, 62);
buttonCollectionAdd.Margin = new Padding(2, 1, 2, 1);
buttonCollectionAdd.Location = new Point(4, 132);
buttonCollectionAdd.Margin = new Padding(4, 2, 4, 2);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(199, 22);
buttonCollectionAdd.Size = new Size(370, 47);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
@ -223,10 +226,10 @@
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(116, 43);
radioButtonList.Margin = new Padding(2, 1, 2, 1);
radioButtonList.Location = new Point(215, 92);
radioButtonList.Margin = new Padding(4, 2, 4, 2);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(66, 19);
radioButtonList.Size = new Size(125, 36);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
@ -234,19 +237,19 @@
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(2, 22);
textBoxCollectionName.Margin = new Padding(2, 1, 2, 1);
textBoxCollectionName.Location = new Point(4, 47);
textBoxCollectionName.Margin = new Padding(4, 2, 4, 2);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(201, 23);
textBoxCollectionName.Size = new Size(370, 39);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(37, 5);
labelCollectionName.Margin = new Padding(2, 0, 2, 0);
labelCollectionName.Location = new Point(69, 11);
labelCollectionName.Margin = new Padding(4, 0, 4, 0);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(125, 15);
labelCollectionName.Size = new Size(251, 32);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
@ -256,20 +259,20 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(3, 234);
comboBoxSelectorCompany.Margin = new Padding(2, 1, 2, 1);
comboBoxSelectorCompany.Location = new Point(6, 499);
comboBoxSelectorCompany.Margin = new Padding(4, 2, 4, 2);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(201, 23);
comboBoxSelectorCompany.Size = new Size(370, 40);
comboBoxSelectorCompany.TabIndex = 0;
comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged;
//
// pictureBox
//
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 24);
pictureBox.Margin = new Padding(2, 1, 2, 1);
pictureBox.Location = new Point(0, 40);
pictureBox.Margin = new Padding(4, 2, 4, 2);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(722, 497);
pictureBox.Size = new Size(1341, 1189);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
@ -279,8 +282,7 @@
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Padding = new Padding(3, 1, 0, 1);
menuStrip.Size = new Size(931, 24);
menuStrip.Size = new Size(1729, 40);
menuStrip.TabIndex = 2;
menuStrip.Text = "menuStrip1";
//
@ -288,14 +290,14 @@
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(48, 22);
файлToolStripMenuItem.Size = new Size(90, 36);
файлToolStripMenuItem.Text = "Файл";
//
// saveToolStripMenuItem
//
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S;
saveToolStripMenuItem.Size = new Size(181, 22);
saveToolStripMenuItem.Size = new Size(361, 44);
saveToolStripMenuItem.Text = "Сохранение";
saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
@ -303,7 +305,7 @@
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L;
loadToolStripMenuItem.Size = new Size(181, 22);
loadToolStripMenuItem.Size = new Size(361, 44);
loadToolStripMenuItem.Text = "Загрузка";
loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -315,16 +317,40 @@
//
openFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonSortByType.Location = new Point(4, 413);
buttonSortByType.Margin = new Padding(4, 2, 4, 2);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(367, 77);
buttonSortByType.TabIndex = 7;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += ButtonSortByType_Click;
//
// buttonSortByColor
//
buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonSortByColor.Location = new Point(4, 494);
buttonSortByColor.Margin = new Padding(4, 2, 4, 2);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(367, 77);
buttonSortByColor.TabIndex = 8;
buttonSortByColor.Text = "Сортировка по цвету ";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += ButtonSortByColor_Click;
//
// FormShipCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(13F, 32F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(931, 521);
ClientSize = new Size(1729, 1229);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Margin = new Padding(2, 1, 2, 1);
Margin = new Padding(4, 2, 4, 2);
Name = "FormShipCollection";
Text = "Коллекция кораблей";
groupBoxTools.ResumeLayout(false);
@ -365,5 +391,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -104,7 +104,7 @@ public partial class FormShipCollection : Form
}
int position = Convert.ToInt32(maskedTextBox.Text);
try
try
{
if (_company - position != null)
{
@ -156,7 +156,7 @@ public partial class FormShipCollection : Form
{
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (ship == null)
{
return;
@ -221,7 +221,7 @@ public partial class FormShipCollection : 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);
@ -337,4 +337,38 @@ public partial class FormShipCollection : Form
}
}
}
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
private void CompareShips(IComparer<DrawningShip?> comparer)
{
if (_company == null)
{
return;
}
_company.Sort(comparer);
pictureBox.Image = _company.Show();
}
/// <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());
}
}