diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs index 094d599..157a9d6 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -14,7 +14,7 @@ public interface ICollectionGenericObjects /// /// Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { set; } /// /// Добавление объекта в коллекцию /// @@ -40,4 +40,13 @@ public interface ICollectionGenericObjects /// Позиция /// Объект T? Get(int position); + /// + /// Получение типа коллекции + /// + CollectionType GetCollectionType { get; } + /// + /// Получение объектов коллекции по одному + /// + /// Поэлементый вывод элементов коллекции + IEnumerable GetItems(); } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs index 06e23e5..560a689 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs @@ -11,6 +11,7 @@ public class ListGenericObjects : ICollectionGenericObjects /// Максимально допустимое число объектов в списке /// private int _maxCount; + public CollectionType GetCollectionType => CollectionType.List; public int Count => _collection.Count; public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } /// @@ -53,4 +54,11 @@ public class ListGenericObjects : ICollectionGenericObjects _collection.RemoveAt(position); return obj; } + public IEnumerable GetItems() + { + for (int i = 0; i < Count; ++i) + { + yield return _collection[i]; + } + } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs index a87e788..8801a79 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs @@ -12,10 +12,28 @@ public class MassiveGenericObjects : ICollectionGenericObjects /// private T?[] _collection; public int Count => _collection.Length; - public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } - /// - /// Конструктор - /// + public CollectionType GetCollectionType => CollectionType.Massive; + public int MaxCount + { + get + { + return _collection.Length; + } + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } public MassiveGenericObjects() { _collection = Array.Empty(); @@ -87,4 +105,11 @@ public class MassiveGenericObjects : ICollectionGenericObjects _collection[position] = null; return obj; } + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; ++i) + { + yield return _collection[i]; + } + } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs index 491cfb5..405ec89 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs @@ -2,7 +2,7 @@ namespace SelfPropelledArtilleryUnit.CollectionGenericObjects; public class StorageCollection - where T : class + where T : DrawningPropelledArtillery { /// /// Словарь (хранилище) с коллекциями @@ -61,4 +61,5 @@ public class StorageCollection return null; } } + } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningPropelledArtillery.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningPropelledArtillery.cs index 8c05e24..72706bd 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningPropelledArtillery.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningPropelledArtillery.cs @@ -99,7 +99,10 @@ public class DrawningPropelledArtillery _drawningPropelledArtilleryWidth = drawningPropelledArtilleryWidth; _drawningPropelledArtilleryHeight = drawningPropelledArtilleryHeight; } - + public DrawningPropelledArtillery(EntityPropelledArtillery propelledArtillery) : this() + { + EntityPropelledArtillery = new EntityPropelledArtillery(propelledArtillery.Speed, propelledArtillery.Weight, propelledArtillery.BodyColor); + } /// /// Установка границ поля /// diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningSelfPropelledArtilleryUnit.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningSelfPropelledArtilleryUnit.cs index c0cea51..eb2cb69 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningSelfPropelledArtilleryUnit.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningSelfPropelledArtilleryUnit.cs @@ -18,6 +18,10 @@ public class DrawningSelfPropelledArtilleryUnit : DrawningPropelledArtillery EntityPropelledArtillery = new EntitySelfPropelledArtilleryUnit(speed, weight, bodyColor, additionalColor, turretCannon, launchBattery); //дописать } + public DrawningSelfPropelledArtilleryUnit(EntitySelfPropelledArtilleryUnit ship) : base(135, 105) + { + EntityPropelledArtillery = new EntitySelfPropelledArtilleryUnit(ship.Speed, ship.Weight, ship.BodyColor, ship.AdditionalColor, ship.TurretCannon, ship.LaunchBattery); + } public override void DrawTransport(Graphics g) { diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/ExtentionDrawningPropelledArtillery.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/ExtentionDrawningPropelledArtillery.cs new file mode 100644 index 0000000..6feebc1 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/ExtentionDrawningPropelledArtillery.cs @@ -0,0 +1,50 @@ +using SelfPropelledArtilleryUnit.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.Drawnings; + +public static class ExtentionDrawningPropelledArtillery +{ + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separatorForObject = ":"; + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + public static DrawningPropelledArtillery? CreateDrawningPropelledArtillery(this string info) + { + string[] strs = info.Split(_separatorForObject); + EntityPropelledArtillery? propelledArtillery = EntitySelfPropelledArtilleryUnit.CreateEntitySelfPropelledArtilleryUnit(strs); + if (propelledArtillery != null) + { + return new DrawningSelfPropelledArtilleryUnit((EntitySelfPropelledArtilleryUnit)propelledArtillery); + } + propelledArtillery = EntityPropelledArtillery.CreateEntityPropelledArtillery(strs); + if (propelledArtillery != null) + { + return new DrawningPropelledArtillery(propelledArtillery); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningPropelledArtillery drawningCar) + { + string[]? array = drawningCar?.EntityPropelledArtillery?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separatorForObject, array); + } +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntityPropelledArtillery.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntityPropelledArtillery.cs index e25a793..5a33d73 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntityPropelledArtillery.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntityPropelledArtillery.cs @@ -36,4 +36,26 @@ public class EntityPropelledArtillery Weight = weight; BodyColor = bodyColor; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityPropelledArtillery), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityPropelledArtillery? CreateEntityPropelledArtillery(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityPropelledArtillery)) + { + return null; + } + return new EntityPropelledArtillery(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntitySelfPropelledArtilleryUnit.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntitySelfPropelledArtilleryUnit.cs index 6c7ca62..17e18d9 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntitySelfPropelledArtilleryUnit.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Entities/EntitySelfPropelledArtilleryUnit.cs @@ -37,5 +37,28 @@ public class EntitySelfPropelledArtilleryUnit : EntityPropelledArtillery TurretCannon = turretcannon; LaunchBattery = launchbattery; } + /// + /// Получение строк со значениями свойств продвинутого объекта класса-сущности + /// + /// + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntitySelfPropelledArtilleryUnit), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, + TurretCannon.ToString(), LaunchBattery.ToString()}; + } + /// + /// Создание продвинутого объекта из массива строк + /// + /// + /// + public static EntitySelfPropelledArtilleryUnit? CreateEntitySelfPropelledArtilleryUnit(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntitySelfPropelledArtilleryUnit)) + { + return null; + } + return new EntitySelfPropelledArtilleryUnit(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]), + Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } }