From 3a5e2daaf1d90b554a34536bdc5d5d9c1c741777 Mon Sep 17 00:00:00 2001 From: vladimir_zinovev Date: Tue, 14 Nov 2023 23:35:11 +0400 Subject: [PATCH] lab4 is done --- ...ArmoredTransportCollectionForm.Designer.cs | 79 ++++++++++- .../ArmoredTransportCollectionForm.cs | 124 +++++++++++++++--- .../ArmoredTransportsGenericCollection.cs | 35 ++--- .../ArmoredTransportsGenericStorage.cs | 76 +++++++++++ ProjectTank/ProjectTank/SetGeneric.cs | 93 +++++++------ 5 files changed, 328 insertions(+), 79 deletions(-) create mode 100644 ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs diff --git a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs index d99b1e8..583fb6d 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs @@ -30,6 +30,11 @@ { this.components = new System.ComponentModel.Container(); this.ArmoredTransportCollectionFormToolsGroup = new System.Windows.Forms.GroupBox(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonDelObject = new System.Windows.Forms.Button(); + this.buttonAddObject = new System.Windows.Forms.Button(); this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonRemoveArmoredTransport = new System.Windows.Forms.Button(); this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); @@ -37,6 +42,7 @@ this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.ArmoredTransportCollectionFormToolsGroup.SuspendLayout(); + this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); // @@ -44,6 +50,7 @@ // this.ArmoredTransportCollectionFormToolsGroup.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); + this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.groupBox1); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRefreshCollection); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRemoveArmoredTransport); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.maskedTextBoxNumber); @@ -55,10 +62,65 @@ this.ArmoredTransportCollectionFormToolsGroup.TabStop = false; this.ArmoredTransportCollectionFormToolsGroup.Text = "Инструменты"; // + // groupBox1 + // + this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.groupBox1.Controls.Add(this.textBoxStorageName); + this.groupBox1.Controls.Add(this.listBoxStorages); + this.groupBox1.Controls.Add(this.buttonDelObject); + this.groupBox1.Controls.Add(this.buttonAddObject); + this.groupBox1.Location = new System.Drawing.Point(22, 38); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(391, 434); + this.groupBox1.TabIndex = 4; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Наборы"; + // + // textBoxStorageName + // + this.textBoxStorageName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.textBoxStorageName.Location = new System.Drawing.Point(19, 38); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(353, 39); + this.textBoxStorageName.TabIndex = 3; + // + // listBoxStorages + // + this.listBoxStorages.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 32; + this.listBoxStorages.Location = new System.Drawing.Point(19, 165); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(353, 196); + this.listBoxStorages.TabIndex = 2; + this.listBoxStorages.Click += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); + // + // buttonDelObject + // + this.buttonDelObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDelObject.Location = new System.Drawing.Point(19, 374); + this.buttonDelObject.Name = "buttonDelObject"; + this.buttonDelObject.Size = new System.Drawing.Size(353, 46); + this.buttonDelObject.TabIndex = 1; + this.buttonDelObject.Text = "Удалить набор"; + this.buttonDelObject.UseVisualStyleBackColor = true; + this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // buttonAddObject + // + this.buttonAddObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonAddObject.Location = new System.Drawing.Point(19, 98); + this.buttonAddObject.Name = "buttonAddObject"; + this.buttonAddObject.Size = new System.Drawing.Size(353, 46); + this.buttonAddObject.TabIndex = 0; + this.buttonAddObject.Text = "Добавить набор"; + this.buttonAddObject.UseVisualStyleBackColor = true; + this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // // ButtonRefreshCollection // this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 394); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 773); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(391, 47); this.ButtonRefreshCollection.TabIndex = 3; @@ -69,7 +131,7 @@ // ButtonRemoveArmoredTransport // this.ButtonRemoveArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 251); + this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 641); this.ButtonRemoveArmoredTransport.Name = "ButtonRemoveArmoredTransport"; this.ButtonRemoveArmoredTransport.Size = new System.Drawing.Size(391, 82); this.ButtonRemoveArmoredTransport.TabIndex = 2; @@ -80,15 +142,17 @@ // maskedTextBoxNumber // this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 188); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 589); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(275, 39); this.maskedTextBoxNumber.TabIndex = 1; + this.maskedTextBoxNumber.Tag = ""; + this.maskedTextBoxNumber.Text = "_"; // // ButtonAddArmoredTransport // this.ButtonAddArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 88); + this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 492); this.ButtonAddArmoredTransport.Name = "ButtonAddArmoredTransport"; this.ButtonAddArmoredTransport.Size = new System.Drawing.Size(391, 82); this.ButtonAddArmoredTransport.TabIndex = 0; @@ -123,6 +187,8 @@ this.Text = "ArmoredTransportCollectionForm"; this.ArmoredTransportCollectionFormToolsGroup.ResumeLayout(false); this.ArmoredTransportCollectionFormToolsGroup.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -138,5 +204,10 @@ private Button ButtonAddArmoredTransport; private ContextMenuStrip contextMenuStrip1; private PictureBox pictureBoxCollection; + private GroupBox groupBox1; + private TextBox textBoxStorageName; + private ListBox listBoxStorages; + private Button buttonDelObject; + private Button buttonAddObject; } } \ No newline at end of file diff --git a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs index 826e20c..40deffe 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs @@ -1,34 +1,92 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using ProjectTank.DrawningObjects; +using ProjectTank.DrawningObjects; using ProjectTank.Generics; using ProjectTank.MovementStrategy; namespace ProjectTank { /// - /// Форма для работы с набором объектов класса DrawningCar + /// Форма для работы с набором объектов класса DrawningArmoredTransport /// public partial class ArmoredTransportCollectionForm : Form { /// /// Набор объектов /// - private readonly ArmoredTransportsGenericCollection _armoredTransports; + private readonly ArmoredTransportsGenericStorage _storage; /// /// Конструктор /// public ArmoredTransportCollectionForm() { InitializeComponent(); - _armoredTransports = new ArmoredTransportsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new ArmoredTransportsGenericStorage(pictureBoxCollection.Width, + pictureBoxCollection.Height); + } + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorages.Items.Add(_storage.Keys[i]); + } + if (listBoxStorages.Items.Count > 0 && (index == -1 || index + >= listBoxStorages.Items.Count)) + { + listBoxStorages.SelectedIndex = 0; + } + else if (listBoxStorages.Items.Count > 0 && index > -1 && + index < listBoxStorages.Items.Count) + { + listBoxStorages.SelectedIndex = index; + } + } + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + /// + /// Выбор набора + /// + /// + /// + private void ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowArmoredTransports(); + } + /// + /// Удаление набора + /// + /// + /// + 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(); + } } /// /// Добавление объекта в набор @@ -37,13 +95,22 @@ namespace ProjectTank /// private void ButtonAddArmoredTransport_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString()??string.Empty]; + if (obj == null) + { + return; + } TankForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_armoredTransports + form.SelectedArmoredTransport != null) + if (obj + form.SelectedArmoredTransport) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); + pictureBoxCollection.Image = obj.ShowArmoredTransports(); } else { @@ -58,16 +125,24 @@ namespace ProjectTank /// private void ButtonRemoveArmoredTransport_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + 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 (_armoredTransports - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); + pictureBoxCollection.Image = obj.ShowArmoredTransports(); } else { @@ -82,7 +157,16 @@ namespace ProjectTank private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowArmoredTransports(); } } } diff --git a/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs b/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs index fe37629..c668a2a 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs @@ -17,6 +17,10 @@ namespace ProjectTank.Generics where T : DrawningArmoredTransport where U : IMoveableObject { + /// + /// Коэффициент изменения ширины парковки + /// + readonly int koefWidth = 2; /// /// Ширина окна прорисовки /// @@ -56,13 +60,13 @@ namespace ProjectTank.Generics /// /// /// - public static int operator +(ArmoredTransportsGenericCollection collect, T? obj) + public static bool operator +(ArmoredTransportsGenericCollection collect, T? obj) { if (obj == null || collect == null) { - return -1; + return false; } - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj)??false; } /// /// Перегрузка оператора вычитания @@ -70,14 +74,14 @@ namespace ProjectTank.Generics /// /// /// - public static bool operator -(ArmoredTransportsGenericCollection collect, int pos) + public static T? operator -(ArmoredTransportsGenericCollection collect, int pos) { - T? obj = collect?._collection.Get(pos); - if (obj != null && collect != null) + T? obj = collect._collection[pos]; + if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } - return false; + return obj; } /// /// Получение объекта IMoveableObject @@ -86,7 +90,7 @@ namespace ProjectTank.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -107,7 +111,7 @@ namespace ProjectTank.Generics private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); - for (int i = 0; i < (_pictureWidth / _placeSizeWidth); i++) //изменение ширины парковки + for (int i = 0; i < (_pictureWidth / _placeSizeWidth)/koefWidth; i++) //изменение ширины парковки { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { @@ -128,18 +132,19 @@ namespace ProjectTank.Generics /// private void DrawObjects(Graphics g) { - int width = (_pictureWidth / _placeSizeWidth); - int height = _pictureHeight / _placeSizeHeight; - for (int i = 0; i < _collection.Count; i++) + int width = (_pictureWidth / _placeSizeWidth) / 2; + int height = _pictureHeight / _placeSizeHeight; + int i = 0; + foreach (var armoredTransport in _collection.GetArmoredTrasports()) { - DrawningArmoredTransport? armoredTransport = _collection.Get(i); if (armoredTransport != null) { int row = height - 1 - (i / width); int col = width - 1 - (i % width); - armoredTransport.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); + armoredTransport.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); armoredTransport.DrawTransport(g); } + i++; } } } diff --git a/ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs b/ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs new file mode 100644 index 0000000..cd6ee0b --- /dev/null +++ b/ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs @@ -0,0 +1,76 @@ +using System; +using ProjectTank.DrawningObjects; +using ProjectTank.MovementStrategy; + +namespace ProjectTank.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class ArmoredTransportsGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _armoredTransportStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _armoredTransportStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public ArmoredTransportsGenericStorage(int pictureWidth, int pictureHeight) + { + _armoredTransportStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + _armoredTransportStorages.Add(name, new ArmoredTransportsGenericCollection(_pictureWidth, _pictureHeight)); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_armoredTransportStorages.ContainsKey(name)) + { + return; + } + _armoredTransportStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public ArmoredTransportsGenericCollection? this[string ind] + { + get + { + if (_armoredTransportStorages.ContainsKey(ind)) + { + return _armoredTransportStorages[ind]; + } + return null; + } + } + } +} \ No newline at end of file diff --git a/ProjectTank/ProjectTank/SetGeneric.cs b/ProjectTank/ProjectTank/SetGeneric.cs index 63a2874..5167aec 100644 --- a/ProjectTank/ProjectTank/SetGeneric.cs +++ b/ProjectTank/ProjectTank/SetGeneric.cs @@ -6,67 +6,59 @@ using System.Threading.Tasks; namespace ProjectTank.Generics { + /// /// Параметризованный набор объектов /// /// internal class SetGeneric - where T : class + where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор /// - /// Добавляемый автомобиль + /// Добавляемый броне-транспорт /// - public int Insert(T armoredTransport) + public bool Insert(T armoredTransport) { - if (_places[Count - 1] != null) - { - return -1; - } - else - { - return Insert(armoredTransport,0); - } + if (_places.Count == _maxCount) + return false; + Insert(armoredTransport, 0); + return true; } /// /// Добавление объекта в набор на конкретную позицию /// - /// Добавляемый автомобиль + /// Добавляемый броне-транспорт /// Позиция /// - public int Insert(T armoredTransport, int position) + public bool Insert(T armoredTransport, int position) { - if (!(position >= 0 && position < Count)) - return -1; - if (_places[position] != null) - { - int ind = position; - while (ind < Count && _places[ind] != null) - ind++; - if (ind == Count) - return -1; - for (int i = ind - 1; i >= position; i--) - _places[i + 1] = _places[i]; - } - _places[position] = armoredTransport; - return position; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + return false; + _places.Insert(position, armoredTransport); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -75,11 +67,9 @@ namespace ProjectTank.Generics /// public bool Remove(int position) { - if (!(position >= 0 && position < Count) || _places[position] == null) - { + if (!(position >= 0 && position < Count)) return false; - } - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -87,13 +77,36 @@ namespace ProjectTank.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (!(position >= 0 && position < Count)) + get { - return null; + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + return; + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetArmoredTrasports(int? maxArmoredTransport = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxArmoredTransport.HasValue && i == maxArmoredTransport.Value) + { + yield break; + } } - return _places[position]; } } -} +} \ No newline at end of file