This commit is contained in:
KirillFilippow 2023-12-06 18:47:21 +04:00
parent 5c698b6514
commit aa766d5f54
13 changed files with 550 additions and 277 deletions

View File

@ -90,7 +90,8 @@ namespace ProjectContainerShip.MovementStrategy
/// <summary> /// <summary>
/// Параметры объекта /// Параметры объекта
/// </summary> /// </summary>
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary> /// <summary>
/// Шаг объекта /// Шаг объекта
/// </summary> /// </summary>

View File

@ -57,13 +57,14 @@ namespace ProjectContainerShip.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 +(ContainerGenericCollection<T, U> collect, T? obj) public static bool operator +(ContainerGenericCollection<T, U> collect, T?
obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect?._collection.Insert(obj); return collect?._collection.Insert(obj) ?? false;
} }
/// <summary> /// <summary>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания
@ -71,16 +72,14 @@ namespace ProjectContainerShip.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 -(ContainerGenericCollection<T, U> collect, int public static T? operator -(ContainerGenericCollection<T, U> collect, int pos)
pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj is not null) if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
return true;
} }
return false; return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
@ -89,7 +88,7 @@ namespace ProjectContainerShip.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetObjectPosition; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -112,7 +111,8 @@ namespace ProjectContainerShip.Generics
Pen pen = new(Color.Black, 2); Pen pen = new(Color.Black, 2);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{ {
g.DrawLine(pen, i * _placeSizeWidth, j * g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth , j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth , j *
@ -126,13 +126,17 @@ namespace ProjectContainerShip.Generics
/// Метод прорисовки объектов /// Метод прорисовки объектов
/// </summary> /// </summary>
/// <param name="g"></param> /// <param name="g"></param>
///
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++) for (int i = 0; i < _collection.Count; i++)
foreach (var contShip in _collection.GetShip())
{ {
DrawningShip? ship = _collection.Get(i); if (contShip != null)
{
T? ship = _collection[i];
if (ship == null) if (ship == null)
continue; continue;
int row = height - 1 - (i / width); int row = height - 1 - (i / width);
@ -143,4 +147,5 @@ namespace ProjectContainerShip.Generics
} }
} }
} }
}

View File

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

View File

@ -28,8 +28,8 @@ namespace ProjectContainerShip.Entities
/// <param name="weight">Вес </param> /// <param name="weight">Вес </param>
/// <param name="bodyColor">Основной цвет</param> /// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param> /// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="сrane">Признак наличия труб</param> /// <param name="Crane">Признак наличия труб</param>
/// <param name="сontainer">Признак наличия топлива</param> /// <param name="Container">Признак наличия топлива</param>
public EntityContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, bool crane, bool container) : base(speed, weight, bodyColor) public EntityContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, bool crane, bool container) : base(speed, weight, bodyColor)
{ {
AdditionalColor = additionalColor; AdditionalColor = additionalColor;

View File

@ -0,0 +1,186 @@
namespace ProjectContainerShip
{
partial class FormContainerCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
pictureBoxCollection = new PictureBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonAddEx = new Button();
buttonRemoveEx = new Button();
buttonRefreshCollection = new Button();
groupBox1 = new GroupBox();
groupBox2 = new GroupBox();
textBoxStorageName = new TextBox();
buttonDelObject = new Button();
listBoxStorages = new ListBox();
buttonAddObject = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(932, 503);
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(21, 326);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(95, 27);
maskedTextBoxNumber.TabIndex = 1;
//
// buttonAddEx
//
buttonAddEx.Location = new Point(21, 282);
buttonAddEx.Name = "buttonAddEx";
buttonAddEx.Size = new Size(95, 27);
buttonAddEx.TabIndex = 2;
buttonAddEx.Text = "Добавить";
buttonAddEx.UseVisualStyleBackColor = true;
buttonAddEx.Click += ButtonAddShip_Click;
//
// buttonRemoveEx
//
buttonRemoveEx.Location = new Point(19, 359);
buttonRemoveEx.Name = "buttonRemoveEx";
buttonRemoveEx.Size = new Size(95, 27);
buttonRemoveEx.TabIndex = 3;
buttonRemoveEx.Text = "Удалить";
buttonRemoveEx.UseVisualStyleBackColor = true;
buttonRemoveEx.Click += ButtonRemoveShip_Click;
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(21, 405);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(95, 27);
buttonRefreshCollection.TabIndex = 4;
buttonRefreshCollection.Text = "Обновить";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// groupBox1
//
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(buttonAddEx);
groupBox1.Controls.Add(buttonRefreshCollection);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(buttonRemoveEx);
groupBox1.Location = new Point(812, 0);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(120, 461);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// groupBox2
//
groupBox2.Controls.Add(textBoxStorageName);
groupBox2.Controls.Add(buttonDelObject);
groupBox2.Controls.Add(listBoxStorages);
groupBox2.Controls.Add(buttonAddObject);
groupBox2.Location = new Point(13, 28);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(116, 214);
groupBox2.TabIndex = 5;
groupBox2.TabStop = false;
groupBox2.Text = "Наборы";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(8, 26);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(95, 27);
textBoxStorageName.TabIndex = 6;
textBoxStorageName.TextChanged += textBoxStorageName_TextChanged;
//
// buttonDelObject
//
buttonDelObject.Location = new Point(8, 162);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(95, 27);
buttonDelObject.TabIndex = 8;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += ButtonDelObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(8, 92);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(95, 64);
listBoxStorages.TabIndex = 6;
listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(8, 59);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(95, 27);
buttonAddObject.TabIndex = 7;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += ButtonAddObject_Click;
//
// FormContainerCollection
//
ClientSize = new Size(932, 503);
Controls.Add(groupBox1);
Controls.Add(pictureBoxCollection);
Name = "FormContainerCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
private Button buttonAddEx;
private Button buttonRemoveEx;
private Button buttonRefreshCollection;
private GroupBox groupBox1;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private Button buttonDelObject;
private ListBox listBoxStorages;
private Button buttonAddObject;
}
}

View File

@ -0,0 +1,190 @@
using ProjectContainerShip;
using ProjectContainerShip.DrawningObjects;
using ProjectContainerShip.Generics;
using ProjectContainerShip.MovementStrategy;
namespace ProjectContainerShip
{
/// <summary>
/// Форма для работы с набором объектов класса DrawningShip
/// </summary>
public partial class FormContainerCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly ContainerGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormContainerCollection()
{
InitializeComponent();
_storage = new ContainerGenericStorage(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;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowContainer();
}
}
/// <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 ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowContainer();
}
/// <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>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddShip_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormContainerShip form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (obj + form.SelectedShip)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowContainer();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveShip_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 = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowContainer();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowContainer();
}
private void textBoxStorageName_TextChanged(object sender, EventArgs e)
{
}
}
}

View File

@ -1,121 +0,0 @@
namespace ProjectContainerShip
{
partial class FormShipCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
pictureBoxCollection = new PictureBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonAddShip = new Button();
buttonRemoveShip = new Button();
buttonRefreshCollection = new Button();
groupBox1 = new GroupBox();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(932, 503);
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(6, 120);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(108, 27);
maskedTextBoxNumber.TabIndex = 1;
//
// buttonAddShip
//
buttonAddShip.Location = new Point(6, 57);
buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Size = new Size(108, 30);
buttonAddShip.TabIndex = 2;
buttonAddShip.Text = "Добавить ";
buttonAddShip.UseVisualStyleBackColor = true;
buttonAddShip.Click += ButtonAddShip_Click;
//
// buttonRemoveShip
//
buttonRemoveShip.Location = new Point(6, 166);
buttonRemoveShip.Name = "buttonRemoveShip";
buttonRemoveShip.Size = new Size(108, 30);
buttonRemoveShip.TabIndex = 3;
buttonRemoveShip.Text = "Удалить ";
buttonRemoveShip.UseVisualStyleBackColor = true;
buttonRemoveShip.Click += ButtonRemoveShip_Click;
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(6, 230);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(108, 30);
buttonRefreshCollection.TabIndex = 4;
buttonRefreshCollection.Text = "Обновить ";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// groupBox1
//
groupBox1.Controls.Add(buttonAddShip);
groupBox1.Controls.Add(buttonRefreshCollection);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(buttonRemoveShip);
groupBox1.Location = new Point(812, 21);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(120, 461);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// FormShipCollection
//
ClientSize = new Size(932, 503);
Controls.Add(groupBox1);
Controls.Add(pictureBoxCollection);
Name = "FormShipCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
private Button buttonAddShip;
private Button buttonRemoveShip;
private Button buttonRefreshCollection;
private GroupBox groupBox1;
}
}

View File

@ -1,82 +0,0 @@
using ProjectContainerShip.DrawningObjects;
using ProjectContainerShip.Generics;
using ProjectContainerShip.MovementStrategy;
namespace ProjectContainerShip
{
/// <summary>
/// Форма для работы с набором объектов класса
/// </summary>
public partial class FormShipCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly ContainerGenericCollection<DrawningShip,
DrawningObjectShip> _containerShip;
/// <summary>
/// Конструктор
/// </summary>
public FormShipCollection()
{
InitializeComponent();
_containerShip = new ContainerGenericCollection<DrawningShip,
DrawningObjectShip>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddShip_Click(object sender, EventArgs e)
{
FormContainerShip form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_containerShip + form.SelectedShip > -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _containerShip.ShowContainer();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveShip_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_containerShip - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _containerShip.ShowContainer();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _containerShip.ShowContainer();
}
}
}

View File

@ -59,4 +59,3 @@ namespace ProjectContainerShip.MovementStrategy
} }

View File

@ -13,7 +13,7 @@ namespace ProjectContainerShip
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormShipCollection()); Application.Run(new FormContainerCollection());
} }
} }

View File

@ -14,64 +14,39 @@ namespace ProjectContainerShip.Generics
where T : class where T : class
{ {
/// <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="ship">Добавляемый корабль</param> /// <param name="ship">Добавляемый контейнеровоз</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T ship) public bool Insert(T ship)
{ {
return Insert(ship, 0); if (_places.Count >= _maxCount)
{
return false;
} }
/// <summary> _places.Insert(0, ship);
/// Добавление объекта в набор на конкретную позицию return true;
/// </summary>
/// <param name="ship">Добавляемый корабля</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T ship, int position)
{
if (position < 0 && position > Count)
{
return -1;
}
if (_places[position] != null)
{
int d = 0;
for (int j = 1; j < Count - position; j++)
{
if (_places[position + j] == null)
{
d = position + j;
break;
}
}
if (d == 0)
{
return -1;
}
for (int j = d; j > position; j--)
{
_places[j] = _places[j - 1];
}
}
_places[position] = ship;
return position;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -80,11 +55,11 @@ namespace ProjectContainerShip.Generics
/// <returns></returns> /// <returns></returns>
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= _places.Length) if (position < 0 || position >= _places.Count)
{ {
return false; return false;
} }
_places[position] = null; _places.RemoveAt(position);
return true; return true;
} }
/// <summary> /// <summary>
@ -92,14 +67,47 @@ namespace ProjectContainerShip.Generics
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public T? Get(int position) public T? this[int position]
{
get
{
if (position < 0 || position >= _places.Count)
{ {
if (position < 0 || position >= _places.Length)
return null; return null;
}
return _places[position]; return _places[position];
} }
set
{
if (position < 0 || position >= _places.Count)
{
return;
}
if (_places.Count >= _maxCount)
{
return;
}
_places.Insert(position, value);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetShip(int? maxShip = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxShip.HasValue && i == maxShip.Value)
{
yield break;
}
}
}
} }
} }