ISBd_21.GordeevI.V._LabWork_03 #7

Closed
Igor_Gordeev wants to merge 2 commits from ISBd_21.GordeevI.V._LabWork_03 into ISBd_21.GordeevI.V._LabWork_02
12 changed files with 525 additions and 35 deletions

View File

@ -11,7 +11,7 @@ namespace ElectricLocomotive
public class DrawningElectricLocomotive : DrawningLocomotive
{
public DrawningElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, bool Horns, int width, int height)
: base (speed, weight, bodyColor, additionalColor, width , height)
: base (speed, weight, bodyColor, width , height)
{
if (EntityLocomotive != null)
{

View File

@ -1,4 +1,5 @@
using ElectricLocomotive.Entities;
using ElectricLocomotive.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
@ -30,7 +31,7 @@ namespace ElectricLocomotive.DrawningObject
public int GetWidth => _locoWidth;
public int GetHeight => _locoHeight;
public DrawningLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, int width, int heigth)
public DrawningLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
{
if (width < _locoWidth || heigth < _locoHeight)
{
@ -38,7 +39,7 @@ namespace ElectricLocomotive.DrawningObject
}
_pictureWidth = width;
_pictureHeight = heigth;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor, additionalColor);
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
protected DrawningLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, int width,
@ -52,7 +53,7 @@ namespace ElectricLocomotive.DrawningObject
_pictureHeight = height;
_locoWidth = locoWidth;
_locoHeight = locoHeight;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor, additionalColor);
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
/// <summary>
/// Установка позиции
@ -123,8 +124,8 @@ namespace ElectricLocomotive.DrawningObject
{
return;
}
Pen pen = new(EntityLocomotive.BodyColor);
Brush brush = new SolidBrush(EntityLocomotive.AdditionalColor);
Pen pen = new(Color.Black);
Brush brush = new SolidBrush(EntityLocomotive.BodyColor);
///ВЛ60к-1595
///ходовая
@ -206,5 +207,7 @@ namespace ElectricLocomotive.DrawningObject
DirectionType.Down => _startPosY + EntityLocomotive.Step < _pictureHeight,
};
}
public IMoveableObject GetMoveableObject => new DrawningObjectLocomotive(this);
}
}

View File

@ -38,6 +38,7 @@
buttonCreateLocomotive = new Button();
comboBoxStrategy = new ComboBox();
buttonStep = new Button();
buttonChoiceLocomotive = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxElectroLocomotiv).BeginInit();
SuspendLayout();
//
@ -51,7 +52,6 @@
pictureBoxElectroLocomotiv.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxElectroLocomotiv.TabIndex = 5;
pictureBoxElectroLocomotiv.TabStop = false;
//
// buttonUp
//
@ -108,9 +108,9 @@
buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive";
buttonCreateElectricLocomotive.Size = new Size(208, 34);
buttonCreateElectricLocomotive.TabIndex = 6;
buttonCreateElectricLocomotive.Text = "Создать Электролокомотив";
buttonCreateElectricLocomotive.Text = "Нарисовать Электролокомотив";
buttonCreateElectricLocomotive.UseVisualStyleBackColor = true;
buttonCreateElectricLocomotive.Click += buttonCreateElectricLocomotive_Click;
buttonCreateElectricLocomotive.Click += PaintElectricLocoButton_click;
//
// buttonCreateLocomotive
//
@ -119,9 +119,9 @@
buttonCreateLocomotive.Name = "buttonCreateLocomotive";
buttonCreateLocomotive.Size = new Size(197, 34);
buttonCreateLocomotive.TabIndex = 11;
buttonCreateLocomotive.Text = "Создать локомотив";
buttonCreateLocomotive.Text = "Нарисовать локомотив";
buttonCreateLocomotive.UseVisualStyleBackColor = true;
buttonCreateLocomotive.Click += buttonCreateLocomotive_Click;
buttonCreateLocomotive.Click += PaintLocoButton_click;
//
// comboBoxStrategy
//
@ -148,11 +148,22 @@
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// buttonChoiceLocomotive
//
buttonChoiceLocomotive.Location = new Point(430, 425);
buttonChoiceLocomotive.Name = "buttonChoiceLocomotive";
buttonChoiceLocomotive.Size = new Size(206, 34);
buttonChoiceLocomotive.TabIndex = 14;
buttonChoiceLocomotive.Text = "Выбрать локомотив";
buttonChoiceLocomotive.UseVisualStyleBackColor = true;
buttonChoiceLocomotive.Click += ButtonSelectLocomotive_Click;
//
// ElectricLocomotive
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(854, 467);
Controls.Add(buttonChoiceLocomotive);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateLocomotive);
@ -180,5 +191,6 @@
private Button buttonCreateLocomotive;
private ComboBox comboBoxStrategy;
private Button buttonStep;
private Button buttonChoiceLocomotive;
}
}

View File

@ -10,12 +10,14 @@ namespace ElectricLocomotive
{
private DrawningLocomotive? _drawningLocomotive;
private AbstractStrategy? _abstractStrategy;
public DrawningLocomotive? SelectedLocomotive { get; private set; }
public ElectricLocomotive()
{
InitializeComponent();
_abstractStrategy = null;
SelectedLocomotive = null;
}
private void Draw()
@ -30,26 +32,32 @@ namespace ElectricLocomotive
pictureBoxElectroLocomotiv.Image = bmp;
}
private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e)
private void PaintLocoButton_click(object sender, EventArgs e)
{
Random random = new Random();
_drawningLocomotive = new DrawningElectricLocomotive(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)), pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
Random random = new();
Color color = 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;
_drawningLocomotive = new DrawningLocomotive(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateLocomotive_Click(object sender, EventArgs e)
private void PaintElectricLocoButton_click(object sender, EventArgs e)
{
Random rnd = new Random();
_drawningLocomotive = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 3000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
_drawningLocomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
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));
bool Horns = Convert.ToBoolean(random.Next(0, 2));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
color = dialog.Color;
if (dialog.ShowDialog() == DialogResult.OK)
dopColor = dialog.Color;
_drawningLocomotive = new DrawningElectricLocomotive(random.Next(100, 300), random.Next(1000, 3000), color, dopColor, Horns, pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
@ -114,6 +122,10 @@ namespace ElectricLocomotive
}
}
private void ButtonSelectLocomotive_Click(object sender, EventArgs e)
{
SelectedLocomotive = _drawningLocomotive;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -9,12 +9,15 @@ namespace ElectricLocomotive.Entities
{
public class EntityElectroLocomotive : EntityLocomotive
{
public Color AdditionalColor { get; private set; }
public bool Horns { get; private set; }
/// <summary>
/// Признак (опция) roga
/// </summary>
public EntityElectroLocomotive(int Speed, double weight, Color bodyColor, Color additionalColor, bool horns) : base(Speed,weight,bodyColor, additionalColor)
public EntityElectroLocomotive(int Speed, double weight, Color bodyColor, Color additionalColor, bool horns) : base(Speed,weight,bodyColor)
{
AdditionalColor = additionalColor;
Horns = horns;
}
}

View File

@ -21,10 +21,6 @@ namespace ElectricLocomotive.Entities
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличия обвеса
/// </summary>
public double Step => (double)Speed * 100 / Weight;
@ -34,12 +30,11 @@ namespace ElectricLocomotive.Entities
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityLocomotive(int speed, double weight, Color bodyColor, Color additionalColor)
public EntityLocomotive(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
}
}
}

View File

@ -0,0 +1,123 @@
namespace ElectricLocomotive
{
partial class FormLocomotiveCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
CollectionPictureBox = new PictureBox();
ToolsBox = new GroupBox();
Input = new TextBox();
RefreshCollection = new Button();
DelLocomotive = new Button();
AddLocomotive = new Button();
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit();
ToolsBox.SuspendLayout();
SuspendLayout();
//
// CollectionPictureBox
//
CollectionPictureBox.Location = new Point(3, 0);
CollectionPictureBox.Name = "CollectionPictureBox";
CollectionPictureBox.Size = new Size(634, 457);
CollectionPictureBox.TabIndex = 0;
CollectionPictureBox.TabStop = false;
//
// ToolsBox
//
ToolsBox.Controls.Add(Input);
ToolsBox.Controls.Add(RefreshCollection);
ToolsBox.Controls.Add(DelLocomotive);
ToolsBox.Controls.Add(AddLocomotive);
ToolsBox.Location = new Point(643, 0);
ToolsBox.Name = "ToolsBox";
ToolsBox.Size = new Size(200, 457);
ToolsBox.TabIndex = 1;
ToolsBox.TabStop = false;
ToolsBox.Text = "Инструменты";
//
// Input
//
Input.Location = new Point(7, 91);
Input.Name = "Input";
Input.Size = new Size(186, 23);
Input.TabIndex = 3;
//
// RefreshCollection
//
RefreshCollection.Location = new Point(7, 240);
RefreshCollection.Name = "RefreshCollection";
RefreshCollection.Size = new Size(187, 46);
RefreshCollection.TabIndex = 2;
RefreshCollection.Text = "Обновить коллекцию";
RefreshCollection.UseVisualStyleBackColor = true;
RefreshCollection.Click += refreshCollection_Click;
//
// DelLocomotive
//
DelLocomotive.Location = new Point(7, 134);
DelLocomotive.Name = "DelLocomotive";
DelLocomotive.Size = new Size(187, 46);
DelLocomotive.TabIndex = 1;
DelLocomotive.Text = "Удалить локомотив";
DelLocomotive.UseVisualStyleBackColor = true;
DelLocomotive.Click += deleteLoco_Click;
//
// AddLocomotive
//
AddLocomotive.Location = new Point(6, 22);
AddLocomotive.Name = "AddLocomotive";
AddLocomotive.Size = new Size(187, 46);
AddLocomotive.TabIndex = 0;
AddLocomotive.Text = "Добавить локомотив";
AddLocomotive.UseVisualStyleBackColor = true;
AddLocomotive.Click += addLocomotiv_Click;
//
// FormLocomotiveCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(848, 469);
Controls.Add(ToolsBox);
Controls.Add(CollectionPictureBox);
Name = "FormLocomotiveCollection";
Text = "Коллекция";
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit();
ToolsBox.ResumeLayout(false);
ToolsBox.PerformLayout();
ResumeLayout(false);
}
#endregion
private PictureBox CollectionPictureBox;
private GroupBox ToolsBox;
private TextBox Input;
private Button RefreshCollection;
private Button DelLocomotive;
private Button AddLocomotive;
}
}

View File

@ -0,0 +1,65 @@
using ElectricLocomotive.DrawningObject;
using ElectricLocomotive.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;
namespace ElectricLocomotive
{
public partial class FormLocomotiveCollection : Form
{
private readonly LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive> _locos;
public FormLocomotiveCollection()
{
InitializeComponent();
_locos = new(CollectionPictureBox.Width, CollectionPictureBox.Height);
}
private void addLocomotiv_Click(object sender, EventArgs e)
{
ElectricLocomotive form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_locos + form.SelectedLocomotive != null)
{
MessageBox.Show("Объект добавлен");
CollectionPictureBox.Image = _locos.ShowLocomotives();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void deleteLoco_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(Input.Text);
if (_locos - pos != null)
{
MessageBox.Show("Объект удален");
CollectionPictureBox.Image = _locos.ShowLocomotives();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void refreshCollection_Click(object sender, EventArgs e)
{
CollectionPictureBox.Image = _locos.ShowLocomotives();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectricLocomotive.DrawningObject;
using ElectricLocomotive.MovementStrategy;
using ProjectElectricLocomotive.Generics;
namespace ElectricLocomotive.Generics
{
internal class LocomotivesGenericCollection<T, U>
where T : DrawningLocomotive
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection;
public LocomotivesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(LocomotivesGenericCollection<T, U> collect, T? locomotive)
{
if (locomotive == null)
{
return -1;
}
return collect?._collection.Insert(locomotive) ?? -1;
}
public static bool operator -(LocomotivesGenericCollection<T, U> collect, int pos)
{
T? locomotive = collect._collection.Get(pos);
if (locomotive != null)
return collect._collection.Remove(pos);
return false;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
public Bitmap ShowLocomotives()
{
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)
{
int HLoco = _pictureHeight / _placeSizeHeight;
int Wloco = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++)
{
T? type = _collection.Get(i);
if (type != null)
{
type.SetPosition(
(int)(i / HLoco * _placeSizeWidth),
(HLoco - 1) * _placeSizeHeight - (int)(i % HLoco * _placeSizeHeight)
);
type?.DrawTransport(g);
}
}
}
}
}

View File

@ -1,3 +1,5 @@
using ProjectElectricLocomotive;
namespace ElectricLocomotive
{
internal static class Program
@ -11,7 +13,7 @@ namespace ElectricLocomotive
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new ElectricLocomotive());
Application.Run(new FormLocomotiveCollection());
}
}
}

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T locomotive)
{
return Insert(locomotive, 0);
}
public int Insert(T locomotive, int position)
{
int Full = 0;
int temp = 0;
for (int i = position; i < Count; i++)
{
if (_places[i] != null) Full++;
}
if (Full == Count - position - 1)
return -1;
if (position < Count && position >= 0)
{
for (int j = position; j < Count; j++)
{
if (_places[j] == null)
{
temp = j;
break;
}
}
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = locomotive;
return position;
}
return -1;
}
public bool Remove(int position)
{
if (position >= Count || position < 0)
if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
_places[position] = null;
return true;
}
public T Get(int position)
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
}
}