Готовая 4 лабораторная

This commit is contained in:
Даниил Путинцев 2023-10-24 18:58:46 +04:00
parent 5d96a63aa7
commit 25a690a85e
9 changed files with 387 additions and 90 deletions

View File

@ -5,9 +5,9 @@ using RoadTrain;
namespace RoadTrain.MovementStrategy
{
/// <summary>
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter)
/// </summary>
public class DrawningObjectTrain : IMoveableObject
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningRoadTrain (паттерн Adapter)
/// </summary>
public class DrawningObjectTrain : IMoveableObject
{
private readonly DrawningRoadTrain? _drawningRoadTrain = null;
public DrawningObjectTrain (DrawningRoadTrain drawningRoadTrain)

View File

@ -102,6 +102,22 @@ namespace RoadTrain.DrawningObjects
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0)
{
x = 0;
}
else if (x + _trainWidth > _pictureWidth)
{
x = _pictureWidth - _trainWidth;
}
if (y < 0)
{
y = 0;
}
else if (y + _trainHeight > _pictureHeight)
{
y = _pictureHeight - _trainHeight;
}
_startPosX = x;
_startPosY = y;
}

View File

@ -20,7 +20,7 @@ namespace RoadTrain.DrawningObjects
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawningTrain(int speed, double weight, Color bodyColor, Color
additionalColor, bool waterContainer, bool sweepingBrush, int width, int height) :base(speed, weight, bodyColor, 70, 30)
additionalColor, bool waterContainer, bool sweepingBrush, int width, int height) :base(speed, weight, bodyColor, width, height)
{
if (EntityRoadTrain != null)
{

View File

@ -44,8 +44,9 @@
// pictureBoxRoadTrain
//
pictureBoxRoadTrain.Location = new Point(0, 0);
pictureBoxRoadTrain.Margin = new Padding(3, 4, 3, 4);
pictureBoxRoadTrain.Name = "pictureBoxRoadTrain";
pictureBoxRoadTrain.Size = new Size(685, 361);
pictureBoxRoadTrain.Size = new Size(776, 481);
pictureBoxRoadTrain.TabIndex = 10;
pictureBoxRoadTrain.TabStop = false;
//
@ -54,9 +55,10 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.left;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(533, 269);
buttonLeft.Location = new Point(609, 359);
buttonLeft.Margin = new Padding(3, 4, 3, 4);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.Size = new Size(34, 40);
buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
@ -66,9 +68,10 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.up;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(577, 223);
buttonUp.Location = new Point(659, 297);
buttonUp.Margin = new Padding(3, 4, 3, 4);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.Size = new Size(34, 40);
buttonUp.TabIndex = 3;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
@ -78,9 +81,10 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(620, 269);
buttonRight.Location = new Point(709, 359);
buttonRight.Margin = new Padding(3, 4, 3, 4);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.Size = new Size(34, 40);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
@ -90,9 +94,10 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.down;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(577, 315);
buttonDown.Location = new Point(659, 420);
buttonDown.Margin = new Padding(3, 4, 3, 4);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.Size = new Size(34, 40);
buttonDown.TabIndex = 5;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
@ -102,16 +107,18 @@
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "Центр формы", "Граница формы" });
comboBoxStrategy.Location = new Point(552, 12);
comboBoxStrategy.Location = new Point(631, 16);
comboBoxStrategy.Margin = new Padding(3, 4, 3, 4);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.Size = new Size(138, 28);
comboBoxStrategy.TabIndex = 8;
//
// ButtonStep
//
ButtonStep.Location = new Point(581, 50);
ButtonStep.Location = new Point(664, 67);
ButtonStep.Margin = new Padding(3, 4, 3, 4);
ButtonStep.Name = "ButtonStep";
ButtonStep.Size = new Size(75, 23);
ButtonStep.Size = new Size(86, 31);
ButtonStep.TabIndex = 9;
ButtonStep.Text = "Шаг";
ButtonStep.UseVisualStyleBackColor = true;
@ -119,9 +126,10 @@
//
// ButtonCreateTrain
//
ButtonCreateTrain.Location = new Point(30, 285);
ButtonCreateTrain.Location = new Point(34, 380);
ButtonCreateTrain.Margin = new Padding(3, 4, 3, 4);
ButtonCreateTrain.Name = "ButtonCreateTrain";
ButtonCreateTrain.Size = new Size(115, 43);
ButtonCreateTrain.Size = new Size(131, 80);
ButtonCreateTrain.TabIndex = 11;
ButtonCreateTrain.Text = "Создать моющую машину";
ButtonCreateTrain.UseVisualStyleBackColor = true;
@ -129,9 +137,10 @@
//
// ButtonCreateRoadTrain
//
ButtonCreateRoadTrain.Location = new Point(180, 285);
ButtonCreateRoadTrain.Location = new Point(206, 380);
ButtonCreateRoadTrain.Margin = new Padding(3, 4, 3, 4);
ButtonCreateRoadTrain.Name = "ButtonCreateRoadTrain";
ButtonCreateRoadTrain.Size = new Size(93, 43);
ButtonCreateRoadTrain.Size = new Size(106, 80);
ButtonCreateRoadTrain.TabIndex = 12;
ButtonCreateRoadTrain.Text = "Создать поезд";
ButtonCreateRoadTrain.UseVisualStyleBackColor = true;
@ -139,9 +148,10 @@
//
// ButtonSelectTrain
//
ButtonSelectTrain.Location = new Point(567, 94);
ButtonSelectTrain.Location = new Point(648, 125);
ButtonSelectTrain.Margin = new Padding(3, 4, 3, 4);
ButtonSelectTrain.Name = "ButtonSelectTrain";
ButtonSelectTrain.Size = new Size(89, 42);
ButtonSelectTrain.Size = new Size(102, 56);
ButtonSelectTrain.TabIndex = 13;
ButtonSelectTrain.Text = "Добавить поезд";
ButtonSelectTrain.UseVisualStyleBackColor = true;
@ -149,9 +159,9 @@
//
// FormRoadTrain
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(679, 363);
ClientSize = new Size(776, 484);
Controls.Add(ButtonSelectTrain);
Controls.Add(ButtonCreateRoadTrain);
Controls.Add(ButtonCreateTrain);
@ -162,6 +172,7 @@
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(pictureBoxRoadTrain);
Margin = new Padding(3, 4, 3, 4);
Name = "FormRoadTrain";
Text = "FormRoadTrain";
((System.ComponentModel.ISupportInitialize)pictureBoxRoadTrain).EndInit();

View File

@ -29,40 +29,110 @@
private void InitializeComponent()
{
panel1 = new Panel();
label2 = new Label();
panel2 = new Panel();
textBoxStorageName = new TextBox();
ButtonDelObject = new Button();
ButtonAddObject = new Button();
listBoxStorages = new ListBox();
InputTextBox = new TextBox();
ButtonRefreshCollection = new Button();
ButtonRemoveTrain = new Button();
ButtonAddTrain = new Button();
label1 = new Label();
pictureBoxCollection = new PictureBox();
label = new Label();
panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// panel1
//
panel1.Controls.Add(label2);
panel1.Controls.Add(panel2);
panel1.Controls.Add(InputTextBox);
panel1.Controls.Add(ButtonRefreshCollection);
panel1.Controls.Add(ButtonRemoveTrain);
panel1.Controls.Add(ButtonAddTrain);
panel1.Controls.Add(label1);
panel1.Location = new Point(597, 12);
panel1.Dock = DockStyle.Right;
panel1.Location = new Point(678, 0);
panel1.Margin = new Padding(3, 4, 3, 4);
panel1.Name = "panel1";
panel1.Size = new Size(200, 426);
panel1.Size = new Size(236, 600);
panel1.TabIndex = 0;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(43, 18);
label2.Name = "label2";
label2.Size = new Size(66, 20);
label2.TabIndex = 0;
label2.Text = "Наборы";
//
// panel2
//
panel2.Controls.Add(textBoxStorageName);
panel2.Controls.Add(ButtonDelObject);
panel2.Controls.Add(ButtonAddObject);
panel2.Controls.Add(listBoxStorages);
panel2.Location = new Point(24, 29);
panel2.Name = "panel2";
panel2.Size = new Size(190, 278);
panel2.TabIndex = 5;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(21, 30);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(150, 27);
textBoxStorageName.TabIndex = 3;
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(21, 179);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(150, 29);
ButtonDelObject.TabIndex = 2;
ButtonDelObject.Text = "Удалить набор";
ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(21, 77);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(150, 29);
ButtonAddObject.TabIndex = 1;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(21, 121);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(150, 44);
listBoxStorages.TabIndex = 0;
listBoxStorages.SelectedIndexChanged += ListBoxStorages_SelectedIndexChanged;
//
// InputTextBox
//
InputTextBox.Location = new Point(49, 118);
InputTextBox.Location = new Point(59, 384);
InputTextBox.Margin = new Padding(3, 4, 3, 4);
InputTextBox.Name = "InputTextBox";
InputTextBox.Size = new Size(100, 23);
InputTextBox.Size = new Size(114, 27);
InputTextBox.TabIndex = 4;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(39, 233);
ButtonRefreshCollection.Location = new Point(45, 485);
ButtonRefreshCollection.Margin = new Padding(3, 4, 3, 4);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(126, 44);
ButtonRefreshCollection.Size = new Size(144, 59);
ButtonRefreshCollection.TabIndex = 3;
ButtonRefreshCollection.Text = "Обновить коллекцию";
ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -70,9 +140,10 @@
//
// ButtonRemoveTrain
//
ButtonRemoveTrain.Location = new Point(39, 160);
ButtonRemoveTrain.Location = new Point(45, 428);
ButtonRemoveTrain.Margin = new Padding(3, 4, 3, 4);
ButtonRemoveTrain.Name = "ButtonRemoveTrain";
ButtonRemoveTrain.Size = new Size(126, 30);
ButtonRemoveTrain.Size = new Size(144, 40);
ButtonRemoveTrain.TabIndex = 2;
ButtonRemoveTrain.Text = "Удалить поезд";
ButtonRemoveTrain.UseVisualStyleBackColor = true;
@ -80,9 +151,10 @@
//
// ButtonAddTrain
//
ButtonAddTrain.Location = new Point(39, 45);
ButtonAddTrain.Location = new Point(45, 322);
ButtonAddTrain.Margin = new Padding(3, 4, 3, 4);
ButtonAddTrain.Name = "ButtonAddTrain";
ButtonAddTrain.Size = new Size(126, 33);
ButtonAddTrain.Size = new Size(144, 44);
ButtonAddTrain.TabIndex = 1;
ButtonAddTrain.Text = "Добавить поезд";
ButtonAddTrain.UseVisualStyleBackColor = true;
@ -90,34 +162,49 @@
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(66, 14);
label1.Location = new Point(0, 0);
label1.Name = "label1";
label1.Size = new Size(83, 15);
label1.TabIndex = 0;
label1.Text = "Инструменты";
label1.Size = new Size(114, 31);
label1.TabIndex = 2;
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(0, 1);
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(591, 446);
pictureBoxCollection.Size = new Size(678, 600);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// label
//
label.AutoSize = true;
label.Location = new Point(718, 16);
label.Name = "label";
label.Size = new Size(103, 20);
label.TabIndex = 5;
label.Text = "Инструменты";
//
// FormTrainCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(914, 600);
Controls.Add(label);
Controls.Add(pictureBoxCollection);
Controls.Add(panel1);
Controls.Add(label1);
Margin = new Padding(3, 4, 3, 4);
Name = "FormTrainCollection";
Text = "FormTrainCollection";
panel1.ResumeLayout(false);
panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -129,5 +216,12 @@
private Button ButtonRefreshCollection;
private Button ButtonRemoveTrain;
private PictureBox pictureBoxCollection;
private Label label;
private Label label2;
private Panel panel2;
private ListBox listBoxStorages;
private Button ButtonDelObject;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
}
}

View File

@ -15,56 +15,144 @@ namespace RoadTrain
{
public partial class FormTrainCollection : Form
{
private readonly RoadTrainGenericCollection<DrawningRoadTrain,
DrawningObjectTrain> _trains;
private readonly RoadTrainGenericStorage _storage;
public FormTrainCollection()
{
InitializeComponent();
_trains = new RoadTrainGenericCollection<DrawningRoadTrain,
DrawningObjectTrain>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new RoadTrainGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
private void ButtonAddTrain_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormRoadTrain form = new();
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
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveTrain_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(InputTextBox.Text);
if (_trains - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _trains.ShowTrains();
pictureBoxCollection.Image = obj.ShowTrains();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _trains.ShowTrains();
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowTrains();
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
private void ListBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains();
}
}
}

View File

@ -70,7 +70,7 @@ namespace RoadTrain.Generics
public static T? operator -(RoadTrainGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
@ -84,7 +84,7 @@ namespace RoadTrain.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -126,14 +126,13 @@ namespace RoadTrain.Generics
{
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
DrawningRoadTrain? tank = _collection.Get(i);
if (tank == null)
DrawningRoadTrain? train = _collection[i];
if (train == null)
continue;
tank.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight);
tank.DrawTransport(g);
train.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight);
train.DrawTransport(g);
}
}
}

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoadTrain.DrawningObjects;
using RoadTrain.MovementStrategy;
namespace RoadTrain.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class RoadTrainGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, RoadTrainGenericCollection<DrawningRoadTrain,
DrawningObjectTrain>> _trainStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _trainStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public RoadTrainGenericStorage(int pictureWidth, int pictureHeight)
{
_trainStorages = new Dictionary<string,
RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_trainStorages.ContainsKey(name)) return;
_trainStorages[name] = new RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_trainStorages.ContainsKey(name)) _trainStorages.Remove(name);
return;
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>?
this[string ind]
{
get
{
if (_trainStorages.ContainsKey(ind)) return _trainStorages[ind];
return null;
}
}
}
}

View File

@ -12,35 +12,28 @@ namespace RoadTrain.Generics
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?> (count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T train)
{
for (int i = Count - 1; i >= 1; i--)
{
if (_places[i - 1] == null)
continue;
_places[i] = _places[i - 1];
}
_places[0] = train;
return 0;
return Insert(train, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -50,16 +43,12 @@ namespace RoadTrain.Generics
/// <returns></returns>
public int Insert(T train, int position)
{
if ((position < 0) && (position > Count)) return -1;
if (_places[position] != null)
{
for (int i = Count - 1; i >= position; i--)
{
if (_places[i - 1] != null)
_places[i] = _places[i - 1];
}
}
_places[position] = train;
if (position < 0 || position >= _maxCount)
return -1;
if (Count >= _maxCount)
return -1;
_places.Insert(position, train);
return position;
}
/// <summary>
@ -69,7 +58,7 @@ namespace RoadTrain.Generics
/// <returns></returns>
public bool Remove(int position)
{
if ((position < 0) || (position > Count)) return false;
if ((position < 0) || (position > _maxCount)) return false;
_places[position] = null;
return true;
}
@ -78,11 +67,31 @@ namespace RoadTrain.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
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 > _maxCount)) return null;
return _places[position];
}
set
{
if ((position < 0) || (position > _maxCount)) return;
_places[position] = value;
}
}
}
public IEnumerable<T?> GetTrains(int? maxTrains = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTrains.HasValue && i == maxTrains.Value)
{
yield break;
}
}
}
}
}