Лабораторная работа 4

This commit is contained in:
Efi 2023-12-07 21:24:31 +04:00
parent 04b4e75288
commit 86f46e9568
5 changed files with 305 additions and 83 deletions

View File

@ -28,29 +28,84 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
panel1 = new Panel(); panelCollection = new Panel();
panelObject = new Panel();
textBoxStorageName = new TextBox();
buttonAddObject = new Button();
buttonDelObject = new Button();
listBoxStorages = new ListBox();
Tools = new Label(); Tools = new Label();
buttonRefreshCollection = new Button(); buttonRefreshCollection = new Button();
buttonRemove = new Button(); buttonRemove = new Button();
buttonAdd = new Button(); buttonAdd = new Button();
maskedTextBoxNumber = new MaskedTextBox(); maskedTextBoxNumber = new MaskedTextBox();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
panel1.SuspendLayout(); panelCollection.SuspendLayout();
panelObject.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// panel1 // panelCollection
// //
panel1.Controls.Add(Tools); panelCollection.Controls.Add(panelObject);
panel1.Controls.Add(buttonRefreshCollection); panelCollection.Controls.Add(Tools);
panel1.Controls.Add(buttonRemove); panelCollection.Controls.Add(buttonRefreshCollection);
panel1.Controls.Add(buttonAdd); panelCollection.Controls.Add(buttonRemove);
panel1.Controls.Add(maskedTextBoxNumber); panelCollection.Controls.Add(buttonAdd);
panel1.Dock = DockStyle.Right; panelCollection.Controls.Add(maskedTextBoxNumber);
panel1.Location = new Point(617, 0); panelCollection.Dock = DockStyle.Right;
panel1.Name = "panel1"; panelCollection.Location = new Point(617, 0);
panel1.Size = new Size(183, 450); panelCollection.Name = "panelCollection";
panel1.TabIndex = 0; panelCollection.Size = new Size(183, 450);
panelCollection.TabIndex = 0;
//
// panelObject
//
panelObject.Controls.Add(textBoxStorageName);
panelObject.Controls.Add(buttonAddObject);
panelObject.Controls.Add(buttonDelObject);
panelObject.Controls.Add(listBoxStorages);
panelObject.Location = new Point(6, 27);
panelObject.Name = "panelObject";
panelObject.Size = new Size(171, 214);
panelObject.TabIndex = 4;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(7, 19);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(158, 23);
textBoxStorageName.TabIndex = 2;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(7, 48);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(158, 31);
buttonAddObject.TabIndex = 2;
buttonAddObject.Text = "Добавить объект";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += ButtonAddObject_Click;
//
// buttonDelObject
//
buttonDelObject.Location = new Point(7, 167);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(158, 31);
buttonDelObject.TabIndex = 2;
buttonDelObject.Text = "Удалить объект";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += ButtonDelObject__Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(7, 85);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(158, 64);
listBoxStorages.TabIndex = 2;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
// //
// Tools // Tools
// //
@ -63,7 +118,7 @@
// //
// buttonRefreshCollection // buttonRefreshCollection
// //
buttonRefreshCollection.Location = new Point(6, 179); buttonRefreshCollection.Location = new Point(3, 395);
buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(174, 43); buttonRefreshCollection.Size = new Size(174, 43);
buttonRefreshCollection.TabIndex = 2; buttonRefreshCollection.TabIndex = 2;
@ -73,7 +128,7 @@
// //
// buttonRemove // buttonRemove
// //
buttonRemove.Location = new Point(6, 120); buttonRemove.Location = new Point(3, 336);
buttonRemove.Name = "buttonRemove"; buttonRemove.Name = "buttonRemove";
buttonRemove.Size = new Size(174, 31); buttonRemove.Size = new Size(174, 31);
buttonRemove.TabIndex = 2; buttonRemove.TabIndex = 2;
@ -83,7 +138,7 @@
// //
// buttonAdd // buttonAdd
// //
buttonAdd.Location = new Point(6, 39); buttonAdd.Location = new Point(3, 255);
buttonAdd.Name = "buttonAdd"; buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(174, 31); buttonAdd.Size = new Size(174, 31);
buttonAdd.TabIndex = 2; buttonAdd.TabIndex = 2;
@ -93,7 +148,7 @@
// //
// maskedTextBoxNumber // maskedTextBoxNumber
// //
maskedTextBoxNumber.Location = new Point(6, 91); maskedTextBoxNumber.Location = new Point(3, 307);
maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(174, 23); maskedTextBoxNumber.Size = new Size(174, 23);
maskedTextBoxNumber.TabIndex = 0; maskedTextBoxNumber.TabIndex = 0;
@ -113,23 +168,30 @@
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 450);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(panel1); Controls.Add(panelCollection);
Name = "FormWarAirplaneCollection"; Name = "FormWarAirplaneCollection";
Text = "Набор военных самолётов"; Text = "Набор военных самолётов";
panel1.ResumeLayout(false); panelCollection.ResumeLayout(false);
panel1.PerformLayout(); panelCollection.PerformLayout();
panelObject.ResumeLayout(false);
panelObject.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
#endregion #endregion
private Panel panel1; private Panel panelCollection;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private Label Tools; private Label Tools;
private Button buttonRefreshCollection; private Button buttonRefreshCollection;
private Button buttonRemove; private Button buttonRemove;
private Button buttonAdd; private Button buttonAdd;
private MaskedTextBox maskedTextBoxNumber; private MaskedTextBox maskedTextBoxNumber;
private ListBox listBoxStorages;
private Button buttonAddObject;
private Button buttonDelObject;
private Panel panelObject;
private TextBox textBoxStorageName;
} }
} }

View File

@ -15,21 +15,86 @@ namespace AirBomber
{ {
public partial class FormWarAirplaneCollection : Form public partial class FormWarAirplaneCollection : Form
{ {
private readonly WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane> _warAirplane; private readonly WarAirplaneGenericStorage _storage;
public FormWarAirplaneCollection() public FormWarAirplaneCollection()
{ {
InitializeComponent(); InitializeComponent();
_warAirplane = new WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>(pictureBoxCollection.Width, pictureBoxCollection.Height); _storage = new WarAirplaneGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
} }
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 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 ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowWarAirplane();
}
private void ButtonAddWarAirplane_Click(object sender, EventArgs e) private void ButtonAddWarAirplane_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormAirBomber form = new(); FormAirBomber form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_warAirplane + form.SelectedWarAirplane != null) if (obj + form.SelectedWarAirplane)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); pictureBoxCollection.Image = obj.ShowWarAirplane();
} }
else else
{ {
@ -39,25 +104,45 @@ namespace AirBomber
} }
private void ButtonRemoveWarAirplane_Click(object sender, EventArgs e) private void ButtonRemoveWarAirplane_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("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxNumber.Text); int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_warAirplane - pos != null) if (obj - pos != null)
{ {
MessageBox.Show("Объект удалён"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); pictureBoxCollection.Image = obj.ShowWarAirplane();
} }
else else
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
private void ButtonRefreshCollection_Click(object sender, EventArgs e) private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowWarAirplane();
} }
} }

View File

@ -9,58 +9,80 @@ namespace AirBomber.Generics
internal class SetGeneric<T> internal class SetGeneric<T>
where T : class where T : class
{ {
private readonly T?[] _places; private readonly List<T?> _places;
public int Count => _places.Length; public int Count => _places.Count;
public int startPointer = 0; public int startPointer = 0;
public readonly int _maxCount;
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
public int Insert(T WarAirplane) public bool Insert(T WarAirplane)
{ {
if (_places[Count - 1] != null) if (_places.Count == _maxCount)
return -1; {
return Insert(WarAirplane, 0); return false;
} }
public int Insert(T WarAirplane, int position) Insert(WarAirplane, 0);
{ return true;
if (!(position >= 0 && position < Count))
return -1;
if (_places[position] != null)
{
int indexEnd = position + 1;
while (_places[indexEnd] != null)
{
indexEnd++;
} }
for (int i = indexEnd + 1; i > position; i--) public bool Insert(T WarAirplane, int position)
{ {
_places[i] = _places[i - 1]; if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
{
return false;
} }
} _places.Insert(position, WarAirplane);
_places[position] = WarAirplane; return true;
return position;
} }
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < Count && position >= 0) if (position < 0 || position >= Count)
{ {
_places[position] = null;
return true;
}
return false; return false;
}
_places.RemoveAt(position);
return true;
} }
public T? Get(int position) public T? this[int position]
{ {
if (position < Count && position >= 0) get
{ {
if (position < 0 || position >= _maxCount)
{
return null;
}
return _places[position]; return _places[position];
} }
return null; set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
_places.Insert(position, value);
return;
} }
} }
public IEnumerable<T?> GetWarAirplane(int? maxWarAirplane = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxWarAirplane.HasValue && i == maxWarAirplane.Value)
{
yield break;
}
}
}
}
} }

View File

@ -26,19 +26,19 @@ namespace AirBomber.Generics
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
public static int operator +(WarAirplaneGenericCollection<T, U> collect, T? public static bool operator +(WarAirplaneGenericCollection<T, U> collect, T?
obj) obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect._collection.Insert(obj); return collect?._collection.Insert(obj) ?? false;
} }
public static bool operator -(WarAirplaneGenericCollection<T, U> collect, int public static bool operator -(WarAirplaneGenericCollection<T, U> collect, int
pos) pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
@ -47,7 +47,7 @@ namespace AirBomber.Generics
} }
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
public Bitmap ShowWarAirplane() public Bitmap ShowWarAirplane()
{ {
@ -75,11 +75,8 @@ namespace AirBomber.Generics
} }
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
DrawingWarAirplane warAirplane; int i = 0;
foreach (var warAirplane in _collection.GetWarAirplane()) {
for (int i = 0; i < _collection.Count; i++)
{
warAirplane = _collection.Get(i);
if (warAirplane != null) if (warAirplane != null)
{ {
int numPlacesInRow = _pictureWidth / _placeSizeWidth; int numPlacesInRow = _pictureWidth / _placeSizeWidth;
@ -88,6 +85,7 @@ namespace AirBomber.Generics
warAirplane.DrawTransport(g); warAirplane.DrawTransport(g);
} }
i++;
} }
} }
} }

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.DrawingObjects;
using AirBomber.MovementStrategy;
namespace AirBomber.Generics
{
internal class WarAirplaneGenericStorage
{
readonly Dictionary<string, WarAirplaneGenericCollection<DrawingWarAirplane,
DrawingObjectWarAirplane>> _warAirplaneStorages;
public List<string> Keys => _warAirplaneStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public WarAirplaneGenericStorage(int pictureWidth, int pictureHeight)
{
_warAirplaneStorages = new Dictionary<string, WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_warAirplaneStorages.Add(name, new WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_warAirplaneStorages.ContainsKey(name))
{
return;
}
_warAirplaneStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>? this[string ind]
{
get
{
if (_warAirplaneStorages.ContainsKey(ind))
{
return _warAirplaneStorages[ind];
}
return null;
}
}
}
}