From 6b094689912dc6529375116ebaed912f845f2ea0 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Sun, 6 Nov 2022 23:39:21 +0400 Subject: [PATCH] Lab 4 --- Catamaran/Catamaran.csproj | 1 + Catamaran/FormMapWithSetBoats.Designer.cs | 94 ++++++++++++--- Catamaran/FormMapWithSetBoats.cs | 140 ++++++++++++++++------ Catamaran/MapWithSetBoatsGeneric.cs | 19 +-- Catamaran/MapsCollection.cs | 74 ++++++++++++ Catamaran/SetBoatsGeneric.cs | 75 +++++++++--- 6 files changed, 322 insertions(+), 81 deletions(-) create mode 100644 Catamaran/MapsCollection.cs diff --git a/Catamaran/Catamaran.csproj b/Catamaran/Catamaran.csproj index 4f33ddc..b0275f4 100644 --- a/Catamaran/Catamaran.csproj +++ b/Catamaran/Catamaran.csproj @@ -66,6 +66,7 @@ FormMapWithSetBoats.cs + diff --git a/Catamaran/FormMapWithSetBoats.Designer.cs b/Catamaran/FormMapWithSetBoats.Designer.cs index 4cba060..2a45d76 100644 --- a/Catamaran/FormMapWithSetBoats.Designer.cs +++ b/Catamaran/FormMapWithSetBoats.Designer.cs @@ -40,18 +40,24 @@ this.buttonUp = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); this.groupBoxTools.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.groupBoxMaps.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonShowStorage); this.groupBoxTools.Controls.Add(this.buttonRemoveBoat); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddBoat); - this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonUp); @@ -59,14 +65,14 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(653, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(303, 557); + this.groupBoxTools.Size = new System.Drawing.Size(303, 647); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Tools"; // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(24, 402); + this.buttonShowOnMap.Location = new System.Drawing.Point(24, 490); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(191, 31); this.buttonShowOnMap.TabIndex = 5; @@ -76,7 +82,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(24, 344); + this.buttonShowStorage.Location = new System.Drawing.Point(24, 451); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(191, 33); this.buttonShowStorage.TabIndex = 4; @@ -86,7 +92,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(24, 271); + this.buttonRemoveBoat.Location = new System.Drawing.Point(24, 415); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(191, 30); this.buttonRemoveBoat.TabIndex = 3; @@ -96,7 +102,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 201); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 383); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(191, 26); @@ -104,7 +110,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(24, 123); + this.buttonAddBoat.Location = new System.Drawing.Point(24, 345); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(191, 32); this.buttonAddBoat.TabIndex = 1; @@ -118,18 +124,18 @@ this.comboBoxSelectorMap.Items.AddRange(new object[] { "SimpleMap", "SecondMap"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(24, 51); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(14, 77); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(191, 28); this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + //this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // buttonLeft // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Catamaran.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(10, 472); + this.buttonLeft.Location = new System.Drawing.Point(10, 562); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 4; @@ -141,7 +147,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Catamaran.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(70, 472); + this.buttonRight.Location = new System.Drawing.Point(70, 562); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 5; @@ -153,7 +159,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Catamaran.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(40, 450); + this.buttonUp.Location = new System.Drawing.Point(40, 540); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 2; @@ -165,7 +171,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Catamaran.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(40, 480); + this.buttonDown.Location = new System.Drawing.Point(40, 570); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 3; @@ -177,15 +183,66 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(653, 557); + this.pictureBox.Size = new System.Drawing.Size(653, 647); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.buttonAddMap); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(10, 25); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(213, 314); + this.groupBoxMaps.TabIndex = 2; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Maps"; + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(14, 45); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(191, 26); + this.textBoxNewMapName.TabIndex = 0; + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(14, 112); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(191, 27); + this.buttonAddMap.TabIndex = 1; + this.buttonAddMap.Text = "AddMap"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(14, 233); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(191, 25); + this.buttonDeleteMap.TabIndex = 2; + this.buttonDeleteMap.Text = "DeleteMap"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(14, 146); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(191, 84); + this.listBoxMaps.TabIndex = 3; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // // FormMapWithSetBoats // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(956, 557); + this.ClientSize = new System.Drawing.Size(956, 647); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetBoats"; @@ -193,6 +250,8 @@ this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); this.ResumeLayout(false); } @@ -211,5 +270,10 @@ private System.Windows.Forms.Button buttonDown; private System.Windows.Forms.Button buttonLeft; private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.GroupBox groupBoxMaps; + private System.Windows.Forms.ListBox listBoxMaps; + private System.Windows.Forms.Button buttonDeleteMap; + private System.Windows.Forms.Button buttonAddMap; + private System.Windows.Forms.TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/Catamaran/FormMapWithSetBoats.cs b/Catamaran/FormMapWithSetBoats.cs index 6accea3..0e1f398 100644 --- a/Catamaran/FormMapWithSetBoats.cs +++ b/Catamaran/FormMapWithSetBoats.cs @@ -12,13 +12,77 @@ namespace Catamaran { public partial class FormMapWithSetBoats : Form { - private MapWithSetBoatsGeneric _mapBoatsCollectionGeneric; + /// + /// Словарь для выпадающего списка + /// + private readonly Dictionary _mapsDict = new Dictionary() + { + { "SimpleMap", new SimpleMap() }, + {"SecondMap", new SecondMap() } + }; + /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; + /// + /// Конструктор + /// public FormMapWithSetBoats() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } + } + /// + /// Заполнение listBoxMaps + /// + private void ReloadMaps() + { + int index = listBoxMaps.SelectedIndex; + listBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) + { + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); + } + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) + { + listBoxMaps.SelectedIndex = 0; + } + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) + { + listBoxMaps.SelectedIndex = index; + } + } + + /// + /// Добавление карты + /// + /// + /// + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, + _mapsDict[comboBoxSelectorMap.Text]); + ReloadMaps(); } /// @@ -26,33 +90,29 @@ namespace Catamaran /// /// /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) - { - case "SimpleMap": - map = new SimpleMap(); - break; - case "SecondMap": - map = new SecondMap(); - break; - } - if (map != null) - { - _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric(pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapBoatsCollectionGeneric = null; - } + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// - /// Добавление объекта + /// Удаление карты /// /// /// + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", + "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); + } + } /// /// Удаление объекта @@ -61,6 +121,10 @@ namespace Catamaran /// private void buttonRemoveBoat_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -71,10 +135,10 @@ namespace Catamaran return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapBoatsCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -88,11 +152,12 @@ namespace Catamaran /// private void buttonShowStorage_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } /// /// Вывод карты @@ -101,13 +166,13 @@ namespace Catamaran /// private void buttonShowOnMap_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBoatsCollectionGeneric.ShowOnMap(); - } + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); + } /// /// Перемещение /// @@ -115,7 +180,7 @@ namespace Catamaran /// private void buttonMove_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -137,12 +202,17 @@ namespace Catamaran dir = Direction.Right; break; } - pictureBox.Image = _mapBoatsCollectionGeneric.MoveObject(dir); - } + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + /// + /// Добавление объекта + /// + /// + /// private void buttonAddBoat_Click_1(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -150,15 +220,17 @@ namespace Catamaran if (form.ShowDialog() == DialogResult.OK) { DrawingObjectBoat boat = new DrawingObjectBoat(form.SelectedBoat); - if (_mapBoatsCollectionGeneric + boat != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat != null) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось добавить объект"); } + } } } diff --git a/Catamaran/MapWithSetBoatsGeneric.cs b/Catamaran/MapWithSetBoatsGeneric.cs index 6c97f4c..267d621 100644 --- a/Catamaran/MapWithSetBoatsGeneric.cs +++ b/Catamaran/MapWithSetBoatsGeneric.cs @@ -61,8 +61,6 @@ namespace Catamaran /// /// /// - /// - public static int operator +(MapWithSetBoatsGeneric map, T Boat) { return map._setBoats.Insert(Boat); @@ -96,13 +94,9 @@ namespace Catamaran public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setBoats.Count; i++) + foreach (var boat in _setBoats.GetBoats()) { - var Boat = _setBoats.Get(i); - if (Boat != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, Boat); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, boat); } return new Bitmap(_pictureWidth, _pictureHeight); } @@ -127,11 +121,11 @@ namespace Catamaran int j = _setBoats.Count - 1; for (int i = 0; i < _setBoats.Count; i++) { - if (_setBoats.Get(i) == null) + if (_setBoats[i] == null) { for (; j > i; j--) { - var Boat = _setBoats.Get(j); + var Boat = _setBoats[j]; if (Boat != null) { _setBoats.Insert(Boat, i); @@ -184,10 +178,9 @@ namespace Catamaran for (int i = 0; i < _setBoats.Count; i++) { // TODO установка позиции - _setBoats.Get(i)?.SetObject(i % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - i / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setBoats.Get(i)?.DrawingObject(g); + _setBoats[i]?.SetObject(i % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - i / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setBoats[i]?.DrawingObject(g); } } } } - diff --git a/Catamaran/MapsCollection.cs b/Catamaran/MapsCollection.cs new file mode 100644 index 0000000..3b9c006 --- /dev/null +++ b/Catamaran/MapsCollection.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class MapsCollection + { + /// + /// Словарь (хранилище) с картами + /// + readonly Dictionary> _mapStorages; + + /// + /// Возвращение списка названий карт + /// + public List Keys => _mapStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление карты + /// + /// Название карты + /// Карта + public void AddMap(string name, AbstractMap map) + { + if (!_mapStorages.ContainsKey(name)) + { + _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + } + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetBoatsGeneric this[string ind] + { + get + { + _mapStorages.TryGetValue(ind, out var map); + return map; + } + } + + } +} diff --git a/Catamaran/SetBoatsGeneric.cs b/Catamaran/SetBoatsGeneric.cs index 4c74923..811fe0c 100644 --- a/Catamaran/SetBoatsGeneric.cs +++ b/Catamaran/SetBoatsGeneric.cs @@ -16,18 +16,21 @@ namespace Catamaran /// /// Массив объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + + private readonly int _maxCount; /// /// Конструктор /// /// public SetBoatsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -36,11 +39,12 @@ namespace Catamaran /// public int Insert(T boat) { - for (int i = 0; i < Count; i++) + + for (int i = 0; i < _maxCount; i++) { - if (_places[i] == null) + if (i == Count) { - _places[i] = boat; + _places.Insert(i, boat); return i; } } @@ -55,26 +59,26 @@ namespace Catamaran public int Insert(T boat, int position) { // TODO проверка позиции - if (position < 0 || position >= Count) return -1; + if (position < 0 || position >= _maxCount) return -1; // TODO проверка, что элемент массива по этой позиции пустой,если нет, то // проверка, что после вставляемого элемента в массиве есть пустой элемент // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - if (_places[position] == null) + if (position == Count) { - _places[position] = boat; + _places.Insert(position, boat); return position; } else { - for (int i = position + 1; i < Count; i++) + for (int i = position + 1; i < _maxCount; i++) { - if (_places[i] == null) + if (i == Count) { for (int j = i - 1; j >= position; j--) { _places[j + 1] = _places[j]; } - _places[position] = boat; + _places.Insert(position, boat); return position; } } @@ -92,22 +96,55 @@ namespace Catamaran // TODO проверка позиции if (position < 0 || position >= Count) return null; // TODO удаление объекта из массива, присовив элементу массива значение null - T removed = _places[position]; - _places[position] = null; - return removed; + var result = _places[position]; + _places.RemoveAt(position); + return result; } /// /// Получение объекта из набора по позиции /// /// /// - public T Get(int position) + public T this[int position] { // TODO проверка позиции - if (position < 0 || position >= Count) return null; - return _places[position]; + get + { + if (position >= 0 && position < _maxCount && position < Count) + { + return _places[position]; + } + else + { + return null; + } + } + set + { + if (position > 0 || position < Count) + { + Insert(value, position); + } + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetBoats() + { + foreach (var boat in _places) + { + if (boat != null) + { + yield return boat; + } + else + { + yield break; + } + } } } } - -- 2.25.1