From 203249781cebf4a22aed89a3685005bc3b08349f Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Sat, 21 Oct 2023 03:21:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormShipCollection.Designer.cs | 100 ++++++++++---- .../Lab1ContainersShip/FormShipCollection.cs | 99 +++++++++++-- .../Lab1ContainersShip.csproj | 1 + .../Lab1ContainersShip/SetGeneric.cs | 130 ++++++++++-------- .../ShipGenericCollection.cs | 16 ++- .../Lab1ContainersShip/ShipGenericStorage.cs | 96 +++++++++++++ 6 files changed, 339 insertions(+), 103 deletions(-) create mode 100644 Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs diff --git a/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.Designer.cs b/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.Designer.cs index 16457f6..70162f1 100644 --- a/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.Designer.cs +++ b/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.Designer.cs @@ -29,10 +29,14 @@ private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); - this.ButtonAddCar = new System.Windows.Forms.Button(); - this.ButtonRemoveCar = new System.Windows.Forms.Button(); - this.ButtonRefreshCollection = new System.Windows.Forms.Button(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.ButtonAddObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.ButtonDelObject = new System.Windows.Forms.Button(); this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); + this.ButtonRefreshCollection = new System.Windows.Forms.Button(); + this.ButtonRemoveCar = new System.Windows.Forms.Button(); + this.ButtonAddCar = new System.Windows.Forms.Button(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); @@ -40,6 +44,10 @@ // // panel1 // + this.panel1.Controls.Add(this.textBoxStorageName); + this.panel1.Controls.Add(this.ButtonAddObject); + this.panel1.Controls.Add(this.listBoxStorages); + this.panel1.Controls.Add(this.ButtonDelObject); this.panel1.Controls.Add(this.maskedTextBoxNumber); this.panel1.Controls.Add(this.ButtonRefreshCollection); this.panel1.Controls.Add(this.ButtonRemoveCar); @@ -49,42 +57,78 @@ this.panel1.Size = new System.Drawing.Size(157, 449); this.panel1.TabIndex = 1; // - // ButtonAddCar + // textBoxStorageName // - this.ButtonAddCar.Location = new System.Drawing.Point(16, 29); - this.ButtonAddCar.Name = "ButtonAddCar"; - this.ButtonAddCar.Size = new System.Drawing.Size(118, 53); - this.ButtonAddCar.TabIndex = 0; - this.ButtonAddCar.Text = "добавить"; - this.ButtonAddCar.UseVisualStyleBackColor = true; - this.ButtonAddCar.Click += new System.EventHandler(this.ButtonAddCar_Click); + this.textBoxStorageName.Location = new System.Drawing.Point(18, 25); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(118, 20); + this.textBoxStorageName.TabIndex = 5; // - // ButtonRemoveCar + // ButtonAddObject // - this.ButtonRemoveCar.Location = new System.Drawing.Point(16, 209); - this.ButtonRemoveCar.Name = "ButtonRemoveCar"; - this.ButtonRemoveCar.Size = new System.Drawing.Size(118, 50); - this.ButtonRemoveCar.TabIndex = 1; - this.ButtonRemoveCar.Text = "удалить"; - this.ButtonRemoveCar.UseVisualStyleBackColor = true; - this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click); + this.ButtonAddObject.Location = new System.Drawing.Point(18, 51); + this.ButtonAddObject.Name = "ButtonAddObject"; + this.ButtonAddObject.Size = new System.Drawing.Size(118, 29); + this.ButtonAddObject.TabIndex = 2; + this.ButtonAddObject.Text = "добавить набор"; + this.ButtonAddObject.UseVisualStyleBackColor = true; + this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.Location = new System.Drawing.Point(18, 86); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(120, 95); + this.listBoxStorages.TabIndex = 4; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); + // + // ButtonDelObject + // + this.ButtonDelObject.Location = new System.Drawing.Point(18, 187); + this.ButtonDelObject.Name = "ButtonDelObject"; + this.ButtonDelObject.Size = new System.Drawing.Size(118, 30); + this.ButtonDelObject.TabIndex = 2; + this.ButtonDelObject.Text = "удалить набор"; + this.ButtonDelObject.UseVisualStyleBackColor = true; + this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(16, 330); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20); + this.maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(16, 341); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(16, 401); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(118, 50); + this.ButtonRefreshCollection.Size = new System.Drawing.Size(118, 37); this.ButtonRefreshCollection.TabIndex = 2; this.ButtonRefreshCollection.Text = "обновить коллекцию"; this.ButtonRefreshCollection.UseVisualStyleBackColor = true; this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // - // maskedTextBoxNumber + // ButtonRemoveCar // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(25, 183); - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20); - this.maskedTextBoxNumber.TabIndex = 3; + this.ButtonRemoveCar.Location = new System.Drawing.Point(16, 356); + this.ButtonRemoveCar.Name = "ButtonRemoveCar"; + this.ButtonRemoveCar.Size = new System.Drawing.Size(118, 39); + this.ButtonRemoveCar.TabIndex = 1; + this.ButtonRemoveCar.Text = "удалить контейнеровоз"; + this.ButtonRemoveCar.UseVisualStyleBackColor = true; + this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click); + // + // ButtonAddCar + // + this.ButtonAddCar.Location = new System.Drawing.Point(16, 286); + this.ButtonAddCar.Name = "ButtonAddCar"; + this.ButtonAddCar.Size = new System.Drawing.Size(118, 38); + this.ButtonAddCar.TabIndex = 0; + this.ButtonAddCar.Text = "добавить контейнеровоз"; + this.ButtonAddCar.UseVisualStyleBackColor = true; + this.ButtonAddCar.Click += new System.EventHandler(this.ButtonAddCar_Click); // // pictureBoxCollection // @@ -119,5 +163,9 @@ private System.Windows.Forms.Button ButtonRefreshCollection; private System.Windows.Forms.Button ButtonRemoveCar; private System.Windows.Forms.Button ButtonAddCar; + private System.Windows.Forms.TextBox textBoxStorageName; + private System.Windows.Forms.Button ButtonAddObject; + private System.Windows.Forms.ListBox listBoxStorages; + private System.Windows.Forms.Button ButtonDelObject; } } \ No newline at end of file diff --git a/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.cs b/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.cs index c8f1303..cd21bce 100644 --- a/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.cs +++ b/Lab1ContainersShip/Lab1ContainersShip/FormShipCollection.cs @@ -14,28 +14,73 @@ namespace Lab1ContainersShip { public partial class FormShipCollection : Form { - private readonly ShipGenericCollection _ships; + private readonly ShipGenericStorage _storage; public FormShipCollection() { InitializeComponent(); - _ships = new ShipGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new ShipGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } private void FormShipCollection_Load(object sender, EventArgs e) { } + 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]?.ShowShips(); + + } + + + private void ButtonAddCar_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 FormContainerShip(); if (form.ShowDialog() == DialogResult.OK) { - if (_ships + form.SelectedShip != -1) + if ((obj + form.SelectedShip) != 1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _ships.ShowShips(); + pictureBoxCollection.Image = obj.ShowShips(); } else { @@ -43,20 +88,31 @@ DrawningObjectShip>(pictureBoxCollection.Width, pictureBoxCollection.Height); } } + } private void ButtonRemoveCar_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) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (!(_ships - pos)) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _ships.ShowShips(); + pictureBoxCollection.Image = obj.ShowShips(); } else { @@ -67,7 +123,32 @@ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _ships.ShowShips(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowShips(); + + } + + 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(); + } } } } diff --git a/Lab1ContainersShip/Lab1ContainersShip/Lab1ContainersShip.csproj b/Lab1ContainersShip/Lab1ContainersShip/Lab1ContainersShip.csproj index 32ff75e..f83dbfb 100644 --- a/Lab1ContainersShip/Lab1ContainersShip/Lab1ContainersShip.csproj +++ b/Lab1ContainersShip/Lab1ContainersShip/Lab1ContainersShip.csproj @@ -73,6 +73,7 @@ + FormContainerShip.cs diff --git a/Lab1ContainersShip/Lab1ContainersShip/SetGeneric.cs b/Lab1ContainersShip/Lab1ContainersShip/SetGeneric.cs index b6017c9..000768f 100644 --- a/Lab1ContainersShip/Lab1ContainersShip/SetGeneric.cs +++ b/Lab1ContainersShip/Lab1ContainersShip/SetGeneric.cs @@ -14,18 +14,20 @@ namespace Lab1ContainersShip // /// Массив объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + public int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор @@ -35,35 +37,13 @@ namespace Lab1ContainersShip public int Insert(T ship) { // TODO вставка в начало набора - int temp = 0; - int k = 0; - for(int j = 0; j < _places.Length; j++) { - if (_places[j] != null) - { - k++; - } - } - if (k == _places.Length) + if(_places.Count >= _maxCount) { return -1; } else { - for (int i = 0; i < _places.Length; i++) - { - if (_places[i] == null) - { - temp = i; - break; - } - - } - for (int i = temp; i > 0; i--) - { - _places[i] = _places[i -1]; - - } - _places[0] = ship; + _places.Insert(0, ship); return 0; } } @@ -83,40 +63,23 @@ namespace Lab1ContainersShip // сдвиг всех объектов, находящихся справа от //позиции до первого пустого элемента // TODO вставка по позиции - int temp = 0; - int k = 0; - for (int j = position; j < _places.Length; j++) - { - if (_places[j] != null) - { - k++; - } - } - if (position < _places.Length && k < _places.Length-position) - { - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - temp = i; - break; - } - - } - for (int i = temp; i > position; i--) - { - _places[i] = _places[i - 1]; - - } - - _places[position] = ship; - return true; - } - else + if(_places.Count >= _maxCount) { return false; } - + if(position < 0 || position > _places.Count) + { + return false; + } + if(position == _places.Count) + { + _places.Add(ship); + } + else + { + _places.Insert(position, ship); + } + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -128,7 +91,7 @@ namespace Lab1ContainersShip // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива //значение null - if(position < _places.Length) + if(position < _places.Count && _places.Count < _maxCount) { _places[position] = null; return true; @@ -147,7 +110,7 @@ namespace Lab1ContainersShip public T Get(int position) { // TODO проверка позиции - if(position < _places.Length) + if(position < _places.Count && position >= 0) { return _places[position]; } @@ -156,6 +119,51 @@ namespace Lab1ContainersShip return null; } } - + public T this[int position] + { + get + { + if (position < _places.Count && position >= 0) + { + return _places[position]; + } + else + { + return null; + } + // TODO проверка позиции + + } + set + { + /*if ((position < _places.Count && position >= 0) && _places.Count < _maxCount) + { + _places[position] = value; + } + + else + { + return; + }*/ + Insert(value, position); + + // TODO проверка позиции + // TODO проверка свободных мест в списке + // TODO вставка в список по позиции + } + } + public IEnumerable GetShips(int? maxCars = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCars.HasValue && i == maxCars.Value) + { + yield break; + } + } + } + + } } diff --git a/Lab1ContainersShip/Lab1ContainersShip/ShipGenericCollection.cs b/Lab1ContainersShip/Lab1ContainersShip/ShipGenericCollection.cs index c8272cd..6fd49c7 100644 --- a/Lab1ContainersShip/Lab1ContainersShip/ShipGenericCollection.cs +++ b/Lab1ContainersShip/Lab1ContainersShip/ShipGenericCollection.cs @@ -70,7 +70,7 @@ namespace Lab1ContainersShip public static bool operator -(ShipGenericCollection collect, int pos) { - T obj = collect._collection.Get(pos); + T obj = collect._collection[pos]; if (obj != null) { return collect._collection.Remove(pos); @@ -84,7 +84,7 @@ namespace Lab1ContainersShip /// public U GetU(int pos) { - return (U)_collection.Get(pos)?.GetMoveableObject; + return (U)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -124,15 +124,17 @@ namespace Lab1ContainersShip /// private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var ship in _collection.GetShips()) { - T t = _collection.Get(i); - if (t != null) + + if (ship != null) { - t.SetPosition(((_collection.Count -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection.Count - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); - t.DrawShip(g); + ship.SetPosition(((_collection._maxCount -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection._maxCount - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); + ship.DrawShip(g); } + i++; // TODO получение объекта // TODO установка позиции // TODO прорисовка объекта diff --git a/Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs b/Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs new file mode 100644 index 0000000..5efa8d2 --- /dev/null +++ b/Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using Lab1ContainersShip.DrawingObjects; +using Lab1ContainersShip.MovementStrategy; + + +namespace Lab1ContainersShip +{ + internal class ShipGenericStorage + { + readonly Dictionary> _shipStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _shipStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public ShipGenericStorage(int pictureWidth, int pictureHeight) + { + _shipStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + // TODO Прописать логику для добавления + if (_shipStorages.ContainsKey(name)) + { + return; + } + else + { + _shipStorages[name] = new ShipGenericCollection(_pictureWidth, _pictureHeight); + } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_shipStorages.ContainsKey(name)) + { + return; + } + else + { + _shipStorages.Remove(name); + } + // TODO Прописать логику для удаления + } + /// + /// Доступ к набору + /// + /// + /// + public ShipGenericCollection + this[string ind] + { + get + { + // TODO Продумать логику получения набора + if (_shipStorages.ContainsKey(ind)) + { + return _shipStorages[ind]; + } + else + { + return null; + } + } + } + + } +}