PIbd-22 Puchkina A.A. LabWork_04 #4

Closed
a.puchkina wants to merge 3 commits from LabWork_4 into LabWork_3
10 changed files with 354 additions and 111 deletions

View File

@ -67,14 +67,8 @@ namespace AirplaneWithRadar
{
bodyColor = dialog.Color;
}
Color additionalColor = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
additionalColor = dialog.Color;
}
_drawningAirplane = new DrawningAirplane(random.Next(100, 300), random.Next(1000, 3000),
bodyColor, additionalColor,
bodyColor,
pictureBoxAirplane.Width, pictureBoxAirplane.Height);
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10,
100));

View File

@ -56,13 +56,13 @@ namespace AirplaneWithRadar.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(AirplanesGenericCollection<T, U> collect, T? obj)
public static bool operator +(AirplanesGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect?._collection.Insert(obj) ?? -1;
return (bool)collect?._collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
@ -73,7 +73,7 @@ namespace AirplaneWithRadar.Generics
public static bool operator -(AirplanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
@ -87,7 +87,7 @@ namespace AirplaneWithRadar.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -126,25 +126,18 @@ namespace AirplaneWithRadar.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
T? t;
int Rows = _pictureWidth / _placeSizeWidth;
int diff = 1;
int currWidth = 0;
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var airplane in _collection.GetAirplanes())
{
currWidth++;
if (currWidth > Rows)
if (airplane!= null)
{
diff++;
currWidth = 1;
}
t = _collection.Get(i);
if (t != null)
{
t.SetPosition((Rows - 1 - (i % Rows)) * _placeSizeWidth, i / Rows * _placeSizeHeight);
t.DrawTransport(g);
airplane.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (airplane is DrawningAirplaneWithRadar)
(airplane as DrawningAirplaneWithRadar).DrawTransport(g);
else airplane.DrawTransport(g);
airplane.DrawTransport(g);
}
i++;
}
}
}

View File

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

View File

@ -52,12 +52,10 @@ namespace AirplaneWithRadar.DrawningObjects
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет фюзеляжа</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public DrawningAirplane(int speed, double weight, Color bodyColor, Color
additionalColor, int width, int height)
public DrawningAirplane(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
@ -65,8 +63,7 @@ namespace AirplaneWithRadar.DrawningObjects
{
return;
}
EntityAirplane = new EntityAirplane(speed, weight, bodyColor,
additionalColor);
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор
@ -78,7 +75,7 @@ namespace AirplaneWithRadar.DrawningObjects
/// <param name="height">Высота картинки</param>
/// <param name="airplaneWidth">Ширина прорисовки автомобиля</param>
/// <param name="airplaneHeight">Высота прорисовки автомобиля</param>
protected DrawningAirplane(int speed, double weight, Color bodyColor, Color additionalColor, int
protected DrawningAirplane(int speed, double weight, Color bodyColor, int
width, int height, int airplaneWidth, int airplaneHeight)
{
_pictureWidth = width;
@ -89,7 +86,7 @@ namespace AirplaneWithRadar.DrawningObjects
{
return;
}
EntityAirplane = new EntityAirplane(speed, weight, bodyColor, additionalColor);
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
/// <summary>
/// Установка позиции
@ -194,7 +191,6 @@ namespace AirplaneWithRadar.DrawningObjects
return;
}
Pen penBlack = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor);
//фюзеляж
@ -202,7 +198,7 @@ namespace AirplaneWithRadar.DrawningObjects
g.DrawRectangle(penBlack, _startPosX + 4, _startPosY + 40, 150, 30);
//киль
g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) });
g.FillPolygon(bodyBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) });
g.DrawPolygon(penBlack, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) });
//стабилизатор и крыло
@ -225,7 +221,7 @@ namespace AirplaneWithRadar.DrawningObjects
//кабина
Brush brLightBlue = new SolidBrush(Color.LightBlue);
Pen penBlue = new Pen(Color.CadetBlue);
g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 72) });
g.FillPolygon(bodyBrush, new Point[] { new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 72) });
g.FillPolygon(brLightBlue, new Point[] { new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 38), new Point(_startPosX + 190, _startPosY + 55) });
g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 38));
g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 38), new Point(_startPosX + 190, _startPosY + 55));

View File

@ -24,7 +24,7 @@ namespace AirplaneWithRadar.DrawningObjects
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color
additionalColor, bool radar, bool tank, bool pin, int width, int height) :
base(speed, weight, bodyColor, additionalColor, width, height)
base(speed, weight, bodyColor, width, height, 200, 85)
{
if (EntityAirplane != null)
{
@ -39,8 +39,7 @@ namespace AirplaneWithRadar.DrawningObjects
return;
}
Pen penBlack = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor);
Brush additionalBrush = new SolidBrush(airplaneWithRadar.AdditionalColor);
Pen penGray = new Pen(Color.Gray);
// радар

View File

@ -24,10 +24,6 @@ namespace AirplaneWithRadar.Entities
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Шаг перемещения самолета
/// </summary>
public double Step => (double)Speed * 100 / Weight;
@ -37,14 +33,11 @@ namespace AirplaneWithRadar.Entities
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолета</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
public EntityAirplane(int speed, double weight, Color bodyColor, Color
additionalColor)
public EntityAirplane(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
}
}

View File

@ -11,6 +11,10 @@ namespace AirplaneWithRadar.Entities
/// </summary>
public class EntityAirplaneWithRadar : EntityAirplane
{
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличия радара
/// </summary>
@ -33,9 +37,10 @@ namespace AirplaneWithRadar.Entities
/// <param name="radar">Признак наличия радара</param>
/// <param name="tank">Признак наличия доп.бака</param>
/// <param name="pin">Признак наличия штыря</param>
public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color
additionalColor, bool radar, bool tank, bool pin) : base (speed, weight, bodyColor, additionalColor)
public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor,
Color additionalColor, bool radar, bool tank, bool pin) : base (speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Radar = radar;
Tank = tank;
Pin = pin;

View File

@ -29,43 +29,102 @@
private void InitializeComponent()
{
groupBoxInstruments = new GroupBox();
groupBoxSets = new GroupBox();
buttonDeleteSet = new Button();
listBoxStorages = new ListBox();
buttonAddInSet = new Button();
textBoxStorageName = new TextBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonUpdate = new Button();
buttonAdd = new Button();
buttonDelete = new Button();
pictureBoxCollection = new PictureBox();
groupBoxInstruments.SuspendLayout();
groupBoxSets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// groupBoxInstruments
//
groupBoxInstruments.Controls.Add(groupBoxSets);
groupBoxInstruments.Controls.Add(maskedTextBoxNumber);
groupBoxInstruments.Controls.Add(buttonUpdate);
groupBoxInstruments.Controls.Add(buttonAdd);
groupBoxInstruments.Controls.Add(buttonDelete);
groupBoxInstruments.Dock = DockStyle.Right;
groupBoxInstruments.Location = new Point(699, 0);
groupBoxInstruments.Margin = new Padding(2, 2, 2, 2);
groupBoxInstruments.Location = new Point(707, 0);
groupBoxInstruments.Margin = new Padding(2);
groupBoxInstruments.Name = "groupBoxInstruments";
groupBoxInstruments.Padding = new Padding(2, 2, 2, 2);
groupBoxInstruments.Size = new Size(170, 497);
groupBoxInstruments.Padding = new Padding(2);
groupBoxInstruments.Size = new Size(214, 570);
groupBoxInstruments.TabIndex = 5;
groupBoxInstruments.TabStop = false;
groupBoxInstruments.Text = "Инструменты";
//
// groupBoxSets
//
groupBoxSets.Controls.Add(buttonDeleteSet);
groupBoxSets.Controls.Add(listBoxStorages);
groupBoxSets.Controls.Add(buttonAddInSet);
groupBoxSets.Controls.Add(textBoxStorageName);
groupBoxSets.Location = new Point(14, 33);
groupBoxSets.Name = "groupBoxSets";
groupBoxSets.Size = new Size(182, 313);
groupBoxSets.TabIndex = 6;
groupBoxSets.TabStop = false;
groupBoxSets.Text = "Наборы";
//
// buttonDeleteSet
//
buttonDeleteSet.Location = new Point(9, 223);
buttonDeleteSet.Margin = new Padding(2);
buttonDeleteSet.Name = "buttonDeleteSet";
buttonDeleteSet.Size = new Size(166, 30);
buttonDeleteSet.TabIndex = 9;
buttonDeleteSet.Text = "Удалить набор";
buttonDeleteSet.UseVisualStyleBackColor = true;
buttonDeleteSet.Click += buttonDeleteSet_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(14, 111);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(152, 84);
listBoxStorages.TabIndex = 8;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// buttonAddInSet
//
buttonAddInSet.Location = new Point(9, 65);
buttonAddInSet.Margin = new Padding(2);
buttonAddInSet.Name = "buttonAddInSet";
buttonAddInSet.Size = new Size(166, 30);
buttonAddInSet.TabIndex = 7;
buttonAddInSet.Text = "Добавить в набор";
buttonAddInSet.UseVisualStyleBackColor = true;
buttonAddInSet.Click += buttonAddInSet_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(9, 33);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(162, 27);
textBoxStorageName.TabIndex = 0;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(31, 142);
maskedTextBoxNumber.Margin = new Padding(2, 2, 2, 2);
maskedTextBoxNumber.Location = new Point(51, 425);
maskedTextBoxNumber.Margin = new Padding(2);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(119, 27);
maskedTextBoxNumber.TabIndex = 5;
//
// buttonUpdate
//
buttonUpdate.Location = new Point(18, 247);
buttonUpdate.Margin = new Padding(2, 2, 2, 2);
buttonUpdate.Location = new Point(38, 509);
buttonUpdate.Margin = new Padding(2);
buttonUpdate.Name = "buttonUpdate";
buttonUpdate.Size = new Size(142, 50);
buttonUpdate.TabIndex = 4;
@ -75,10 +134,10 @@
//
// buttonAdd
//
buttonAdd.Location = new Point(18, 53);
buttonAdd.Margin = new Padding(2, 2, 2, 2);
buttonAdd.Location = new Point(30, 369);
buttonAdd.Margin = new Padding(2);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(142, 30);
buttonAdd.Size = new Size(166, 30);
buttonAdd.TabIndex = 1;
buttonAdd.Text = "Добавить самолет";
buttonAdd.UseVisualStyleBackColor = true;
@ -86,8 +145,8 @@
//
// buttonDelete
//
buttonDelete.Location = new Point(18, 171);
buttonDelete.Margin = new Padding(2, 2, 2, 2);
buttonDelete.Location = new Point(38, 456);
buttonDelete.Margin = new Padding(2);
buttonDelete.Name = "buttonDelete";
buttonDelete.Size = new Size(142, 30);
buttonDelete.TabIndex = 3;
@ -99,9 +158,9 @@
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Margin = new Padding(2, 2, 2, 2);
pictureBoxCollection.Margin = new Padding(2);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(699, 497);
pictureBoxCollection.Size = new Size(707, 570);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxCollection.TabIndex = 6;
pictureBoxCollection.TabStop = false;
@ -110,14 +169,16 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(869, 497);
ClientSize = new Size(921, 570);
Controls.Add(pictureBoxCollection);
Controls.Add(groupBoxInstruments);
Margin = new Padding(2, 2, 2, 2);
Margin = new Padding(2);
Name = "FormAirplaneCollection";
Text = "FormAirplaneCollection";
groupBoxInstruments.ResumeLayout(false);
groupBoxInstruments.PerformLayout();
groupBoxSets.ResumeLayout(false);
groupBoxSets.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
PerformLayout();
@ -130,5 +191,10 @@
private GroupBox groupBoxInstruments;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBoxSets;
private Button buttonDeleteSet;
private ListBox listBoxStorages;
private Button buttonAddInSet;
private TextBox textBoxStorageName;
}
}

View File

@ -21,16 +21,82 @@ namespace AirplaneWithRadar
/// <summary>
/// Набор объектов
/// </summary>
private readonly AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane> _airplanes;
private readonly AirplanesGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormAirplaneCollection()
{
InitializeComponent();
_airplanes = new AirplanesGenericCollection<DrawningAirplane,
DrawningObjectAirplane>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new AirplanesGenericStorage(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 buttonAddInSet_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 listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDeleteSet_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>
/// Добавление объекта в набор
@ -39,13 +105,23 @@ namespace AirplaneWithRadar
/// <param name="e"></param>
private void buttonAdd_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
AirplaneWithRadarForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_airplanes + form.SelectedAirplane != -1)
if (obj + form.SelectedAirplane)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _airplanes.ShowAirplanes();
pictureBoxCollection.Image = obj.ShowAirplanes();
}
else
{
@ -60,21 +136,32 @@ namespace AirplaneWithRadar
/// <param name="e"></param>
private void buttonDelete_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(maskedTextBoxNumber.Text);
if (_airplanes - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _airplanes.ShowAirplanes();
pictureBoxCollection.Image = obj.ShowAirplanes();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
@ -83,7 +170,18 @@ namespace AirplaneWithRadar
/// <param name="e"></param>
private void buttonUpdate_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _airplanes.ShowAirplanes();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowAirplanes();
}
}
}

View File

@ -14,58 +14,51 @@ namespace AirplaneWithRadar.Generics
where T : class
{
/// <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>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый самолет</param>
/// <param name="airplane">Добавляемый самолет</param>
/// <returns></returns>
public int Insert(T airplane)
public bool Insert(T airplane)
{
return Insert(airplane, 0);
if (_places.Count == _maxCount)
{
return false;
}
Insert(airplane, 0);
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="airplane">Добавляемый самолет</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T airplane, int position)
public bool Insert(T airplane, int position)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
{
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] = airplane;
return position;
if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return false;
_places.Insert(position, airplane);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -75,7 +68,7 @@ namespace AirplaneWithRadar.Generics
public bool Remove(int position)
{
if (position < 0 || position >= Count) return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
/// <summary>
@ -83,10 +76,34 @@ namespace AirplaneWithRadar.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 >= 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?> GetAirplanes(int? maxAirplanes = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxAirplanes.HasValue && i == maxAirplanes.Value)
{
yield break;
}
}
}
}
}