pochti vse
This commit is contained in:
parent
d802aa0ec6
commit
a261b3d661
@ -18,7 +18,7 @@ public interface ICollectionGenericObject<T>
|
||||
/// <summary>
|
||||
/// Установка макс кол-ва элементов
|
||||
/// </summary>
|
||||
int SetMaxCount { set; }
|
||||
int MaxCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Добавление объекта в коллекцию
|
||||
@ -49,4 +49,14 @@ public interface ICollectionGenericObject<T>
|
||||
/// <returns>Объект</returns>
|
||||
T? Get(int position);
|
||||
|
||||
/// <summary>
|
||||
/// Получение типа коллекции
|
||||
/// </summary>
|
||||
CollectionType GetCollectionType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Получение объектов из коллекции по одному
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IEnumerable<T> GetItems();
|
||||
}
|
||||
|
@ -22,7 +22,21 @@ public class ListGenericObjects<T> : ICollectionGenericObject<T>
|
||||
|
||||
public int Count => _collection.Count;
|
||||
|
||||
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
|
||||
public int MaxCount {
|
||||
get
|
||||
{
|
||||
return _collection.Count;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
_maxCount = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CollectionType GetCollectionType => CollectionType.List;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
@ -77,4 +91,8 @@ public class ListGenericObjects<T> : ICollectionGenericObject<T>
|
||||
return obj;
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetItems()
|
||||
{
|
||||
for (int i = 0; i < Count; i++) yield return _collection[i];
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,12 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
|
||||
|
||||
public int Count => _collection.Length;
|
||||
|
||||
public int SetMaxCount
|
||||
public int MaxCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return _collection.Length;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
@ -36,6 +40,8 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
|
||||
}
|
||||
}
|
||||
|
||||
public CollectionType GetCollectionType => CollectionType.Massive;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -115,4 +121,9 @@ public class MassiveGenericObjects<T> : ICollectionGenericObject<T>
|
||||
_collection[position] = null;
|
||||
return temp;
|
||||
}
|
||||
|
||||
public IEnumerable<T?> GetItems()
|
||||
{
|
||||
for (int i = 0; i < _collection.Length; i++) yield return _collection[i];
|
||||
}
|
||||
}
|
@ -1,13 +1,15 @@
|
||||
using System;
|
||||
using ProjectDumpTruck.Drawnings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectDumpTruck.CollectionGenericObject;
|
||||
|
||||
public class StorageCollection<T>
|
||||
where T : class
|
||||
where T : DrawningTruck
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище) с коллекциями
|
||||
@ -19,6 +21,20 @@ public class StorageCollection<T>
|
||||
/// </summary>
|
||||
public List<string> Keys => _storages.Keys.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Ключевое слово, с которого должен начинаться файл
|
||||
/// </summary>
|
||||
private readonly string _collectionKey = "CollectionsStorage";
|
||||
/// <summary>
|
||||
/// Разделитель для записи ключа и значения элемента словаря
|
||||
/// </summary>
|
||||
private readonly string _separatorForKeyValue = "|";
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly string _separatorItems = ";";
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -81,4 +97,115 @@ public class StorageCollection<T>
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename)) File.Delete(filename);
|
||||
|
||||
if (_storages.Count == 0) return false;
|
||||
|
||||
using (StreamWriter sw = new StreamWriter(filename))
|
||||
{
|
||||
sw.Write(_collectionKey);
|
||||
|
||||
foreach (KeyValuePair<string, ICollectionGenericObject<T>> value in _storages)
|
||||
{
|
||||
sw.Write(Environment.NewLine);
|
||||
// не сохраняем пустые коллекции
|
||||
if (value.Value.Count == 0) continue;
|
||||
|
||||
sw.Write(value.Key);
|
||||
sw.Write(_separatorForKeyValue);
|
||||
sw.Write(value.Value.GetCollectionType);
|
||||
sw.Write(_separatorForKeyValue);
|
||||
sw.Write(value.Value.MaxCount);
|
||||
sw.Write(_separatorForKeyValue);
|
||||
|
||||
foreach (T? item in value.Value.GetItems())
|
||||
{
|
||||
string data = item?.GetDataForSave() ?? string.Empty;
|
||||
|
||||
if (string.IsNullOrEmpty(data)) continue;
|
||||
|
||||
sw.Write(data);
|
||||
sw.Write(_separatorItems);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Загрузка информации по автомобилям в хранилище из файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename)) return false;
|
||||
|
||||
using (StreamReader sr = new(filename))
|
||||
{
|
||||
string line = sr.ReadLine();
|
||||
|
||||
if (line == null || line.Length == 0) return false;
|
||||
|
||||
if (!line.Equals(_collectionKey))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
return false;
|
||||
}
|
||||
_storages.Clear();
|
||||
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 4)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||
ICollectionGenericObject<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||
if (collection == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
if (elem?.CreateDrawningTruck() is T warship)
|
||||
{
|
||||
if (collection.Insert(warship) == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_storages.Add(record[0], collection);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание коллекции по типу
|
||||
/// </summary>
|
||||
/// <param name="collectionType"></param>
|
||||
/// <returns></returns>
|
||||
private static ICollectionGenericObject<T>? CreateCollection(CollectionType collectionType)
|
||||
{
|
||||
return collectionType switch
|
||||
{
|
||||
CollectionType.Massive => new MassiveGenericObjects<T>(),
|
||||
CollectionType.List => new ListGenericObjects<T>(),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,15 @@ public class DrawningDumpTruck : DrawningTruck
|
||||
{
|
||||
EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, awning, tent);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор для класса Extention
|
||||
/// </summary>
|
||||
/// <param name="truck"></param>
|
||||
public DrawningDumpTruck(EntityTruck? dumpTruck) : base(dumpTruck)
|
||||
{
|
||||
EntityTruck = dumpTruck;
|
||||
}
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTruck == null || EntityTruck is not EntityDumpTruck dumpTruck || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
|
@ -81,6 +81,15 @@ public class DrawningTruck
|
||||
_drawningTruckHeight= drawningTruckHeight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор для класса Extention
|
||||
/// </summary>
|
||||
/// <param name="truck"></param>
|
||||
public DrawningTruck(EntityTruck? truck) : this()
|
||||
{
|
||||
EntityTruck = truck;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
|
@ -0,0 +1,48 @@
|
||||
using ProjectDumpTruck.Entities;
|
||||
using ProjectDumpTruck.Drawnings;
|
||||
|
||||
namespace ProjectDumpTruck.Drawnings;
|
||||
|
||||
public static class ExtentionDrawningTruck
|
||||
{
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly string _separatorForObject = ":";
|
||||
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info">Строка с данными для создания объекта</param>
|
||||
/// <returns>Объект</returns>
|
||||
public static DrawningTruck? CreateDrawningTruck(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
EntityTruck? truck = EntityDumpTruck.CreateEntityDumpTruck(strs);
|
||||
|
||||
if (truck != null) return new DrawningDumpTruck(truck);
|
||||
|
||||
|
||||
truck = EntityTruck.CreateEntityTruck(strs);
|
||||
if (truck != null)
|
||||
{
|
||||
return new DrawningTruck(truck);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
/// <param name="drawningTruck">Сохраняемый объект</param>
|
||||
/// <returns>Строка с данными по объекту</returns>
|
||||
public static string GetDataForSave(this DrawningTruck drawningTruck)
|
||||
{
|
||||
string[]? array = drawningTruck?.EntityTruck?.GetStringRepresentation();
|
||||
|
||||
if (array == null) return string.Empty;
|
||||
|
||||
return string.Join(_separatorForObject, array);
|
||||
}
|
||||
}
|
@ -42,4 +42,17 @@ public class EntityDumpTruck : EntityTruck
|
||||
Awning = awning;
|
||||
Tent = tent;
|
||||
}
|
||||
|
||||
public virtual string[] GetStringRepresentation()
|
||||
{
|
||||
return new[] { nameof(EntityDumpTruck), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Awning.ToString() , Tent.ToString() };
|
||||
}
|
||||
|
||||
public static EntityDumpTruck? CreateEntityDumpTruck(string[] strs)
|
||||
{
|
||||
if (strs.Length != 7 || strs[0] != nameof(EntityDumpTruck)) return null;
|
||||
|
||||
return new EntityDumpTruck(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]),
|
||||
Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]));
|
||||
}
|
||||
}
|
||||
|
@ -51,4 +51,27 @@ public class EntityTruck
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение строк со значениями свойств объекта класса-сущности
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual string[] GetStringRepresentation()
|
||||
{
|
||||
return new[] { nameof(EntityTruck), Speed.ToString(), Weight.ToString(), BodyColor.Name };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание объекта из массива строк
|
||||
/// </summary>
|
||||
/// <param name="strs"></param>
|
||||
/// <returns></returns>
|
||||
public static EntityTruck? CreateEntityTruck(string[] strs)
|
||||
{
|
||||
if (strs.Length != 4 || strs[0] != nameof(EntityTruck))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new EntityTruck(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]));
|
||||
}
|
||||
}
|
||||
|
@ -46,10 +46,15 @@
|
||||
maskedTextBoxPosition = new MaskedTextBox();
|
||||
comboBoxSelectorCompany = new ComboBox();
|
||||
pictureBox = new PictureBox();
|
||||
menuStrip = new MenuStrip();
|
||||
файлToolStripMenuItem = new ToolStripMenuItem();
|
||||
SaveToolStripMenuItem = new ToolStripMenuItem();
|
||||
LoadToolStripMenuItem = new ToolStripMenuItem();
|
||||
groupBoxTools.SuspendLayout();
|
||||
panelStorage.SuspendLayout();
|
||||
panelCompanyTools.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
|
||||
menuStrip.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// groupBoxTools
|
||||
@ -59,9 +64,9 @@
|
||||
groupBoxTools.Controls.Add(panelCompanyTools);
|
||||
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
|
||||
groupBoxTools.Dock = DockStyle.Right;
|
||||
groupBoxTools.Location = new Point(888, 0);
|
||||
groupBoxTools.Location = new Point(888, 28);
|
||||
groupBoxTools.Name = "groupBoxTools";
|
||||
groupBoxTools.Size = new Size(238, 688);
|
||||
groupBoxTools.Size = new Size(238, 660);
|
||||
groupBoxTools.TabIndex = 0;
|
||||
groupBoxTools.TabStop = false;
|
||||
groupBoxTools.Text = "Инструменты";
|
||||
@ -164,7 +169,7 @@
|
||||
panelCompanyTools.Controls.Add(maskedTextBoxPosition);
|
||||
panelCompanyTools.Dock = DockStyle.Bottom;
|
||||
panelCompanyTools.Enabled = false;
|
||||
panelCompanyTools.Location = new Point(3, 385);
|
||||
panelCompanyTools.Location = new Point(3, 357);
|
||||
panelCompanyTools.Name = "panelCompanyTools";
|
||||
panelCompanyTools.Size = new Size(232, 300);
|
||||
panelCompanyTools.TabIndex = 10;
|
||||
@ -232,12 +237,43 @@
|
||||
// pictureBox
|
||||
//
|
||||
pictureBox.Dock = DockStyle.Fill;
|
||||
pictureBox.Location = new Point(0, 0);
|
||||
pictureBox.Location = new Point(0, 28);
|
||||
pictureBox.Name = "pictureBox";
|
||||
pictureBox.Size = new Size(888, 688);
|
||||
pictureBox.Size = new Size(888, 660);
|
||||
pictureBox.TabIndex = 1;
|
||||
pictureBox.TabStop = false;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
menuStrip.ImageScalingSize = new Size(20, 20);
|
||||
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
|
||||
menuStrip.Location = new Point(0, 0);
|
||||
menuStrip.Name = "menuStrip";
|
||||
menuStrip.Size = new Size(1126, 28);
|
||||
menuStrip.TabIndex = 2;
|
||||
menuStrip.Text = "menuStrip1";
|
||||
//
|
||||
// файлToolStripMenuItem
|
||||
//
|
||||
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
|
||||
файлToolStripMenuItem.Name = "файлToolStripMenuItem";
|
||||
файлToolStripMenuItem.Size = new Size(59, 24);
|
||||
файлToolStripMenuItem.Text = "Файл";
|
||||
//
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
SaveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S;
|
||||
SaveToolStripMenuItem.Size = new Size(227, 26);
|
||||
SaveToolStripMenuItem.Text = "Сохранение";
|
||||
//
|
||||
// LoadToolStripMenuItem
|
||||
//
|
||||
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
|
||||
LoadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L;
|
||||
LoadToolStripMenuItem.Size = new Size(227, 26);
|
||||
LoadToolStripMenuItem.Text = "Загрузка";
|
||||
//
|
||||
// FormTruckCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
@ -245,6 +281,8 @@
|
||||
ClientSize = new Size(1126, 688);
|
||||
Controls.Add(pictureBox);
|
||||
Controls.Add(groupBoxTools);
|
||||
Controls.Add(menuStrip);
|
||||
MainMenuStrip = menuStrip;
|
||||
Name = "FormTruckCollection";
|
||||
Text = "Коллекция грузовиков";
|
||||
groupBoxTools.ResumeLayout(false);
|
||||
@ -253,7 +291,10 @@
|
||||
panelCompanyTools.ResumeLayout(false);
|
||||
panelCompanyTools.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
|
||||
menuStrip.ResumeLayout(false);
|
||||
menuStrip.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -276,5 +317,9 @@
|
||||
private TextBox textBoxCollectionName;
|
||||
private Label labelCollectionName;
|
||||
private Button buttonCollectionDel;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem файлToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
}
|
||||
}
|
@ -117,4 +117,7 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
</root>
|
@ -114,47 +114,6 @@ public partial class FormTruckConfig : Form
|
||||
DrawObject();
|
||||
|
||||
}
|
||||
//допка
|
||||
//private void LabelObject_MouseDown(object sender, MouseEventArgs e)
|
||||
//{
|
||||
// Label? label = sender as Label;
|
||||
// if (label?.Name == "labelSimpleObject")
|
||||
// {
|
||||
// label?.DoDragDrop(new DrawningTruck((int)numericUpDownSpeed.Value,
|
||||
// (double)numericUpDownWeight.Value, Color.White), DragDropEffects.Copy);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// label?.DoDragDrop(new DrawningDumpTruck((int)numericUpDownSpeed.Value,
|
||||
// (double)numericUpDownWeight.Value, Color.White, Color.Black, checkBoxAwning.Checked, checkBoxTent.Checked), DragDropEffects.Copy);
|
||||
// }
|
||||
//}
|
||||
|
||||
//private void PanelObject_DragDrop(object sender, DragEventArgs e)
|
||||
//{
|
||||
// if ((DrawningTruck?)e.Data.GetData(typeof(DrawningTruck)) != null)
|
||||
// {
|
||||
// _truck = (DrawningTruck?)e.Data.GetData(typeof(DrawningTruck));
|
||||
// }
|
||||
// else if ((DrawningDumpTruck?)e.Data.GetData(typeof(DrawningDumpTruck)) != null)
|
||||
// {
|
||||
// _truck = (DrawningDumpTruck?)e.Data.GetData(typeof(DrawningDumpTruck));
|
||||
// }
|
||||
|
||||
// DrawObject();
|
||||
//}
|
||||
|
||||
//private void PanelObject_DragEnter(object sender, DragEventArgs e)
|
||||
//{
|
||||
// if ((e.Data.GetDataPresent(typeof(DrawningTruck))) || e.Data.GetDataPresent(typeof(DrawningDumpTruck)))
|
||||
// {
|
||||
// e.Effect = DragDropEffects.Copy;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// e.Effect = DragDropEffects.None;
|
||||
// }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Панель отправка цвета при нажатии на Panel
|
||||
|
Loading…
Reference in New Issue
Block a user