lab4 is done

This commit is contained in:
vladimir_zinovev 2023-11-14 23:35:11 +04:00
parent 2cf2804e6e
commit 3a5e2daaf1
5 changed files with 328 additions and 79 deletions

View File

@ -30,6 +30,11 @@
{ {
this.components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
this.ArmoredTransportCollectionFormToolsGroup = new System.Windows.Forms.GroupBox(); this.ArmoredTransportCollectionFormToolsGroup = new System.Windows.Forms.GroupBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonDelObject = new System.Windows.Forms.Button();
this.buttonAddObject = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveArmoredTransport = new System.Windows.Forms.Button(); this.ButtonRemoveArmoredTransport = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); this.maskedTextBoxNumber = new System.Windows.Forms.TextBox();
@ -37,6 +42,7 @@
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.ArmoredTransportCollectionFormToolsGroup.SuspendLayout(); this.ArmoredTransportCollectionFormToolsGroup.SuspendLayout();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -44,6 +50,7 @@
// //
this.ArmoredTransportCollectionFormToolsGroup.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.ArmoredTransportCollectionFormToolsGroup.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.groupBox1);
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRefreshCollection); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRefreshCollection);
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRemoveArmoredTransport); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRemoveArmoredTransport);
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.maskedTextBoxNumber); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.maskedTextBoxNumber);
@ -55,10 +62,65 @@
this.ArmoredTransportCollectionFormToolsGroup.TabStop = false; this.ArmoredTransportCollectionFormToolsGroup.TabStop = false;
this.ArmoredTransportCollectionFormToolsGroup.Text = "Инструменты"; this.ArmoredTransportCollectionFormToolsGroup.Text = "Инструменты";
// //
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.textBoxStorageName);
this.groupBox1.Controls.Add(this.listBoxStorages);
this.groupBox1.Controls.Add(this.buttonDelObject);
this.groupBox1.Controls.Add(this.buttonAddObject);
this.groupBox1.Location = new System.Drawing.Point(22, 38);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(391, 434);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Наборы";
//
// textBoxStorageName
//
this.textBoxStorageName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxStorageName.Location = new System.Drawing.Point(19, 38);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(353, 39);
this.textBoxStorageName.TabIndex = 3;
//
// listBoxStorages
//
this.listBoxStorages.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 32;
this.listBoxStorages.Location = new System.Drawing.Point(19, 165);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(353, 196);
this.listBoxStorages.TabIndex = 2;
this.listBoxStorages.Click += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
//
// buttonDelObject
//
this.buttonDelObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDelObject.Location = new System.Drawing.Point(19, 374);
this.buttonDelObject.Name = "buttonDelObject";
this.buttonDelObject.Size = new System.Drawing.Size(353, 46);
this.buttonDelObject.TabIndex = 1;
this.buttonDelObject.Text = "Удалить набор";
this.buttonDelObject.UseVisualStyleBackColor = true;
this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// buttonAddObject
//
this.buttonAddObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAddObject.Location = new System.Drawing.Point(19, 98);
this.buttonAddObject.Name = "buttonAddObject";
this.buttonAddObject.Size = new System.Drawing.Size(353, 46);
this.buttonAddObject.TabIndex = 0;
this.buttonAddObject.Text = "Добавить набор";
this.buttonAddObject.UseVisualStyleBackColor = true;
this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// ButtonRefreshCollection // ButtonRefreshCollection
// //
this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 394); this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 773);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(391, 47); this.ButtonRefreshCollection.Size = new System.Drawing.Size(391, 47);
this.ButtonRefreshCollection.TabIndex = 3; this.ButtonRefreshCollection.TabIndex = 3;
@ -69,7 +131,7 @@
// ButtonRemoveArmoredTransport // ButtonRemoveArmoredTransport
// //
this.ButtonRemoveArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonRemoveArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 251); this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 641);
this.ButtonRemoveArmoredTransport.Name = "ButtonRemoveArmoredTransport"; this.ButtonRemoveArmoredTransport.Name = "ButtonRemoveArmoredTransport";
this.ButtonRemoveArmoredTransport.Size = new System.Drawing.Size(391, 82); this.ButtonRemoveArmoredTransport.Size = new System.Drawing.Size(391, 82);
this.ButtonRemoveArmoredTransport.TabIndex = 2; this.ButtonRemoveArmoredTransport.TabIndex = 2;
@ -80,15 +142,17 @@
// maskedTextBoxNumber // maskedTextBoxNumber
// //
this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 188); this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 589);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(275, 39); this.maskedTextBoxNumber.Size = new System.Drawing.Size(275, 39);
this.maskedTextBoxNumber.TabIndex = 1; this.maskedTextBoxNumber.TabIndex = 1;
this.maskedTextBoxNumber.Tag = "";
this.maskedTextBoxNumber.Text = "_";
// //
// ButtonAddArmoredTransport // ButtonAddArmoredTransport
// //
this.ButtonAddArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonAddArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 88); this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 492);
this.ButtonAddArmoredTransport.Name = "ButtonAddArmoredTransport"; this.ButtonAddArmoredTransport.Name = "ButtonAddArmoredTransport";
this.ButtonAddArmoredTransport.Size = new System.Drawing.Size(391, 82); this.ButtonAddArmoredTransport.Size = new System.Drawing.Size(391, 82);
this.ButtonAddArmoredTransport.TabIndex = 0; this.ButtonAddArmoredTransport.TabIndex = 0;
@ -123,6 +187,8 @@
this.Text = "ArmoredTransportCollectionForm"; this.Text = "ArmoredTransportCollectionForm";
this.ArmoredTransportCollectionFormToolsGroup.ResumeLayout(false); this.ArmoredTransportCollectionFormToolsGroup.ResumeLayout(false);
this.ArmoredTransportCollectionFormToolsGroup.PerformLayout(); this.ArmoredTransportCollectionFormToolsGroup.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -138,5 +204,10 @@
private Button ButtonAddArmoredTransport; private Button ButtonAddArmoredTransport;
private ContextMenuStrip contextMenuStrip1; private ContextMenuStrip contextMenuStrip1;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private GroupBox groupBox1;
private TextBox textBoxStorageName;
private ListBox listBoxStorages;
private Button buttonDelObject;
private Button buttonAddObject;
} }
} }

View File

@ -1,34 +1,92 @@
using System; using ProjectTank.DrawningObjects;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectTank.DrawningObjects;
using ProjectTank.Generics; using ProjectTank.Generics;
using ProjectTank.MovementStrategy; using ProjectTank.MovementStrategy;
namespace ProjectTank namespace ProjectTank
{ {
/// <summary> /// <summary>
/// Форма для работы с набором объектов класса DrawningCar /// Форма для работы с набором объектов класса DrawningArmoredTransport
/// </summary> /// </summary>
public partial class ArmoredTransportCollectionForm : Form public partial class ArmoredTransportCollectionForm : Form
{ {
/// <summary> /// <summary>
/// Набор объектов /// Набор объектов
/// </summary> /// </summary>
private readonly ArmoredTransportsGenericCollection<DrawningArmoredTransport, DrawningObjectArmoredTransport> _armoredTransports; private readonly ArmoredTransportsGenericStorage _storage;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public ArmoredTransportCollectionForm() public ArmoredTransportCollectionForm()
{ {
InitializeComponent(); InitializeComponent();
_armoredTransports = new ArmoredTransportsGenericCollection<DrawningArmoredTransport,DrawningObjectArmoredTransport>(pictureBoxCollection.Width, pictureBoxCollection.Height); _storage = new ArmoredTransportsGenericStorage(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;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowArmoredTransports();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
@ -37,13 +95,22 @@ namespace ProjectTank
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonAddArmoredTransport_Click(object sender, EventArgs e) private void ButtonAddArmoredTransport_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString()??string.Empty];
if (obj == null)
{
return;
}
TankForm form = new(); TankForm form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_armoredTransports + form.SelectedArmoredTransport != null) if (obj + form.SelectedArmoredTransport)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); pictureBoxCollection.Image = obj.ShowArmoredTransports();
} }
else else
{ {
@ -58,16 +125,24 @@ namespace ProjectTank
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRemoveArmoredTransport_Click(object sender, EventArgs e) private void ButtonRemoveArmoredTransport_Click(object sender, EventArgs e)
{ {
if (MessageBox.Show("Удалить объект?", "Удаление", if (listBoxStorages.SelectedIndex == -1)
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxNumber.Text); int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_armoredTransports - pos != null) if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); pictureBoxCollection.Image = obj.ShowArmoredTransports();
} }
else else
{ {
@ -82,7 +157,16 @@ namespace ProjectTank
private void ButtonRefreshCollection_Click(object sender, EventArgs private void ButtonRefreshCollection_Click(object sender, EventArgs
e) e)
{ {
pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowArmoredTransports();
} }
} }
} }

View File

@ -17,6 +17,10 @@ namespace ProjectTank.Generics
where T : DrawningArmoredTransport where T : DrawningArmoredTransport
where U : IMoveableObject where U : IMoveableObject
{ {
/// <summary>
/// Коэффициент изменения ширины парковки
/// </summary>
readonly int koefWidth = 2;
/// <summary> /// <summary>
/// Ширина окна прорисовки /// Ширина окна прорисовки
/// </summary> /// </summary>
@ -56,13 +60,13 @@ namespace ProjectTank.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
public static int operator +(ArmoredTransportsGenericCollection<T, U> collect, T? obj) public static bool operator +(ArmoredTransportsGenericCollection<T, U> collect, T? obj)
{ {
if (obj == null || collect == null) if (obj == null || collect == null)
{ {
return -1; return false;
} }
return collect._collection.Insert(obj); return collect?._collection.Insert(obj)??false;
} }
/// <summary> /// <summary>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания
@ -70,14 +74,14 @@ namespace ProjectTank.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="pos"></param> /// <param name="pos"></param>
/// <returns></returns> /// <returns></returns>
public static bool operator -(ArmoredTransportsGenericCollection<T, U> collect, int pos) public static T? operator -(ArmoredTransportsGenericCollection<T, U> collect, int pos)
{ {
T? obj = collect?._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null && collect != null) if (obj != null)
{ {
return collect._collection.Remove(pos); collect._collection.Remove(pos);
} }
return false; return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
@ -86,7 +90,7 @@ namespace ProjectTank.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -107,7 +111,7 @@ namespace ProjectTank.Generics
private void DrawBackground(Graphics g) private void DrawBackground(Graphics g)
{ {
Pen pen = new(Color.Black, 3); Pen pen = new(Color.Black, 3);
for (int i = 0; i < (_pictureWidth / _placeSizeWidth); i++) //изменение ширины парковки for (int i = 0; i < (_pictureWidth / _placeSizeWidth)/koefWidth; i++) //изменение ширины парковки
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{ {
@ -128,18 +132,19 @@ namespace ProjectTank.Generics
/// <param name="g"></param> /// <param name="g"></param>
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
int width = (_pictureWidth / _placeSizeWidth); int width = (_pictureWidth / _placeSizeWidth) / 2;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++) int i = 0;
foreach (var armoredTransport in _collection.GetArmoredTrasports())
{ {
DrawningArmoredTransport? armoredTransport = _collection.Get(i);
if (armoredTransport != null) if (armoredTransport != null)
{ {
int row = height - 1 - (i / width); int row = height - 1 - (i / width);
int col = width - 1 - (i % width); int col = width - 1 - (i % width);
armoredTransport.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); armoredTransport.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight);
armoredTransport.DrawTransport(g); armoredTransport.DrawTransport(g);
} }
i++;
} }
} }
} }

View File

@ -0,0 +1,76 @@
using System;
using ProjectTank.DrawningObjects;
using ProjectTank.MovementStrategy;
namespace ProjectTank.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class ArmoredTransportsGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, ArmoredTransportsGenericCollection<DrawningArmoredTransport, DrawningObjectArmoredTransport>> _armoredTransportStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _armoredTransportStorages.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 ArmoredTransportsGenericStorage(int pictureWidth, int pictureHeight)
{
_armoredTransportStorages = new Dictionary<string, ArmoredTransportsGenericCollection<DrawningArmoredTransport, DrawningObjectArmoredTransport>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
_armoredTransportStorages.Add(name, new ArmoredTransportsGenericCollection<DrawningArmoredTransport, DrawningObjectArmoredTransport>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_armoredTransportStorages.ContainsKey(name))
{
return;
}
_armoredTransportStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public ArmoredTransportsGenericCollection<DrawningArmoredTransport, DrawningObjectArmoredTransport>? this[string ind]
{
get
{
if (_armoredTransportStorages.ContainsKey(ind))
{
return _armoredTransportStorages[ind];
}
return null;
}
}
}
}

View File

@ -6,67 +6,59 @@ using System.Threading.Tasks;
namespace ProjectTank.Generics namespace ProjectTank.Generics
{ {
/// <summary> /// <summary>
/// Параметризованный набор объектов /// Параметризованный набор объектов
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
internal class SetGeneric<T> internal class SetGeneric<T>
where T : class where T : class
{ {
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Список объектов, которые храним
/// </summary> /// </summary>
private readonly T?[] _places; private readonly List<T?> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в массиве
/// </summary> /// </summary>
public int Count => _places.Length; public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="armoredTransport">Добавляемый автомобиль</param> /// <param name="armoredTransport">Добавляемый броне-транспорт</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T armoredTransport) public bool Insert(T armoredTransport)
{ {
if (_places[Count - 1] != null) if (_places.Count == _maxCount)
{ return false;
return -1; Insert(armoredTransport, 0);
} return true;
else
{
return Insert(armoredTransport,0);
}
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
/// </summary> /// </summary>
/// <param name="car">Добавляемый автомобиль</param> /// <param name="car">Добавляемый броне-транспорт</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T armoredTransport, int position) public bool Insert(T armoredTransport, int position)
{ {
if (!(position >= 0 && position < Count)) if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
return -1; return false;
if (_places[position] != null) _places.Insert(position, armoredTransport);
{ return true;
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = armoredTransport;
return position;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -75,11 +67,9 @@ namespace ProjectTank.Generics
/// <returns></returns> /// <returns></returns>
public bool Remove(int position) public bool Remove(int position)
{ {
if (!(position >= 0 && position < Count) || _places[position] == null) if (!(position >= 0 && position < Count))
{
return false; return false;
} _places.RemoveAt(position);
_places[position] = null;
return true; return true;
} }
/// <summary> /// <summary>
@ -87,13 +77,36 @@ namespace ProjectTank.Generics
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public T? Get(int position) public T? this[int position]
{ {
if (!(position >= 0 && position < Count)) get
{ {
return null; if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetArmoredTrasports(int? maxArmoredTransport = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxArmoredTransport.HasValue && i == maxArmoredTransport.Value)
{
yield break;
}
} }
return _places[position];
} }
} }
} }