Замена массива на список и добавление нового класса

This commit is contained in:
Максим Егоров 2024-01-15 22:44:26 +03:00
parent 1e10a3cb96
commit 375e66b131
6 changed files with 312 additions and 117 deletions

View File

@ -56,14 +56,13 @@ namespace Sailboat.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(BoatsGenericCollection<T, U> collect, T?
obj)
public static bool operator +(BoatsGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
return (bool)collect?._collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
@ -71,16 +70,16 @@ namespace Sailboat.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(BoatsGenericCollection<T, U> collect, int
pos)
public static bool operator -(BoatsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return false;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
@ -88,7 +87,7 @@ namespace Sailboat.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -128,18 +127,17 @@ namespace Sailboat.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var boat in _collection.GetBoats())
{
DrawingBoat boat = _collection.Get(i);
if (boat != null)
{
int width = _pictureWidth / _placeSizeWidth;
boat.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight);
boat.DrawTransport(g);
}
i++;
}
}
}
}

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
using Sailboat.MovementStrategy;
namespace Sailboat.Generics
{
internal class BoatsGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>> _boatStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _boatStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public BoatsGenericStorage(int pictureWidth, int pictureHeight)
{
_boatStorages = new Dictionary<string,
BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_boatStorages.ContainsKey(name))
{
return;
}
_boatStorages[name] = new BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_boatStorages.ContainsKey(name))
{
return;
}
_boatStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>?
this[string ind]
{
get
{
if (_boatStorages.ContainsKey(ind))
{
return _boatStorages[ind];
}
return null;
}
}
}
}

View File

@ -28,89 +28,96 @@
/// </summary>
private void InitializeComponent()
{
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.panelTools = new System.Windows.Forms.Panel();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.buttonRemoveBoat = new System.Windows.Forms.Button();
this.buttonAddBoat = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panelTools.SuspendLayout();
this.SuspendLayout();
pictureBoxCollection = new PictureBox();
panelTools = new Panel();
maskedTextBoxNumber = new MaskedTextBox();
buttonRefreshCollection = new Button();
buttonRemoveBoat = new Button();
buttonAddBoat = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panelTools.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(750, 450);
this.pictureBoxCollection.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Margin = new Padding(3, 2, 3, 2);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(750, 450);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
pictureBoxCollection.Click += pictureBoxCollection_Click;
//
// panelTools
//
this.panelTools.BackColor = System.Drawing.SystemColors.ScrollBar;
this.panelTools.Controls.Add(this.maskedTextBoxNumber);
this.panelTools.Controls.Add(this.buttonRefreshCollection);
this.panelTools.Controls.Add(this.buttonRemoveBoat);
this.panelTools.Controls.Add(this.buttonAddBoat);
this.panelTools.Location = new System.Drawing.Point(756, 12);
this.panelTools.Name = "panelTools";
this.panelTools.Size = new System.Drawing.Size(209, 426);
this.panelTools.TabIndex = 1;
panelTools.BackColor = SystemColors.ScrollBar;
panelTools.Controls.Add(maskedTextBoxNumber);
panelTools.Controls.Add(buttonRefreshCollection);
panelTools.Controls.Add(buttonRemoveBoat);
panelTools.Controls.Add(buttonAddBoat);
panelTools.Location = new Point(662, 9);
panelTools.Margin = new Padding(3, 2, 3, 2);
panelTools.Name = "panelTools";
panelTools.Size = new Size(183, 320);
panelTools.TabIndex = 1;
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(40, 87);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(125, 27);
this.maskedTextBoxNumber.TabIndex = 3;
maskedTextBoxNumber.Location = new Point(35, 65);
maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(110, 23);
maskedTextBoxNumber.TabIndex = 3;
//
// buttonRefreshCollection
//
this.buttonRefreshCollection.Location = new System.Drawing.Point(16, 210);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(180, 34);
this.buttonRefreshCollection.TabIndex = 2;
this.buttonRefreshCollection.Text = "Обновить коллекцию";
this.buttonRefreshCollection.UseVisualStyleBackColor = true;
this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click);
buttonRefreshCollection.Location = new Point(14, 158);
buttonRefreshCollection.Margin = new Padding(3, 2, 3, 2);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(158, 26);
buttonRefreshCollection.TabIndex = 2;
buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += buttonRefreshCollection_Click;
//
// buttonRemoveBoat
//
this.buttonRemoveBoat.Location = new System.Drawing.Point(16, 159);
this.buttonRemoveBoat.Name = "buttonRemoveBoat";
this.buttonRemoveBoat.Size = new System.Drawing.Size(180, 34);
this.buttonRemoveBoat.TabIndex = 1;
this.buttonRemoveBoat.Text = "Удалить лодку";
this.buttonRemoveBoat.UseVisualStyleBackColor = true;
this.buttonRemoveBoat.Click += new System.EventHandler(this.buttonRemoveBoat_Click);
buttonRemoveBoat.Location = new Point(14, 119);
buttonRemoveBoat.Margin = new Padding(3, 2, 3, 2);
buttonRemoveBoat.Name = "buttonRemoveBoat";
buttonRemoveBoat.Size = new Size(158, 26);
buttonRemoveBoat.TabIndex = 1;
buttonRemoveBoat.Text = "Удалить лодку";
buttonRemoveBoat.UseVisualStyleBackColor = true;
buttonRemoveBoat.Click += buttonRemoveBoat_Click;
//
// buttonAddBoat
//
this.buttonAddBoat.Location = new System.Drawing.Point(16, 14);
this.buttonAddBoat.Name = "buttonAddBoat";
this.buttonAddBoat.Size = new System.Drawing.Size(180, 34);
this.buttonAddBoat.TabIndex = 0;
this.buttonAddBoat.Text = "Добавить лодку";
this.buttonAddBoat.UseVisualStyleBackColor = true;
this.buttonAddBoat.Click += new System.EventHandler(this.buttonAddBoat_Click);
buttonAddBoat.Location = new Point(14, 10);
buttonAddBoat.Margin = new Padding(3, 2, 3, 2);
buttonAddBoat.Name = "buttonAddBoat";
buttonAddBoat.Size = new Size(158, 26);
buttonAddBoat.TabIndex = 0;
buttonAddBoat.Text = "Добавить лодку";
buttonAddBoat.UseVisualStyleBackColor = true;
buttonAddBoat.Click += buttonAddBoat_Click;
//
// FormBoatCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(969, 450);
this.Controls.Add(this.panelTools);
this.Controls.Add(this.pictureBoxCollection);
this.Name = "FormBoatCollection";
this.Text = "FormBoatCollection";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.panelTools.ResumeLayout(false);
this.panelTools.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(848, 338);
Controls.Add(pictureBoxCollection);
Controls.Add(panelTools);
Margin = new Padding(3, 2, 3, 2);
Name = "FormBoatCollection";
Text = "FormBoatCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
panelTools.ResumeLayout(false);
panelTools.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion

View File

@ -57,12 +57,16 @@ namespace Sailboat
MessageBox.Show("Не удалось удалить объект");
pictureBoxCollection.Image = _boats.ShowBoats();
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _boats.ShowBoats();
}
private void pictureBoxCollection_Click(object sender, EventArgs e)
{
}
}
}

View File

@ -1,4 +1,64 @@
<root>
<?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">

View File

@ -8,64 +8,106 @@ namespace Sailboat.Generics
{
internal class SetGeneric<T> where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
/// <summary>
/// Список объектов, которые храним
/// </summary>
private readonly List <T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
public int Insert(T boat)
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <returns></returns>
public bool Insert(T boat)
{
return Insert(boat, 0);
}
public int Insert(T boat, int position)
if (_places.Count == _maxCount)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
return false;
}
Insert(boat, 0);
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T boat, int position)
{
return -1;
if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) {
return false;
}
for (i = position; i < Count; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
{
return -1;
}
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = boat;
return position;
_places.Insert(position, boat);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= Count)
{
return false;
}
_places[position] = null;
_places.RemoveAt(position);
return true;
}
public T? Get(int position)
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
if (position < 0 || position >= Count)
get
{
if (position < 0 || position >= Count) {
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount)) {
return;
}
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetBoats(int? maxBoats = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxBoats.HasValue && i == maxBoats.Value)
{
yield break;
}
}
}
}
}