From 794d1d800c35198ff578398682707f3f2041596b Mon Sep 17 00:00:00 2001 From: "leonteva.v" Date: Sat, 15 Jun 2024 07:47:15 +0400 Subject: [PATCH] lab 8 --- .../AbstractCompany.cs | 5 +- .../CollectionInfo.cs | 73 ++ .../ICollectionGenericObjects.cs | 5 +- .../ListGenericObjects.cs | 16 +- .../MassiveGenericObjects.cs | 26 +- .../StorageCollection.cs | 41 +- .../Drawnings/DrawningCompareByColor.cs | 36 + .../Drawnings/DrawningCompareByType.cs | 36 + .../Drawnings/DrawningShipEqutables.cs | 62 ++ .../Exceptions/ObjectExistsException.cs | 20 + .../FormShipCollection.Designer.cs | 688 ++++++++++-------- .../ProjectBattleship/FormShipCollection.cs | 255 ++++--- .../ProjectBattleship/FormShipCollection.resx | 3 + 13 files changed, 794 insertions(+), 472 deletions(-) create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionInfo.cs create mode 100644 ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByColor.cs create mode 100644 ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByType.cs create mode 100644 ProjectBattleship/ProjectBattleship/Drawnings/DrawningShipEqutables.cs create mode 100644 ProjectBattleship/ProjectBattleship/Exceptions/ObjectExistsException.cs diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs index 36b24b7..1cf4395 100644 --- a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs @@ -1,5 +1,6 @@ using ProjectBattleship.Drawnings; using System; +using Battleship.Drawnings; using System.Collections.Generic; using System.Linq; using System.Text; @@ -63,9 +64,9 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawingShip ship) { - return company._collection.Insert(ship); + return company._collection.Insert(ship, new DrawningShipEqutables()); } - + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); /// /// Перегрузка оператора удаления для класса /// diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionInfo.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..590398f --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,73 @@ +using ProjectBattleship.CollectionGenericObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.CollectionGenericObjects; +/// +/// Класс, хранящиий информацию по коллекции +/// +public class CollectionInfo : IEquatable +{ + /// + /// Название + /// + public string Name { get; private set; } + /// + /// Тип + /// + public CollectionType CollectionType { get; private set; } + /// + /// Описание + /// + public string Description { get; private set; } + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separator = "-"; + /// + /// Конструктор + /// + /// Название + /// Тип + /// Описание + public CollectionInfo(string name, CollectionType collectionType, string description) + { + Name = name; + CollectionType = collectionType; + Description = description; + } + /// + /// Создание объекта из строки + /// + /// Строка + /// Объект или null + 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(); + } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs index 57262eb..b020062 100644 --- a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -24,14 +24,14 @@ namespace ProjectBattleship.CollectionGenericObjects /// /// Добавляемый объект /// true - удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция /// true - удачно, false - вставка не удалась - bool Insert(T obj, int position); + bool Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -56,5 +56,6 @@ namespace ProjectBattleship.CollectionGenericObjects /// /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); + void CollectionSort(IComparer comparer); } } \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs index a8e9cc7..8540162 100644 --- a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs @@ -45,21 +45,25 @@ public class ListGenericObjects : ICollectionGenericObjects throw new PositionOutOfCollectionException(position); return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - if (_collection.Count + 1 <= _maxCount) + if (Count + 1 <= _maxCount) { + if (_collection.Contains(obj, comparer)) + throw new ObjectExistsException(); _collection.Add(obj); - return _collection.Count - 1; + return Count - 1; } throw new CollectionOverflowException(MaxCount); } - public bool Insert(T obj, int position) + public bool Insert(T obj, int position, IEqualityComparer? comparer = null) { if (_collection.Count + 1 > MaxCount) throw new CollectionOverflowException(MaxCount); if (position < 0 || position >= MaxCount) throw new PositionOutOfCollectionException(position); + if (_collection.Contains(obj, comparer)) + throw new ObjectExistsException(); _collection.Insert(position, obj); return true; } @@ -78,4 +82,8 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs index ebd1ceb..28ea8a7 100644 --- a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs @@ -63,22 +63,24 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - for (int i = 0; i < _collection.Length; i++) + int index = Array.IndexOf(_collection, null); + if (_collection.Contains(obj, comparer)) + throw new ObjectExistsException(index); + if (index >= 0) { - if (_collection[i] == null) - { - _collection[i] = obj; - return i; - } + _collection[index] = obj; + return index; } throw new CollectionOverflowException(_collection.Length); } - public bool Insert(T obj, int position) + public bool Insert(T obj, int position, IEqualityComparer? comparer = null) { if (position < 0 || position >= _collection.Length) // проверка позиции throw new PositionOutOfCollectionException(position); + if (_collection.Contains(obj, comparer)) + throw new ObjectExistsException(position); if (_collection[position] == null) // Попытка вставить на указанную позицию { _collection[position] = obj; @@ -120,4 +122,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + public void CollectionSort(IComparer comparer) + { + if (_collection?.Length > 0) + { + Array.Sort(_collection, comparer); + Array.Reverse(_collection); + } + } } \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs index a4f85fa..d15d5cd 100644 --- a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs @@ -2,6 +2,8 @@ using ProjectBattleship.Drawnings; using System.Text; using Battleship.Exceptions; +using Microsoft.AspNetCore.Http; +using Battleship.CollectionGenericObjects; namespace ProjectBattleship.CollectionGenericObjects; @@ -15,11 +17,11 @@ public class StorageCollection /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Конструктор /// @@ -28,7 +30,7 @@ public class StorageCollection private readonly string _separatorItems = ";"; public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// /// Добавление коллекции в хранилище @@ -37,15 +39,16 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (string.IsNullOrEmpty(name) || _storages.ContainsKey(name)) + CollectionInfo tempInfo = new(name, collectionType, string.Empty); + if (string.IsNullOrEmpty(name) || _storages.ContainsKey(tempInfo)) return; switch (collectionType) { case CollectionType.List: - _storages.Add(name, new ListGenericObjects()); + _storages.Add(tempInfo, new ListGenericObjects()); break; case CollectionType.Massive: - _storages.Add(name, new MassiveGenericObjects()); + _storages.Add(tempInfo, new MassiveGenericObjects()); break; default: break; @@ -57,8 +60,9 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - if (_storages.ContainsKey(name)) - _storages.Remove(name); + CollectionInfo tempInfo = new(name, CollectionType.None, string.Empty); + if (tempInfo.Name != null && _storages.ContainsKey(tempInfo)) + _storages.Remove(tempInfo); } /// /// Доступ к коллекции @@ -69,9 +73,9 @@ public class StorageCollection { get { - if (_storages.TryGetValue(name, out ICollectionGenericObjects? value)) - return value; - return null; + CollectionInfo tempInfo = new(name, CollectionType.None, string.Empty); + if (tempInfo == null || !_storages.ContainsKey(tempInfo)) { return null; } + return _storages[tempInfo]; } } public void SaveData(string filename) @@ -88,15 +92,13 @@ public class StorageCollection using (StreamWriter sw = new StreamWriter(filename)) { sw.WriteLine(_collectionKey.ToString()); - foreach (KeyValuePair> kvpair in _storages) + foreach (KeyValuePair> kvpair in _storages) { // не сохраняем пустые коллекции if (kvpair.Value.Count == 0) continue; sb.Append(kvpair.Key); sb.Append(_separatorForKeyValue); - sb.Append(kvpair.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); sb.Append(kvpair.Value.MaxCount); sb.Append(_separatorForKeyValue); foreach (T? item in kvpair.Value.GetItems()) @@ -128,18 +130,19 @@ public class StorageCollection while ((str = sr.ReadLine()) != null) { string[] record = str.Split(_separatorForKeyValue); - if (record.Length != 4) + if (record.Length != 3) { continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции:" + record[0]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType); if (collection == null) { throw new InvalidOperationException("Не удалось определить тип коллекции:" + 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?.CreateDrawningShip() is T ship) @@ -157,7 +160,7 @@ public class StorageCollection } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByColor.cs b/ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByColor.cs new file mode 100644 index 0000000..113ca92 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByColor.cs @@ -0,0 +1,36 @@ +using ProjectBattleship.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.Drawnings; +public class DrawningCompareByColor : IComparer +{ + public int Compare(DrawingShip? x, DrawingShip? 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.EntityShip.BodyColor.Name != y.EntityShip.BodyColor.Name) + { + return x.EntityShip.BodyColor.Name.CompareTo(y.EntityShip.BodyColor.Name); + } + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByType.cs b/ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByType.cs new file mode 100644 index 0000000..ebf9c00 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/Drawnings/DrawningCompareByType.cs @@ -0,0 +1,36 @@ +using ProjectBattleship.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace Battleship.Drawnings; + +public class DrawningCompareByType : IComparer +{ + public int Compare(DrawingShip? x, DrawingShip? 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); + } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/Drawnings/DrawningShipEqutables.cs b/ProjectBattleship/ProjectBattleship/Drawnings/DrawningShipEqutables.cs new file mode 100644 index 0000000..25e9f83 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/Drawnings/DrawningShipEqutables.cs @@ -0,0 +1,62 @@ +using ProjectBattleship.Entities; +using ProjectBattleship.Drawnings; +using ProjectBattleship.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace Battleship.Drawnings; + +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawningShipEqutables : IEqualityComparer +{ + public bool Equals(DrawingShip? x, DrawingShip? 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 DrawingBattleship && y is DrawingBattleship) + { + EntityBattleship EntityX = (EntityBattleship)x.EntityShip; + EntityBattleship EntityY = (EntityBattleship)y.EntityShip; + if (EntityX.RocketLauncher != EntityY.RocketLauncher) + { + return false; + } + if (EntityX.Turret != EntityY.Turret) + { + return false; + } + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawingShip obj) + { + return obj.GetHashCode(); + } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/Exceptions/ObjectExistsException.cs b/ProjectBattleship/ProjectBattleship/Exceptions/ObjectExistsException.cs new file mode 100644 index 0000000..578897d --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/Exceptions/ObjectExistsException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.Exceptions; +/// +/// Класс, описывающий ошибку переполнения коллекции +/// +[Serializable] +internal class ObjectExistsException : ApplicationException +{ + public ObjectExistsException(int count) : base("Вставка существующего объекта") { } + public ObjectExistsException() : base() { } + public ObjectExistsException(string message) : base(message) { } + public ObjectExistsException(string message, Exception exception) : base(message, exception) { } + protected ObjectExistsException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs b/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs index 4026aee..9f7727c 100644 --- a/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs +++ b/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs @@ -1,329 +1,375 @@ namespace ProjectBattleship { - partial class FormShipCollection - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; + partial class FormShipCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } - #region Windows Form Designer generated code + #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - pictureBox = new PictureBox(); - groupBoxTools = new GroupBox(); - buttonCollectionDel = new Button(); - listBoxCollection = new ListBox(); - buttonCollectionAdd = new Button(); - radioButtonList = new RadioButton(); - radioButtonMassive = new RadioButton(); - textBoxCollectionName = new TextBox(); - labelCollectionName = new Label(); - comboBoxSelectorCompany = new ComboBox(); - buttonCreateCompany = new Button(); - buttonRefresh = new Button(); - buttonGoToCheck = new Button(); - buttonRemoveShip = new Button(); - maskedTextBoxPosition = new MaskedTextBox(); - buttonAddShip = new Button(); - menuStrip = new MenuStrip(); - fileToolStripMenuItem = new ToolStripMenuItem(); - saveToolStripMenuItem = new ToolStripMenuItem(); - loadToolStripMenuItem = new ToolStripMenuItem(); - openFileDialog = new OpenFileDialog(); - saveFileDialog = new SaveFileDialog(); - panelCompanyTools = new Panel(); - ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); - groupBoxTools.SuspendLayout(); - menuStrip.SuspendLayout(); - panelCompanyTools.SuspendLayout(); - SuspendLayout(); - // - // pictureBox - // - pictureBox.Dock = DockStyle.Left; - pictureBox.Location = new Point(0, 24); - pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(783, 592); - pictureBox.TabIndex = 0; - pictureBox.TabStop = false; - // - // groupBoxTools - // - groupBoxTools.Controls.Add(buttonCreateCompany); - groupBoxTools.Controls.Add(buttonCollectionDel); - groupBoxTools.Controls.Add(listBoxCollection); - groupBoxTools.Controls.Add(buttonCollectionAdd); - groupBoxTools.Controls.Add(radioButtonList); - groupBoxTools.Controls.Add(radioButtonMassive); - groupBoxTools.Controls.Add(textBoxCollectionName); - groupBoxTools.Controls.Add(labelCollectionName); - groupBoxTools.Controls.Add(comboBoxSelectorCompany); - groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(784, 24); - groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(178, 592); - groupBoxTools.TabIndex = 0; - groupBoxTools.TabStop = false; - groupBoxTools.Text = "Инструменты"; - // - // buttonCollectionDel - // - buttonCollectionDel.Location = new Point(7, 232); - buttonCollectionDel.Name = "buttonCollectionDel"; - buttonCollectionDel.Size = new Size(167, 23); - buttonCollectionDel.TabIndex = 13; - buttonCollectionDel.Text = "Удалить коллекцию"; - buttonCollectionDel.UseVisualStyleBackColor = true; - buttonCollectionDel.Click += buttonCollectionDel_Click; - // - // listBoxCollection - // - listBoxCollection.FormattingEnabled = true; - listBoxCollection.ItemHeight = 15; - listBoxCollection.Location = new Point(7, 118); - listBoxCollection.Name = "listBoxCollection"; - listBoxCollection.Size = new Size(167, 109); - listBoxCollection.TabIndex = 12; - // - // buttonCollectionAdd - // - buttonCollectionAdd.Location = new Point(7, 90); - buttonCollectionAdd.Name = "buttonCollectionAdd"; - buttonCollectionAdd.Size = new Size(167, 23); - buttonCollectionAdd.TabIndex = 11; - buttonCollectionAdd.Text = "Добавить коллекцию"; - buttonCollectionAdd.UseVisualStyleBackColor = true; - buttonCollectionAdd.Click += buttonCollectionAdd_Click; - // - // radioButtonList - // - radioButtonList.AutoSize = true; - radioButtonList.Location = new Point(102, 64); - radioButtonList.Name = "radioButtonList"; - radioButtonList.Size = new Size(66, 19); - radioButtonList.TabIndex = 10; - radioButtonList.TabStop = true; - radioButtonList.Text = "Список"; - radioButtonList.UseVisualStyleBackColor = true; - // - // radioButtonMassive - // - radioButtonMassive.AutoSize = true; - radioButtonMassive.Location = new Point(20, 64); - radioButtonMassive.Name = "radioButtonMassive"; - radioButtonMassive.Size = new Size(67, 19); - radioButtonMassive.TabIndex = 9; - radioButtonMassive.TabStop = true; - radioButtonMassive.Text = "Массив"; - radioButtonMassive.UseVisualStyleBackColor = true; - // - // textBoxCollectionName - // - textBoxCollectionName.Location = new Point(7, 36); - textBoxCollectionName.Name = "textBoxCollectionName"; - textBoxCollectionName.Size = new Size(167, 23); - textBoxCollectionName.TabIndex = 8; - // - // labelCollectionName - // - labelCollectionName.AutoSize = true; - labelCollectionName.Location = new Point(31, 18); - labelCollectionName.Name = "labelCollectionName"; - labelCollectionName.Size = new Size(125, 15); - labelCollectionName.TabIndex = 7; - labelCollectionName.Text = "Название коллекции:"; - // - // comboBoxSelectorCompany - // - comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxSelectorCompany.FormattingEnabled = true; - comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(7, 274); - comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; - comboBoxSelectorCompany.Size = new Size(166, 23); - comboBoxSelectorCompany.TabIndex = 0; - comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; - // - // buttonCreateCompany - // - buttonCreateCompany.Location = new Point(9, 313); - buttonCreateCompany.Name = "buttonCreateCompany"; - buttonCreateCompany.Size = new Size(165, 23); - buttonCreateCompany.TabIndex = 14; - buttonCreateCompany.Text = "Создать компанию"; - buttonCreateCompany.UseVisualStyleBackColor = true; - buttonCreateCompany.Click += buttonCreateCompany_Click; - // - // buttonRefresh - // - buttonRefresh.Location = new Point(8, 208); - buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(164, 40); - buttonRefresh.TabIndex = 6; - buttonRefresh.Text = "Обновить"; - buttonRefresh.UseVisualStyleBackColor = true; - buttonRefresh.Click += ButtonRefresh_Click; - // - // buttonGoToCheck - // - buttonGoToCheck.Location = new Point(8, 162); - buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(164, 40); - buttonGoToCheck.TabIndex = 5; - buttonGoToCheck.Text = "Передать на тесты"; - buttonGoToCheck.UseVisualStyleBackColor = true; - buttonGoToCheck.Click += ButtonGoToCheck_Click; - // - // buttonRemoveShip - // - buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveShip.Location = new Point(8, 116); - buttonRemoveShip.Name = "buttonRemoveShip"; - buttonRemoveShip.Size = new Size(164, 40); - buttonRemoveShip.TabIndex = 4; - buttonRemoveShip.Text = "Удалить корабль"; - buttonRemoveShip.UseVisualStyleBackColor = true; - buttonRemoveShip.Click += ButtonRemoveShip_Click; - // - // maskedTextBoxPosition - // - maskedTextBoxPosition.Location = new Point(8, 87); - maskedTextBoxPosition.Mask = "00"; - maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - maskedTextBoxPosition.Size = new Size(164, 23); - maskedTextBoxPosition.TabIndex = 3; - // - // buttonAddShip - // - buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddShip.Location = new Point(8, 41); - buttonAddShip.Name = "buttonAddShip"; - buttonAddShip.Size = new Size(164, 40); - buttonAddShip.TabIndex = 1; - buttonAddShip.Text = "Добавление корабля"; - buttonAddShip.UseVisualStyleBackColor = true; - buttonAddShip.Click += ButtonAddShip_Click; - // - // menuStrip - // - menuStrip.ImageScalingSize = new Size(20, 20); - menuStrip.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); - menuStrip.Location = new Point(0, 0); - menuStrip.Name = "menuStrip"; - menuStrip.Padding = new Padding(5, 2, 0, 2); - menuStrip.Size = new Size(962, 24); - menuStrip.TabIndex = 1; - menuStrip.Text = "menuStrip1"; - // - // fileToolStripMenuItem - // - fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); - fileToolStripMenuItem.Name = "fileToolStripMenuItem"; - fileToolStripMenuItem.Size = new Size(48, 20); - fileToolStripMenuItem.Text = "Файл"; - // - // saveToolStripMenuItem - // - saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - saveToolStripMenuItem.Size = new Size(133, 22); - saveToolStripMenuItem.Text = "Сохранить"; - saveToolStripMenuItem.Click += saveToolStripMenuItem_Click; - // - // loadToolStripMenuItem - // - loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - loadToolStripMenuItem.Size = new Size(133, 22); - loadToolStripMenuItem.Text = "Загрузить"; - loadToolStripMenuItem.Click += loadToolStripMenuItem_Click; - // - // openFileDialog - // - openFileDialog.FileName = "Ships"; - // - // saveFileDialog - // - saveFileDialog.FileName = "Ships"; - // - // panelCompanyTools - // - panelCompanyTools.Controls.Add(buttonAddShip); - panelCompanyTools.Controls.Add(maskedTextBoxPosition); - panelCompanyTools.Controls.Add(buttonRemoveShip); - panelCompanyTools.Controls.Add(buttonGoToCheck); - panelCompanyTools.Controls.Add(buttonRefresh); - panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(784, 366); - panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(178, 250); - panelCompanyTools.TabIndex = 15; - // - // FormShipCollection - // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(962, 616); - Controls.Add(panelCompanyTools); - Controls.Add(groupBoxTools); - Controls.Add(pictureBox); - Controls.Add(menuStrip); - MainMenuStrip = menuStrip; - Name = "FormShipCollection"; - Text = "Коллекция кораблей"; - ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); - groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); - menuStrip.ResumeLayout(false); - menuStrip.PerformLayout(); - panelCompanyTools.ResumeLayout(false); - panelCompanyTools.PerformLayout(); - ResumeLayout(false); - PerformLayout(); - } + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + pictureBox = new PictureBox(); + groupBoxTools = new GroupBox(); + buttonCreateCompany = new Button(); + buttonCollectionDel = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonMassive = new RadioButton(); + textBoxCollectionName = new TextBox(); + labelCollectionName = new Label(); + comboBoxSelectorCompany = new ComboBox(); + buttonRefresh = new Button(); + buttonGoToCheck = new Button(); + buttonRemoveShip = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddShip = new Button(); + menuStrip = new MenuStrip(); + fileToolStripMenuItem = new ToolStripMenuItem(); + saveToolStripMenuItem = new ToolStripMenuItem(); + loadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); + panelCompanyTools = new Panel(); + buttonSortByType = new Button(); + buttonSortByColor = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + groupBoxTools.SuspendLayout(); + menuStrip.SuspendLayout(); + panelCompanyTools.SuspendLayout(); + SuspendLayout(); + // + // pictureBox + // + pictureBox.Dock = DockStyle.Left; + pictureBox.Location = new Point(0, 30); + pictureBox.Margin = new Padding(3, 4, 3, 4); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(895, 836); + pictureBox.TabIndex = 0; + pictureBox.TabStop = false; + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonSortByColor); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonSortByType); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(buttonCollectionDel); + groupBoxTools.Controls.Add(listBoxCollection); + groupBoxTools.Controls.Add(buttonCollectionAdd); + groupBoxTools.Controls.Add(radioButtonList); + groupBoxTools.Controls.Add(radioButtonMassive); + groupBoxTools.Controls.Add(textBoxCollectionName); + groupBoxTools.Controls.Add(labelCollectionName); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(897, 30); + groupBoxTools.Margin = new Padding(3, 4, 3, 4); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Padding = new Padding(3, 4, 3, 4); + groupBoxTools.Size = new Size(203, 836); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(9, 384); + buttonCreateCompany.Margin = new Padding(3, 4, 3, 4); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(189, 31); + buttonCreateCompany.TabIndex = 14; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += buttonCreateCompany_Click; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(8, 309); + buttonCollectionDel.Margin = new Padding(3, 4, 3, 4); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(191, 31); + buttonCollectionDel.TabIndex = 13; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += buttonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(8, 157); + listBoxCollection.Margin = new Padding(3, 4, 3, 4); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(190, 144); + listBoxCollection.TabIndex = 12; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(8, 120); + buttonCollectionAdd.Margin = new Padding(3, 4, 3, 4); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(191, 31); + buttonCollectionAdd.TabIndex = 11; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(117, 85); + radioButtonList.Margin = new Padding(3, 4, 3, 4); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(80, 24); + radioButtonList.TabIndex = 10; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(23, 85); + radioButtonMassive.Margin = new Padding(3, 4, 3, 4); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(82, 24); + radioButtonMassive.TabIndex = 9; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(8, 48); + textBoxCollectionName.Margin = new Padding(3, 4, 3, 4); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(190, 27); + textBoxCollectionName.TabIndex = 8; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(35, 24); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(158, 20); + labelCollectionName.TabIndex = 7; + labelCollectionName.Text = "Название коллекции:"; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(10, 348); + comboBoxSelectorCompany.Margin = new Padding(3, 4, 3, 4); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(189, 28); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; + // + // buttonRefresh + // + buttonRefresh.Location = new Point(9, 277); + buttonRefresh.Margin = new Padding(3, 4, 3, 4); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(187, 53); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Location = new Point(9, 216); + buttonGoToCheck.Margin = new Padding(3, 4, 3, 4); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(187, 53); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveShip + // + buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveShip.Location = new Point(9, 155); + buttonRemoveShip.Margin = new Padding(3, 4, 3, 4); + buttonRemoveShip.Name = "buttonRemoveShip"; + buttonRemoveShip.Size = new Size(187, 53); + buttonRemoveShip.TabIndex = 4; + buttonRemoveShip.Text = "Удалить корабль"; + buttonRemoveShip.UseVisualStyleBackColor = true; + buttonRemoveShip.Click += ButtonRemoveShip_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(9, 116); + maskedTextBoxPosition.Margin = new Padding(3, 4, 3, 4); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(187, 27); + maskedTextBoxPosition.TabIndex = 3; + // + // buttonAddShip + // + buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddShip.Location = new Point(9, 55); + buttonAddShip.Margin = new Padding(3, 4, 3, 4); + buttonAddShip.Name = "buttonAddShip"; + buttonAddShip.Size = new Size(187, 53); + buttonAddShip.TabIndex = 1; + buttonAddShip.Text = "Добавление корабля"; + buttonAddShip.UseVisualStyleBackColor = true; + buttonAddShip.Click += ButtonAddShip_Click; + // + // menuStrip + // + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Padding = new Padding(6, 3, 0, 3); + menuStrip.Size = new Size(1100, 30); + menuStrip.TabIndex = 1; + menuStrip.Text = "menuStrip1"; + // + // fileToolStripMenuItem + // + fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + fileToolStripMenuItem.Size = new Size(59, 24); + fileToolStripMenuItem.Text = "Файл"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.Size = new Size(166, 26); + saveToolStripMenuItem.Text = "Сохранить"; + saveToolStripMenuItem.Click += saveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.Size = new Size(166, 26); + loadToolStripMenuItem.Text = "Загрузить"; + loadToolStripMenuItem.Click += loadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "Ships"; + // + // saveFileDialog + // + saveFileDialog.FileName = "Ships"; + // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddShip); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonRemoveShip); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(0, 507); + panelCompanyTools.Margin = new Padding(3, 4, 3, 4); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(203, 329); + panelCompanyTools.TabIndex = 15; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(9, 423); + buttonSortByType.Margin = new Padding(3, 4, 3, 4); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(189, 31); + buttonSortByType.TabIndex = 15; + buttonSortByType.Text = "Сортировать по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(10, 462); + buttonSortByColor.Margin = new Padding(3, 4, 3, 4); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(189, 31); + buttonSortByColor.TabIndex = 16; + buttonSortByColor.Text = "Сортировать по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // FormShipCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1100, 866); + Controls.Add(groupBoxTools); + Controls.Add(pictureBox); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; + Margin = new Padding(3, 4, 3, 4); + Name = "FormShipCollection"; + Text = "Коллекция кораблей"; + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); + ResumeLayout(false); + PerformLayout(); + } - #endregion + #endregion - private PictureBox pictureBox; - private GroupBox groupBoxTools; - private ComboBox comboBoxSelectorCompany; - private Button buttonAddShip; - private MaskedTextBox maskedTextBoxPosition; - private Button buttonRefresh; - private Button buttonGoToCheck; - private Button buttonRemoveShip; - private ListBox listBoxCollection; - private Button buttonCollectionAdd; - private RadioButton radioButtonList; - private RadioButton radioButtonMassive; - private TextBox textBoxCollectionName; - private Label labelCollectionName; - private Button buttonCollectionDel; - private Button buttonCreateCompany; - private MenuStrip menuStrip; - private ToolStripMenuItem fileToolStripMenuItem; - private ToolStripMenuItem saveToolStripMenuItem; - private ToolStripMenuItem loadToolStripMenuItem; - private OpenFileDialog openFileDialog; - private SaveFileDialog saveFileDialog; - private Panel panelCompanyTools; - } + private PictureBox pictureBox; + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddShip; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonRefresh; + private Button buttonGoToCheck; + private Button buttonRemoveShip; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private Button buttonCollectionDel; + private Button buttonCreateCompany; + private MenuStrip menuStrip; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; + private Panel panelCompanyTools; + private Button buttonSortByColor; + private Button buttonSortByType; + } } \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/FormShipCollection.cs b/ProjectBattleship/ProjectBattleship/FormShipCollection.cs index e12c09b..41ab577 100644 --- a/ProjectBattleship/ProjectBattleship/FormShipCollection.cs +++ b/ProjectBattleship/ProjectBattleship/FormShipCollection.cs @@ -4,17 +4,17 @@ using ProjectBattleship.Drawnings; using System.Windows.Forms; using Battleship.Exceptions; using Microsoft.Extensions.Logging; +using Battleship.Drawnings; namespace ProjectBattleship { - public partial class FormShipCollection : Form - - { - private readonly StorageCollection _storageCollection; - /// - /// Компания - /// - private AbstractCompany? _company = null; + public partial class FormShipCollection : Form + { + private readonly StorageCollection _storageCollection; + /// + /// Компания + /// + private AbstractCompany? _company = null; private readonly ILogger _logger; /// /// Конструктор @@ -31,26 +31,26 @@ namespace ProjectBattleship /// /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) - { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new ShipDocks(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } - panelCompanyTools.Enabled = false; - } - /// - /// Добавление обычного автомобиля - /// - /// - /// - private void ButtonAddShip_Click(object sender, EventArgs e) - { - FormShipConfig form = new(); + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new ShipDocks(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + panelCompanyTools.Enabled = false; + } + /// + /// Добавление обычного автомобиля + /// + /// + /// + private void ButtonAddShip_Click(object sender, EventArgs e) + { + FormShipConfig form = new(); form.Show(); form.AddEvent(SetShip); - } + } private void SetShip(DrawingShip? ship) { if (_company == null || ship == null) @@ -68,18 +68,23 @@ namespace ProjectBattleship MessageBoxIcon.Error); _logger.LogError($"Ошибка: {ex.Message}", ex.Message); } + catch (ObjectExistsException ex) + { + MessageBox.Show("Такой объект есть в коллекции"); + _logger.LogWarning($"Добавление существующего объекта: {ex.Message}"); + } } private void ButtonRemoveShip_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) - { - return; - } + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) + { + return; + } - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) - { - return; - } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } try { @@ -106,84 +111,84 @@ namespace ProjectBattleship MessageBox.Show("Ошибка: неправильная позиция"); _logger.LogError("Ошибка: {Message}", ex.Message); } - } - /// - /// Передача объекта в другую форму - /// - /// - /// - private void ButtonGoToCheck_Click(object sender, EventArgs e) - { - if (_company == null) - { - return; - } + } + /// + /// Передача объекта в другую форму + /// + /// + /// + private void ButtonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } - DrawingShip? car = null; - int counter = 100; - while (car == null) - { - car = _company.GetRandomObject(); - counter--; - if (counter <= 0) - { - break; - } - } + DrawingShip? car = null; + int counter = 100; + while (car == null) + { + car = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } - if (car == null) - { - return; - } + if (car == null) + { + return; + } - FormBattleship form = new() - { - SetShip = car - }; - form.ShowDialog(); - } - /// - /// Перерисовка коллекции - /// - /// - /// - private void ButtonRefresh_Click(object sender, EventArgs e) - { - if (_company == null) - { - return; - } + FormBattleship form = new() + { + SetShip = car + }; + form.ShowDialog(); + } + /// + /// Перерисовка коллекции + /// + /// + /// + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } - pictureBox.Image = _company.Show(); - } + pictureBox.Image = _company.Show(); + } - private void buttonCreateCompany_Click(object sender, EventArgs e) - { - if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) - { - MessageBox.Show("Коллекция не выбрана"); - return; - } + private void buttonCreateCompany_Click(object sender, EventArgs e) + { + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } - ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; - if (collection == null) - { - MessageBox.Show("Коллекция не проинициализирована"); - return; - } + ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; + if (collection == null) + { + MessageBox.Show("Коллекция не проинициализирована"); + return; + } - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new ShipDocks(pictureBox.Width, pictureBox.Height, collection); - break; - } - panelCompanyTools.Enabled = true; - RerfreshListBoxItems(); - } + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new ShipDocks(pictureBox.Width, pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } - private void buttonCollectionDel_Click(object sender, EventArgs e) - { + private void buttonCollectionDel_Click(object sender, EventArgs e) + { if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) { MessageBox.Show("Коллекция не выбрана"); @@ -195,20 +200,20 @@ namespace ProjectBattleship _logger.LogInformation("Удаление коллекции с названием {name}", listBoxCollection.SelectedItem.ToString()); RerfreshListBoxItems(); } - private void RerfreshListBoxItems() - { + private void RerfreshListBoxItems() + { 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); } } } - private void buttonCollectionAdd_Click(object sender, EventArgs e) - { + private void buttonCollectionAdd_Click(object sender, EventArgs e) + { if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -230,8 +235,8 @@ namespace ProjectBattleship RerfreshListBoxItems(); } - private void saveToolStripMenuItem_Click(object sender, EventArgs e) - { + private void saveToolStripMenuItem_Click(object sender, EventArgs e) + { if (saveFileDialog.ShowDialog() == DialogResult.OK) { @@ -249,8 +254,8 @@ namespace ProjectBattleship } } - private void loadToolStripMenuItem_Click(object sender, EventArgs e) - { + private void loadToolStripMenuItem_Click(object sender, EventArgs e) + { if (openFileDialog.ShowDialog() == DialogResult.OK) { try @@ -266,6 +271,24 @@ namespace ProjectBattleship _logger.LogError("Ошибка: {Message}", ex.Message); } } - } - } + } + private void buttonSortByType_Click(object sender, EventArgs e) + { + CompareShips(new DrawningCompareByType()); + } + private void buttonSortByColor_Click(object sender, EventArgs e) + { + CompareShips(new DrawningCompareByColor()); + } + private void CompareShips(IComparer comparer) + { + if (_company == null) + { + return; + } + + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } + } } diff --git a/ProjectBattleship/ProjectBattleship/FormShipCollection.resx b/ProjectBattleship/ProjectBattleship/FormShipCollection.resx index 4e47bf5..b460b22 100644 --- a/ProjectBattleship/ProjectBattleship/FormShipCollection.resx +++ b/ProjectBattleship/ProjectBattleship/FormShipCollection.resx @@ -126,4 +126,7 @@ 307, 17 + + 25 + \ No newline at end of file