From 1edff814a3e06697a3a12eb27af9fc1272122c3e Mon Sep 17 00:00:00 2001 From: ValAnn Date: Sat, 9 Dec 2023 11:00:23 +0400 Subject: [PATCH 1/2] done --- DumpTruck/DumpTruck/CarsGenericCollection.cs | 30 ++-- DumpTruck/DumpTruck/CarsGenericStorage.cs | 83 +++++++++++ .../DumpTruck/FormCarCollection.Designer.cs | 136 ++++++++++++++---- DumpTruck/DumpTruck/FormCarCollection.cs | 129 ++++++++++++++--- DumpTruck/DumpTruck/SetGeneric.cs | 71 ++++++--- 5 files changed, 368 insertions(+), 81 deletions(-) create mode 100644 DumpTruck/DumpTruck/CarsGenericStorage.cs diff --git a/DumpTruck/DumpTruck/CarsGenericCollection.cs b/DumpTruck/DumpTruck/CarsGenericCollection.cs index 5b79fe6..880078c 100644 --- a/DumpTruck/DumpTruck/CarsGenericCollection.cs +++ b/DumpTruck/DumpTruck/CarsGenericCollection.cs @@ -59,6 +59,7 @@ namespace DumpTruck.Generics { return -1; } + return collect._collection.Insert(obj); } /// @@ -67,16 +68,17 @@ namespace DumpTruck.Generics /// /// /// - public static bool operator -(CarsGenericCollection collect, int - pos) + public static T? operator -(CarsGenericCollection collect, int +pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); } - return true; + return obj; } + /// /// Получение объекта IMoveableObject /// @@ -124,21 +126,23 @@ namespace DumpTruck.Generics /// private void DrawObjects(Graphics g) { - DrawningCar car; + int numPlacesInRow = _pictureWidth / _placeSizeWidth; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var car in _collection.GetCars()) { - // TODO получение объекта - // TODO установка позиции - // TODO прорисовка объекта - car = _collection.Get(i); + if (car != null) { - car.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10); - //car.SetPosition(_placeSizeWidth * (i/ numPlacesInColumn) + _placeSizeWidth / 20, (i % numPlacesInColumn ) *_placeSizeHeight + _placeSizeHeight / 10); - car.DrawTransport(g); + + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + car.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10); + car.DrawTransport(g); } + i += 1; } } } diff --git a/DumpTruck/DumpTruck/CarsGenericStorage.cs b/DumpTruck/DumpTruck/CarsGenericStorage.cs new file mode 100644 index 0000000..4409f1d --- /dev/null +++ b/DumpTruck/DumpTruck/CarsGenericStorage.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawningObjects; +using DumpTruck.MovementStrategy; + +namespace DumpTruck.Generics +{ + internal class CarsGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _carStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _carStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public CarsGenericStorage(int pictureWidth, int pictureHeight) + { + _carStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + // TODO Прописать логику для добавления + _carStorages.Add(name, new CarsGenericCollection(_pictureWidth, _pictureHeight)); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_carStorages.ContainsKey(name)) + { + return; + } + _carStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public CarsGenericCollection? + this[string ind] + { + get + { + if (_carStorages.ContainsKey(ind)) + { + return _carStorages[ind]; + } + return null; + } + } + } + +} + diff --git a/DumpTruck/DumpTruck/FormCarCollection.Designer.cs b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs index 390d97c..9d28df0 100644 --- a/DumpTruck/DumpTruck/FormCarCollection.Designer.cs +++ b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs @@ -30,55 +30,43 @@ { this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.panelCollection = new System.Windows.Forms.Panel(); + this.ButtonRefreshCollection = new System.Windows.Forms.Button(); + this.panel1 = new System.Windows.Forms.Panel(); this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); this.ButtonRemoveCar = new System.Windows.Forms.Button(); - this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonAddCar = new System.Windows.Forms.Button(); + this.panelObjects = new System.Windows.Forms.Panel(); + this.ButtonDelObject_ = new System.Windows.Forms.Button(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.ButtonAddObject = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.panelCollection.SuspendLayout(); + this.panel1.SuspendLayout(); + this.panelObjects.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // - this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(800, 450); + this.pictureBoxCollection.Size = new System.Drawing.Size(630, 482); this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; // // panelCollection // - this.panelCollection.Controls.Add(this.maskedTextBoxNumber); - this.panelCollection.Controls.Add(this.ButtonRemoveCar); this.panelCollection.Controls.Add(this.ButtonRefreshCollection); - this.panelCollection.Controls.Add(this.ButtonAddCar); - this.panelCollection.Location = new System.Drawing.Point(601, 12); + this.panelCollection.Controls.Add(this.panel1); + this.panelCollection.Controls.Add(this.panelObjects); + this.panelCollection.Location = new System.Drawing.Point(636, 0); this.panelCollection.Name = "panelCollection"; - this.panelCollection.Size = new System.Drawing.Size(187, 426); + this.panelCollection.Size = new System.Drawing.Size(217, 470); this.panelCollection.TabIndex = 1; // - // maskedTextBoxNumber - // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 96); - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(181, 23); - this.maskedTextBoxNumber.TabIndex = 2; - this.maskedTextBoxNumber.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.maskedTextBoxNumber_MaskInputRejected); - // - // ButtonRemoveCar - // - this.ButtonRemoveCar.Location = new System.Drawing.Point(3, 146); - this.ButtonRemoveCar.Name = "ButtonRemoveCar"; - this.ButtonRemoveCar.Size = new System.Drawing.Size(181, 41); - this.ButtonRemoveCar.TabIndex = 2; - this.ButtonRemoveCar.Text = "Удалить машину"; - this.ButtonRemoveCar.UseVisualStyleBackColor = true; - this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click); - // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(3, 193); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(20, 414); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(181, 41); this.ButtonRefreshCollection.TabIndex = 1; @@ -86,9 +74,37 @@ this.ButtonRefreshCollection.UseVisualStyleBackColor = true; this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // + // panel1 + // + this.panel1.Controls.Add(this.maskedTextBoxNumber); + this.panel1.Controls.Add(this.ButtonRemoveCar); + this.panel1.Controls.Add(this.ButtonAddCar); + this.panel1.Location = new System.Drawing.Point(17, 265); + this.panel1.Name = "panel1"; + this.panel1.Size = new System.Drawing.Size(187, 128); + this.panel1.TabIndex = 3; + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 50); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(181, 23); + this.maskedTextBoxNumber.TabIndex = 2; + this.maskedTextBoxNumber.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.maskedTextBoxNumber_MaskInputRejected); + // + // ButtonRemoveCar + // + this.ButtonRemoveCar.Location = new System.Drawing.Point(3, 79); + this.ButtonRemoveCar.Name = "ButtonRemoveCar"; + this.ButtonRemoveCar.Size = new System.Drawing.Size(181, 41); + this.ButtonRemoveCar.TabIndex = 2; + this.ButtonRemoveCar.Text = "Удалить машину"; + this.ButtonRemoveCar.UseVisualStyleBackColor = true; + this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click); + // // ButtonAddCar // - this.ButtonAddCar.Location = new System.Drawing.Point(3, 37); + this.ButtonAddCar.Location = new System.Drawing.Point(3, 3); this.ButtonAddCar.Name = "ButtonAddCar"; this.ButtonAddCar.Size = new System.Drawing.Size(181, 41); this.ButtonAddCar.TabIndex = 0; @@ -96,18 +112,70 @@ this.ButtonAddCar.UseVisualStyleBackColor = true; this.ButtonAddCar.Click += new System.EventHandler(this.ButtonAddCar_Click); // + // panelObjects + // + this.panelObjects.Controls.Add(this.ButtonDelObject_); + this.panelObjects.Controls.Add(this.textBoxStorageName); + this.panelObjects.Controls.Add(this.listBoxStorages); + this.panelObjects.Controls.Add(this.ButtonAddObject); + this.panelObjects.Location = new System.Drawing.Point(17, 3); + this.panelObjects.Name = "panelObjects"; + this.panelObjects.Size = new System.Drawing.Size(187, 241); + this.panelObjects.TabIndex = 4; + // + // ButtonDelObject_ + // + this.ButtonDelObject_.Location = new System.Drawing.Point(3, 194); + this.ButtonDelObject_.Name = "ButtonDelObject_"; + this.ButtonDelObject_.Size = new System.Drawing.Size(181, 41); + this.ButtonDelObject_.TabIndex = 3; + this.ButtonDelObject_.Text = "Удалить набор"; + this.ButtonDelObject_.UseVisualStyleBackColor = true; + this.ButtonDelObject_.Click += new System.EventHandler(this.ButtonDelObject__Click); + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(3, 12); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(181, 23); + this.textBoxStorageName.TabIndex = 3; + this.textBoxStorageName.TextChanged += new System.EventHandler(this.textBoxStorageName_TextChanged); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 15; + this.listBoxStorages.Location = new System.Drawing.Point(3, 109); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(181, 79); + this.listBoxStorages.TabIndex = 2; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); + // + // ButtonAddObject + // + this.ButtonAddObject.Location = new System.Drawing.Point(3, 41); + this.ButtonAddObject.Name = "ButtonAddObject"; + this.ButtonAddObject.Size = new System.Drawing.Size(181, 41); + this.ButtonAddObject.TabIndex = 1; + this.ButtonAddObject.Text = "Добавить набор"; + this.ButtonAddObject.UseVisualStyleBackColor = true; + this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // // FormCarCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(848, 482); this.Controls.Add(this.panelCollection); this.Controls.Add(this.pictureBoxCollection); this.Name = "FormCarCollection"; - this.Text = "Form2"; + this.Text = "Набор грузовиков"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.panelCollection.ResumeLayout(false); - this.panelCollection.PerformLayout(); + this.panel1.ResumeLayout(false); + this.panel1.PerformLayout(); + this.panelObjects.ResumeLayout(false); + this.panelObjects.PerformLayout(); this.ResumeLayout(false); } @@ -120,5 +188,11 @@ private MaskedTextBox maskedTextBoxNumber; private Button ButtonRemoveCar; private Button ButtonRefreshCollection; + private Panel panelObjects; + private ListBox listBoxStorages; + private Button ButtonAddObject; + private Panel panel1; + private Button ButtonDelObject_; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormCarCollection.cs b/DumpTruck/DumpTruck/FormCarCollection.cs index 8965dde..455780c 100644 --- a/DumpTruck/DumpTruck/FormCarCollection.cs +++ b/DumpTruck/DumpTruck/FormCarCollection.cs @@ -19,12 +19,13 @@ namespace DumpTruck { public partial class FormCarCollection : Form { - private readonly CarsGenericCollection _cars; - + //private readonly CarsGenericCollection _cars; + private readonly CarsGenericStorage _storage; public FormCarCollection() { InitializeComponent(); - _cars = new CarsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new CarsGenericStorage(pictureBoxCollection.Width, +pictureBoxCollection.Height); } private void Form2_Load(object sender, EventArgs e) @@ -37,42 +38,84 @@ namespace DumpTruck } + 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 ButtonAddCar_Click(object sender, EventArgs e) { + + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + + if (obj == null) + { + + return; + } + FormDumpTruck form = new(); - - if (form.ShowDialog() == DialogResult.OK) { - if (_cars + form.SelectedCar != null) + + if (obj + form.SelectedCar != -1) { + MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _cars.ShowCars(); + pictureBoxCollection.Image = obj.ShowCars(); } else { MessageBox.Show("Не удалось добавить объект"); } } + } private void ButtonRemoveCar_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", -MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorages.SelectedIndex == -1) { return; } - int pos; - if (maskedTextBoxNumber.Text == null || !int.TryParse(maskedTextBoxNumber.Text, out pos)) { - MessageBox.Show("Введите номер парковочного места"); - return; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; } - - if (_cars - pos != null) + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _cars.ShowCars(); + pictureBoxCollection.Image = obj.ShowCars(); } else { @@ -83,12 +126,64 @@ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _cars.ShowCars(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowCars(); } private void maskedTextBoxNumber_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) { } + + 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]?.ShowCars(); + } + + 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(); + } + + } + + private void textBoxStorageName_TextChanged(object sender, EventArgs e) + { + + } } } + diff --git a/DumpTruck/DumpTruck/SetGeneric.cs b/DumpTruck/DumpTruck/SetGeneric.cs index c76f900..cdea295 100644 --- a/DumpTruck/DumpTruck/SetGeneric.cs +++ b/DumpTruck/DumpTruck/SetGeneric.cs @@ -14,20 +14,23 @@ namespace DumpTruck.Generics /// /// Массив объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; /// /// Конструктор /// /// /// public int startPointer = 0; + + public int countMax = 0; public SetGeneric(int count) { - _places = new T?[count]; + _places = new List(count); + countMax = count; } /// /// Добавление объекта в набор @@ -36,8 +39,9 @@ namespace DumpTruck.Generics /// public int Insert(T car) { - if (_places[Count - 1] != null) - return -1; + + if (_places.Count == countMax) { return -1; } + return Insert(car, 0); } /// @@ -48,22 +52,13 @@ namespace DumpTruck.Generics /// public int Insert(T car, int position) { - if (!(position >= 0 && position < Count)) - return -1; - if (_places[position] != null) - { - int indexEnd = position + 1; - while (_places[indexEnd] != null) - { - indexEnd++; - } - for (int i = indexEnd + 1; i > position; i--) - { - _places[i] = _places[i - 1]; - } - } - _places[position] = car; + if (!(position >= 0 && position <= Count && _places.Count < countMax)) + { + return -1; + } + + _places.Insert(position, car); return position; } // TODO проверка, что элемент массива по этой позиции пустой, если нет, то @@ -89,6 +84,42 @@ namespace DumpTruck.Generics } + + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? this[int position] + { + get + { + // TODO проверка позиции + return _places[position]; + } + set + { + // TODO проверка позиции + // TODO проверка свободных мест в списке + // TODO вставка в список по позиции + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetCars(int? maxCars = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCars.HasValue && i == maxCars.Value) + { + yield break; + } + } + } + /// /// Получение объекта из набора по позиции /// -- 2.25.1 From 07a37a10ffe11e988034f6f5a98355c1f0f9093a Mon Sep 17 00:00:00 2001 From: ValAnn Date: Sat, 9 Dec 2023 11:11:12 +0400 Subject: [PATCH 2/2] done done --- DumpTruck/DumpTruck/SetGeneric.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DumpTruck/DumpTruck/SetGeneric.cs b/DumpTruck/DumpTruck/SetGeneric.cs index cdea295..f8f1590 100644 --- a/DumpTruck/DumpTruck/SetGeneric.cs +++ b/DumpTruck/DumpTruck/SetGeneric.cs @@ -92,16 +92,16 @@ namespace DumpTruck.Generics /// public T? this[int position] { - get - { - // TODO проверка позиции + get { + if (!(position >= 0 && position <= Count)) + return null; return _places[position]; } set { - // TODO проверка позиции - // TODO проверка свободных мест в списке - // TODO вставка в список по позиции + if (!(position >= 0 && position <= Count)) + return; + _places.Insert(position, value); } } /// -- 2.25.1