From aa766d5f54eab146ad52f55506bef67a466016c3 Mon Sep 17 00:00:00 2001 From: KirillFilippow Date: Wed, 6 Dec 2023 18:47:21 +0400 Subject: [PATCH] Laba04 --- .../ContainerShip/AbstractStrategy.cs | 3 +- .../ContainerGenericCollection.cs | 47 +++-- .../ContainerShip/ContainerGenericStorage.cs | 87 ++++++++ ContainerShip/ContainerShip/DrawningShip.cs | 4 +- .../ContainerShip/EntityContainerShip.cs | 4 +- .../FormContainerCollection.Designer.cs | 186 +++++++++++++++++ .../ContainerShip/FormContainerCollection.cs | 190 ++++++++++++++++++ ...tion.resx => FormContainerCollection.resx} | 0 .../FormShipCollection.Designer.cs | 121 ----------- .../ContainerShip/FormShipCollection.cs | 82 -------- ContainerShip/ContainerShip/MoveToCenter.cs | 1 - ContainerShip/ContainerShip/Program.cs | 2 +- ContainerShip/ContainerShip/SetGeneric.cs | 100 ++++----- 13 files changed, 550 insertions(+), 277 deletions(-) create mode 100644 ContainerShip/ContainerShip/ContainerGenericStorage.cs create mode 100644 ContainerShip/ContainerShip/FormContainerCollection.Designer.cs create mode 100644 ContainerShip/ContainerShip/FormContainerCollection.cs rename ContainerShip/ContainerShip/{FormShipCollection.resx => FormContainerCollection.resx} (100%) delete mode 100644 ContainerShip/ContainerShip/FormShipCollection.Designer.cs delete mode 100644 ContainerShip/ContainerShip/FormShipCollection.cs diff --git a/ContainerShip/ContainerShip/AbstractStrategy.cs b/ContainerShip/ContainerShip/AbstractStrategy.cs index dd957ac..927a3b5 100644 --- a/ContainerShip/ContainerShip/AbstractStrategy.cs +++ b/ContainerShip/ContainerShip/AbstractStrategy.cs @@ -90,7 +90,8 @@ namespace ProjectContainerShip.MovementStrategy /// /// Параметры объекта /// - protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + protected ObjectParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; /// /// Шаг объекта /// diff --git a/ContainerShip/ContainerShip/ContainerGenericCollection.cs b/ContainerShip/ContainerShip/ContainerGenericCollection.cs index 968f6df..191f18c 100644 --- a/ContainerShip/ContainerShip/ContainerGenericCollection.cs +++ b/ContainerShip/ContainerShip/ContainerGenericCollection.cs @@ -57,13 +57,14 @@ namespace ProjectContainerShip.Generics /// /// /// - public static int? operator +(ContainerGenericCollection collect, T? obj) + public static bool operator +(ContainerGenericCollection collect, T? + obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj); + return collect?._collection.Insert(obj) ?? false; } /// /// Перегрузка оператора вычитания @@ -71,16 +72,14 @@ namespace ProjectContainerShip.Generics /// /// /// - public static bool operator -(ContainerGenericCollection collect, int - pos) + public static T? operator -(ContainerGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); - if (obj is not null) + T? obj = collect._collection[pos]; + if (obj != null) { collect._collection.Remove(pos); - return true; } - return false; + return obj; } /// /// Получение объекта IMoveableObject @@ -89,7 +88,7 @@ namespace ProjectContainerShip.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetObjectPosition; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -112,7 +111,8 @@ namespace ProjectContainerShip.Generics Pen pen = new(Color.Black, 2); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) { g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth , j * @@ -126,21 +126,26 @@ namespace ProjectContainerShip.Generics /// Метод прорисовки объектов /// /// + /// private void DrawObjects(Graphics g) { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _collection.Count; i++) - { - DrawningShip? ship = _collection.Get(i); - if (ship == null) - continue; - int row = height - 1 - (i / width); - int col = width - 1 - (i % width); - ship.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight + 25 ); - ship.DrawTransport(g); - } - } + foreach (var contShip in _collection.GetShip()) + { + if (contShip != null) + { + T? ship = _collection[i]; + if (ship == null) + continue; + int row = height - 1 - (i / width); + int col = width - 1 - (i % width); + ship.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight + 25); + ship.DrawTransport(g); + } + } + } } } diff --git a/ContainerShip/ContainerShip/ContainerGenericStorage.cs b/ContainerShip/ContainerShip/ContainerGenericStorage.cs new file mode 100644 index 0000000..3a77585 --- /dev/null +++ b/ContainerShip/ContainerShip/ContainerGenericStorage.cs @@ -0,0 +1,87 @@ +using ProjectContainerShip.DrawningObjects; +using ProjectContainerShip.Generics; +using ProjectContainerShip.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectContainerShip +{ + /// + /// Класс для хранения коллекции + /// + internal class ContainerGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _shipStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _shipStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public ContainerGenericStorage(int pictureWidth, int pictureHeight) + { + _shipStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (!_shipStorages.ContainsKey(name)) + { + ContainerGenericCollection newSet = new ContainerGenericCollection(_pictureWidth, _pictureHeight); + _shipStorages.Add(name, newSet); + } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (_shipStorages.ContainsKey(name)) + { + _shipStorages.Remove(name); + } + } + /// + /// Доступ к набору + /// + /// + /// + public ContainerGenericCollection? this[string ind] + { + get + { + if (_shipStorages.ContainsKey(ind)) + { + return _shipStorages[ind]; + } + return null; + } + } + } +} + diff --git a/ContainerShip/ContainerShip/DrawningShip.cs b/ContainerShip/ContainerShip/DrawningShip.cs index 60bfd79..3784b27 100644 --- a/ContainerShip/ContainerShip/DrawningShip.cs +++ b/ContainerShip/ContainerShip/DrawningShip.cs @@ -176,11 +176,11 @@ namespace ProjectContainerShip.DrawningObjects /// /// public virtual void DrawTransport(Graphics g) - { + { if (EntityShip == null) { return; - } + } Brush bodyBrush = new SolidBrush(EntityShip.BodyColor); //границы корабля Brush brRd = new SolidBrush(Color.Red); diff --git a/ContainerShip/ContainerShip/EntityContainerShip.cs b/ContainerShip/ContainerShip/EntityContainerShip.cs index e1bfc29..c29793c 100644 --- a/ContainerShip/ContainerShip/EntityContainerShip.cs +++ b/ContainerShip/ContainerShip/EntityContainerShip.cs @@ -28,8 +28,8 @@ namespace ProjectContainerShip.Entities /// Вес /// Основной цвет /// Дополнительный цвет - /// Признак наличия труб - /// Признак наличия топлива + /// Признак наличия труб + /// Признак наличия топлива public EntityContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, bool crane, bool container) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; diff --git a/ContainerShip/ContainerShip/FormContainerCollection.Designer.cs b/ContainerShip/ContainerShip/FormContainerCollection.Designer.cs new file mode 100644 index 0000000..9e579cb --- /dev/null +++ b/ContainerShip/ContainerShip/FormContainerCollection.Designer.cs @@ -0,0 +1,186 @@ +namespace ProjectContainerShip +{ + partial class FormContainerCollection + { + /// + /// 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); + } + + #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() + { + pictureBoxCollection = new PictureBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonAddEx = new Button(); + buttonRemoveEx = new Button(); + buttonRefreshCollection = new Button(); + groupBox1 = new GroupBox(); + groupBox2 = new GroupBox(); + textBoxStorageName = new TextBox(); + buttonDelObject = new Button(); + listBoxStorages = new ListBox(); + buttonAddObject = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBox1.SuspendLayout(); + groupBox2.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(932, 503); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(21, 326); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(95, 27); + maskedTextBoxNumber.TabIndex = 1; + // + // buttonAddEx + // + buttonAddEx.Location = new Point(21, 282); + buttonAddEx.Name = "buttonAddEx"; + buttonAddEx.Size = new Size(95, 27); + buttonAddEx.TabIndex = 2; + buttonAddEx.Text = "Добавить"; + buttonAddEx.UseVisualStyleBackColor = true; + buttonAddEx.Click += ButtonAddShip_Click; + // + // buttonRemoveEx + // + buttonRemoveEx.Location = new Point(19, 359); + buttonRemoveEx.Name = "buttonRemoveEx"; + buttonRemoveEx.Size = new Size(95, 27); + buttonRemoveEx.TabIndex = 3; + buttonRemoveEx.Text = "Удалить"; + buttonRemoveEx.UseVisualStyleBackColor = true; + buttonRemoveEx.Click += ButtonRemoveShip_Click; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(21, 405); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(95, 27); + buttonRefreshCollection.TabIndex = 4; + buttonRefreshCollection.Text = "Обновить"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // groupBox1 + // + groupBox1.Controls.Add(groupBox2); + groupBox1.Controls.Add(buttonAddEx); + groupBox1.Controls.Add(buttonRefreshCollection); + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(buttonRemoveEx); + groupBox1.Location = new Point(812, 0); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(120, 461); + groupBox1.TabIndex = 5; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // groupBox2 + // + groupBox2.Controls.Add(textBoxStorageName); + groupBox2.Controls.Add(buttonDelObject); + groupBox2.Controls.Add(listBoxStorages); + groupBox2.Controls.Add(buttonAddObject); + groupBox2.Location = new Point(13, 28); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new Size(116, 214); + groupBox2.TabIndex = 5; + groupBox2.TabStop = false; + groupBox2.Text = "Наборы"; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(8, 26); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(95, 27); + textBoxStorageName.TabIndex = 6; + textBoxStorageName.TextChanged += textBoxStorageName_TextChanged; + // + // buttonDelObject + // + buttonDelObject.Location = new Point(8, 162); + buttonDelObject.Name = "buttonDelObject"; + buttonDelObject.Size = new Size(95, 27); + buttonDelObject.TabIndex = 8; + buttonDelObject.Text = "Удалить набор"; + buttonDelObject.UseVisualStyleBackColor = true; + buttonDelObject.Click += ButtonDelObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(8, 92); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(95, 64); + listBoxStorages.TabIndex = 6; + listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(8, 59); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(95, 27); + buttonAddObject.TabIndex = 7; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += ButtonAddObject_Click; + // + // FormContainerCollection + // + ClientSize = new Size(932, 503); + Controls.Add(groupBox1); + Controls.Add(pictureBoxCollection); + Name = "FormContainerCollection"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + groupBox2.ResumeLayout(false); + groupBox2.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + private Button buttonAddEx; + private Button buttonRemoveEx; + private Button buttonRefreshCollection; + private GroupBox groupBox1; + private GroupBox groupBox2; + private TextBox textBoxStorageName; + private Button buttonDelObject; + private ListBox listBoxStorages; + private Button buttonAddObject; + } +} \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormContainerCollection.cs b/ContainerShip/ContainerShip/FormContainerCollection.cs new file mode 100644 index 0000000..2c61240 --- /dev/null +++ b/ContainerShip/ContainerShip/FormContainerCollection.cs @@ -0,0 +1,190 @@ +using ProjectContainerShip; +using ProjectContainerShip.DrawningObjects; +using ProjectContainerShip.Generics; +using ProjectContainerShip.MovementStrategy; + +namespace ProjectContainerShip +{ + /// + /// Форма для работы с набором объектов класса DrawningShip + /// + public partial class FormContainerCollection : Form + { + /// + /// Набор объектов + /// + private readonly ContainerGenericStorage _storage; + /// + /// Конструктор + /// + public FormContainerCollection() + { + InitializeComponent(); + _storage = new ContainerGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorages.Items.Add(_storage.Keys[i]); + } + if (listBoxStorages.Items.Count > 0 && (index == -1 || index + >= listBoxStorages.Items.Count)) + { + listBoxStorages.SelectedIndex = 0; + } + else if (listBoxStorages.Items.Count > 0 && index > -1 && + index < listBoxStorages.Items.Count) + { + listBoxStorages.SelectedIndex = index; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowContainer(); + } + } + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + /// + /// Выбор набора + /// + /// + /// + private void ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowContainer(); + } + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorages.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddShip_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + FormContainerShip form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (obj + form.SelectedShip) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowContainer(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveShip_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (obj - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowContainer(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowContainer(); + } + private void textBoxStorageName_TextChanged(object sender, EventArgs e) + { + } + } +} + + + diff --git a/ContainerShip/ContainerShip/FormShipCollection.resx b/ContainerShip/ContainerShip/FormContainerCollection.resx similarity index 100% rename from ContainerShip/ContainerShip/FormShipCollection.resx rename to ContainerShip/ContainerShip/FormContainerCollection.resx diff --git a/ContainerShip/ContainerShip/FormShipCollection.Designer.cs b/ContainerShip/ContainerShip/FormShipCollection.Designer.cs deleted file mode 100644 index 1e8dcd5..0000000 --- a/ContainerShip/ContainerShip/FormShipCollection.Designer.cs +++ /dev/null @@ -1,121 +0,0 @@ -namespace ProjectContainerShip -{ - 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); - } - - #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() - { - pictureBoxCollection = new PictureBox(); - maskedTextBoxNumber = new MaskedTextBox(); - buttonAddShip = new Button(); - buttonRemoveShip = new Button(); - buttonRefreshCollection = new Button(); - groupBox1 = new GroupBox(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); - groupBox1.SuspendLayout(); - SuspendLayout(); - // - // pictureBoxCollection - // - pictureBoxCollection.Dock = DockStyle.Fill; - pictureBoxCollection.Location = new Point(0, 0); - pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(932, 503); - pictureBoxCollection.TabIndex = 0; - pictureBoxCollection.TabStop = false; - // - // maskedTextBoxNumber - // - maskedTextBoxNumber.Location = new Point(6, 120); - maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(108, 27); - maskedTextBoxNumber.TabIndex = 1; - // - // buttonAddShip - // - buttonAddShip.Location = new Point(6, 57); - buttonAddShip.Name = "buttonAddShip"; - buttonAddShip.Size = new Size(108, 30); - buttonAddShip.TabIndex = 2; - buttonAddShip.Text = "Добавить "; - buttonAddShip.UseVisualStyleBackColor = true; - buttonAddShip.Click += ButtonAddShip_Click; - // - // buttonRemoveShip - // - buttonRemoveShip.Location = new Point(6, 166); - buttonRemoveShip.Name = "buttonRemoveShip"; - buttonRemoveShip.Size = new Size(108, 30); - buttonRemoveShip.TabIndex = 3; - buttonRemoveShip.Text = "Удалить "; - buttonRemoveShip.UseVisualStyleBackColor = true; - buttonRemoveShip.Click += ButtonRemoveShip_Click; - // - // buttonRefreshCollection - // - buttonRefreshCollection.Location = new Point(6, 230); - buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(108, 30); - buttonRefreshCollection.TabIndex = 4; - buttonRefreshCollection.Text = "Обновить "; - buttonRefreshCollection.UseVisualStyleBackColor = true; - buttonRefreshCollection.Click += ButtonRefreshCollection_Click; - // - // groupBox1 - // - groupBox1.Controls.Add(buttonAddShip); - groupBox1.Controls.Add(buttonRefreshCollection); - groupBox1.Controls.Add(maskedTextBoxNumber); - groupBox1.Controls.Add(buttonRemoveShip); - groupBox1.Location = new Point(812, 21); - groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(120, 461); - groupBox1.TabIndex = 5; - groupBox1.TabStop = false; - groupBox1.Text = "Инструменты"; - // - // FormShipCollection - // - ClientSize = new Size(932, 503); - Controls.Add(groupBox1); - Controls.Add(pictureBoxCollection); - Name = "FormShipCollection"; - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); - groupBox1.ResumeLayout(false); - groupBox1.PerformLayout(); - ResumeLayout(false); - } - - #endregion - - private PictureBox pictureBoxCollection; - private MaskedTextBox maskedTextBoxNumber; - private Button buttonAddShip; - private Button buttonRemoveShip; - private Button buttonRefreshCollection; - private GroupBox groupBox1; - } -} \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormShipCollection.cs b/ContainerShip/ContainerShip/FormShipCollection.cs deleted file mode 100644 index 744252c..0000000 --- a/ContainerShip/ContainerShip/FormShipCollection.cs +++ /dev/null @@ -1,82 +0,0 @@ -using ProjectContainerShip.DrawningObjects; -using ProjectContainerShip.Generics; -using ProjectContainerShip.MovementStrategy; - -namespace ProjectContainerShip -{ - /// - /// Форма для работы с набором объектов класса - /// - public partial class FormShipCollection : Form - { - /// - /// Набор объектов - /// - private readonly ContainerGenericCollection _containerShip; - /// - /// Конструктор - /// - public FormShipCollection() - { - InitializeComponent(); - _containerShip = new ContainerGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); - } - /// - /// Добавление объекта в набор - /// - /// - /// - private void ButtonAddShip_Click(object sender, EventArgs e) - { - FormContainerShip form = new(); - if (form.ShowDialog() == DialogResult.OK) - { - if (_containerShip + form.SelectedShip > -1) - { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _containerShip.ShowContainer(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } - } - } - /// - /// Удаление объекта из набора - /// - /// - /// - private void ButtonRemoveShip_Click(object sender, EventArgs e) - { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - { - return; - } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_containerShip - pos != null) - { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _containerShip.ShowContainer(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); - } - } - /// - /// Обновление рисунка по набору - /// - /// - /// - private void ButtonRefreshCollection_Click(object sender, EventArgs e) - { - pictureBoxCollection.Image = _containerShip.ShowContainer(); - } - } -} - - diff --git a/ContainerShip/ContainerShip/MoveToCenter.cs b/ContainerShip/ContainerShip/MoveToCenter.cs index 2d0c83f..e61f5e5 100644 --- a/ContainerShip/ContainerShip/MoveToCenter.cs +++ b/ContainerShip/ContainerShip/MoveToCenter.cs @@ -59,4 +59,3 @@ namespace ProjectContainerShip.MovementStrategy } - diff --git a/ContainerShip/ContainerShip/Program.cs b/ContainerShip/ContainerShip/Program.cs index a57705c..7bf5e39 100644 --- a/ContainerShip/ContainerShip/Program.cs +++ b/ContainerShip/ContainerShip/Program.cs @@ -13,7 +13,7 @@ namespace ProjectContainerShip // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormShipCollection()); + Application.Run(new FormContainerCollection()); } } diff --git a/ContainerShip/ContainerShip/SetGeneric.cs b/ContainerShip/ContainerShip/SetGeneric.cs index 9b636f0..afdd0f3 100644 --- a/ContainerShip/ContainerShip/SetGeneric.cs +++ b/ContainerShip/ContainerShip/SetGeneric.cs @@ -14,64 +14,39 @@ namespace ProjectContainerShip.Generics where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор /// - /// Добавляемый корабль + /// Добавляемый контейнеровоз /// - public int Insert(T ship) + public bool Insert(T ship) { - return Insert(ship, 0); - } - /// - /// Добавление объекта в набор на конкретную позицию - /// - /// Добавляемый корабля - /// Позиция - /// - public int Insert(T ship, int position) - { - if (position < 0 && position > Count) + if (_places.Count >= _maxCount) { - return -1; + return false; } - if (_places[position] != null) - { - int d = 0; - for (int j = 1; j < Count - position; j++) - { - if (_places[position + j] == null) - { - d = position + j; - break; - } - } - if (d == 0) - { - return -1; - } - for (int j = d; j > position; j--) - { - _places[j] = _places[j - 1]; - } - } - _places[position] = ship; - return position; + _places.Insert(0, ship); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -80,11 +55,11 @@ namespace ProjectContainerShip.Generics /// public bool Remove(int position) { - if (position < 0 || position >= _places.Length) + if (position < 0 || position >= _places.Count) { return false; } - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -92,14 +67,47 @@ namespace ProjectContainerShip.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= _places.Length) - return null; - return _places[position]; + get + { + if (position < 0 || position >= _places.Count) + { + return null; + } + return _places[position]; + } + set + { + if (position < 0 || position >= _places.Count) + { + return; + } + if (_places.Count >= _maxCount) + { + return; + } + _places.Insert(position, value); + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetShip(int? maxShip = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxShip.HasValue && i == maxShip.Value) + { + yield break; + } + } } } } +