PIbd-22. Safiulova K.N. LabWork_04 #4

Closed
safiulova.k wants to merge 3 commits from LabWork_04 into LabWork_03
6 changed files with 361 additions and 89 deletions

View File

@ -19,7 +19,7 @@ namespace Catamaran
private AbstractStrategy? _strategy;
/// <summary>
/// Выбранный автомобиль
/// Выбранный катамаран
/// </summary>
public DrawningCatamaran? SelectedCatamaran { get; private set; }
@ -83,7 +83,7 @@ namespace Catamaran
}
/// <summary>
/// Создание простого автомобиля
/// Создание простого катамарана
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
@ -106,7 +106,7 @@ namespace Catamaran
}
/// <summary>
/// Изменение положения автомобиля
/// Изменение положения катамарана
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
@ -175,7 +175,7 @@ namespace Catamaran
}
}
/// <summary>
/// Выбор автомобиля
/// Выбор катамарана
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>

View File

@ -56,13 +56,13 @@ namespace Catamaran.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(CatamaransGenericCollection<T, U> collect, T? obj)
public static bool operator +(CatamaransGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
return collect?._collection.Insert(obj) ?? false;
}
/// <summary>
/// Перегрузка оператора вычитания
@ -70,15 +70,16 @@ namespace Catamaran.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(CatamaransGenericCollection<T, U> collect, int pos)
public static T? operator -(CatamaransGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
collect._collection.Remove(pos);
}
return false;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
@ -86,8 +87,9 @@ namespace Catamaran.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
@ -126,27 +128,29 @@ namespace Catamaran.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
T? obj;
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int diff = 1, currWidth = 0;
for (int i = 0; i < _collection.Count; i++)
{
currWidth++;
if (currWidth > width)
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int diff = 1, currWidth = 0, i = 0;
foreach (var catamaran in _collection.GetCatamarans())
{
diff++;
currWidth = 1;
}
obj = _collection.Get(i);
if (obj != null)
{
obj.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40,
(height - diff) * _placeSizeHeight + _placeSizeHeight / 15);
obj.DrawTransport(g);
currWidth++;
if (currWidth > width)
{
diff++;
currWidth = 1;
}
if (catamaran != null)
{
catamaran.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40,
(height - diff) * _placeSizeHeight + _placeSizeHeight / 15);
catamaran.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,77 @@
using Catamaran.DrawningObjects;
using Catamaran.MovementStrategy;
namespace Catamaran.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class CatamaransGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, CatamaransGenericCollection<DrawningCatamaran,
DrawningObjectCatamaran>> _catamaranStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _catamaranStorages.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 CatamaransGenericStorage(int pictureWidth, int pictureHeight)
{
_catamaranStorages = new Dictionary<string,
CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
// TODO Прописать логику для добавления
if (_catamaranStorages.ContainsKey(name)) return;
_catamaranStorages[name] = new CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
// TODO Прописать логику для удаления
if (!_catamaranStorages.ContainsKey(name))
return;
_catamaranStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>?
this[string ind]
{
get
{
// TODO Продумать логику получения набора
if (_catamaranStorages.ContainsKey(ind))
return _catamaranStorages[ind];
return null;
}
}
}
}

View File

@ -30,24 +30,31 @@
{
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.groupBoxSets = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.buttonDelObject = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonAddObject = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveCatamaran = new System.Windows.Forms.Button();
this.ButtonAddCatamaran = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.groupBoxTools.SuspendLayout();
this.groupBoxSets.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxCollection
//
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 1);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(741, 459);
this.pictureBoxCollection.Size = new System.Drawing.Size(736, 459);
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.groupBoxSets);
this.groupBoxTools.Controls.Add(this.maskedTextBoxNumber);
this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection);
this.groupBoxTools.Controls.Add(this.ButtonRemoveCatamaran);
@ -59,18 +66,68 @@
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// groupBoxSets
//
this.groupBoxSets.Controls.Add(this.textBoxStorageName);
this.groupBoxSets.Controls.Add(this.buttonDelObject);
this.groupBoxSets.Controls.Add(this.listBoxStorages);
this.groupBoxSets.Controls.Add(this.buttonAddObject);
this.groupBoxSets.Location = new System.Drawing.Point(3, 21);
this.groupBoxSets.Name = "groupBoxSets";
this.groupBoxSets.Size = new System.Drawing.Size(138, 245);
this.groupBoxSets.TabIndex = 4;
this.groupBoxSets.TabStop = false;
this.groupBoxSets.Text = "Наборы";
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(3, 36);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(134, 23);
this.textBoxStorageName.TabIndex = 4;
//
// buttonDelObject
//
this.buttonDelObject.Location = new System.Drawing.Point(3, 209);
this.buttonDelObject.Name = "buttonDelObject";
this.buttonDelObject.Size = new System.Drawing.Size(134, 27);
this.buttonDelObject.TabIndex = 3;
this.buttonDelObject.Text = "Удалить набор";
this.buttonDelObject.UseVisualStyleBackColor = true;
this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 15;
this.listBoxStorages.Location = new System.Drawing.Point(3, 102);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(134, 94);
this.listBoxStorages.TabIndex = 2;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
//
// buttonAddObject
//
this.buttonAddObject.Location = new System.Drawing.Point(3, 65);
this.buttonAddObject.Name = "buttonAddObject";
this.buttonAddObject.Size = new System.Drawing.Size(134, 25);
this.buttonAddObject.TabIndex = 1;
this.buttonAddObject.Text = "Добавить набор";
this.buttonAddObject.UseVisualStyleBackColor = true;
this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(0, 108);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 321);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(141, 23);
this.maskedTextBoxNumber.Size = new System.Drawing.Size(137, 23);
this.maskedTextBoxNumber.TabIndex = 3;
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(0, 211);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(0, 420);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(141, 34);
this.ButtonRefreshCollection.Size = new System.Drawing.Size(140, 34);
this.ButtonRefreshCollection.TabIndex = 2;
this.ButtonRefreshCollection.Text = "Обновить коллекцию";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -78,9 +135,9 @@
//
// ButtonRemoveCatamaran
//
this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 157);
this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 363);
this.ButtonRemoveCatamaran.Name = "ButtonRemoveCatamaran";
this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(141, 34);
this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(140, 34);
this.ButtonRemoveCatamaran.TabIndex = 1;
this.ButtonRemoveCatamaran.Text = "Удалить катамаран";
this.ButtonRemoveCatamaran.UseVisualStyleBackColor = true;
@ -88,9 +145,9 @@
//
// ButtonAddCatamaran
//
this.ButtonAddCatamaran.Location = new System.Drawing.Point(0, 22);
this.ButtonAddCatamaran.Location = new System.Drawing.Point(3, 270);
this.ButtonAddCatamaran.Name = "ButtonAddCatamaran";
this.ButtonAddCatamaran.Size = new System.Drawing.Size(141, 34);
this.ButtonAddCatamaran.Size = new System.Drawing.Size(137, 34);
this.ButtonAddCatamaran.TabIndex = 0;
this.ButtonAddCatamaran.Text = "Добавить катамаран";
this.ButtonAddCatamaran.UseVisualStyleBackColor = true;
@ -104,10 +161,12 @@
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.pictureBoxCollection);
this.Name = "FormCatamaranCollection";
this.Text = "FormCatamaranCollection";
this.Text = "Набор катамаранов";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
this.groupBoxSets.ResumeLayout(false);
this.groupBoxSets.PerformLayout();
this.ResumeLayout(false);
}
@ -120,5 +179,10 @@
private Button ButtonRefreshCollection;
private Button ButtonRemoveCatamaran;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBoxSets;
private Button buttonAddObject;
private Button buttonDelObject;
private ListBox listBoxStorages;
private TextBox textBoxStorageName;
}
}

View File

@ -12,17 +12,87 @@ namespace Catamaran
/// <summary>
/// Набор объектов
/// </summary>
private readonly CatamaransGenericCollection<DrawningCatamaran,
DrawningObjectCatamaran> _catamarans;
private readonly CatamaransGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormCatamaranCollection()
{
InitializeComponent();
_catamarans = new CatamaransGenericCollection<DrawningCatamaran,
DrawningObjectCatamaran>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new CatamaransGenericStorage(pictureBoxCollection.Width,pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxStorages
/// </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 ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCatamarans();
}
/// <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>
@ -30,13 +100,23 @@ namespace Catamaran
/// <param name="e"></param>
private void ButtonAddCatamaran_Click(object sender, EventArgs e)
{
FormCatamaran form = new();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormCatamaran form = new FormCatamaran();
if (form.ShowDialog() == DialogResult.OK)
{
if (_catamarans + form.SelectedCatamaran != -1)
if (obj + form.SelectedCatamaran)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _catamarans.ShowCatamarans();
pictureBoxCollection.Image = obj.ShowCatamarans();
}
else
{
@ -46,27 +126,38 @@ namespace Catamaran
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// </summary>listBoxStorages
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveCatamaran_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 (_catamarans - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _catamarans.ShowCatamarans();
pictureBoxCollection.Image = obj.ShowCatamarans();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
@ -75,7 +166,18 @@ namespace Catamaran
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
{
pictureBoxCollection.Image = _catamarans.ShowCatamarans();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowCatamarans();
}
}
}

View File

@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
namespace Catamaran.Generics
{
/// <summary>
/// Параметризованный набор объектов
@ -14,30 +15,40 @@ namespace Catamaran
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?>(_maxCount);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый катамаран</param>
/// <returns></returns>
public int Insert(T catamaran)
public bool Insert(T catamaran)
{
// TODO вставка в начало набора
return Insert(catamaran, 0);
if (_places.Count == _maxCount)
{
return false;
}
Insert(catamaran, 0);
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -45,36 +56,20 @@ namespace Catamaran
/// <param name="catamaran">Добавляемый катамаран</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T catamaran, int position)
public bool Insert(T catamaran, int position)
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой,
//если нет, то проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
int nullIndex = -1, i;
if (position < 0 || position >= Count)
return -1;
for (i = position; i < Count; i++)
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
return -1;
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
return false;
}
_places[position] = catamaran;
return position;
_places.Insert(position, catamaran);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -85,9 +80,11 @@ namespace Catamaran
{
// TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива значение null
if (position < 0 || position >= Count) {
return false; }
_places[position] = null;
if (position < 0 || position >= Count)
{
return false;
}
_places.RemoveAt(position);
return true;
}
/// <summary>
@ -95,12 +92,40 @@ namespace Catamaran
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
// TODO проверка позиции
if (position < 0 || position >= Count)
return null;
return _places[position];
get
{
// TODO проверка позиции
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set
{
// TODO проверка позиции
// TODO проверка свободных мест в списке
// TODO вставка в список по позиции
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetCatamarans(int? maxCatamarans = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCatamarans.HasValue && i == maxCatamarans.Value)
{
yield break;
}
}
}
}
}