Начала лаб 6

This commit is contained in:
TUSHKANCH11K 2024-04-15 05:09:11 +04:00
parent 9712e38913
commit b2dd4bf3a1
9 changed files with 152 additions and 7 deletions

View File

@ -14,7 +14,7 @@ public interface ICollectionGenericObjects<T>
/// <summary> /// <summary>
/// Установка максимального количества элементов /// Установка максимального количества элементов
/// </summary> /// </summary>
int SetMaxCount { set; } int MaxCount { set; }
/// <summary> /// <summary>
/// Добавление объекта в коллекцию /// Добавление объекта в коллекцию
/// </summary> /// </summary>
@ -40,4 +40,13 @@ public interface ICollectionGenericObjects<T>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns>Объект</returns> /// <returns>Объект</returns>
T? Get(int position); T? Get(int position);
/// <summary>
/// Получение типа коллекции
/// </summary>
CollectionType GetCollectionType { get; }
/// <summary>
/// Получение объектов коллекции по одному
/// </summary>
/// <returns>Поэлементый вывод элементов коллекции</returns>
IEnumerable<T?> GetItems();
} }

View File

@ -11,6 +11,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
/// Максимально допустимое число объектов в списке /// Максимально допустимое число объектов в списке
/// </summary> /// </summary>
private int _maxCount; private int _maxCount;
public CollectionType GetCollectionType => CollectionType.List;
public int Count => _collection.Count; public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
/// <summary> /// <summary>
@ -53,4 +54,11 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
_collection.RemoveAt(position); _collection.RemoveAt(position);
return obj; return obj;
} }
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < Count; ++i)
{
yield return _collection[i];
}
}
} }

View File

@ -12,10 +12,28 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
/// </summary> /// </summary>
private T?[] _collection; private T?[] _collection;
public int Count => _collection.Length; public int Count => _collection.Length;
public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } public CollectionType GetCollectionType => CollectionType.Massive;
/// <summary> public int MaxCount
/// Конструктор {
/// </summary> get
{
return _collection.Length;
}
set
{
if (value > 0)
{
if (_collection.Length > 0)
{
Array.Resize(ref _collection, value);
}
else
{
_collection = new T?[value];
}
}
}
}
public MassiveGenericObjects() public MassiveGenericObjects()
{ {
_collection = Array.Empty<T?>(); _collection = Array.Empty<T?>();
@ -87,4 +105,11 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
_collection[position] = null; _collection[position] = null;
return obj; return obj;
} }
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Length; ++i)
{
yield return _collection[i];
}
}
} }

View File

@ -2,7 +2,7 @@
namespace SelfPropelledArtilleryUnit.CollectionGenericObjects; namespace SelfPropelledArtilleryUnit.CollectionGenericObjects;
public class StorageCollection<T> public class StorageCollection<T>
where T : class where T : DrawningPropelledArtillery
{ {
/// <summary> /// <summary>
/// Словарь (хранилище) с коллекциями /// Словарь (хранилище) с коллекциями
@ -61,4 +61,5 @@ public class StorageCollection<T>
return null; return null;
} }
} }
} }

View File

@ -99,7 +99,10 @@ public class DrawningPropelledArtillery
_drawningPropelledArtilleryWidth = drawningPropelledArtilleryWidth; _drawningPropelledArtilleryWidth = drawningPropelledArtilleryWidth;
_drawningPropelledArtilleryHeight = drawningPropelledArtilleryHeight; _drawningPropelledArtilleryHeight = drawningPropelledArtilleryHeight;
} }
public DrawningPropelledArtillery(EntityPropelledArtillery propelledArtillery) : this()
{
EntityPropelledArtillery = new EntityPropelledArtillery(propelledArtillery.Speed, propelledArtillery.Weight, propelledArtillery.BodyColor);
}
/// <summary> /// <summary>
/// Установка границ поля /// Установка границ поля
/// </summary> /// </summary>

View File

@ -18,6 +18,10 @@ public class DrawningSelfPropelledArtilleryUnit : DrawningPropelledArtillery
EntityPropelledArtillery = new EntitySelfPropelledArtilleryUnit(speed, weight, bodyColor, additionalColor, turretCannon, launchBattery); 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) public override void DrawTransport(Graphics g)
{ {

View File

@ -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
{
/// <summary>
/// Разделитель для записи информации по объекту в файл
/// </summary>
private static readonly string _separatorForObject = ":";
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="info">Строка с данными для создания объекта</param>
/// <returns>Объект</returns>
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;
}
/// <summary>
/// Получение данных для сохранения в файл
/// </summary>
/// <param name="drawningCar">Сохраняемый объект</param>
/// <returns>Строка с данными по объекту</returns>
public static string GetDataForSave(this DrawningPropelledArtillery drawningCar)
{
string[]? array = drawningCar?.EntityPropelledArtillery?.GetStringRepresentation();
if (array == null)
{
return string.Empty;
}
return string.Join(_separatorForObject, array);
}
}

View File

@ -36,4 +36,26 @@ public class EntityPropelledArtillery
Weight = weight; Weight = weight;
BodyColor = bodyColor; BodyColor = bodyColor;
} }
/// <summary>
/// Получение строк со значениями свойств объекта класса-сущности
/// </summary>
/// <returns></returns>
public virtual string[] GetStringRepresentation()
{
return new[] { nameof(EntityPropelledArtillery), Speed.ToString(), Weight.ToString(), BodyColor.Name };
}
/// <summary>
/// Создание объекта из массива строк
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
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]));
}
} }

View File

@ -37,5 +37,28 @@ public class EntitySelfPropelledArtilleryUnit : EntityPropelledArtillery
TurretCannon = turretcannon; TurretCannon = turretcannon;
LaunchBattery = launchbattery; LaunchBattery = launchbattery;
} }
/// <summary>
/// Получение строк со значениями свойств продвинутого объекта класса-сущности
/// </summary>
/// <returns></returns>
public override string[] GetStringRepresentation()
{
return new[] { nameof(EntitySelfPropelledArtilleryUnit), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name,
TurretCannon.ToString(), LaunchBattery.ToString()};
}
/// <summary>
/// Создание продвинутого объекта из массива строк
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
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]));
}
} }