diff --git a/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs index 1df90f4..dc910b4 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs @@ -14,14 +14,6 @@ dopColor, bool flightDeck, bool hangarDeck, bool route) : { AircraftCarrier = new EntityModernAircraftCarrier(speed, weight, bodyColor, dopColor, flightDeck, hangarDeck, route); } - public void ReturnColor(Color returnColor) - { - if (AircraftCarrier is not EntityModernAircraftCarrier modernAircraftCarrier) - { - return; - } - modernAircraftCarrier.DopColor = returnColor; - } public override void DrawTransport(Graphics g) { if (AircraftCarrier is not EntityModernAircraftCarrier modernAircraftCarrier) diff --git a/AircraftCarrier/AircraftCarrier/DrawningObjectAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningObjectAircraftCarrier.cs index 3f8246c..e28ee81 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningObjectAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningObjectAircraftCarrier.cs @@ -30,5 +30,8 @@ namespace AircraftCarrier { _aircraftcarrier.DrawTransport(g); } + public string GetInfo() => _aircraftcarrier?.AircraftCarrier.ToString(); + + public static IDrawningObject Create(string data) => new DrawningObjectAircraftCarrier(data.CreateDrawningAircraftCarrier()); } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs index b6dbf8c..76f978a 100644 --- a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs @@ -8,6 +8,7 @@ namespace AircraftCarrier { public class EntityAircraftCarrier { + private static readonly char _separatorForObject = ':'; public int Speed { get; private set; } public float Weight { get; private set; } public Color BodyColor { get; set; } @@ -19,5 +20,10 @@ namespace AircraftCarrier Weight = weight <= 0 ? rnd.Next(40, 70) : weight; BodyColor = bodyColor; } + public override string ToString() + { + var str = $"{Speed}{_separatorForObject}{Weight}{_separatorForObject}{BodyColor.Name}"; + return str; + } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/EntityModernAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/EntityModernAircraftCarrier.cs index a998c0c..85ba59e 100644 --- a/AircraftCarrier/AircraftCarrier/EntityModernAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/EntityModernAircraftCarrier.cs @@ -4,11 +4,13 @@ using System.Linq; using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; +using static System.Reflection.Metadata.BlobBuilder; namespace AircraftCarrier { internal class EntityModernAircraftCarrier : EntityAircraftCarrier { + private static readonly char _separatorForObject = ':'; public Color DopColor { get; set; } public bool FlightDeck { get; private set; } public bool HangarDeck { get; private set; } @@ -20,5 +22,10 @@ namespace AircraftCarrier HangarDeck = hangarDeck; Route = route; } + public override string ToString() + { + var str = base.ToString(); + return $"{str}{_separatorForObject}{DopColor.Name}{_separatorForObject}{FlightDeck}{_separatorForObject}{HangarDeck}{_separatorForObject}{Route}"; + } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/ExtentionAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/ExtentionAircraftCarrier.cs new file mode 100644 index 0000000..b13e771 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/ExtentionAircraftCarrier.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal static class ExtentionAircraftCarrier + { + private static readonly char _separatorForObject = ':'; + public static DrawningAircraftCarrier CreateDrawningAircraftCarrier(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningAircraftCarrier(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawningModernAircraftCarrier(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), + Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } + return null; + } + public static string GetDataForSave(this DrawningAircraftCarrier drawningAircraftCarrier) + { + var aircraftcarrier = drawningAircraftCarrier.AircraftCarrier; + var str = + $"{aircraftcarrier.Speed}{_separatorForObject}{aircraftcarrier.Weight}{_separatorForObject}{aircraftcarrier.BodyColor.Name}"; + if (aircraftcarrier is not EntityModernAircraftCarrier modernAircraftCarrier) + { + return str; + } + return + $"{str}{_separatorForObject}{modernAircraftCarrier.DopColor.Name}{_separatorForObject}{modernAircraftCarrier.FlightDeck}{_separatorForObject}{modernAircraftCarrier.HangarDeck}{_separatorForObject}{modernAircraftCarrier.Route}"; + } + } +} \ No newline at end of file