diff --git a/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs b/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs
index b546a4e..a7b343f 100644
--- a/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs
+++ b/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs
@@ -34,8 +34,14 @@
this.ButtonAddTrain = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveTrain = new System.Windows.Forms.Button();
+ this.panel2 = new System.Windows.Forms.Panel();
+ this.textBoxSetName = new System.Windows.Forms.TextBox();
+ this.buttonAddSet = new System.Windows.Forms.Button();
+ this.listBoxStorage = new System.Windows.Forms.ListBox();
+ this.buttonRemoveSet = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panel1.SuspendLayout();
+ this.panel2.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxCollection
@@ -49,6 +55,8 @@
//
// panel1
//
+ this.panel1.AccessibleName = "";
+ this.panel1.Controls.Add(this.panel2);
this.panel1.Controls.Add(this.maskedTextBoxNumber);
this.panel1.Controls.Add(this.ButtonAddTrain);
this.panel1.Controls.Add(this.ButtonRefreshCollection);
@@ -61,14 +69,14 @@
//
// maskedTextBoxNumber
//
- this.maskedTextBoxNumber.Location = new System.Drawing.Point(59, 170);
+ this.maskedTextBoxNumber.Location = new System.Drawing.Point(33, 365);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20);
this.maskedTextBoxNumber.TabIndex = 4;
//
// ButtonAddTrain
//
- this.ButtonAddTrain.Location = new System.Drawing.Point(71, 31);
+ this.ButtonAddTrain.Location = new System.Drawing.Point(43, 327);
this.ButtonAddTrain.Name = "ButtonAddTrain";
this.ButtonAddTrain.Size = new System.Drawing.Size(75, 23);
this.ButtonAddTrain.TabIndex = 3;
@@ -78,9 +86,9 @@
//
// ButtonRefreshCollection
//
- this.ButtonRefreshCollection.Location = new System.Drawing.Point(71, 320);
+ this.ButtonRefreshCollection.Location = new System.Drawing.Point(21, 438);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
- this.ButtonRefreshCollection.Size = new System.Drawing.Size(75, 23);
+ this.ButtonRefreshCollection.Size = new System.Drawing.Size(121, 23);
this.ButtonRefreshCollection.TabIndex = 2;
this.ButtonRefreshCollection.Text = "Refresh Collection";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
@@ -88,14 +96,62 @@
//
// ButtonRemoveTrain
//
- this.ButtonRemoveTrain.Location = new System.Drawing.Point(71, 196);
+ this.ButtonRemoveTrain.Location = new System.Drawing.Point(33, 400);
this.ButtonRemoveTrain.Name = "ButtonRemoveTrain";
- this.ButtonRemoveTrain.Size = new System.Drawing.Size(75, 23);
+ this.ButtonRemoveTrain.Size = new System.Drawing.Size(100, 23);
this.ButtonRemoveTrain.TabIndex = 0;
this.ButtonRemoveTrain.Text = "Remove Train";
this.ButtonRemoveTrain.UseVisualStyleBackColor = true;
this.ButtonRemoveTrain.Click += new System.EventHandler(this.ButtonRemoveTrain_Click);
//
+ // panel2
+ //
+ this.panel2.AccessibleName = "";
+ this.panel2.Controls.Add(this.buttonRemoveSet);
+ this.panel2.Controls.Add(this.listBoxStorage);
+ this.panel2.Controls.Add(this.buttonAddSet);
+ this.panel2.Controls.Add(this.textBoxSetName);
+ this.panel2.Location = new System.Drawing.Point(14, 12);
+ this.panel2.Name = "panel2";
+ this.panel2.Size = new System.Drawing.Size(140, 208);
+ this.panel2.TabIndex = 5;
+ //
+ // textBoxSetName
+ //
+ this.textBoxSetName.Location = new System.Drawing.Point(8, 12);
+ this.textBoxSetName.Name = "textBoxSetName";
+ this.textBoxSetName.Size = new System.Drawing.Size(120, 20);
+ this.textBoxSetName.TabIndex = 0;
+ //
+ // buttonAddSet
+ //
+ this.buttonAddSet.Location = new System.Drawing.Point(8, 38);
+ this.buttonAddSet.Name = "buttonAddSet";
+ this.buttonAddSet.Size = new System.Drawing.Size(120, 23);
+ this.buttonAddSet.TabIndex = 1;
+ this.buttonAddSet.Text = "Add set";
+ this.buttonAddSet.UseVisualStyleBackColor = true;
+ this.buttonAddSet.Click += new System.EventHandler(this.buttonAddSet_Click);
+ //
+ // listBoxStorage
+ //
+ this.listBoxStorage.FormattingEnabled = true;
+ this.listBoxStorage.Location = new System.Drawing.Point(8, 67);
+ this.listBoxStorage.Name = "listBoxStorage";
+ this.listBoxStorage.Size = new System.Drawing.Size(120, 95);
+ this.listBoxStorage.TabIndex = 2;
+ this.listBoxStorage.SelectedIndexChanged += new System.EventHandler(this.listBoxStorage_SelectedIndexChanged);
+ //
+ // buttonRemoveSet
+ //
+ this.buttonRemoveSet.Location = new System.Drawing.Point(8, 168);
+ this.buttonRemoveSet.Name = "buttonRemoveSet";
+ this.buttonRemoveSet.Size = new System.Drawing.Size(120, 23);
+ this.buttonRemoveSet.TabIndex = 3;
+ this.buttonRemoveSet.Text = "Remove set";
+ this.buttonRemoveSet.UseVisualStyleBackColor = true;
+ this.buttonRemoveSet.Click += new System.EventHandler(this.buttonRemoveSet_Click);
+ //
// FormTrainCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -108,6 +164,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
+ this.panel2.ResumeLayout(false);
+ this.panel2.PerformLayout();
this.ResumeLayout(false);
}
@@ -120,5 +178,10 @@
private System.Windows.Forms.Button ButtonRemoveTrain;
private System.Windows.Forms.Button ButtonAddTrain;
private System.Windows.Forms.TextBox maskedTextBoxNumber;
+ private System.Windows.Forms.Panel panel2;
+ private System.Windows.Forms.Button buttonRemoveSet;
+ private System.Windows.Forms.ListBox listBoxStorage;
+ private System.Windows.Forms.Button buttonAddSet;
+ private System.Windows.Forms.TextBox textBoxSetName;
}
}
\ No newline at end of file
diff --git a/Laba1Loco/Laba1Loco/FormTrainCollection.cs b/Laba1Loco/Laba1Loco/FormTrainCollection.cs
index d53116f..0ef95df 100644
--- a/Laba1Loco/Laba1Loco/FormTrainCollection.cs
+++ b/Laba1Loco/Laba1Loco/FormTrainCollection.cs
@@ -7,6 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
+using static System.Net.Mime.MediaTypeNames;
namespace Laba1Loco
{
@@ -15,15 +16,36 @@ namespace Laba1Loco
///
/// Набор объектов
///
- private readonly TrainsGenericCollection _trains;
+ private readonly TrainsGenericStorage _storage;
///
/// Конструктор
///
public FormTrainCollection()
{
InitializeComponent();
- _trains = new TrainsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height);
+ _storage = new TrainsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
+ ///
+ /// Заполнение listBoxObjects
+ ///
+ private void ReloadObjects()
+ {
+ int index = listBoxStorage.SelectedIndex;
+ listBoxStorage.Items.Clear();
+ for (int i = 0; i < _storage.Keys.Count; i++)
+ {
+ listBoxStorage.Items.Add(_storage.Keys[i]);
+ }
+ if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
+ {
+ listBoxStorage.SelectedIndex = 0;
+ }
+ else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count)
+ {
+ listBoxStorage.SelectedIndex = index;
+ }
+ }
+
///
/// Добавление объекта в набор
///
@@ -31,13 +53,22 @@ namespace Laba1Loco
///
private void ButtonAddTrain_Click(object sender, EventArgs e)
{
+ if (listBoxStorage.SelectedIndex == -1)
+ {
+ return;
+ }
+ var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
+ if (obj == null)
+ {
+ return;
+ }
FormLocomotive form = new FormLocomotive();
if (form.ShowDialog() == DialogResult.OK)
{
- if ((_trains + form.SelectedTrain) != -1)
+ if ((obj + form.SelectedTrain) != -1)
{
MessageBox.Show("Объект добавлен");
- pictureBoxCollection.Image = _trains.ShowTrains();
+ pictureBoxCollection.Image = obj.ShowTrains();
}
else
{
@@ -53,6 +84,11 @@ namespace Laba1Loco
///
private void ButtonRemoveTrain_Click(object sender, EventArgs e)
{
+ if (listBoxStorage.SelectedIndex == -1)
+ {
+ return;
+ }
+ var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
@@ -64,11 +100,16 @@ namespace Laba1Loco
MessageBox.Show("Не удалось удалить объект");
return;
}
+ if (maskedTextBoxNumber.Text.Length == 0)
+ {
+ MessageBox.Show("Не удалось удалить объект");
+ return;
+ }
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
- if (_trains - pos != null)
+ if (obj - pos != null)
{
MessageBox.Show("Объект удален");
- pictureBoxCollection.Image = _trains.ShowTrains();
+ pictureBoxCollection.Image = obj.ShowTrains();
}
else
{
@@ -82,7 +123,58 @@ namespace Laba1Loco
///
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
- pictureBoxCollection.Image = _trains.ShowTrains();
+ if (listBoxStorage.SelectedIndex == -1)
+ {
+ return;
+ }
+ var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
+ pictureBoxCollection.Image = obj.ShowTrains();
+ }
+
+ ///
+ /// Добавление набора в коллекцию
+ ///
+ ///
+ ///
+ private void buttonAddSet_Click(object sender, EventArgs e)
+ {
+ if (string.IsNullOrEmpty(textBoxSetName.Text))
+ {
+ MessageBox.Show("Не все данные заполнены", "Ошибка",
+ MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return;
+ }
+ _storage.AddSet(textBoxSetName.Text);
+ ReloadObjects();
+ }
+
+ ///
+ /// Выбор набора
+ ///
+ ///
+ ///
+ private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
+ {
+ pictureBoxCollection.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains();
+ }
+
+ ///
+ /// Удаление набора
+ ///
+ ///
+ ///
+ private void buttonRemoveSet_Click(object sender, EventArgs e)
+ {
+ if (listBoxStorage.SelectedIndex == -1)
+ {
+ return;
+ }
+ if (MessageBox.Show($"Удалить объект { listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
+ {
+ _storage.DelSet(listBoxStorage.SelectedItem.ToString()?? string.Empty);
+ ReloadObjects();
+ }
+
}
}
}
diff --git a/Laba1Loco/Laba1Loco/Laba1Loco.csproj b/Laba1Loco/Laba1Loco/Laba1Loco.csproj
index ca12671..28cd54b 100644
--- a/Laba1Loco/Laba1Loco/Laba1Loco.csproj
+++ b/Laba1Loco/Laba1Loco/Laba1Loco.csproj
@@ -74,6 +74,7 @@
+
FormLocomotive.cs
diff --git a/Laba1Loco/Laba1Loco/SetGeneric.cs b/Laba1Loco/Laba1Loco/SetGeneric.cs
index f641f2e..5483060 100644
--- a/Laba1Loco/Laba1Loco/SetGeneric.cs
+++ b/Laba1Loco/Laba1Loco/SetGeneric.cs
@@ -16,18 +16,23 @@ namespace Laba1Loco
///
/// Массив объектов, которые храним
///
- 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);
}
///
/// Добавление объекта в набор
@@ -36,20 +41,10 @@ namespace Laba1Loco
///
public int Insert(T train)
{
- int i = 0;
- for (;i < _places.Length; i++)
- {
- if (_places[i] == null)
- break;
- }
- if (i == _places.Length)
+ if (_places.Count >= _maxCount)
return -1;
- for (; i > 0; i--)
- {
- _places[i] = _places[i - 1];
- }
- _places[i] = train;
- return i;
+ _places.Insert(0, train);
+ return 0;
}
///
/// Добавление объекта в набор на конкретную позицию
@@ -59,20 +54,17 @@ namespace Laba1Loco
///
public bool Insert(T train, int position)
{
- if (position < 0 || position >= _places.Length)
+ if (_places.Count >= _maxCount)
return false;
- for (; position < _places.Length; position++)
- {
- if (_places[position] == null)
- break;
- }
- if (position == _places.Length)
+
+ if (position < 0 || position > _places.Count)
return false;
- for (; position > 0; position--)
- {
- _places[position] = _places[position - 1];
- }
- _places[position] = train;
+
+ if (position == _places.Count)
+ _places.Add(train);
+ else
+ _places.Insert(position, train);
+
return true;
}
///
@@ -82,9 +74,9 @@ namespace Laba1Loco
///
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;
}
///
@@ -94,10 +86,43 @@ namespace Laba1Loco
///
public T Get(int position)
{
- if (position < 0 || position >= _places.Length)
+ if (position < 0 || position >= _places.Count)
return null;
return _places[position];
}
- }
+ ///
+ /// Получение объекта из набора по позиции
+ ///
+ ///
+ ///
+ public T this[int position]
+ {
+ get
+ {
+ if (position < 0 || position >= _places.Count)
+ return null;
+ return _places[position];
+ }
+ set
+ {
+ Insert(value, position);
+ }
+ }
+ ///
+ /// Проход по списку
+ ///
+ ///
+ public IEnumerable GetTrains(int? maxTrains = null)
+ {
+ for (int i = 0; i < _places.Count; ++i)
+ {
+ yield return _places[i];
+ if (maxTrains.HasValue && i == maxTrains.Value)
+ {
+ yield break;
+ }
+ }
+ }
+ }
}
diff --git a/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs b/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs
index 0c16192..3ad62b6 100644
--- a/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs
+++ b/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs
@@ -67,13 +67,14 @@ where U : IMoveableObject
///
public static T operator -(TrainsGenericCollection collect, int pos)
{
- T obj = collect._collection.Get(pos);
+ T obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
+
///
/// Получение объекта IMoveableObject
///
@@ -81,7 +82,7 @@ where U : IMoveableObject
///
public U GetU(int pos)
{
- return (U)_collection.Get(pos)?.GetMoveableObject;
+ return (U)_collection[pos]?.GetMoveableObject;
}
///
/// Вывод всего набора объектов
@@ -118,17 +119,18 @@ where U : IMoveableObject
///
private void DrawObjects(Graphics g)
{
- for (int i = 0; i < _collection.Count; i++)
+ int i = 0;
+ foreach (var train in _collection.GetTrains())
{
- T t = _collection.Get(i);
- if (t != null)
+ if (train != null)
{
- t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
- if (t is DrawingLoco)
- (t as DrawingLoco).DrawTransport(g);
+ train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
+ if (train is DrawingLoco)
+ (train as DrawingLoco).DrawTransport(g);
else
- t.DrawTransport(g);
+ train.DrawTransport(g);
}
+ i++;
}
}
}
diff --git a/Laba1Loco/Laba1Loco/TrainsGenericStorage.cs b/Laba1Loco/Laba1Loco/TrainsGenericStorage.cs
new file mode 100644
index 0000000..c581eb7
--- /dev/null
+++ b/Laba1Loco/Laba1Loco/TrainsGenericStorage.cs
@@ -0,0 +1,75 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Laba1Loco
+{
+ internal class TrainsGenericStorage
+ {
+ ///
+ /// Словарь (хранилище)
+ ///
+ readonly Dictionary> _trainStorages;
+ ///
+ /// Возвращение списка названий наборов
+ ///
+ public List Keys => _trainStorages.Keys.ToList();
+ ///
+ /// Ширина окна отрисовки
+ ///
+ private readonly int _pictureWidth;
+ ///
+ /// Высота окна отрисовки
+ ///
+ private readonly int _pictureHeight;
+ ///
+ /// Конструктор
+ ///
+ ///
+ ///
+ public TrainsGenericStorage(int pictureWidth, int pictureHeight)
+ {
+ _trainStorages = new Dictionary>();
+ _pictureWidth = pictureWidth;
+ _pictureHeight = pictureHeight;
+ }
+ ///
+ /// Добавление набора
+ ///
+ /// Название набора
+ public void AddSet(string name)
+ {
+ if (_trainStorages.ContainsKey(name))
+ return;
+ _trainStorages[name] = new TrainsGenericCollection(_pictureWidth, _pictureHeight);
+ }
+ ///
+ /// Удаление набора
+ ///
+ /// Название набора
+ public void DelSet(string name)
+ {
+ if (!_trainStorages.ContainsKey(name))
+ return;
+ _trainStorages.Remove(name);
+ }
+ ///
+ /// Доступ к набору
+ ///
+ ///
+ ///
+ public TrainsGenericCollection
+ this[string ind]
+ {
+ get
+ {
+ if (_trainStorages.ContainsKey(ind))
+ return _trainStorages[ind];
+ return null;
+ }
+ }
+
+ }
+}