From 534f4489ba7f23c55b9bc3662a13f3e62b77d712 Mon Sep 17 00:00:00 2001 From: sofiaivv Date: Tue, 12 Dec 2023 01:01:01 +0400 Subject: [PATCH] done lab4 --- MotorBoat/MotorBoat/BoatGenericCollection.cs | 42 ++++--- MotorBoat/MotorBoat/BoatsGenericStorage.cs | 86 ++++++++++++++ .../MotorBoat/FormBoatCollection.Designer.cs | 82 ++++++++++++-- MotorBoat/MotorBoat/FormBoatCollection.cs | 106 ++++++++++++++++-- MotorBoat/MotorBoat/SetGeneric.cs | 92 +++++++++------ 5 files changed, 336 insertions(+), 72 deletions(-) create mode 100644 MotorBoat/MotorBoat/BoatsGenericStorage.cs diff --git a/MotorBoat/MotorBoat/BoatGenericCollection.cs b/MotorBoat/MotorBoat/BoatGenericCollection.cs index f3ffc8b..75bd9fe 100644 --- a/MotorBoat/MotorBoat/BoatGenericCollection.cs +++ b/MotorBoat/MotorBoat/BoatGenericCollection.cs @@ -63,14 +63,13 @@ namespace MotorBoat.Generics /// /// /// - public static int operator +(BoatsGenericCollection collect, T? - obj) + public static bool operator +(BoatsGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj) ?? -1; + return collect?._collection.Insert(obj) ?? false; } /// @@ -79,15 +78,14 @@ namespace MotorBoat.Generics /// /// /// - public static bool operator -(BoatsGenericCollection collect, int - pos) + public static T? operator -(BoatsGenericCollection? collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); } - return false; + return obj; } /// @@ -97,7 +95,7 @@ namespace MotorBoat.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// @@ -122,15 +120,13 @@ namespace MotorBoat.Generics Pen pen = new(Color.Black, 3); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + - 1; ++j) - {//линия рамзетки места - g.DrawLine(pen, i * _placeSizeWidth, j * - _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * - _placeSizeHeight); + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия разметки места + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, + i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); } - g.DrawLine(pen, i * _placeSizeWidth, 0, i * - _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, + _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } @@ -140,16 +136,16 @@ namespace MotorBoat.Generics /// private void DrawObjects(Graphics g) { - T? t; + int i = 0; int Rows = _pictureWidth / _placeSizeWidth; - for (int i = 0; i < _collection.Count; i++) + foreach(var boat in _collection.GetBoats()) { - t = _collection.Get(i); - if (t != null) + if (boat != null) { - t.SetPosition(i % Rows * _placeSizeWidth, i / Rows * _placeSizeHeight + 5); - t.DrawTransport(g); + boat.SetPosition(i % Rows * _placeSizeWidth, i / Rows * _placeSizeHeight + 5); + boat.DrawTransport(g); } + i++; } } } diff --git a/MotorBoat/MotorBoat/BoatsGenericStorage.cs b/MotorBoat/MotorBoat/BoatsGenericStorage.cs new file mode 100644 index 0000000..fd681f2 --- /dev/null +++ b/MotorBoat/MotorBoat/BoatsGenericStorage.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MotorBoat.DrawningObjects; +using MotorBoat.MovementStrategy; + +namespace MotorBoat.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class BoatsGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _boatStorages; + + /// + /// Возвращение списка названий наборов + /// + public List Keys => _boatStorages.Keys.ToList(); + + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + + /// + /// Конструктор + /// + /// + /// + public BoatsGenericStorage(int pictureWidth, int pictureHeight) + { + _boatStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (_boatStorages.ContainsKey(name)) + return; + _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_boatStorages.ContainsKey(name)) + return; + _boatStorages.Remove(name); + } + + /// + /// Доступ к набору + /// + /// + /// + public BoatsGenericCollection? + this[string ind] + { + get + { + if (_boatStorages.ContainsKey(ind)) + return _boatStorages[ind]; + return null; + } + } + } +} diff --git a/MotorBoat/MotorBoat/FormBoatCollection.Designer.cs b/MotorBoat/MotorBoat/FormBoatCollection.Designer.cs index 0551667..118add4 100644 --- a/MotorBoat/MotorBoat/FormBoatCollection.Designer.cs +++ b/MotorBoat/MotorBoat/FormBoatCollection.Designer.cs @@ -34,8 +34,14 @@ ButtonRemoveBoat = new Button(); ButtonRefreshCollection = new Button(); groupBoxTools = new GroupBox(); + groupBoxCollections = new GroupBox(); + ButtonRemoveObject = new Button(); + listBoxStorages = new ListBox(); + ButtonAddObject = new Button(); + textBoxStorageName = new MaskedTextBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); groupBoxTools.SuspendLayout(); + groupBoxCollections.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection @@ -43,20 +49,20 @@ pictureBoxCollection.Dock = DockStyle.Left; pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(578, 461); + pictureBoxCollection.Size = new Size(578, 500); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(54, 135); + maskedTextBoxNumber.Location = new Point(51, 371); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(100, 23); maskedTextBoxNumber.TabIndex = 0; // // ButtonAddBoat // - ButtonAddBoat.Location = new Point(6, 22); + ButtonAddBoat.Location = new Point(6, 325); ButtonAddBoat.Name = "ButtonAddBoat"; ButtonAddBoat.Size = new Size(188, 40); ButtonAddBoat.TabIndex = 1; @@ -66,9 +72,9 @@ // // ButtonRemoveBoat // - ButtonRemoveBoat.Location = new Point(11, 171); + ButtonRemoveBoat.Location = new Point(6, 407); ButtonRemoveBoat.Name = "ButtonRemoveBoat"; - ButtonRemoveBoat.Size = new Size(183, 41); + ButtonRemoveBoat.Size = new Size(188, 41); ButtonRemoveBoat.TabIndex = 2; ButtonRemoveBoat.Text = "Удалить лодку"; ButtonRemoveBoat.UseVisualStyleBackColor = true; @@ -76,9 +82,9 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(11, 218); + ButtonRefreshCollection.Location = new Point(6, 454); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - ButtonRefreshCollection.Size = new Size(183, 39); + ButtonRefreshCollection.Size = new Size(188, 39); ButtonRefreshCollection.TabIndex = 3; ButtonRefreshCollection.Text = "Обновить коллекцию"; ButtonRefreshCollection.UseVisualStyleBackColor = true; @@ -86,6 +92,7 @@ // // groupBoxTools // + groupBoxTools.Controls.Add(groupBoxCollections); groupBoxTools.Controls.Add(ButtonRefreshCollection); groupBoxTools.Controls.Add(ButtonRemoveBoat); groupBoxTools.Controls.Add(ButtonAddBoat); @@ -93,16 +100,66 @@ groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(584, 0); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(200, 461); + groupBoxTools.Size = new Size(200, 500); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // groupBoxCollections + // + groupBoxCollections.Controls.Add(ButtonRemoveObject); + groupBoxCollections.Controls.Add(listBoxStorages); + groupBoxCollections.Controls.Add(ButtonAddObject); + groupBoxCollections.Controls.Add(textBoxStorageName); + groupBoxCollections.Location = new Point(6, 22); + groupBoxCollections.Name = "groupBoxCollections"; + groupBoxCollections.Size = new Size(188, 261); + groupBoxCollections.TabIndex = 4; + groupBoxCollections.TabStop = false; + groupBoxCollections.Text = "Наборы"; + // + // ButtonRemoveObject + // + ButtonRemoveObject.Location = new Point(6, 214); + ButtonRemoveObject.Name = "ButtonRemoveObject"; + ButtonRemoveObject.Size = new Size(176, 41); + ButtonRemoveObject.TabIndex = 3; + ButtonRemoveObject.Text = "Удалить набор"; + ButtonRemoveObject.UseVisualStyleBackColor = true; + ButtonRemoveObject.Click += ButtonRemoveObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(6, 99); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(176, 109); + listBoxStorages.TabIndex = 2; + listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(6, 54); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(176, 39); + ButtonAddObject.TabIndex = 1; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += buttonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(6, 25); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(176, 23); + textBoxStorageName.TabIndex = 0; + // // FormBoatCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(784, 461); + ClientSize = new Size(784, 500); Controls.Add(groupBoxTools); Controls.Add(pictureBoxCollection); Name = "FormBoatCollection"; @@ -110,6 +167,8 @@ ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); groupBoxTools.ResumeLayout(false); groupBoxTools.PerformLayout(); + groupBoxCollections.ResumeLayout(false); + groupBoxCollections.PerformLayout(); ResumeLayout(false); } @@ -121,5 +180,10 @@ private Button ButtonRemoveBoat; private Button ButtonRefreshCollection; private GroupBox groupBoxTools; + private GroupBox groupBoxCollections; + private Button ButtonRemoveObject; + private ListBox listBoxStorages; + private Button ButtonAddObject; + private MaskedTextBox textBoxStorageName; } } \ No newline at end of file diff --git a/MotorBoat/MotorBoat/FormBoatCollection.cs b/MotorBoat/MotorBoat/FormBoatCollection.cs index d78e35e..c209ca6 100644 --- a/MotorBoat/MotorBoat/FormBoatCollection.cs +++ b/MotorBoat/MotorBoat/FormBoatCollection.cs @@ -21,7 +21,7 @@ namespace MotorBoat /// /// Набор объектов /// - private readonly BoatsGenericCollection _boats; + private readonly BoatsGenericStorage _storage; /// /// Конструктор @@ -29,7 +29,74 @@ namespace MotorBoat public FormBoatCollection() { InitializeComponent(); - _boats = new BoatsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + /// + /// Заполнение listBoxStorages + /// + 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]?.ShowBoats(); + } + + /// + /// Удаление набора + /// + /// + /// + private void ButtonRemoveObject_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(); + } } /// @@ -39,13 +106,21 @@ namespace MotorBoat /// private void ButtonAddBoat_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + return; + + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + + if (obj == null) + return; + FormMotorBoat form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_boats + form.SelectedBoat != -1) + if (obj + form.SelectedBoat) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _boats.ShowBoats(); + pictureBoxCollection.Image = obj.ShowBoats(); } else { @@ -61,22 +136,29 @@ namespace MotorBoat /// private void ButtonRemoveBoat_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 (_boats - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _boats.ShowBoats(); + pictureBoxCollection.Image = obj.ShowBoats(); } else { MessageBox.Show("Не удалось удалить объект"); } - } /// @@ -86,7 +168,15 @@ namespace MotorBoat /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _boats.ShowBoats(); + if (listBoxStorages.SelectedIndex == -1) + return; + + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + + if (obj == null) + return; + + pictureBoxCollection.Image = obj.ShowBoats(); } } } \ No newline at end of file diff --git a/MotorBoat/MotorBoat/SetGeneric.cs b/MotorBoat/MotorBoat/SetGeneric.cs index 58727a6..5f137c1 100644 --- a/MotorBoat/MotorBoat/SetGeneric.cs +++ b/MotorBoat/MotorBoat/SetGeneric.cs @@ -14,14 +14,19 @@ namespace MotorBoat.Generics 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; /// /// Конструктор @@ -29,47 +34,40 @@ namespace MotorBoat.Generics /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор /// - /// Добавляемый автомобиль + /// Добавляемая лодка /// - public int Insert(T boat) + public bool Insert(T boat) { - return Insert(boat, 0); + if (_places.Count == _maxCount) + { + return false; + } + Insert(boat, 0); + return true; } /// /// Добавление объекта в набор на конкретную позицию /// - /// Добавляемый автомобиль + /// Добавляемая лодка /// Позиция /// - public int Insert(T boat, int position) + public bool Insert(T boat, int position) { - int nullIndex = -1, i; - if (position < 0 || position >= Count) + if(!(position >= 0 && position <= Count && _places.Count < _maxCount)) { - return -1; + return false; } - for (i = position; i < Count; i++) - { - 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] = boat; - return position; + _places.Insert(position, boat); + return true; } + /// /// Удаление объекта из набора с конкретной позиции /// @@ -78,7 +76,7 @@ namespace MotorBoat.Generics public bool Remove(int position) { if (position < 0 || position >= Count) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -86,10 +84,40 @@ namespace MotorBoat.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) return null; - return _places[position]; + get + { + if (position < 0 || position > _maxCount) + return null; + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + { + return; + } + _places.Insert(position, value); + return; + } + } + + /// + /// Проход по списку + /// + /// + /// + public IEnumerable GetBoats(int? maxBoats = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxBoats.HasValue && i == maxBoats.Value) + { + yield break; + } + } } } } \ No newline at end of file -- 2.25.1