From 38e6f2c469c4f972597bbfb6ca70821b90f7b280 Mon Sep 17 00:00:00 2001 From: shadowik Date: Thu, 24 Nov 2022 21:26:12 +0400 Subject: [PATCH] Final --- .../FormMapWithSetBuses.Designer.cs | 119 +++++++++++++----- .../DoubleDeckerBus/FormMapWithSetBuses.cs | 98 ++++++++++----- .../DoubleDeckerBus/MapWithSetBusesGeneric.cs | 19 ++- .../DoubleDeckerBus/MapsCollection.cs | 43 +++++++ .../DoubleDeckerBus/SetBusesGeneric.cs | 58 +++++---- 5 files changed, 245 insertions(+), 92 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs index 3d4f89d..6125aa2 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBoxSettings = new System.Windows.Forms.GroupBox(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.ButtonDeleteMap = new System.Windows.Forms.Button(); + this.ButtonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.buttonDeleteBus = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddBus = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBoxSettings.SuspendLayout(); + this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBoxSettings // + this.groupBoxSettings.Controls.Add(this.groupBox1); this.groupBoxSettings.Controls.Add(this.buttonRight); this.groupBoxSettings.Controls.Add(this.buttonDown); this.groupBoxSettings.Controls.Add(this.buttonLeft); @@ -55,21 +62,79 @@ this.groupBoxSettings.Controls.Add(this.buttonDeleteBus); this.groupBoxSettings.Controls.Add(this.maskedTextBoxPosition); this.groupBoxSettings.Controls.Add(this.buttonAddBus); - this.groupBoxSettings.Controls.Add(this.comboBoxSelectorMap); this.groupBoxSettings.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxSettings.Location = new System.Drawing.Point(600, 0); + this.groupBoxSettings.Location = new System.Drawing.Point(645, 0); this.groupBoxSettings.Name = "groupBoxSettings"; - this.groupBoxSettings.Size = new System.Drawing.Size(200, 450); + this.groupBoxSettings.Size = new System.Drawing.Size(200, 643); this.groupBoxSettings.TabIndex = 0; this.groupBoxSettings.TabStop = false; this.groupBoxSettings.Text = "Инструменты"; // + // groupBox1 + // + this.groupBox1.Controls.Add(this.listBoxMaps); + this.groupBox1.Controls.Add(this.ButtonDeleteMap); + this.groupBox1.Controls.Add(this.ButtonAddMap); + this.groupBox1.Controls.Add(this.textBoxNewMapName); + this.groupBox1.Controls.Add(this.comboBoxSelectorMap); + this.groupBox1.Location = new System.Drawing.Point(0, 22); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(200, 339); + this.groupBox1.TabIndex = 11; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Карты"; + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 15; + this.listBoxMaps.Location = new System.Drawing.Point(6, 150); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(188, 124); + this.listBoxMaps.TabIndex = 4; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged); + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(6, 280); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(194, 53); + this.ButtonDeleteMap.TabIndex = 3; + this.ButtonDeleteMap.Text = "Удалить карту"; + this.ButtonDeleteMap.UseVisualStyleBackColor = true; + this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(6, 84); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(194, 53); + this.ButtonAddMap.TabIndex = 2; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(6, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(188, 23); + this.textBoxNewMapName.TabIndex = 1; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 55); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); + this.comboBoxSelectorMap.TabIndex = 0; + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.RightArrow; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(109, 398); + this.buttonRight.Location = new System.Drawing.Point(109, 591); this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(26, 22); @@ -83,7 +148,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.DownArrow; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(86, 417); + this.buttonDown.Location = new System.Drawing.Point(86, 610); this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(26, 22); @@ -97,7 +162,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.LeftArrow; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(63, 398); + this.buttonLeft.Location = new System.Drawing.Point(63, 591); this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(26, 22); @@ -111,7 +176,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.UpArrow; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(85, 379); + this.buttonUp.Location = new System.Drawing.Point(85, 572); this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(26, 22); @@ -122,7 +187,7 @@ // // button1 // - this.button1.Location = new System.Drawing.Point(6, 320); + this.button1.Location = new System.Drawing.Point(6, 521); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(182, 35); this.button1.TabIndex = 5; @@ -132,7 +197,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 261); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 480); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(182, 35); this.buttonShowStorage.TabIndex = 4; @@ -142,7 +207,7 @@ // // buttonDeleteBus // - this.buttonDeleteBus.Location = new System.Drawing.Point(6, 181); + this.buttonDeleteBus.Location = new System.Drawing.Point(6, 438); this.buttonDeleteBus.Name = "buttonDeleteBus"; this.buttonDeleteBus.Size = new System.Drawing.Size(188, 36); this.buttonDeleteBus.TabIndex = 3; @@ -152,7 +217,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 152); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 409); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); @@ -160,7 +225,7 @@ // // buttonAddBus // - this.buttonAddBus.Location = new System.Drawing.Point(6, 87); + this.buttonAddBus.Location = new System.Drawing.Point(6, 367); this.buttonAddBus.Name = "buttonAddBus"; this.buttonAddBus.Size = new System.Drawing.Size(188, 36); this.buttonAddBus.TabIndex = 1; @@ -168,25 +233,12 @@ this.buttonAddBus.UseVisualStyleBackColor = true; this.buttonAddBus.Click += new System.EventHandler(this.ButtonAddBus_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Водная карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); - this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.Text = "Простая карта"; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); - // // pictureBox // 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(600, 450); + this.pictureBox.Size = new System.Drawing.Size(645, 643); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -194,13 +246,15 @@ // 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(845, 643); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxSettings); this.Name = "FormMapWithSetBuses"; - this.Text = "FormMapWithSetBuses"; + this.Text = "Карты с набором объектов"; this.groupBoxSettings.ResumeLayout(false); this.groupBoxSettings.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -211,7 +265,6 @@ private GroupBox groupBoxSettings; private PictureBox pictureBox; private Button buttonAddBus; - private ComboBox comboBoxSelectorMap; private Button buttonDeleteBus; private MaskedTextBox maskedTextBoxPosition; private Button buttonShowStorage; @@ -220,5 +273,11 @@ private Button buttonDown; private Button buttonLeft; private Button buttonUp; + private GroupBox groupBox1; + private TextBox textBoxNewMapName; + private ComboBox comboBoxSelectorMap; + private Button ButtonDeleteMap; + private Button ButtonAddMap; + private ListBox listBoxMaps; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs index 1d5818d..80207fe 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs @@ -13,42 +13,47 @@ namespace DoubleDeckerBus { public partial class FormMapWithSetBuses : Form { - private MapWithSetBusesGeneric _mapBusesCollectionGeneric; + + private readonly Dictionary _mapsDict = new Dictionary() { + { "Простая карта", new SimpleMap() }, + { "Водная карта", new WaterMap() } + }; + + private readonly MapsCollection _mapsCollection; public FormMapWithSetBuses() { InitializeComponent(); - AbstractMap map = new SimpleMap(); - _mapBusesCollectionGeneric = new MapWithSetBusesGeneric( - pictureBox.Width, pictureBox.Height, map); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var item in _mapsDict) { + comboBoxSelectorMap.Items.Add(item.Key); + } } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + int index = listBoxMaps.SelectedIndex; + + listBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Водная карта": - map = new WaterMap(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { - _mapBusesCollectionGeneric = new MapWithSetBusesGeneric( - pictureBox.Width, pictureBox.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) { - _mapBusesCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; } } private void ButtonAddBus_Click(object sender, EventArgs e) { - if (_mapBusesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -63,10 +68,10 @@ namespace DoubleDeckerBus DrawingObjectBus bus = new(form.SelectedBus); - if (_mapBusesCollectionGeneric + bus != -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + bus != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapBusesCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -86,10 +91,10 @@ namespace DoubleDeckerBus return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapBusesCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapBusesCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -99,25 +104,25 @@ namespace DoubleDeckerBus private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapBusesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBusesCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapBusesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBusesCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapBusesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -139,7 +144,42 @@ namespace DoubleDeckerBus dir = Direction.Right; break; } - pictureBox.Image = _mapBusesCollectionGeneric.MoveObject(dir); + pictureBox.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 (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); + ReloadMaps(); + } + + private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + 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(); + } } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs index 39696a7..5de41e8 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs @@ -50,13 +50,9 @@ namespace DoubleDeckerBus public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setBuses.Count; i++) + foreach (var bus in _setBuses.GetBuses()) { - var bus = _setBuses.Get(i); - if (bus != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, bus); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, bus); } return new(_pictureWidth, _pictureHeight); } @@ -75,11 +71,11 @@ namespace DoubleDeckerBus int j = _setBuses.Count - 1; for (int i = 0; i < _setBuses.Count; i++) { - if (_setBuses.Get(i) == null) + if (_setBuses[i] == null) { for (; j > i; j--) { - var bus = _setBuses.Get(j); + var bus = _setBuses[j]; if (bus != null) { _setBuses.Insert(bus, i); @@ -126,12 +122,13 @@ namespace DoubleDeckerBus int currentWidth = width - 1; int currentHeight = 0; - for (int i = 0; i < _setBuses.Count; i++) { + foreach (var bus in _setBuses.GetBuses()) + { - _setBuses.Get(i)?.SetObject(currentWidth * _placeSizeWidth, + bus?.SetObject(currentWidth * _placeSizeWidth, currentHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setBuses.Get(i)?.DrawingObject(g); + bus?.DrawingObject(g); if (currentWidth > 0) currentWidth--; diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs new file mode 100644 index 0000000..bb943e4 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs @@ -0,0 +1,43 @@ +using DoubleDeckerBus; + +namespace DoubleDeckerBus +{ + 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 (Keys.Contains(name)) return; + _mapStorages.Add(name, new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + + public MapWithSetBusesGeneric this[string ind] + { + get + { + _mapStorages.TryGetValue(ind, out var result); + return result; + } + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs index 9bcd653..1da17a2 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -9,50 +9,64 @@ namespace DoubleDeckerBus internal class SetBusesGeneric where T : class { - private readonly T[] _places; + private readonly List _places; - public int Count => _places.Length; + public int Count => _places.Count; private int BusyPlaces = 0; + private readonly int _maxCount; + public SetBusesGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T bus) { return Insert(bus, 0); } - + public int Insert(T bus, int position) { - if (position < 0 || position >= _places.Length || BusyPlaces == _places.Length) return -1; + if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1; BusyPlaces++; - while (_places[position] != null) { - for (int i = _places.Length - 1; i > 0; --i) { - if (_places[i] == null && _places[i - 1] != null) { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - } - _places[position] = bus; + _places.Insert(position, bus); return position; } - + public T Remove(int position) { - if (position < 0 || position >= _places.Length) return null; + if (position < 0 || position >= _maxCount) return null; T savedBus = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return savedBus; } - - public T Get(int position) - { - if (position < 0 || position >= _places.Length) return null; - return _places[position]; + + public T this[int position] { + get { + if (position < 0 || position >= _maxCount) return null; + return _places[position]; + } + set { + Insert(value, position); + } + + } + + public IEnumerable GetBuses() { + foreach (var bus in _places) + { + if (bus != null) + { + yield return bus; + } + else + { + yield break; + } + } } } }