From f17a1c473577880ec58885e59639970449554efb Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Sat, 19 Nov 2022 22:38:17 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotive.Designer.cs | 75 ++++++++++--- .../FormMapWithSetLocomotive.cs | 106 ++++++++++++++---- .../ElectricLocomotive/MapsCollection.cs | 44 ++++++++ 3 files changed, 193 insertions(+), 32 deletions(-) create mode 100644 ElectricLocomotive/ElectricLocomotive/MapsCollection.cs diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs index 9f7b19b..ef3926a 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs @@ -40,6 +40,10 @@ this.buttonRemoveCar = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.ButtonDeleteMap = new System.Windows.Forms.Button(); + this.ListBoxMaps = new System.Windows.Forms.ListBox(); + this.ButtonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // @@ -50,7 +54,7 @@ | System.Windows.Forms.AnchorStyles.Right))); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(631, 449); + this.pictureBox1.Size = new System.Drawing.Size(808, 721); this.pictureBox1.TabIndex = 10; this.pictureBox1.TabStop = false; // @@ -60,7 +64,7 @@ this.buttonUp.AutoSize = true; this.buttonUp.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowup; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(699, 372); + this.buttonUp.Location = new System.Drawing.Point(892, 624); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 14; @@ -73,7 +77,7 @@ this.buttonRight.AutoSize = true; this.buttonRight.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowright; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(735, 408); + this.buttonRight.Location = new System.Drawing.Point(928, 660); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 13; @@ -86,7 +90,7 @@ this.buttonDown.AutoSize = true; this.buttonDown.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowdown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(699, 408); + this.buttonDown.Location = new System.Drawing.Point(892, 660); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 12; @@ -99,7 +103,7 @@ this.buttonLeft.AutoSize = true; this.buttonLeft.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowleft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(663, 408); + this.buttonLeft.Location = new System.Drawing.Point(856, 660); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 11; @@ -109,7 +113,7 @@ // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(637, 9); + this.label1.Location = new System.Drawing.Point(837, 19); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(83, 15); this.label1.TabIndex = 15; @@ -117,7 +121,7 @@ // // buttonAddLocomotive // - this.buttonAddLocomotive.Location = new System.Drawing.Point(637, 98); + this.buttonAddLocomotive.Location = new System.Drawing.Point(837, 386); this.buttonAddLocomotive.Name = "buttonAddLocomotive"; this.buttonAddLocomotive.Size = new System.Drawing.Size(151, 28); this.buttonAddLocomotive.TabIndex = 17; @@ -133,7 +137,7 @@ "Простая карта", "Карта с грязью", "Карта с кустами"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(637, 43); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(837, 90); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 23); this.comboBoxSelectorMap.TabIndex = 22; @@ -141,7 +145,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(637, 151); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(837, 420); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(151, 23); @@ -150,7 +154,7 @@ // // buttonRemoveCar // - this.buttonRemoveCar.Location = new System.Drawing.Point(637, 178); + this.buttonRemoveCar.Location = new System.Drawing.Point(837, 449); this.buttonRemoveCar.Name = "buttonRemoveCar"; this.buttonRemoveCar.Size = new System.Drawing.Size(151, 28); this.buttonRemoveCar.TabIndex = 24; @@ -160,7 +164,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(637, 239); + this.buttonShowStorage.Location = new System.Drawing.Point(837, 483); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(151, 28); this.buttonShowStorage.TabIndex = 25; @@ -170,7 +174,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(637, 328); + this.buttonShowOnMap.Location = new System.Drawing.Point(837, 546); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(151, 28); this.buttonShowOnMap.TabIndex = 26; @@ -178,11 +182,52 @@ this.buttonShowOnMap.UseVisualStyleBackColor = true; this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(837, 239); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(151, 33); + this.ButtonDeleteMap.TabIndex = 27; + this.ButtonDeleteMap.Text = "Удалить карту"; + this.ButtonDeleteMap.UseVisualStyleBackColor = true; + this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // ListBoxMaps + // + this.ListBoxMaps.FormattingEnabled = true; + this.ListBoxMaps.ItemHeight = 15; + this.ListBoxMaps.Location = new System.Drawing.Point(837, 154); + this.ListBoxMaps.Name = "ListBoxMaps"; + this.ListBoxMaps.Size = new System.Drawing.Size(151, 79); + this.ListBoxMaps.TabIndex = 28; + this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(837, 119); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(151, 29); + this.ButtonAddMap.TabIndex = 29; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(837, 61); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(151, 23); + this.textBoxNewMapName.TabIndex = 30; + // // FormMapWithSetLocomotive // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(1000, 722); + this.Controls.Add(this.textBoxNewMapName); + this.Controls.Add(this.ButtonAddMap); + this.Controls.Add(this.ListBoxMaps); + this.Controls.Add(this.ButtonDeleteMap); this.Controls.Add(this.buttonRemoveCar); this.Controls.Add(this.buttonShowStorage); this.Controls.Add(this.buttonShowOnMap); @@ -216,5 +261,9 @@ private Button buttonRemoveCar; private Button buttonShowStorage; private Button buttonShowOnMap; + private Button ButtonDeleteMap; + private ListBox ListBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs index 1bddbe1..903fcc8 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs @@ -12,10 +12,42 @@ namespace ElectricLocomotive { public partial class FormMapWithSetLocomotive : Form { - private MapWithSetLocomotivGeneric _mapLocomotivesCollectionGeneric; + private MapWithSetLocomotivGeneric _mapLocomotiveCollectionGeneric; + private readonly Dictionary _mapDict = new() + { + {"Простая карта", new SimpleMap() }, + {"Карта с грязью", new FieldMap() }, + {"Карта с кустами",new BushesMap() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetLocomotive() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox1.Width, pictureBox1.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } + } + private void ReloadMaps() + { + int index = ListBoxMaps.SelectedIndex; + + ListBoxMaps.Items.Clear(); + foreach (var list in _mapsCollection.Keys) + { + ListBoxMaps.Items.Add(list); + } + + 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 ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) @@ -35,16 +67,17 @@ namespace ElectricLocomotive } if (map != null) { - _mapLocomotivesCollectionGeneric = new MapWithSetLocomotivGeneric(pictureBox1.Width, pictureBox1.Height, map); + _mapLocomotiveCollectionGeneric = new MapWithSetLocomotivGeneric( + pictureBox1.Width, pictureBox1.Height, map); } else { - _mapLocomotivesCollectionGeneric = null; + _mapLocomotiveCollectionGeneric = null; } } private void ButtonAddLocomotive_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -52,10 +85,10 @@ namespace ElectricLocomotive if (form.ShowDialog() == DialogResult.OK) { DrawningObjectLocomotive Locomotive = new(form.SelectedLocomotive); - if (_mapLocomotivesCollectionGeneric + Locomotive != -1) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + Locomotive != -1) { MessageBox.Show("Объект добавлен"); - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -65,20 +98,23 @@ namespace ElectricLocomotive } private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; } - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapLocomotivesCollectionGeneric - pos != null) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -87,23 +123,23 @@ namespace ElectricLocomotive } private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) - { - return; - } - pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowOnMap(); + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapLocomotivesCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -125,7 +161,39 @@ namespace ElectricLocomotive dir = Direction.Right; break; } - pictureBox1.Image = _mapLocomotivesCollectionGeneric.MoveObject(dir); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[comboBoxSelectorMap.Text]); + ReloadMaps(); + } + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox1.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(); + } } } } diff --git a/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs b/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs new file mode 100644 index 0000000..a0e9286 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/MapsCollection.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + 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 MapWithSetLocomotivGeneric(_pictureWidth, _pictureHeight, map)); + } + } + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); + } + public MapWithSetLocomotivGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + { + return _mapStorages[ind]; + } + return null; + } + } + } +} -- 2.25.1 From 73c5fa94765257505dd16549fd76d4974fc1c413 Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Sat, 19 Nov 2022 22:42:09 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=964?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetLocomotivGeneric.cs | 55 +++---- .../ElectricLocomotive/SetLocomotivGeneric.cs | 137 +++++++++--------- 2 files changed, 91 insertions(+), 101 deletions(-) diff --git a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs index 1647836..70abd9c 100644 --- a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ElectricLocomotive { internal class MapWithSetLocomotivGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject where U : AbstractMap { // Ширина окна отрисовки @@ -22,8 +22,6 @@ namespace ElectricLocomotive private readonly SetLocomotivGeneric _setLocomotive; // Карта private readonly U _map; - - private readonly T[] _places; // Конструктор public MapWithSetLocomotivGeneric(int picWidth, int picHeight, U map) { @@ -42,10 +40,8 @@ namespace ElectricLocomotive // Перегрузка оператора вычитания public static T operator -(MapWithSetLocomotivGeneric map, int position) { - return map._setLocomotive.Remove(position); } - // Вывод всего набора объектов public Bitmap ShowSet() { Bitmap bmp = new(_pictureWidth, _pictureHeight); @@ -58,16 +54,13 @@ namespace ElectricLocomotive public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setLocomotive.Count; i++) + foreach (var locomotive in _setLocomotive.GetLocomotive()) { - var Locomotive = _setLocomotive.Get(i); - if (Locomotive != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, Locomotive); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); } return new(_pictureWidth, _pictureHeight); } + /// // Перемещение объекта по карте public Bitmap MoveObject(Direction direction) { @@ -77,20 +70,21 @@ namespace ElectricLocomotive } return new(_pictureWidth, _pictureHeight); } + // "Взбалтываем" набор, чтобы все элементы оказались в начале private void Shaking() { int j = _setLocomotive.Count - 1; for (int i = 0; i < _setLocomotive.Count; i++) { - if (_setLocomotive.Get(i) == null) + if (_setLocomotive[i] == null) { for (; j > i; j--) { - var Locomotive = _setLocomotive.Get(j); - if (Locomotive != null) + var locomotive = _setLocomotive[j]; + if (locomotive != null) { - _setLocomotive.Insert(Locomotive, i); + _setLocomotive.Insert(locomotive, i); _setLocomotive.Remove(j); break; } @@ -121,28 +115,25 @@ namespace ElectricLocomotive // Метод прорисовки объектов private void DrawLocomotive(Graphics g) { - int widthEl = _pictureWidth / _placeSizeWidth; - int heightEl = _pictureHeight / _placeSizeHeight; + int yNumOfPlaces = _pictureHeight / _placeSizeHeight; + int xNumOfPlaces = _pictureWidth / _placeSizeWidth; - int curWidth = 0; - int curHeight = 0; + int RowIndex = yNumOfPlaces - 1; + int ColumnIndex = 0; - for (int i = _setLocomotive.Count; i >= 0; i--) + foreach (var HardLocomotive in _setLocomotive.GetLocomotive()) { - _setLocomotive.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 85, - curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setLocomotive.Get(i)?.DrawningObject(g); - - if (curWidth < widthEl) - curWidth++; + (float Left, float Top, float Right, float Bottom) = HardLocomotive.GetCurrentPosition(); + HardLocomotive.SetObject(ColumnIndex * _placeSizeWidth, RowIndex * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top) - 70), _pictureWidth, _pictureHeight); + HardLocomotive.DrawningObject(g); + if (ColumnIndex == xNumOfPlaces - 1) + { + ColumnIndex = 0; + RowIndex--; + } else { - curWidth = 1; - curHeight++; - } - if (curHeight > heightEl) - { - return; + ColumnIndex++; } } diff --git a/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs index c4077a9..05c32fa 100644 --- a/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs @@ -7,74 +7,73 @@ using System.Threading.Tasks; namespace ElectricLocomotive { internal class SetLocomotivGeneric -where T : class - { - // Массив объектов, которые храним - private readonly T[] _places; - // Количество объектов в массиве - public int Count => _places.Length; - // Конструктор - public SetLocomotivGeneric(int count) - { - _places = new T[count]; - } - // Добавление объекта в набор - public int Insert(T Locomotive) - { - // вставка в начало набора - return Insert(Locomotive, 0); - } - // Добавление объекта в набор на конкретную позицию - public int Insert(T Locomotive, int position) - { - // проверка позиции - if (position >= _places.Length || position < 0) - return -1; - //проверка, что элемент массива по этой позиции пустой, если нет, то - if (_places[position] == null) - { - _places[position] = Locomotive; - return position; - } - //проверка, что после вставляемого элемента в массиве есть пустой элемент - int findEmptyPos = -1; + where T : class + { + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; + public SetLocomotivGeneric(int count) + { + _maxCount = count; + _places = new List(); + } + public int Insert(T locomotive) + { + if (_places.Count > _maxCount) + { + return -1; + } + // вставка в начало набора + return Insert(locomotive, 0); + } + public int Insert(T locomotive, int position) + { + if (position >= _maxCount || position < 0) return -1; + _places.Insert(position, locomotive); + return position; + } + public T Remove(int position) + { + // проверка позиции + if (position >= _maxCount || position < 0) return null; + // удаление объекта из массива, присовив элементу массива значение null + T temp = _places[position]; + _places.RemoveAt(position); + return temp; + } + public T this[int position] + { + get + { + if (position >= _places.Count || position < 0) + { + return null; + } + return _places[position]; + } + set + { + if (position >= _places.Count || position < 0) + { + return; + } + Insert(value, position); - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - findEmptyPos = i; - break; - } - } - if (findEmptyPos < 0) return -1; - - //сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - for (int i = findEmptyPos; i > position; i--) - { - _places[i] = _places[i - 1]; - } - // вставка по позиции - _places[position] = Locomotive; - return position; - } - // Удаление объекта из набора с конкретной позиции - public T Remove(int position) - { - // проверка позиции - if (position >= _places.Length || position < 0) return null; - // удаление объекта из массива, присовив элементу массива значение null - T temp = _places[position]; - _places[position] = null; - return temp; - } - // Получение объекта из набора по позиции - public T Get(int position) - { - // проверка позиции - if (position >= _places.Length || position < 0) - return null; - return _places[position]; - } - } + } + } + public IEnumerable GetLocomotive() + { + foreach (var locomotive in _places) + { + if (locomotive != null) + { + yield return locomotive; + } + else + { + yield break; + } + } + } + } } -- 2.25.1