This commit is contained in:
Robert 2023-12-29 04:13:04 +04:00
parent 0ec10cf560
commit 2fd36d4785
6 changed files with 174 additions and 11 deletions

View File

@ -2,12 +2,13 @@
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework> <TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<SupportedOSPlatformVersion>10.0.22621.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion>10.0.22621.0</SupportedOSPlatformVersion>
<UseWPF>True</UseWPF> <UseWPF>True</UseWPF>
<StartupObject></StartupObject>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -0,0 +1,67 @@
using DumpTruck.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.DrawningObjects
{
/// <summary>
/// Расширение для класса EntityCar
/// </summary>
public static class ExtentionDrawningTruck
{
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="info">Строка с данными для создания объекта</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
/// <returns>Объект</returns>
public static DrawningTruck? CreateDrawningTruck(this string info, char
separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningTruck(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningDumpTruck(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]),
Color.FromName(strs[3]),
Convert.ToBoolean(strs[4]),
width, height);
}
return null;
}
/// <summary>
/// Получение данных для сохранения в файл
/// </summary>
/// <param name="drawningTruck">Сохраняемый объект</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <returns>Строка с данными по объекту</returns>
public static string GetDataForSave(this DrawningTruck drawningTruck,
char separatorForObject)
{
var truck = drawningTruck.EntityTruck;
if (truck == null)
{
return string.Empty;
}
var str =
$"{truck.Speed}{separatorForObject}{truck.Weight}{separatorForObject}{truck.BodyColor.Name}";
if (truck is not EntityDumpTruck dumpTruck)
{
return str;
}
return
$"{str}{separatorForObject}{dumpTruck.AdditionalColor.Name}{separatorForObject}{dumpTruck.Trailer}";
}
}
}

View File

@ -30,6 +30,7 @@
{ {
groupBox1 = new GroupBox(); groupBox1 = new GroupBox();
groupBox2 = new GroupBox(); groupBox2 = new GroupBox();
textBoxStorageName = new TextBox();
listBoxStorages = new ListBox(); listBoxStorages = new ListBox();
buttonRemoveObject = new Button(); buttonRemoveObject = new Button();
buttonAddObject = new Button(); buttonAddObject = new Button();
@ -38,7 +39,6 @@
buttonRemoveTruck = new Button(); buttonRemoveTruck = new Button();
buttonAddTruck = new Button(); buttonAddTruck = new Button();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
textBoxStorageName = new TextBox();
groupBox1.SuspendLayout(); groupBox1.SuspendLayout();
groupBox2.SuspendLayout(); groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
@ -68,11 +68,18 @@
groupBox2.Controls.Add(buttonAddObject); groupBox2.Controls.Add(buttonAddObject);
groupBox2.Location = new Point(3, 24); groupBox2.Location = new Point(3, 24);
groupBox2.Name = "groupBox2"; groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(220, 331); groupBox2.Size = new Size(220, 302);
groupBox2.TabIndex = 5; groupBox2.TabIndex = 5;
groupBox2.TabStop = false; groupBox2.TabStop = false;
groupBox2.Text = "Наборы"; groupBox2.Text = "Наборы";
// //
// textBoxStorageName
//
textBoxStorageName.Location = new Point(30, 50);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(171, 27);
textBoxStorageName.TabIndex = 3;
//
// listBoxStorages // listBoxStorages
// //
listBoxStorages.FormattingEnabled = true; listBoxStorages.FormattingEnabled = true;
@ -153,13 +160,6 @@
pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
// textBoxStorageName
//
textBoxStorageName.Location = new Point(30, 50);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(171, 27);
textBoxStorageName.TabIndex = 3;
//
// FormTruckCollection // FormTruckCollection
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);

View File

@ -90,7 +90,7 @@
// labelSimpleObject // labelSimpleObject
// //
labelSimpleObject.BorderStyle = BorderStyle.FixedSingle; labelSimpleObject.BorderStyle = BorderStyle.FixedSingle;
labelSimpleObject.Location = new Point(317, 264); labelSimpleObject.Location = new Point(300, 264);
labelSimpleObject.Name = "labelSimpleObject"; labelSimpleObject.Name = "labelSimpleObject";
labelSimpleObject.Size = new Size(140, 40); labelSimpleObject.Size = new Size(140, 40);
labelSimpleObject.TabIndex = 6; labelSimpleObject.TabIndex = 6;

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -29,6 +30,18 @@ namespace DumpTruck.Generics
/// </summary> /// </summary>
private readonly int _pictureHeight; private readonly int _pictureHeight;
/// <summary> /// <summary>
/// Разделитель для записи ключа и значения элемента словаря
/// </summary>
private static readonly char _separatorForKeyValue = '|';
/// <summary>
/// Разделитель для записей коллекции данных в файл
/// </summary>
private readonly char _separatorRecords = ';';
/// <summary>
/// Разделитель для записи информации по объекту в файл
/// </summary>
private static readonly char _separatorForObject = ':';
/// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="pictureWidth"></param> /// <param name="pictureWidth"></param>
@ -73,6 +86,83 @@ namespace DumpTruck.Generics
return null; return null;
} }
} }
/// <summary>
/// Сохранение информации по автомобилям в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, TrucksGenericCollection<DrawningTruck, DrawningObjectTruck>> record in _truckStorages)
{
StringBuilder records = new();
foreach (DrawningTruck? elem in record.Value.GetTrucks)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using StreamWriter sw = new(filename);
sw.Write($"TruckStorage{Environment.NewLine}{data}");
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 str = sr.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!str.StartsWith("TruckStorage"))
{
return false;
}
_truckStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
TrucksGenericCollection<DrawningTruck, DrawningObjectTruck> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningTruck? truck = elem?.CreateDrawningTruck(_separatorForObject, _pictureWidth, _pictureHeight);
if (truck != null)
{
if (collection + truck == -1)
{
return false;
}
}
}
_truckStorages.Add(record[0], collection);
}
}
return true;
} }
}
} }

View File

@ -132,5 +132,10 @@ namespace DumpTruck.Generics
truck.DrawTransport(g); truck.DrawTransport(g);
} }
} }
/// <summary>
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetTrucks => _collection.GetTrucks();
} }
} }