lab4
This commit is contained in:
parent
9b908da61c
commit
9c7884da80
@ -28,35 +28,92 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
groupBoxCollection = new GroupBox();
|
groupBoxCollectionTools = new GroupBox();
|
||||||
|
groupBoxCollectionSets = new GroupBox();
|
||||||
|
textBoxStorageName = new TextBox();
|
||||||
|
buttonDelObject = new Button();
|
||||||
|
buttonAddObject = new Button();
|
||||||
|
listBoxStorages = new ListBox();
|
||||||
maskedTextBoxNumber = new MaskedTextBox();
|
maskedTextBoxNumber = new MaskedTextBox();
|
||||||
buttonRefreshCollection = new Button();
|
buttonRefreshCollection = new Button();
|
||||||
buttonRemoveWarship = new Button();
|
buttonRemoveWarship = new Button();
|
||||||
buttonAddWarship = new Button();
|
buttonAddWarship = new Button();
|
||||||
pictureBoxCollection = new PictureBox();
|
pictureBoxCollection = new PictureBox();
|
||||||
groupBoxCollection.SuspendLayout();
|
groupBoxCollectionTools.SuspendLayout();
|
||||||
|
groupBoxCollectionSets.SuspendLayout();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
// groupBoxCollection
|
// groupBoxCollectionTools
|
||||||
//
|
//
|
||||||
groupBoxCollection.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
|
groupBoxCollectionTools.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
|
||||||
groupBoxCollection.Controls.Add(maskedTextBoxNumber);
|
groupBoxCollectionTools.Controls.Add(groupBoxCollectionSets);
|
||||||
groupBoxCollection.Controls.Add(buttonRefreshCollection);
|
groupBoxCollectionTools.Controls.Add(maskedTextBoxNumber);
|
||||||
groupBoxCollection.Controls.Add(buttonRemoveWarship);
|
groupBoxCollectionTools.Controls.Add(buttonRefreshCollection);
|
||||||
groupBoxCollection.Controls.Add(buttonAddWarship);
|
groupBoxCollectionTools.Controls.Add(buttonRemoveWarship);
|
||||||
groupBoxCollection.Location = new Point(893, 3);
|
groupBoxCollectionTools.Controls.Add(buttonAddWarship);
|
||||||
groupBoxCollection.Margin = new Padding(4, 5, 4, 5);
|
groupBoxCollectionTools.Location = new Point(893, 3);
|
||||||
groupBoxCollection.Name = "groupBoxCollection";
|
groupBoxCollectionTools.Margin = new Padding(4, 5, 4, 5);
|
||||||
groupBoxCollection.Padding = new Padding(4, 5, 4, 5);
|
groupBoxCollectionTools.Name = "groupBoxCollectionTools";
|
||||||
groupBoxCollection.Size = new Size(247, 743);
|
groupBoxCollectionTools.Padding = new Padding(4, 5, 4, 5);
|
||||||
groupBoxCollection.TabIndex = 0;
|
groupBoxCollectionTools.Size = new Size(247, 743);
|
||||||
groupBoxCollection.TabStop = false;
|
groupBoxCollectionTools.TabIndex = 0;
|
||||||
groupBoxCollection.Text = "Tools";
|
groupBoxCollectionTools.TabStop = false;
|
||||||
|
groupBoxCollectionTools.Text = "Tools";
|
||||||
|
//
|
||||||
|
// groupBoxCollectionSets
|
||||||
|
//
|
||||||
|
groupBoxCollectionSets.Controls.Add(textBoxStorageName);
|
||||||
|
groupBoxCollectionSets.Controls.Add(buttonDelObject);
|
||||||
|
groupBoxCollectionSets.Controls.Add(buttonAddObject);
|
||||||
|
groupBoxCollectionSets.Controls.Add(listBoxStorages);
|
||||||
|
groupBoxCollectionSets.Location = new Point(9, 32);
|
||||||
|
groupBoxCollectionSets.Name = "groupBoxCollectionSets";
|
||||||
|
groupBoxCollectionSets.Size = new Size(230, 323);
|
||||||
|
groupBoxCollectionSets.TabIndex = 5;
|
||||||
|
groupBoxCollectionSets.TabStop = false;
|
||||||
|
groupBoxCollectionSets.Text = "Sets";
|
||||||
|
//
|
||||||
|
// textBoxStorageName
|
||||||
|
//
|
||||||
|
textBoxStorageName.Location = new Point(6, 30);
|
||||||
|
textBoxStorageName.Name = "textBoxStorageName";
|
||||||
|
textBoxStorageName.Size = new Size(218, 31);
|
||||||
|
textBoxStorageName.TabIndex = 4;
|
||||||
|
//
|
||||||
|
// buttonDelObject
|
||||||
|
//
|
||||||
|
buttonDelObject.Location = new Point(6, 276);
|
||||||
|
buttonDelObject.Name = "buttonDelObject";
|
||||||
|
buttonDelObject.Size = new Size(218, 41);
|
||||||
|
buttonDelObject.TabIndex = 3;
|
||||||
|
buttonDelObject.Text = "Delete Set";
|
||||||
|
buttonDelObject.UseVisualStyleBackColor = true;
|
||||||
|
buttonDelObject.Click += ButtonDelObject_Click;
|
||||||
|
//
|
||||||
|
// buttonAddObject
|
||||||
|
//
|
||||||
|
buttonAddObject.Location = new Point(6, 67);
|
||||||
|
buttonAddObject.Name = "buttonAddObject";
|
||||||
|
buttonAddObject.Size = new Size(218, 43);
|
||||||
|
buttonAddObject.TabIndex = 2;
|
||||||
|
buttonAddObject.Text = "Add set";
|
||||||
|
buttonAddObject.UseVisualStyleBackColor = true;
|
||||||
|
buttonAddObject.Click += ButtonAddObject_Click;
|
||||||
|
//
|
||||||
|
// listBoxStorages
|
||||||
|
//
|
||||||
|
listBoxStorages.FormattingEnabled = true;
|
||||||
|
listBoxStorages.ItemHeight = 25;
|
||||||
|
listBoxStorages.Location = new Point(6, 116);
|
||||||
|
listBoxStorages.Name = "listBoxStorages";
|
||||||
|
listBoxStorages.Size = new Size(218, 154);
|
||||||
|
listBoxStorages.TabIndex = 1;
|
||||||
|
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
|
||||||
//
|
//
|
||||||
// maskedTextBoxNumber
|
// maskedTextBoxNumber
|
||||||
//
|
//
|
||||||
maskedTextBoxNumber.Location = new Point(56, 115);
|
maskedTextBoxNumber.Location = new Point(56, 468);
|
||||||
maskedTextBoxNumber.Margin = new Padding(4, 5, 4, 5);
|
maskedTextBoxNumber.Margin = new Padding(4, 5, 4, 5);
|
||||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||||
maskedTextBoxNumber.Size = new Size(141, 31);
|
maskedTextBoxNumber.Size = new Size(141, 31);
|
||||||
@ -65,10 +122,10 @@
|
|||||||
//
|
//
|
||||||
// buttonRefreshCollection
|
// buttonRefreshCollection
|
||||||
//
|
//
|
||||||
buttonRefreshCollection.Location = new Point(9, 300);
|
buttonRefreshCollection.Location = new Point(9, 688);
|
||||||
buttonRefreshCollection.Margin = new Padding(4, 5, 4, 5);
|
buttonRefreshCollection.Margin = new Padding(4, 5, 4, 5);
|
||||||
buttonRefreshCollection.Name = "buttonRefreshCollection";
|
buttonRefreshCollection.Name = "buttonRefreshCollection";
|
||||||
buttonRefreshCollection.Size = new Size(224, 45);
|
buttonRefreshCollection.Size = new Size(230, 45);
|
||||||
buttonRefreshCollection.TabIndex = 3;
|
buttonRefreshCollection.TabIndex = 3;
|
||||||
buttonRefreshCollection.Text = "Refresh collection";
|
buttonRefreshCollection.Text = "Refresh collection";
|
||||||
buttonRefreshCollection.UseVisualStyleBackColor = true;
|
buttonRefreshCollection.UseVisualStyleBackColor = true;
|
||||||
@ -76,10 +133,10 @@
|
|||||||
//
|
//
|
||||||
// buttonRemoveWarship
|
// buttonRemoveWarship
|
||||||
//
|
//
|
||||||
buttonRemoveWarship.Location = new Point(9, 187);
|
buttonRemoveWarship.Location = new Point(9, 509);
|
||||||
buttonRemoveWarship.Margin = new Padding(4, 5, 4, 5);
|
buttonRemoveWarship.Margin = new Padding(4, 5, 4, 5);
|
||||||
buttonRemoveWarship.Name = "buttonRemoveWarship";
|
buttonRemoveWarship.Name = "buttonRemoveWarship";
|
||||||
buttonRemoveWarship.Size = new Size(224, 45);
|
buttonRemoveWarship.Size = new Size(230, 45);
|
||||||
buttonRemoveWarship.TabIndex = 2;
|
buttonRemoveWarship.TabIndex = 2;
|
||||||
buttonRemoveWarship.Text = "Remove warship";
|
buttonRemoveWarship.Text = "Remove warship";
|
||||||
buttonRemoveWarship.UseVisualStyleBackColor = true;
|
buttonRemoveWarship.UseVisualStyleBackColor = true;
|
||||||
@ -87,7 +144,7 @@
|
|||||||
//
|
//
|
||||||
// buttonAddWarship
|
// buttonAddWarship
|
||||||
//
|
//
|
||||||
buttonAddWarship.Location = new Point(9, 37);
|
buttonAddWarship.Location = new Point(9, 413);
|
||||||
buttonAddWarship.Margin = new Padding(4, 5, 4, 5);
|
buttonAddWarship.Margin = new Padding(4, 5, 4, 5);
|
||||||
buttonAddWarship.Name = "buttonAddWarship";
|
buttonAddWarship.Name = "buttonAddWarship";
|
||||||
buttonAddWarship.Size = new Size(230, 45);
|
buttonAddWarship.Size = new Size(230, 45);
|
||||||
@ -111,23 +168,30 @@
|
|||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(1143, 750);
|
ClientSize = new Size(1143, 750);
|
||||||
Controls.Add(pictureBoxCollection);
|
Controls.Add(pictureBoxCollection);
|
||||||
Controls.Add(groupBoxCollection);
|
Controls.Add(groupBoxCollectionTools);
|
||||||
Margin = new Padding(4, 5, 4, 5);
|
Margin = new Padding(4, 5, 4, 5);
|
||||||
Name = "FormWarshipCollection";
|
Name = "FormWarshipCollection";
|
||||||
Text = "Warship Collection";
|
Text = "Warship Collection";
|
||||||
groupBoxCollection.ResumeLayout(false);
|
groupBoxCollectionTools.ResumeLayout(false);
|
||||||
groupBoxCollection.PerformLayout();
|
groupBoxCollectionTools.PerformLayout();
|
||||||
|
groupBoxCollectionSets.ResumeLayout(false);
|
||||||
|
groupBoxCollectionSets.PerformLayout();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||||
ResumeLayout(false);
|
ResumeLayout(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private GroupBox groupBoxCollection;
|
private GroupBox groupBoxCollectionTools;
|
||||||
private Button buttonRefreshCollection;
|
private Button buttonRefreshCollection;
|
||||||
private Button buttonRemoveWarship;
|
private Button buttonRemoveWarship;
|
||||||
private Button buttonAddWarship;
|
private Button buttonAddWarship;
|
||||||
private PictureBox pictureBoxCollection;
|
private PictureBox pictureBoxCollection;
|
||||||
private MaskedTextBox maskedTextBoxNumber;
|
private MaskedTextBox maskedTextBoxNumber;
|
||||||
|
private GroupBox groupBoxCollectionSets;
|
||||||
|
private ListBox listBoxStorages;
|
||||||
|
private Button buttonDelObject;
|
||||||
|
private Button buttonAddObject;
|
||||||
|
private TextBox textBoxStorageName;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,36 +11,115 @@ namespace ProjectAircraftCarrier
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Набор объектов
|
/// Набор объектов
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly WarshipsGenericCollection<DrawingWarship,
|
private readonly WarshipsGenericStorage _storage;
|
||||||
DrawingObjectWarship> _warships;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public FormWarshipCollection()
|
public FormWarshipCollection()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_warships = new WarshipsGenericCollection<DrawingWarship,
|
_storage = new WarshipsGenericStorage(pictureBoxCollection.Width,
|
||||||
DrawingObjectWarship>(pictureBoxCollection.Width,
|
|
||||||
pictureBoxCollection.Height);
|
pictureBoxCollection.Height);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <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("Not all data is filled in", "Error",
|
||||||
|
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]?.ShowWarships();
|
||||||
|
}
|
||||||
|
/// <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($"Delete an object" +
|
||||||
|
$"{ listBoxStorages.SelectedItem}?",
|
||||||
|
"Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
|
||||||
|
== DialogResult.Yes)
|
||||||
|
{
|
||||||
|
_storage.DelSet(listBoxStorages.SelectedItem.ToString()
|
||||||
|
?? string.Empty);
|
||||||
|
ReloadObjects();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
/// Добавление объекта в набор
|
/// Добавление объекта в набор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonAddWarship_Click(object sender, EventArgs e)
|
private void ButtonAddWarship_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
if (listBoxStorages.SelectedIndex == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||||
|
string.Empty];
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
FormAircraftCarrier form = new();
|
FormAircraftCarrier form = new();
|
||||||
if (form.ShowDialog() == DialogResult.OK)
|
if (form.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_warships + form.SelectedWarship != -1)
|
if (obj + form.SelectedWarship)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Объект добавлен");
|
MessageBox.Show("Object added");
|
||||||
pictureBoxCollection.Image = _warships.ShowWarships();
|
pictureBoxCollection.Image = obj.ShowWarships();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось добавить объект");
|
MessageBox.Show("Failed to add an object");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,20 +130,31 @@ namespace ProjectAircraftCarrier
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonRemoveWarship_Click(object sender, EventArgs e)
|
private void ButtonRemoveWarship_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("Delete an object?", "Deletion",
|
||||||
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
|
||||||
|
DialogResult.No)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||||
if (_warships - pos)
|
if (obj - pos != null)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Объект удален");
|
MessageBox.Show("Object deleted");
|
||||||
pictureBoxCollection.Image = _warships.ShowWarships();
|
pictureBoxCollection.Image = obj.ShowWarships();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось удалить объект");
|
MessageBox.Show("Failed to delete an object");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -74,7 +164,17 @@ namespace ProjectAircraftCarrier
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
pictureBoxCollection.Image = _warships.ShowWarships();
|
if (listBoxStorages.SelectedIndex == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||||
|
string.Empty];
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBoxCollection.Image = obj.ShowWarships();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace ProjectAircraftCarrier.Generics
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace ProjectAircraftCarrier.Generics
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Параметризованный набор объектов
|
/// Параметризованный набор объектов
|
||||||
@ -10,29 +12,35 @@
|
|||||||
/// <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="warship">Добавляемый автомобиль</param>
|
/// <param name="warship">Добавляемый автомобиль</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T warship)
|
public bool Insert(T warship)
|
||||||
{
|
{
|
||||||
if (_places[Count - 1] != null)
|
if (_places.Count == _maxCount)
|
||||||
return -1;
|
return false;
|
||||||
return Insert(warship, 0);
|
Insert(warship, 0);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор на конкретную позицию
|
/// Добавление объекта в набор на конкретную позицию
|
||||||
@ -40,27 +48,13 @@
|
|||||||
/// <param name="warship">Добавляемый автомобиль</param>
|
/// <param name="warship">Добавляемый автомобиль</param>
|
||||||
/// <param name="position">Позиция</param>
|
/// <param name="position">Позиция</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T warship, int position)
|
public bool Insert(T warship, int position)
|
||||||
{
|
{
|
||||||
int nullIndex = -1, i;
|
if (!(position >= 0 && position <= Count && _places.Count <
|
||||||
if (position < 0 || position >= Count)
|
_maxCount))
|
||||||
return -1;
|
return false;
|
||||||
for (i = position; i < Count; i++)
|
_places.Insert(position, warship);
|
||||||
{
|
return true;
|
||||||
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] = warship;
|
|
||||||
return position;
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление объекта из набора с конкретной позиции
|
/// Удаление объекта из набора с конкретной позиции
|
||||||
@ -69,8 +63,9 @@
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public bool Remove(int position)
|
public bool Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= Count) return false;
|
if (!(position >= 0 && position < Count))
|
||||||
_places[position] = null;
|
return false;
|
||||||
|
_places.RemoveAt(position);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -78,11 +73,33 @@
|
|||||||
/// </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;
|
{
|
||||||
return _places[position];
|
if (!(position >= 0 && position <= Count))
|
||||||
|
return null;
|
||||||
|
return _places[position];
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (!(position >= 0 && position <= Count))
|
||||||
|
return;
|
||||||
|
_places.Insert(position, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Проход по списку
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public IEnumerable<T?> GetWarships(int? maxWarships = null)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _places.Count; ++i)
|
||||||
|
{
|
||||||
|
yield return _places[i];
|
||||||
|
if (maxWarships.HasValue && i == maxWarships.Value)
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -51,12 +51,11 @@ namespace ProjectAircraftCarrier.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 +(WarshipsGenericCollection<T, U> collect, T? obj)
|
public static bool operator +(WarshipsGenericCollection<T, U>?
|
||||||
|
collect, T? obj)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (obj == null || collect == null)
|
||||||
{
|
return false;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return collect._collection.Insert(obj);
|
return collect._collection.Insert(obj);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -65,14 +64,15 @@ namespace ProjectAircraftCarrier.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 -(WarshipsGenericCollection<T, U> collect, int pos)
|
public static T? operator -(WarshipsGenericCollection<T, U>
|
||||||
|
collect, int pos)
|
||||||
{
|
{
|
||||||
T? obj = collect._collection.Get(pos);
|
T? obj = collect._collection[pos];
|
||||||
if (obj != null)
|
if (obj != null)
|
||||||
{
|
{
|
||||||
return collect._collection.Remove(pos);
|
collect._collection.Remove(pos);
|
||||||
}
|
}
|
||||||
return false;
|
return obj;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получение объекта IMoveableObject
|
/// Получение объекта IMoveableObject
|
||||||
@ -81,7 +81,7 @@ namespace ProjectAircraftCarrier.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>
|
||||||
/// Вывод всего набора объектов
|
/// Вывод всего набора объектов
|
||||||
@ -104,15 +104,14 @@ namespace ProjectAircraftCarrier.Generics
|
|||||||
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; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||||
1; ++j)
|
|
||||||
{
|
{
|
||||||
g.DrawLine(pen, i * _placeSizeWidth, j *
|
g.DrawLine(pen, i * _placeSizeWidth, j *
|
||||||
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
|
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth
|
||||||
_placeSizeHeight);
|
/ 2, j * _placeSizeHeight);
|
||||||
}
|
}
|
||||||
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||||||
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
_pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -121,16 +120,19 @@ namespace ProjectAircraftCarrier.Generics
|
|||||||
/// <param name="g"></param>
|
/// <param name="g"></param>
|
||||||
private void DrawObjects(Graphics g)
|
private void DrawObjects(Graphics g)
|
||||||
{
|
{
|
||||||
int inRow = _pictureWidth / _placeSizeWidth;
|
int i = 0;
|
||||||
for (int i = 0; i < _collection.Count; i++)
|
foreach (var warship in _collection.GetWarships())
|
||||||
{
|
{
|
||||||
DrawingWarship warship = _collection.Get(i);
|
|
||||||
if (warship != null)
|
if (warship != null)
|
||||||
{
|
{
|
||||||
warship.SetPosition(i % inRow * _placeSizeWidth + 3, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5);
|
int inRow = _pictureWidth / _placeSizeWidth;
|
||||||
|
warship.SetPosition(i % inRow * _placeSizeWidth,
|
||||||
|
_pictureHeight - _pictureHeight % _placeSizeHeight
|
||||||
|
- (i / inRow + 1) * _placeSizeHeight);
|
||||||
warship.DrawTransport(g);
|
warship.DrawTransport(g);
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
84
AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs
Normal file
84
AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
using ProjectAircraftCarrier.DrawingObjects;
|
||||||
|
using ProjectAircraftCarrier.MovementStrategy;
|
||||||
|
namespace ProjectAircraftCarrier.Generics
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс для хранения коллекции
|
||||||
|
/// </summary>
|
||||||
|
internal class WarshipsGenericStorage
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Словарь (хранилище)
|
||||||
|
/// </summary>
|
||||||
|
readonly Dictionary<string, WarshipsGenericCollection<DrawingWarship,
|
||||||
|
DrawingObjectWarship>> _warshipStorages;
|
||||||
|
/// <summary>
|
||||||
|
/// Возвращение списка названий наборов
|
||||||
|
/// </summary>
|
||||||
|
public List<string> Keys => _warshipStorages.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 WarshipsGenericStorage(int pictureWidth, int pictureHeight)
|
||||||
|
{
|
||||||
|
_warshipStorages = new Dictionary<string,
|
||||||
|
WarshipsGenericCollection<DrawingWarship,
|
||||||
|
DrawingObjectWarship>>();
|
||||||
|
_pictureWidth = pictureWidth;
|
||||||
|
_pictureHeight = pictureHeight;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Добавление набора
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">Название набора</param>
|
||||||
|
public void AddSet(string name)
|
||||||
|
{
|
||||||
|
if (!_warshipStorages.ContainsKey(name))
|
||||||
|
{
|
||||||
|
_warshipStorages.Add(name, new WarshipsGenericCollection
|
||||||
|
<DrawingWarship, DrawingObjectWarship>(_pictureWidth,
|
||||||
|
_pictureHeight));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("This set already exists");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Удаление набора
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">Название набора</param>
|
||||||
|
public void DelSet(string name)
|
||||||
|
{
|
||||||
|
if (!_warshipStorages.ContainsKey(name))
|
||||||
|
return;
|
||||||
|
_warshipStorages.Remove(name);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Доступ к набору
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ind"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public WarshipsGenericCollection<DrawingWarship, DrawingObjectWarship>
|
||||||
|
?this[string ind]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_warshipStorages.ContainsKey(ind))
|
||||||
|
return _warshipStorages[ind];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user