Петрушин Егор ПИбд-22 Лабораторная работа №4 "Самоходная артиллерийская установка" #9

Closed
Egor_Petrushin wants to merge 5 commits from PIbd-22_Petrushin_E.A._Lab4 into PIbd-22_Petrushin_E.A._Lab3
5 changed files with 337 additions and 56 deletions

View File

@ -33,7 +33,15 @@
ButtonAddSPAU = new Button();
ButtonRemoveSPAU = new Button();
ButtonRefreshCollection = new Button();
panel1 = new Panel();
panel2 = new Panel();
listBoxStorages = new ListBox();
ButtonDelObject = new Button();
ButtonAddObject = new Button();
textBoxStorageName = new TextBox();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panel1.SuspendLayout();
panel2.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
@ -46,14 +54,14 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(729, 51);
maskedTextBoxNumber.Location = new Point(18, 71);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(143, 27);
maskedTextBoxNumber.TabIndex = 1;
//
// ButtonAddSPAU
//
ButtonAddSPAU.Location = new Point(748, 102);
ButtonAddSPAU.Location = new Point(44, 19);
ButtonAddSPAU.Name = "ButtonAddSPAU";
ButtonAddSPAU.Size = new Size(102, 33);
ButtonAddSPAU.TabIndex = 2;
@ -63,7 +71,7 @@
//
// ButtonRemoveSPAU
//
ButtonRemoveSPAU.Location = new Point(750, 141);
ButtonRemoveSPAU.Location = new Point(44, 118);
ButtonRemoveSPAU.Name = "ButtonRemoveSPAU";
ButtonRemoveSPAU.Size = new Size(100, 37);
ButtonRemoveSPAU.TabIndex = 3;
@ -73,7 +81,7 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(750, 195);
ButtonRefreshCollection.Location = new Point(44, 172);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(100, 35);
ButtonRefreshCollection.TabIndex = 4;
@ -81,21 +89,81 @@
ButtonRefreshCollection.UseVisualStyleBackColor = true;
ButtonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// panel1
//
panel1.Controls.Add(ButtonRefreshCollection);
panel1.Controls.Add(ButtonRemoveSPAU);
panel1.Controls.Add(ButtonAddSPAU);
panel1.Controls.Add(maskedTextBoxNumber);
panel1.Location = new Point(711, 217);
panel1.Name = "panel1";
panel1.Size = new Size(176, 227);
panel1.TabIndex = 5;
//
// panel2
//
panel2.Controls.Add(listBoxStorages);
panel2.Controls.Add(ButtonDelObject);
panel2.Controls.Add(ButtonAddObject);
panel2.Controls.Add(textBoxStorageName);
panel2.Location = new Point(711, 12);
panel2.Name = "panel2";
panel2.Size = new Size(174, 198);
panel2.TabIndex = 6;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(18, 87);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(143, 64);
listBoxStorages.TabIndex = 7;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(18, 168);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(143, 28);
ButtonDelObject.TabIndex = 3;
ButtonDelObject.Text = "Удалить набор";
ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(18, 53);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(143, 28);
ButtonAddObject.TabIndex = 1;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(18, 20);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(143, 27);
textBoxStorageName.TabIndex = 0;
//
// FormSPAUCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(899, 456);
Controls.Add(ButtonRefreshCollection);
Controls.Add(ButtonRemoveSPAU);
Controls.Add(ButtonAddSPAU);
Controls.Add(maskedTextBoxNumber);
Controls.Add(panel2);
Controls.Add(panel1);
Controls.Add(pictureBoxCollection);
Name = "FormSPAUCollection";
Text = "FormSPAUCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
panel1.ResumeLayout(false);
panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -105,5 +173,11 @@
private Button ButtonAddSPAU;
private Button ButtonRemoveSPAU;
private Button ButtonRefreshCollection;
private Panel panel1;
private Panel panel2;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
private Button ButtonDelObject;
private ListBox listBoxStorages;
}
}

View File

@ -23,14 +23,81 @@ namespace SelfPropelledArtilleryUnit
/// <summary>
/// Набор объектов
/// </summary>
private readonly SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU> _SPAUs;
//private readonly SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU> _SPAUs;
private readonly SPAUGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormSPAUCollection()
{
InitializeComponent();
_SPAUs = new SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new SPAUGenericStorage(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 listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowSPAUs();
}
/// <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>
/// Добавление объекта в набор
@ -39,15 +106,24 @@ namespace SelfPropelledArtilleryUnit
/// <param name="e"></param>
private void ButtonAddSPAU_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormSPAU form = new();
if (form.ShowDialog() == DialogResult.OK)
{
int addedIndex = _SPAUs + form.SelectedSPAU;
int addedIndex = obj + form.SelectedSPAU;
if (addedIndex != -1 && addedIndex <= countPlaces)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _SPAUs.ShowSPAUs();
pictureBoxCollection.Image = obj.ShowSPAUs();
}
else
{
@ -55,6 +131,8 @@ namespace SelfPropelledArtilleryUnit
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
@ -62,24 +140,35 @@ namespace SelfPropelledArtilleryUnit
/// <param name="e"></param>
private void ButtonRemoveSPAU_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)
{
return;
}
int pos;
try {
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
try
{
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
}
catch {
catch
{
MessageBox.Show("Не удалось удалить объект");
return;
}
if (_SPAUs - pos)
if (obj - pos)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _SPAUs.ShowSPAUs();
pictureBoxCollection.Image = obj.ShowSPAUs();
}
else
{
@ -93,7 +182,19 @@ namespace SelfPropelledArtilleryUnit
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _SPAUs.ShowSPAUs();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowSPAUs();
}
}
}

View File

@ -72,7 +72,7 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <returns></returns>
public static bool operator -(SPAUGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
@ -90,7 +90,7 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -135,8 +135,8 @@ namespace SelfPropelledArtilleryUnit.Generics
DrawningSPAU current;
for (int i = 0; i < _collection.Count; i++)
{
current = _collection.Get(i);
current?.SetPosition(stringCount * 200, 280 - j * 100);
current = _collection[i];
current?.SetPosition(stringCount * _placeSizeWidth, 280 - j * _placeSizeHeight);
stringCount++;
if (stringCount >= 3)
{

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SelfPropelledArtilleryUnit.DrawningObjects;
using SelfPropelledArtilleryUnit.MovementStrategy;
namespace SelfPropelledArtilleryUnit.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class SPAUGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>> _SPAUStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _SPAUStorages.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 SPAUGenericStorage(int pictureWidth, int pictureHeight)
{
_SPAUStorages = new Dictionary<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
try
Review

Простые проверки, никаких try-catch

Простые проверки, никаких try-catch
{
_SPAUStorages.Add(name, new SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>(_pictureWidth, _pictureHeight));
}
catch (Exception) { return; }
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
try { _SPAUStorages.Remove(name); } catch (Exception) { return; }
Review

Простые проверки, никаких try-catch

Простые проверки, никаких try-catch
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>? this[string ind]
{
get
{
if (_SPAUStorages.ContainsKey(ind))
{
return _SPAUStorages[ind];
}
else
{
return null;
}
}
}
}
}

View File

@ -1,4 +1,6 @@
using System;
using SelfPropelledArtilleryUnit.DrawningObjects;
using SelfPropelledArtilleryUnit.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
@ -17,18 +19,23 @@ namespace SelfPropelledArtilleryUnit.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>
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>
/// Добавление объекта в набор
@ -47,35 +54,25 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <returns></returns>
public int Insert(T spau, int position)
{
if (position < 0 || spau == null)
{
if (Count == _maxCount)
return -1;
}
if (position >= Count)
{
return -1;
}
// Ищем первую пустую позицию начиная с указанной позиции
int positionNull = Array.FindIndex(_places, position, x => x == null);
if (positionNull == -1 && _places[Count - 1] != null)
{
// Если пустых позиций нет и последняя позиция в массиве занята
if (position < 0 || spau == null)
return -1;
}
else if (positionNull == -1)
if (position >= _maxCount)
return -1;
if (Count == 0)
{
// Если позиция для вставки пустая, а пустых позиций больше нет
positionNull = Count - 1;
_places.Add(spau);
}
// Сдвигаем элементы вправо, начиная с первой пустой позиции и заканчивая указанной позицией
for (int i = positionNull; i > position; i--)
else
{
_places[i] = _places[i - 1];
_places.Insert(position, spau);
}
// Вставка по позиции
_places[position] = spau;
return position;
return Count;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -87,7 +84,7 @@ namespace SelfPropelledArtilleryUnit.Generics
if (position < 0 || position >= Count)
return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
@ -96,12 +93,37 @@ namespace SelfPropelledArtilleryUnit.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;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetSPAUs(int? maxSPAUs = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxSPAUs.HasValue && i == maxSPAUs.Value)
{
yield break;
}
}
}
}
}