diff --git a/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs b/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs index c2f4786..f8a50da 100644 --- a/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs +++ b/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs @@ -46,7 +46,7 @@ pictureBoxMonorail.Dock = DockStyle.Fill; pictureBoxMonorail.Location = new Point(0, 0); pictureBoxMonorail.Name = "pictureBoxMonorail"; - pictureBoxMonorail.Size = new Size(884, 461); + pictureBoxMonorail.Size = new Size(884, 545); pictureBoxMonorail.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxMonorail.TabIndex = 0; pictureBoxMonorail.TabStop = false; @@ -54,7 +54,7 @@ // buttonCreateExtendedMonorail // buttonCreateExtendedMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateExtendedMonorail.Location = new Point(12, 403); + buttonCreateExtendedMonorail.Location = new Point(12, 487); buttonCreateExtendedMonorail.Name = "buttonCreateExtendedMonorail"; buttonCreateExtendedMonorail.Size = new Size(140, 39); buttonCreateExtendedMonorail.TabIndex = 1; @@ -67,7 +67,7 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(770, 419); + buttonLeft.Location = new Point(770, 503); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 2; @@ -79,7 +79,7 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.arrowDown; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(806, 419); + buttonDown.Location = new Point(806, 503); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 3; @@ -91,7 +91,7 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.arrowRight; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(842, 419); + buttonRight.Location = new Point(842, 503); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 4; @@ -103,7 +103,7 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.arrowUp; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(806, 383); + buttonUp.Location = new Point(806, 467); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 5; @@ -124,7 +124,7 @@ // buttonCreateMonorail // buttonCreateMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateMonorail.Location = new Point(168, 403); + buttonCreateMonorail.Location = new Point(168, 487); buttonCreateMonorail.Name = "buttonCreateMonorail"; buttonCreateMonorail.Size = new Size(140, 39); buttonCreateMonorail.TabIndex = 7; @@ -146,7 +146,7 @@ // buttonSelectMonorail // buttonSelectMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonSelectMonorail.Location = new Point(324, 403); + buttonSelectMonorail.Location = new Point(324, 487); buttonSelectMonorail.Name = "buttonSelectMonorail"; buttonSelectMonorail.Size = new Size(140, 39); buttonSelectMonorail.TabIndex = 9; @@ -158,7 +158,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(884, 461); + ClientSize = new Size(884, 545); Controls.Add(buttonSelectMonorail); Controls.Add(buttonStep); Controls.Add(buttonCreateMonorail); diff --git a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs index e2b0474..99b55ed 100644 --- a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs +++ b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs @@ -29,13 +29,20 @@ private void InitializeComponent() { panelTools = new Panel(); + panelSets = new Panel(); + textBoxNameSet = new TextBox(); + buttonAddSet = new Button(); + ButtonDelSet = new Button(); + listBoxSets = new ListBox(); + labelSetsName = new Label(); buttonRefreshCollection = new Button(); - labelName = new Label(); + labelToolsName = new Label(); maskedTextBoxNumber = new MaskedTextBox(); buttonRemoveMonorail = new Button(); buttonAddMonorail = new Button(); pictureBoxCollection = new PictureBox(); panelTools.SuspendLayout(); + panelSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // @@ -43,57 +50,131 @@ // panelTools.Anchor = AnchorStyles.Top | AnchorStyles.Right; panelTools.BorderStyle = BorderStyle.FixedSingle; + panelTools.Controls.Add(panelSets); panelTools.Controls.Add(buttonRefreshCollection); - panelTools.Controls.Add(labelName); + panelTools.Controls.Add(labelToolsName); panelTools.Controls.Add(maskedTextBoxNumber); panelTools.Controls.Add(buttonRemoveMonorail); panelTools.Controls.Add(buttonAddMonorail); panelTools.Location = new Point(788, 10); panelTools.Name = "panelTools"; - panelTools.Size = new Size(186, 430); + panelTools.Size = new Size(187, 580); panelTools.TabIndex = 0; // + // panelSets + // + panelSets.Anchor = AnchorStyles.Top | AnchorStyles.Right; + panelSets.BorderStyle = BorderStyle.FixedSingle; + panelSets.Controls.Add(textBoxNameSet); + panelSets.Controls.Add(buttonAddSet); + panelSets.Controls.Add(ButtonDelSet); + panelSets.Controls.Add(listBoxSets); + panelSets.Controls.Add(labelSetsName); + panelSets.Location = new Point(11, 45); + panelSets.Name = "panelSets"; + panelSets.Size = new Size(163, 292); + panelSets.TabIndex = 4; + // + // textBoxNameSet + // + textBoxNameSet.Location = new Point(7, 40); + textBoxNameSet.Name = "textBoxNameSet"; + textBoxNameSet.Size = new Size(145, 23); + textBoxNameSet.TabIndex = 8; + // + // buttonAddSet + // + buttonAddSet.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonAddSet.BackColor = SystemColors.Window; + buttonAddSet.FlatAppearance.BorderColor = Color.Black; + buttonAddSet.FlatStyle = FlatStyle.Flat; + buttonAddSet.Location = new Point(7, 80); + buttonAddSet.Name = "buttonAddSet"; + buttonAddSet.Size = new Size(145, 34); + buttonAddSet.TabIndex = 7; + buttonAddSet.Text = "Add set"; + buttonAddSet.UseVisualStyleBackColor = false; + buttonAddSet.Click += buttonAddSet_Click; + // + // ButtonDelSet + // + ButtonDelSet.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonDelSet.BackColor = SystemColors.Window; + ButtonDelSet.FlatAppearance.BorderColor = Color.Black; + ButtonDelSet.FlatStyle = FlatStyle.Flat; + ButtonDelSet.Location = new Point(7, 244); + ButtonDelSet.Name = "ButtonDelSet"; + ButtonDelSet.Size = new Size(145, 34); + ButtonDelSet.TabIndex = 5; + ButtonDelSet.Text = "Remove set"; + ButtonDelSet.UseVisualStyleBackColor = false; + ButtonDelSet.Click += ButtonDelSet_Click; + // + // listBoxSets + // + listBoxSets.Anchor = AnchorStyles.Top | AnchorStyles.Right; + listBoxSets.FormattingEnabled = true; + listBoxSets.ItemHeight = 15; + listBoxSets.Location = new Point(7, 133); + listBoxSets.Name = "listBoxSets"; + listBoxSets.Size = new Size(145, 94); + listBoxSets.TabIndex = 6; + listBoxSets.SelectedIndexChanged += listBoxSets_SelectedIndexChanged; + // + // labelSetsName + // + labelSetsName.Anchor = AnchorStyles.Top | AnchorStyles.Right; + labelSetsName.AutoSize = true; + labelSetsName.Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point); + labelSetsName.Location = new Point(7, 6); + labelSetsName.Name = "labelSetsName"; + labelSetsName.Size = new Size(41, 21); + labelSetsName.TabIndex = 5; + labelSetsName.Text = "Sets"; + // // buttonRefreshCollection // - buttonRefreshCollection.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRefreshCollection.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRefreshCollection.BackColor = SystemColors.Window; buttonRefreshCollection.FlatAppearance.BorderColor = Color.Black; buttonRefreshCollection.FlatStyle = FlatStyle.Flat; - buttonRefreshCollection.Location = new Point(18, 281); + buttonRefreshCollection.Location = new Point(5, 534); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(155, 34); + buttonRefreshCollection.Size = new Size(175, 34); buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.Text = "Refresh collection"; buttonRefreshCollection.UseVisualStyleBackColor = false; buttonRefreshCollection.Click += buttonRefreshCollection_Click; // - // labelName + // labelToolsName // - labelName.AutoSize = true; - labelName.Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point); - labelName.Location = new Point(17, -2); - labelName.Name = "labelName"; - labelName.Size = new Size(48, 21); - labelName.TabIndex = 0; - labelName.Text = "Tools"; + labelToolsName.Anchor = AnchorStyles.Top | AnchorStyles.Right; + labelToolsName.AutoSize = true; + labelToolsName.Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point); + labelToolsName.Location = new Point(18, 9); + labelToolsName.Name = "labelToolsName"; + labelToolsName.Size = new Size(48, 21); + labelToolsName.TabIndex = 0; + labelToolsName.Text = "Tools"; // // maskedTextBoxNumber // + maskedTextBoxNumber.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; maskedTextBoxNumber.BorderStyle = BorderStyle.FixedSingle; - maskedTextBoxNumber.Location = new Point(17, 144); + maskedTextBoxNumber.Location = new Point(4, 411); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(155, 23); + maskedTextBoxNumber.Size = new Size(175, 23); maskedTextBoxNumber.TabIndex = 2; // // buttonRemoveMonorail // - buttonRemoveMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRemoveMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRemoveMonorail.BackColor = SystemColors.Window; buttonRemoveMonorail.FlatAppearance.BorderColor = Color.Black; buttonRemoveMonorail.FlatStyle = FlatStyle.Flat; - buttonRemoveMonorail.Location = new Point(18, 173); + buttonRemoveMonorail.Location = new Point(4, 451); buttonRemoveMonorail.Name = "buttonRemoveMonorail"; - buttonRemoveMonorail.Size = new Size(155, 34); + buttonRemoveMonorail.Size = new Size(175, 34); buttonRemoveMonorail.TabIndex = 1; buttonRemoveMonorail.Text = "Remove monorail"; buttonRemoveMonorail.UseVisualStyleBackColor = false; @@ -101,13 +182,13 @@ // // buttonAddMonorail // - buttonAddMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonAddMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonAddMonorail.BackColor = SystemColors.Window; buttonAddMonorail.FlatAppearance.BorderColor = Color.Black; buttonAddMonorail.FlatStyle = FlatStyle.Flat; - buttonAddMonorail.Location = new Point(18, 33); + buttonAddMonorail.Location = new Point(4, 357); buttonAddMonorail.Name = "buttonAddMonorail"; - buttonAddMonorail.Size = new Size(155, 34); + buttonAddMonorail.Size = new Size(175, 34); buttonAddMonorail.TabIndex = 0; buttonAddMonorail.Text = "Add monorail"; buttonAddMonorail.UseVisualStyleBackColor = false; @@ -115,9 +196,9 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(5, 10); + pictureBoxCollection.Location = new Point(12, 10); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(777, 430); + pictureBoxCollection.Size = new Size(774, 580); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -125,13 +206,15 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(974, 439); + ClientSize = new Size(974, 589); Controls.Add(pictureBoxCollection); Controls.Add(panelTools); Name = "FormMonorailCollection"; Text = "Monorail collection"; panelTools.ResumeLayout(false); panelTools.PerformLayout(); + panelSets.ResumeLayout(false); + panelSets.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -139,11 +222,17 @@ #endregion private Panel panelTools; - private Label labelName; + private Label labelToolsName; private Button buttonAddMonorail; private Button buttonRemoveMonorail; private MaskedTextBox maskedTextBoxNumber; private Button buttonRefreshCollection; private PictureBox pictureBoxCollection; + private Panel panelSets; + private Label labelSetsName; + private ListBox listBoxSets; + private Button ButtonDelSet; + private Button buttonAddSet; + private TextBox textBoxNameSet; } } \ No newline at end of file diff --git a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs index e395aa5..9e19d89 100644 --- a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs +++ b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs @@ -1,6 +1,4 @@ using ProjectMonorail.Generics; -using ProjectMonorail.MovementStrategy; -using ProjectMonorail.DrawingObjects; namespace ProjectMonorail { @@ -12,7 +10,7 @@ namespace ProjectMonorail /// /// Набор объектов /// - private readonly MonorailsGenericCollection _monorails; + private readonly MonorailsGenericStorage _storage; /// /// Конструктор @@ -20,7 +18,73 @@ namespace ProjectMonorail public FormMonorailCollection() { InitializeComponent(); - _monorails = new MonorailsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new MonorailsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + /// + /// Заполнение listBoxSets + /// + private void ReloadObjects() + { + int index = listBoxSets.SelectedIndex; + listBoxSets.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxSets.Items.Add(_storage.Keys[i]); + } + if (listBoxSets.Items.Count > 0 && (index == -1 || index >= listBoxSets.Items.Count)) + { + listBoxSets.SelectedIndex = 0; + } + else if (listBoxSets.Items.Count > 0 && index > -1 && index < listBoxSets.Items.Count) + { + listBoxSets.SelectedIndex = index; + } + } + + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void buttonAddSet_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxNameSet.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxNameSet.Text); + ReloadObjects(); + } + + /// + /// Выбор набора + /// + /// + /// + private void listBoxSets_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxSets.SelectedItem?.ToString() ?? string.Empty]?.ShowMonorails(); + } + + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelSet_Click(object sender, EventArgs e) + { + if (listBoxSets.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить набор {listBoxSets.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxSets.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } } /// @@ -30,13 +94,24 @@ namespace ProjectMonorail /// private void buttonAddMonorail_Click(object sender, EventArgs e) { + if (listBoxSets.SelectedIndex == -1) + { + return; + } + + var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + FormMonorail form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_monorails + form.SelectedMonorail != -1) + if (obj + form.SelectedMonorail != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _monorails.ShowMonorails(); + pictureBoxCollection.Image = obj.ShowMonorails(); } else { @@ -52,6 +127,17 @@ namespace ProjectMonorail /// private void buttonRemoveMonorail_Click(object sender, EventArgs e) { + if (listBoxSets.SelectedIndex == -1) + { + return; + } + + var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; @@ -59,10 +145,10 @@ namespace ProjectMonorail int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_monorails - pos) + if (obj - pos) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _monorails.ShowMonorails(); + pictureBoxCollection.Image = obj.ShowMonorails(); } else { @@ -77,7 +163,18 @@ namespace ProjectMonorail /// private void buttonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _monorails.ShowMonorails(); + if (listBoxSets.SelectedIndex == -1) + { + return; + } + + var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + + pictureBoxCollection.Image = obj.ShowMonorails(); } } } diff --git a/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs b/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs index 791d935..2a43cf7 100644 --- a/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs +++ b/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs @@ -74,7 +74,7 @@ namespace ProjectMonorail.Generics /// public static bool operator -(MonorailsGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { return collect._collection.Remove(pos); @@ -89,7 +89,7 @@ namespace ProjectMonorail.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// @@ -130,11 +130,10 @@ namespace ProjectMonorail.Generics /// private void DrawObjects(Graphics g) { - T? obj; int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - int diff = 1, currWidth = 0; - for (int i = 0; i < _collection.Count; i++) + int diff = 1, currWidth = 0, i = 0; + foreach (var monorail in _collection.GetMonorails()) { currWidth++; if (currWidth > width) @@ -142,13 +141,13 @@ namespace ProjectMonorail.Generics diff++; currWidth = 1; } - obj = _collection.Get(i); - if (obj != null) + if (monorail != null) { - obj.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40, - (height - diff) * _placeSizeHeight + _placeSizeHeight / 15); - obj.DrawTransport(g); + monorail.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40, + (height - diff) * _placeSizeHeight + _placeSizeHeight / 15); + monorail.DrawTransport(g); } + i++; } } } diff --git a/ProjectMonorail/ProjectMonorail/MonorailsGenericStorage.cs b/ProjectMonorail/ProjectMonorail/MonorailsGenericStorage.cs new file mode 100644 index 0000000..39a2de4 --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/MonorailsGenericStorage.cs @@ -0,0 +1,78 @@ +using ProjectMonorail.DrawingObjects; +using ProjectMonorail.MovementStrategy; + +namespace ProjectMonorail.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class MonorailsGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _monorailsStorages; + + /// + /// Возвращение списка названий наборов + /// + public List Keys => _monorailsStorages.Keys.ToList(); + + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + + /// + /// Конструктор + /// + /// + /// + public MonorailsGenericStorage(int pictureWidth, int pictureHeight) + { + _monorailsStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (!_monorailsStorages.ContainsKey(name)) + _monorailsStorages.Add(name, new MonorailsGenericCollection(_pictureWidth, _pictureHeight)); + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (_monorailsStorages.ContainsKey(name)) + _monorailsStorages.Remove(name); + } + + /// + /// Доступ к набору + /// + /// + /// + public MonorailsGenericCollection? this[string ind] + { + get + { + if (_monorailsStorages.ContainsKey(ind)) + return _monorailsStorages[ind]; + return null; + } + } + } +} diff --git a/ProjectMonorail/ProjectMonorail/SetGeneric.cs b/ProjectMonorail/ProjectMonorail/SetGeneric.cs index d83f05e..418e6be 100644 --- a/ProjectMonorail/ProjectMonorail/SetGeneric.cs +++ b/ProjectMonorail/ProjectMonorail/SetGeneric.cs @@ -1,6 +1,4 @@ -using System.Numerics; - -namespace ProjectMonorail.Generics +namespace ProjectMonorail.Generics { /// /// Параметризованный набор объектов @@ -9,14 +7,19 @@ namespace ProjectMonorail.Generics internal class SetGeneric 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; /// /// Конструктор @@ -24,7 +27,8 @@ namespace ProjectMonorail.Generics /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// @@ -45,28 +49,10 @@ namespace ProjectMonorail.Generics /// public int Insert(T monorail, int position) { - int nullIndex = -1, i; - - if (position < 0 || position >= Count) + if (position < 0 || position > Count || Count >= _maxCount) return -1; - - for (i = position; i < Count; i++) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - if (nullIndex < 0) - return -1; - - for (i = nullIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = monorail; + _places.Insert(position, monorail); return position; } @@ -79,7 +65,7 @@ namespace ProjectMonorail.Generics { if (position < 0 || position >= Count) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } @@ -88,11 +74,37 @@ namespace ProjectMonorail.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) - return null; - return _places[position]; + get + { + if (position < 0 || position >= Count) + return null; + return _places[position]; + } + set + { + if (position < 0 || position > Count || Count >= _maxCount) + return; + + _places.Insert(position, value); + } + } + + /// + /// Проход по списку + /// + /// + public IEnumerable GetMonorails(int? maxCars = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCars.HasValue && i == maxCars.Value) + { + yield break; + } + } } } }