PIbd-21_Putintsev_D.M._Road.../RoadTrain/ExtentionDrawningTrain.cs

64 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoadTrain.Entities;
namespace RoadTrain.DrawningObjects
{
public static class ExtentionDrawningTrain
{
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="info">Строка с данными для создания объекта</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
/// <returns>Объект</returns>
public static DrawningRoadTrain ? CreateDrawningTrain(this string info, char
separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningRoadTrain(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningTrain(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]),
Color.FromName(strs[3]),
Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]),
width, height);
}
return null;
}
/// <summary>
/// Получение данных для сохранения в файл
/// </summary>
/// <param name="drawningRoadTrain">Сохраняемый объект</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <returns>Строка с данными по объекту</returns>
public static string GetDataForSave(this DrawningRoadTrain drawningRoadTrain,
char separatorForObject)
{
var roadTrain = drawningRoadTrain.EntityRoadTrain;
if (roadTrain == null)
{
return string.Empty;
}
var str = $"{roadTrain.Speed}{separatorForObject}{roadTrain.Weight}{separatorForObject}{roadTrain.BodyColor.Name}";
if (roadTrain is not EntityTrain train)
{
return str;
}
return
$"{str}{separatorForObject}{train.AdditionalColor.Name}{separatorForObject}{train.WaterContainer}{separatorForObject}{train.SweepingBrush}";
}
}
}