diff --git a/DumpTruck/DumpTruck/AbstractStrategy.cs b/DumpTruck/DumpTruck/AbstractStrategy.cs index b5c3a28..1969036 100644 --- a/DumpTruck/DumpTruck/AbstractStrategy.cs +++ b/DumpTruck/DumpTruck/AbstractStrategy.cs @@ -1,4 +1,4 @@ -using DumpTruckr.MovementStrategy; +using DumpTruck.MovementStrategy; using System; using System.Collections.Generic; using System.Linq; diff --git a/DumpTruck/DumpTruck/CarsGenericCollection.cs b/DumpTruck/DumpTruck/CarsGenericCollection.cs new file mode 100644 index 0000000..5b79fe6 --- /dev/null +++ b/DumpTruck/DumpTruck/CarsGenericCollection.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawningObjects; +using DumpTruck.Entities; +using DumpTruck.MovementStrategy; + +namespace DumpTruck.Generics +{ + internal class CarsGenericCollection + where T : DrawningCar + 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 CarsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(CarsGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(CarsGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return true; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowCars() + { + 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) + { + DrawningCar car; + int numPlacesInRow = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count; i++) + { + // 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); + + } + } + } + } +} diff --git a/DumpTruck/DumpTruck/DrawingDumpCar.cs b/DumpTruck/DumpTruck/DrawingDumpCar.cs index 1b47898..afc7b18 100644 --- a/DumpTruck/DumpTruck/DrawingDumpCar.cs +++ b/DumpTruck/DumpTruck/DrawingDumpCar.cs @@ -14,6 +14,8 @@ namespace DumpTruck.DrawningObjects { public class DrawningDumpTruck : DrawningCar { + + public DrawningDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent, int width, int height): base(speed, weight, bodyColor, width, height, 110, 60) { if (EntityCar != null) @@ -21,7 +23,8 @@ namespace DumpTruck.DrawningObjects EntityCar = new EntityDumpTruck(speed, weight, bodyColor,additionalColor, bodyKit, tent); } } - + + public override void DrawTransport(Graphics g) { if (EntityCar is not EntityDumpTruck dumpTruck) diff --git a/DumpTruck/DumpTruck/DrawningCar.cs b/DumpTruck/DumpTruck/DrawningCar.cs index cf1bb21..d7490a1 100644 --- a/DumpTruck/DumpTruck/DrawningCar.cs +++ b/DumpTruck/DumpTruck/DrawningCar.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using DumpTruck.Entities; +using DumpTruck.MovementStrategy; namespace DumpTruck.DrawningObjects @@ -41,6 +42,11 @@ namespace DumpTruck.DrawningObjects /// public int GetHeight => _carHeight; + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectCar(this); + public DrawningCar(int speed, double weight, Color bodyColor, int width, int height) { if(width < _carWidth || height < _carHeight) @@ -51,6 +57,9 @@ namespace DumpTruck.DrawningObjects _pictureHeight = height; EntityCar = new EntityCar(speed, weight, bodyColor); + + + } protected DrawningCar(int speed, double weight, Color bodyColor, int @@ -125,6 +134,7 @@ width, int height, int carWidth, int carHeight) }; } + public virtual void DrawTransport(Graphics g) { if (EntityCar == null) @@ -141,6 +151,7 @@ width, int height, int carWidth, int carHeight) g.FillEllipse(brush, _startPosX, _startPosY + 35 + 10, 15, 15); g.FillEllipse(brush, _startPosX + 15, _startPosY + 35 + 10, 15, 15); g.FillEllipse(brush, _startPosX + 95, _startPosY + 35 + 10, 15, 15); + } } diff --git a/DumpTruck/DumpTruck/DrawningObjectCar.cs b/DumpTruck/DumpTruck/DrawningObjectCar.cs index 4031487..d6a766b 100644 --- a/DumpTruck/DumpTruck/DrawningObjectCar.cs +++ b/DumpTruck/DumpTruck/DrawningObjectCar.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using DumpTruck.DrawningObjects; -using DumpTruckr.MovementStrategy; +using DumpTruck.MovementStrategy; namespace DumpTruck.MovementStrategy { diff --git a/DumpTruck/DumpTruck/EntityDumpTruck.cs b/DumpTruck/DumpTruck/EntityDumpTruck.cs index b5ce3e0..d6024c4 100644 --- a/DumpTruck/DumpTruck/EntityDumpTruck.cs +++ b/DumpTruck/DumpTruck/EntityDumpTruck.cs @@ -26,10 +26,12 @@ namespace DumpTruck.Entities /// Признак (опция) наличия tent /// public bool Tent { get; private set; } + public EntityDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent) : base(speed, weight, bodyColor) - { + { + AdditionalColor = additionalColor; BodyKit = bodyKit; Tent = tent; diff --git a/DumpTruck/DumpTruck/FormCarCollection.Designer.cs b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs new file mode 100644 index 0000000..390d97c --- /dev/null +++ b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs @@ -0,0 +1,124 @@ +namespace DumpTruck +{ + partial class FormCarCollection + { + /// + /// 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() + { + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.panelCollection = 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(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.panelCollection.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.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.Name = "panelCollection"; + this.panelCollection.Size = new System.Drawing.Size(187, 426); + 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.Name = "ButtonRefreshCollection"; + this.ButtonRefreshCollection.Size = new System.Drawing.Size(181, 41); + this.ButtonRefreshCollection.TabIndex = 1; + this.ButtonRefreshCollection.Text = "Обновить коллекцию"; + this.ButtonRefreshCollection.UseVisualStyleBackColor = true; + this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + // + // ButtonAddCar + // + this.ButtonAddCar.Location = new System.Drawing.Point(3, 37); + this.ButtonAddCar.Name = "ButtonAddCar"; + this.ButtonAddCar.Size = new System.Drawing.Size(181, 41); + this.ButtonAddCar.TabIndex = 0; + this.ButtonAddCar.Text = "Добавить машину"; + this.ButtonAddCar.UseVisualStyleBackColor = true; + this.ButtonAddCar.Click += new System.EventHandler(this.ButtonAddCar_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.Controls.Add(this.panelCollection); + this.Controls.Add(this.pictureBoxCollection); + this.Name = "FormCarCollection"; + this.Text = "Form2"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.panelCollection.ResumeLayout(false); + this.panelCollection.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + + private PictureBox pictureBoxCollection; + private Panel panelCollection; + private Button ButtonAddCar; + private MaskedTextBox maskedTextBoxNumber; + private Button ButtonRemoveCar; + private Button ButtonRefreshCollection; + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormCarCollection.cs b/DumpTruck/DumpTruck/FormCarCollection.cs new file mode 100644 index 0000000..8965dde --- /dev/null +++ b/DumpTruck/DumpTruck/FormCarCollection.cs @@ -0,0 +1,94 @@ +using DumpTruck.DrawningObjects; +using DumpTruck.Generics; +using DumpTruck.MovementStrategy; +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 DumpTruck.MovementStrategy; +using DumpTruck.DrawningObjects; +using DumpTruck.Generics; + + +namespace DumpTruck +{ + public partial class FormCarCollection : Form + { + private readonly CarsGenericCollection _cars; + + public FormCarCollection() + { + InitializeComponent(); + _cars = new CarsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void Form2_Load(object sender, EventArgs e) + { + + } + + private void pictureBoxCollection_Click(object sender, EventArgs e) + { + + } + + private void ButtonAddCar_Click(object sender, EventArgs e) + { + FormDumpTruck form = new(); + + + if (form.ShowDialog() == DialogResult.OK) + { + if (_cars + form.SelectedCar != null) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _cars.ShowCars(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void ButtonRemoveCar_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", +MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos; + if (maskedTextBoxNumber.Text == null || !int.TryParse(maskedTextBoxNumber.Text, out pos)) { + MessageBox.Show("Введите номер парковочного места"); + return; + } + + if (_cars - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _cars.ShowCars(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _cars.ShowCars(); + } + + private void maskedTextBoxNumber_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) + { + + } + } +} diff --git a/DumpTruck/DumpTruck/FormCarCollection.resx b/DumpTruck/DumpTruck/FormCarCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/DumpTruck/DumpTruck/FormCarCollection.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs index 9fb2e07..b816474 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs @@ -38,6 +38,7 @@ this.buttonCar = new System.Windows.Forms.Button(); this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); this.buttonStep = new System.Windows.Forms.Button(); + this.buttonSelectCar = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // @@ -142,7 +143,7 @@ this.comboBoxStrategy.Name = "comboBoxStrategy"; this.comboBoxStrategy.Size = new System.Drawing.Size(164, 23); this.comboBoxStrategy.TabIndex = 9; - + this.comboBoxStrategy.SelectedIndexChanged += new System.EventHandler(this.comboBoxStrategy_SelectedIndexChanged); // // buttonStep // @@ -155,11 +156,22 @@ this.buttonStep.UseVisualStyleBackColor = true; this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click); // + // buttonSelectCar + // + this.buttonSelectCar.Location = new System.Drawing.Point(730, 97); + this.buttonSelectCar.Name = "buttonSelectCar"; + this.buttonSelectCar.Size = new System.Drawing.Size(142, 29); + this.buttonSelectCar.TabIndex = 11; + this.buttonSelectCar.Text = "Выбрать машину"; + this.buttonSelectCar.UseVisualStyleBackColor = true; + this.buttonSelectCar.Click += new System.EventHandler(this.buttonSelectCar_Click); + // // FormDumpTruck // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(884, 461); + this.Controls.Add(this.buttonSelectCar); this.Controls.Add(this.buttonStep); this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.buttonCar); @@ -191,5 +203,6 @@ private Button buttonCar; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button buttonSelectCar; } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormDumpTruck.cs b/DumpTruck/DumpTruck/FormDumpTruck.cs index 2d6c230..d14905a 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.cs @@ -7,14 +7,24 @@ namespace DumpTruck { private DrawningCar? _drawningCar; - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + + public DrawningCar? SelectedCar { get; private set; } public FormDumpTruck() { InitializeComponent(); + _strategy = null; + SelectedCar = null; } + private void button_Click(object sender, EventArgs e) + { + + } + + private void Draw() { if (_drawningCar == null) @@ -55,25 +65,44 @@ namespace DumpTruck private void button2_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawningCar = new DrawningCar(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + color, pictureBox.Width, pictureBox.Height); _drawningCar.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); + } private void buttonDumpTruck_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + if (dialog.ShowDialog() == DialogResult.OK) + { + dopColor = dialog.Color; + } + _drawningCar = new DrawningDumpTruck(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)), + color, + dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBox.Width, pictureBox.Height); @@ -117,6 +146,18 @@ namespace DumpTruck comboBoxStrategy.Enabled = true; _abstractStrategy = null; } + + } + + private void comboBoxStrategy_SelectedIndexChanged(object sender, EventArgs e) + { + + } + + private void buttonSelectCar_Click(object sender, EventArgs e) + { + SelectedCar = _drawningCar; + DialogResult = DialogResult.OK; } } } diff --git a/DumpTruck/DumpTruck/IMoveableObject.cs b/DumpTruck/DumpTruck/IMoveableObject.cs index d66a441..a64a507 100644 --- a/DumpTruck/DumpTruck/IMoveableObject.cs +++ b/DumpTruck/DumpTruck/IMoveableObject.cs @@ -6,7 +6,7 @@ using System.Text; using System.Threading.Tasks; using DumpTruck.MovementStrategy; -namespace DumpTruckr.MovementStrategy +namespace DumpTruck.MovementStrategy { /// /// Интерфейс для работы с перемещаемым объектом @@ -32,5 +32,6 @@ namespace DumpTruckr.MovementStrategy /// /// Направление void MoveObject(DirectionType direction); + } } diff --git a/DumpTruck/DumpTruck/MoveToBorder.cs b/DumpTruck/DumpTruck/MoveToBorder.cs index 155dc6d..8ff1c45 100644 --- a/DumpTruck/DumpTruck/MoveToBorder.cs +++ b/DumpTruck/DumpTruck/MoveToBorder.cs @@ -29,29 +29,19 @@ namespace DumpTruck.MovementStrategy { return; } - var diffX = objParams.RightBorder - FieldWidth; + var diffX = FieldWidth - objParams.ObjectMiddleHorizontal; if (Math.Abs(diffX) > GetStep()) { - if (diffX > 0) - { - MoveLeft(); - } - else - { + MoveRight(); - } + } - var diffY = objParams.DownBorder - FieldHeight; + var diffY = FieldHeight - objParams.ObjectMiddleVertical; if (Math.Abs(diffY) > GetStep()) { - if (diffY > 0) - { - MoveUp(); - } - else - { + MoveDown(); - } + } } diff --git a/DumpTruck/DumpTruck/Program.cs b/DumpTruck/DumpTruck/Program.cs index b619d5a..dece68e 100644 --- a/DumpTruck/DumpTruck/Program.cs +++ b/DumpTruck/DumpTruck/Program.cs @@ -11,7 +11,7 @@ namespace DumpTruck // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormDumpTruck()); + Application.Run(new FormCarCollection()); } } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/SetGeneric.cs b/DumpTruck/DumpTruck/SetGeneric.cs new file mode 100644 index 0000000..c76f900 --- /dev/null +++ b/DumpTruck/DumpTruck/SetGeneric.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms.VisualStyles; + +namespace DumpTruck.Generics +{ + internal class SetGeneric + where T : class + + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + /// + public int startPointer = 0; + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public int Insert(T car) + { + if (_places[Count - 1] != null) + return -1; + return Insert(car, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + 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; + return position; + } + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + + + + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < Count && position >= 0) + { + _places[position] = null; + return true; + } + return false; + + + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < Count && position >= 0) { return _places[position]; } + return null; + + } + } +}