From f80ead5cf8860b87f98f69c05acb7de0d49dd31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 7 Oct 2023 00:59:22 +0400 Subject: [PATCH 1/3] Done --- .../DrawningObjects/DrawningBus.cs | 7 + .../FormBusCollection.Designer.cs | 133 +++++++++++++++ .../FormBusCollection.cs | 92 +++++++++++ .../FormBusCollection.resx | 120 ++++++++++++++ .../FormDoubleDeckerBus.Designer.cs | 14 ++ .../FormDoubleDeckerBus.cs | 66 ++++++-- .../Generics/SetGeneric.cs | 118 +++++++++++++ .../Generics/TheBusesGenericCollection.cs | 155 ++++++++++++++++++ .../Program.cs | 2 +- 9 files changed, 689 insertions(+), 18 deletions(-) create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.resx create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs index 16b3f7a..a67f67b 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/DrawningObjects/DrawningBus.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Entities; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy; namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects { @@ -17,6 +18,12 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects /// public EntityBus? EntityBus { get; protected set; } + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectBus(this); + + /// /// Ширина окна /// diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs new file mode 100644 index 0000000..5792355 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs @@ -0,0 +1,133 @@ +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base +{ + partial class FormBusCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + toolsLabel = new Panel(); + ButtonRefreshCollection = new Button(); + ButtonDeleteBus = new Button(); + maskedTextBoxNumber = new TextBox(); + addBus = new Button(); + label1 = new Label(); + pictureBoxCollection = new PictureBox(); + toolsLabel.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // toolsLabel + // + toolsLabel.Controls.Add(ButtonRefreshCollection); + toolsLabel.Controls.Add(ButtonDeleteBus); + toolsLabel.Controls.Add(maskedTextBoxNumber); + toolsLabel.Controls.Add(addBus); + toolsLabel.Controls.Add(label1); + toolsLabel.Location = new Point(581, -2); + toolsLabel.Name = "toolsLabel"; + toolsLabel.Size = new Size(221, 451); + toolsLabel.TabIndex = 0; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new Point(10, 194); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(197, 41); + ButtonRefreshCollection.TabIndex = 4; + ButtonRefreshCollection.Text = "Обновить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // ButtonDeleteBus + // + ButtonDeleteBus.Location = new Point(10, 121); + ButtonDeleteBus.Name = "ButtonDeleteBus"; + ButtonDeleteBus.Size = new Size(197, 41); + ButtonDeleteBus.TabIndex = 3; + ButtonDeleteBus.Text = "Удалить автобус"; + ButtonDeleteBus.UseVisualStyleBackColor = true; + ButtonDeleteBus.Click += ButtonDeleteBus_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(50, 88); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(125, 27); + maskedTextBoxNumber.TabIndex = 2; + // + // addBus + // + addBus.Location = new Point(10, 26); + addBus.Name = "addBus"; + addBus.Size = new Size(197, 41); + addBus.TabIndex = 1; + addBus.Text = "Добавить автобус"; + addBus.UseVisualStyleBackColor = true; + addBus.Click += ButtonAddBus_Click; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(5, 0); + label1.Name = "label1"; + label1.Size = new Size(103, 20); + label1.TabIndex = 0; + label1.Text = "Инструменты"; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(1, -2); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(579, 451); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormBusCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBoxCollection); + Controls.Add(toolsLabel); + Name = "FormBusCollection"; + Text = "Набор автобусов"; + toolsLabel.ResumeLayout(false); + toolsLabel.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel toolsLabel; + private Label label1; + private Button ButtonDeleteBus; + private Button addBus; + private Button ButtonRefreshCollection; + private PictureBox pictureBoxCollection; + public TextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs new file mode 100644 index 0000000..b6e8277 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs @@ -0,0 +1,92 @@ +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics; +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 PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base +{ + /// + /// Форма для работы с набором объектов класса DrawningBus + /// + public partial class FormBusCollection : Form + { + /// + /// Набор объектов + /// + private readonly TheBusesGenericCollection _theBuses; + + /// + /// Конструктор + /// + public FormBusCollection() + { + InitializeComponent(); + _theBuses = new TheBusesGenericCollection + (pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddBus_Click(object sender, EventArgs e) + { + FormDoubleDeckerBus form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_theBuses + form.SelectedBus) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _theBuses.ShowTheBuses(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonDeleteBus_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_theBuses - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _theBuses.ShowTheBuses(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _theBuses.ShowTheBuses(); + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.resx b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs index 6419c2f..2b0ea68 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.Designer.cs @@ -37,6 +37,7 @@ comboBoxStrategy = new ComboBox(); buttonStep = new Button(); buttonCreateBus = new Button(); + ButtonSelectedBus = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); SuspendLayout(); // @@ -141,11 +142,23 @@ buttonCreateBus.UseVisualStyleBackColor = true; buttonCreateBus.Click += buttonCreateBus_Click; // + // ButtonSelectedBus + // + ButtonSelectedBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonSelectedBus.Location = new Point(358, 389); + ButtonSelectedBus.Name = "ButtonSelectedBus"; + ButtonSelectedBus.Size = new Size(167, 52); + ButtonSelectedBus.TabIndex = 11; + ButtonSelectedBus.Text = "Добавить автобус"; + ButtonSelectedBus.UseVisualStyleBackColor = true; + ButtonSelectedBus.Click += ButtonSelectedBus_Click; + // // FormDoubleDeckerBus // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(882, 453); + Controls.Add(ButtonSelectedBus); Controls.Add(buttonCreateBus); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); @@ -174,5 +187,6 @@ private ComboBox comboBoxStrategy; private Button buttonStep; private Button buttonCreateBus; + private Button ButtonSelectedBus; } } \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs index f94315a..8e184b7 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormDoubleDeckerBus.cs @@ -13,7 +13,12 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// /// /// - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + + /// + /// + /// + public DrawningBus? SelectedBus { get; private set; } /// /// @@ -21,6 +26,8 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base public FormDoubleDeckerBus() { InitializeComponent(); + _strategy = null; + SelectedBus = null; } /// @@ -46,10 +53,24 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base private void ButtonCreateDoubleDeckerBus_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialogColor = new(); + if (dialogColor.ShowDialog() == DialogResult.OK) + { + color = dialogColor.Color; + } + + Color dopColor = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialogDopColor = new(); + if (dialogDopColor.ShowDialog() == DialogResult.OK) + { + dopColor = dialogDopColor.Color; + } + _drawningBus = new DrawningDoubleDeckerBus(random.Next(100, 300), - random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)), + random.Next(1000, 3000), color, dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -64,9 +85,16 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base private void buttonCreateBus_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialogColor = new(); + if (dialogColor.ShowDialog() == DialogResult.OK) + { + color = dialogColor.Color; + } + _drawningBus = new DrawningBus(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); + color, pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -114,33 +142,37 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex - switch + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new - DrawningObjectBus(_drawningBus), pictureBoxDoubleDeckerBus.Width, - pictureBoxDoubleDeckerBus.Height); - comboBoxStrategy.Enabled = false; + _strategy.SetData(_drawningBus.GetMoveableObject, + pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } } + + private void ButtonSelectedBus_Click(object sender, EventArgs e) + { + SelectedBus = _drawningBus; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs new file mode 100644 index 0000000..c041aca --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + + /// + /// Добавление объекта в набор + /// + /// Добавляемый автобус + /// + public bool Insert(T bus) + { + if (_places[Count - 1] != null) + { + return false; + } + + return Insert(bus, 0); + } + + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автобус + /// Позиция + /// + public bool Insert(T bus, int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + + if (_places[position] != null) + { + int ind = position; + + while (ind < Count && _places[ind] != null) + { + ind++; + } + + if (ind == Count) + { + return false; + } + + for (int i = ind - 1; i >= position; i--) + { + _places[i + 1] = _places[i]; + } + } + + _places[position] = bus; + return true; + } + + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= _places.Length || _places[position] == null) + { + return false; + } + + _places[position] = null; + return true; + } + + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + + return _places[position]; + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs new file mode 100644 index 0000000..3eb9dea --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualBasic; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningBus + /// + /// + /// + internal class TheBusesGenericCollection + where T : DrawningBus + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + + /// + /// Конструктор + /// + /// + /// + public TheBusesGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator +(TheBusesGenericCollection collect, T? + obj) + { + if (obj == null) + { + return false; + } + return collect?._collection.Insert(obj) ?? false; + } + + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(TheBusesGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return obj; + } + + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowTheBuses() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + 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); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, + _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + for (int i = 0; i < _collection.Count; i++) + { + DrawningBus bus = _collection.Get(i); + + if (bus != null) + { + int inRow = _pictureWidth / _placeSizeWidth; + bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, + i / inRow * _placeSizeHeight); + bus.DrawTransport(g); + } + } + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs index c3f5dfd..03f361e 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs @@ -11,7 +11,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormDoubleDeckerBus()); + Application.Run(new FormBusCollection()); } } } \ No newline at end of file -- 2.25.1 From f57b0374f49514b33ed316b99f94851ad598ecba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 16 Oct 2023 08:53:46 +0400 Subject: [PATCH 2/3] fix --- .../FormBusCollection.cs | 9 ++-- .../Generics/SetGeneric.cs | 46 +++++++++---------- .../Generics/TheBusesGenericCollection.cs | 14 +++--- 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs index b6e8277..5cfbb8e 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs @@ -43,7 +43,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base FormDoubleDeckerBus form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_theBuses + form.SelectedBus) + if (_theBuses + form.SelectedBus != -1) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = _theBuses.ShowTheBuses(); @@ -62,13 +62,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base /// private void ButtonDeleteBus_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_theBuses - pos != null) + + if (_theBuses - pos != true) { MessageBox.Show("Объект удален"); pictureBoxCollection.Image = _theBuses.ShowTheBuses(); diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs index c041aca..d8ee52f 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs @@ -37,13 +37,8 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// Добавляемый автобус /// - public bool Insert(T bus) + public int Insert(T bus) { - if (_places[Count - 1] != null) - { - return false; - } - return Insert(bus, 0); } @@ -53,35 +48,36 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// Добавляемый автобус /// Позиция /// - public bool Insert(T bus, int position) + public int Insert(T bus, int position) { - if (position < 0 || position >= _places.Length) + int nullIndex = -1, i; + + if (position < 0 || position >= Count) { - return false; + return -1; } - if (_places[position] != null) + for (i = position; i < Count; i++) { - int ind = position; - - while (ind < Count && _places[ind] != null) + if (_places[i] == null) { - ind++; + nullIndex = i; + break; } + } - if (ind == Count) - { - return false; - } + if (nullIndex < 0) + { + return -1; + } - for (int i = ind - 1; i >= position; i--) - { - _places[i + 1] = _places[i]; - } + for (i = nullIndex; i > position; i--) + { + _places[i] = _places[i - 1]; } _places[position] = bus; - return true; + return position; } /// @@ -91,7 +87,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// public bool Remove(int position) { - if (position < 0 || position >= _places.Length || _places[position] == null) + if (position < 0 || position >= Count) { return false; } @@ -107,7 +103,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// public T? Get(int position) { - if (position < 0 || position >= _places.Length) + if (position < 0 || position >= Count) { return null; } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs index 3eb9dea..16415d7 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs @@ -63,14 +63,13 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// /// - public static bool operator +(TheBusesGenericCollection collect, T? - obj) + public static int operator +(TheBusesGenericCollection collect, T? obj) { if (obj == null) { - return false; + return -1; } - return collect?._collection.Insert(obj) ?? false; + return collect._collection.Insert(obj); } /// @@ -79,7 +78,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// /// - public static T? operator -(TheBusesGenericCollection collect, int + public static bool operator -(TheBusesGenericCollection collect, int pos) { T? obj = collect._collection.Get(pos); @@ -87,7 +86,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics { collect._collection.Remove(pos); } - return obj; + return false; } /// @@ -145,8 +144,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics if (bus != null) { int inRow = _pictureWidth / _placeSizeWidth; - bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, - i / inRow * _placeSizeHeight); + bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight); bus.DrawTransport(g); } } -- 2.25.1 From 34ea93003f81fb9a831915a5069902ebb7a07726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 17 Oct 2023 19:40:49 +0400 Subject: [PATCH 3/3] fix --- .../FormBusCollection.Designer.cs | 6 +++--- .../Generics/TheBusesGenericCollection.cs | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs index 5792355..d1a86e3 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs @@ -46,7 +46,7 @@ toolsLabel.Controls.Add(maskedTextBoxNumber); toolsLabel.Controls.Add(addBus); toolsLabel.Controls.Add(label1); - toolsLabel.Location = new Point(581, -2); + toolsLabel.Location = new Point(678, -2); toolsLabel.Name = "toolsLabel"; toolsLabel.Size = new Size(221, 451); toolsLabel.TabIndex = 0; @@ -101,7 +101,7 @@ // pictureBoxCollection.Location = new Point(1, -2); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(579, 451); + pictureBoxCollection.Size = new Size(676, 451); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -109,7 +109,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(900, 450); Controls.Add(pictureBoxCollection); Controls.Add(toolsLabel); Name = "FormBusCollection"; diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs index 16415d7..316dfdc 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs @@ -78,8 +78,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// /// - public static bool operator -(TheBusesGenericCollection collect, int - pos) + public static bool operator -(TheBusesGenericCollection collect, int pos) { T? obj = collect._collection.Get(pos); if (obj != null) -- 2.25.1