diff --git a/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs b/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs new file mode 100644 index 0000000..ea0969a --- /dev/null +++ b/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal static class ExtensionArmoredCar + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningArmoredCar CreateDrawningArmoredCar(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningArmoredCar(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningTank(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), + Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5])); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// + /// + public static string GetDataForSave(this DrawningArmoredCar drawningArmoredCar) + { + var armoredCar = drawningArmoredCar.ArmoredCar; + var str = + $"{armoredCar.Speed}{_separatorForObject}{armoredCar.Weight}{_separatorForObject}{armoredCar.BodyColor.Name}"; + if (armoredCar is not EntityTank tank) + { + return str; + } + return + $"{str}{_separatorForObject}{tank.DopColor.Name}{_separatorForObject}{tank.TowerWeapon}{_separatorForObject}{tank.AMachineGun}"; + } + } +} +