4 лаба готовая

This commit is contained in:
Камилия Сафиулова 2023-11-06 18:06:51 +04:00
parent 54db2e3780
commit ccf9eb19d8
6 changed files with 222 additions and 62 deletions

View File

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

View File

@ -56,13 +56,13 @@ namespace Catamaran.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 +(CatamaransGenericCollection<T, U> collect, T? obj) public static bool operator +(CatamaransGenericCollection<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>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания

View File

@ -43,6 +43,8 @@ namespace Catamaran.Generics
public void AddSet(string name) public void AddSet(string name)
{ {
// TODO Прописать логику для добавления // TODO Прописать логику для добавления
if (_catamaranStorages.ContainsKey(name)) return;
_catamaranStorages[name] = new CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>(_pictureWidth, _pictureHeight);
} }
/// <summary> /// <summary>
/// Удаление набора /// Удаление набора
@ -51,6 +53,9 @@ namespace Catamaran.Generics
public void DelSet(string name) public void DelSet(string name)
{ {
// TODO Прописать логику для удаления // TODO Прописать логику для удаления
if (!_catamaranStorages.ContainsKey(name))
return;
_catamaranStorages.Remove(name);
} }
/// <summary> /// <summary>
/// Доступ к набору /// Доступ к набору
@ -63,6 +68,8 @@ namespace Catamaran.Generics
get get
{ {
// TODO Продумать логику получения набора // TODO Продумать логику получения набора
if (_catamaranStorages.ContainsKey(ind))
return _catamaranStorages[ind];
return null; return null;
} }
} }

View File

@ -30,24 +30,31 @@
{ {
this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.groupBoxTools = new System.Windows.Forms.GroupBox(); 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.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveCatamaran = new System.Windows.Forms.Button(); this.ButtonRemoveCatamaran = new System.Windows.Forms.Button();
this.ButtonAddCatamaran = new System.Windows.Forms.Button(); this.ButtonAddCatamaran = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.groupBoxTools.SuspendLayout(); this.groupBoxTools.SuspendLayout();
this.groupBoxSets.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
// //
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 1); this.pictureBoxCollection.Location = new System.Drawing.Point(0, 1);
this.pictureBoxCollection.Name = "pictureBoxCollection"; 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.TabIndex = 0;
this.pictureBoxCollection.TabStop = false; this.pictureBoxCollection.TabStop = false;
// //
// groupBoxTools // groupBoxTools
// //
this.groupBoxTools.Controls.Add(this.groupBoxSets);
this.groupBoxTools.Controls.Add(this.maskedTextBoxNumber); this.groupBoxTools.Controls.Add(this.maskedTextBoxNumber);
this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection); this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection);
this.groupBoxTools.Controls.Add(this.ButtonRemoveCatamaran); this.groupBoxTools.Controls.Add(this.ButtonRemoveCatamaran);
@ -59,18 +66,68 @@
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты"; 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 // 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.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(141, 23); this.maskedTextBoxNumber.Size = new System.Drawing.Size(137, 23);
this.maskedTextBoxNumber.TabIndex = 3; this.maskedTextBoxNumber.TabIndex = 3;
// //
// ButtonRefreshCollection // 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.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.TabIndex = 2;
this.ButtonRefreshCollection.Text = "Обновить коллекцию"; this.ButtonRefreshCollection.Text = "Обновить коллекцию";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true; this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -78,9 +135,9 @@
// //
// ButtonRemoveCatamaran // 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.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.TabIndex = 1;
this.ButtonRemoveCatamaran.Text = "Удалить катамаран"; this.ButtonRemoveCatamaran.Text = "Удалить катамаран";
this.ButtonRemoveCatamaran.UseVisualStyleBackColor = true; this.ButtonRemoveCatamaran.UseVisualStyleBackColor = true;
@ -88,9 +145,9 @@
// //
// ButtonAddCatamaran // 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.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.TabIndex = 0;
this.ButtonAddCatamaran.Text = "Добавить катамаран"; this.ButtonAddCatamaran.Text = "Добавить катамаран";
this.ButtonAddCatamaran.UseVisualStyleBackColor = true; this.ButtonAddCatamaran.UseVisualStyleBackColor = true;
@ -104,10 +161,12 @@
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.pictureBoxCollection);
this.Name = "FormCatamaranCollection"; this.Name = "FormCatamaranCollection";
this.Text = "FormCatamaranCollection"; this.Text = "Набор катамаранов";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout(); this.groupBoxTools.PerformLayout();
this.groupBoxSets.ResumeLayout(false);
this.groupBoxSets.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
@ -120,5 +179,10 @@
private Button ButtonRefreshCollection; private Button ButtonRefreshCollection;
private Button ButtonRemoveCatamaran; private Button ButtonRemoveCatamaran;
private MaskedTextBox maskedTextBoxNumber; 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>
/// Набор объектов /// Набор объектов
/// </summary> /// </summary>
private readonly CatamaransGenericCollection<DrawningCatamaran, private readonly CatamaransGenericStorage _storage;
DrawningObjectCatamaran> _catamarans;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormCatamaranCollection() public FormCatamaranCollection()
{ {
InitializeComponent(); InitializeComponent();
_catamarans = new CatamaransGenericCollection<DrawningCatamaran, _storage = new CatamaransGenericStorage(pictureBoxCollection.Width,pictureBoxCollection.Height);
DrawningObjectCatamaran>(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>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
@ -30,13 +100,23 @@ namespace Catamaran
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonAddCatamaran_Click(object sender, EventArgs e) 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 (form.ShowDialog() == DialogResult.OK)
{ {
if (_catamarans + form.SelectedCatamaran != -1) if (obj + form.SelectedCatamaran)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _catamarans.ShowCatamarans(); pictureBoxCollection.Image = obj.ShowCatamarans();
} }
else else
{ {
@ -46,27 +126,38 @@ namespace Catamaran
} }
/// <summary> /// <summary>
/// Удаление объекта из набора /// Удаление объекта из набора
/// </summary> /// </summary>listBoxStorages
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRemoveCatamaran_Click(object sender, EventArgs e) 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("Удалить объект?", "Удаление", 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 (_catamarans - pos != null) if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _catamarans.ShowCatamarans(); pictureBoxCollection.Image = obj.ShowCatamarans();
} }
else else
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
/// <summary> /// <summary>
/// Обновление рисунка по набору /// Обновление рисунка по набору
/// </summary> /// </summary>
@ -75,7 +166,18 @@ namespace Catamaran
private void ButtonRefreshCollection_Click(object sender, EventArgs private void ButtonRefreshCollection_Click(object sender, EventArgs
e) 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,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -18,7 +19,7 @@ namespace Catamaran.Generics
/// </summary> /// </summary>
private readonly List<T?> _places; private readonly List<T?> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в списке
/// </summary> /// </summary>
public int Count => _places.Count; public int Count => _places.Count;
/// <summary> /// <summary>
@ -32,17 +33,22 @@ namespace Catamaran.Generics
public SetGeneric(int count) public SetGeneric(int count)
{ {
_maxCount = count; _maxCount = count;
_places = new List<T?>(count); _places = new List<T?>(_maxCount);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="car">Добавляемый катамаран</param> /// <param name="car">Добавляемый катамаран</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T catamaran) public bool Insert(T catamaran)
{ {
// TODO вставка в начало набора // TODO вставка в начало набора
return Insert(catamaran, 0); if (_places.Count == _maxCount)
{
return false;
}
Insert(catamaran, 0);
return true;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -50,36 +56,20 @@ namespace Catamaran.Generics
/// <param name="catamaran">Добавляемый катамаран</param> /// <param name="catamaran">Добавляемый катамаран</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T catamaran, int position) public bool Insert(T catamaran, int position)
{ {
// TODO проверка позиции // TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, // TODO проверка, что элемент массива по этой позиции пустой,
//если нет, то проверка, что после вставляемого элемента в массиве есть пустой элемент //если нет, то проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции // TODO вставка по позиции
int nullIndex = -1, i; if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
if (position < 0 || position >= Count)
return -1;
for (i = position; i < Count; i++)
{ {
if (_places[i] == null) return false;
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
return -1;
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
} }
_places[position] = catamaran; _places.Insert(position, catamaran);
return position; return true;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -90,9 +80,11 @@ namespace Catamaran.Generics
{ {
// TODO проверка позиции // TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива значение null // TODO удаление объекта из массива, присвоив элементу массива значение null
if (position < 0 || position >= Count) { if (position < 0 || position >= Count)
return false; } {
_places[position] = null; return false;
}
_places.RemoveAt(position);
return true; return true;
} }
/// <summary> /// <summary>
@ -105,10 +97,8 @@ namespace Catamaran.Generics
get get
{ {
// TODO проверка позиции // TODO проверка позиции
if (position < 0 || position > _maxCount) if (!(position >= 0 && position < Count))
{
return null; return null;
}
return _places[position]; return _places[position];
} }
set set
@ -117,10 +107,7 @@ namespace Catamaran.Generics
// TODO проверка свободных мест в списке // TODO проверка свободных мест в списке
// TODO вставка в список по позиции // TODO вставка в список по позиции
if (!(position >= 0 && position < Count && _places.Count < _maxCount)) if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return; return;
}
_places.Insert(position, value); _places.Insert(position, value);
return; return;
} }