вроде готовая лаба 4

This commit is contained in:
a.puchkina 2023-11-06 20:19:29 +04:00
parent 5ac338e378
commit 3e922ab4dd
5 changed files with 337 additions and 81 deletions

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

@ -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;
}
}
}
}
}