Class finish
This commit is contained in:
parent
763e829619
commit
ed6631e1ea
@ -53,7 +53,7 @@ public abstract class AbstractCompany
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = collection;
|
||||
_collection.SetMaxCount = GetMaxCount;
|
||||
_collection.MaxCount = GetMaxCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using AirBomber.CollectionGenericObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -15,7 +16,7 @@ public interface ICollectionGenericObjects<T>
|
||||
/// <summary>
|
||||
/// Установка максимального количества элементов
|
||||
/// </summary>
|
||||
int SetMaxCount { set; }
|
||||
int MaxCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Добавление объекта в коллекцию
|
||||
@ -45,4 +46,15 @@ public interface ICollectionGenericObjects<T>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns>Объект</returns>
|
||||
T? Get(int position);
|
||||
|
||||
/// <summary>
|
||||
/// Получение типа коллекции
|
||||
/// </summary>
|
||||
CollectionType GetCollectionType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Поэлементный вывод элементов коллекции
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IEnumerable<T?> GetItems();
|
||||
}
|
||||
|
@ -16,7 +16,19 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
private int _maxCount;
|
||||
public int Count => _collection.Count;
|
||||
|
||||
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
|
||||
public int MaxCount
|
||||
{
|
||||
get => _maxCount;
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
_maxCount = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CollectionType GetCollectionType => CollectionType.List;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
@ -64,4 +76,12 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
_collection.RemoveAt(position);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public IEnumerable<T?> GetItems()
|
||||
{
|
||||
for (int i = 0; i < _collection.Count; ++i)
|
||||
{
|
||||
yield return _collection[i];
|
||||
}
|
||||
}
|
||||
}
|
@ -15,8 +15,12 @@ where T : class
|
||||
|
||||
public int Count => _collection.Length;
|
||||
|
||||
public int SetMaxCount
|
||||
public int MaxCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return _collection.Length;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
@ -33,6 +37,8 @@ where T : class
|
||||
}
|
||||
}
|
||||
|
||||
public CollectionType GetCollectionType => CollectionType.Massive;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -106,4 +112,12 @@ where T : class
|
||||
_collection[position] = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
public IEnumerable<T?> GetItems()
|
||||
{
|
||||
for (int i = 0; i < _collection.Length; ++i)
|
||||
{
|
||||
yield return _collection[i];
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using AirBomber.Drawnings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -10,7 +11,7 @@ namespace AirBomber.CollectionGenericObjects;
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class StorageCollection<T>
|
||||
where T : class
|
||||
where T : DrawningAirPlane
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище) с коллекциями
|
||||
@ -22,6 +23,21 @@ public class StorageCollection<T>
|
||||
/// </summary>
|
||||
public List<string> Keys => _storages.Keys.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Ключевое слово, с которого должен начинаться файл
|
||||
/// </summary>
|
||||
private readonly string _collectionKey = "CollectionsStorage";
|
||||
|
||||
/// <summary>
|
||||
/// Разделитель для записи ключа и значения элемента словаря
|
||||
/// </summary>
|
||||
private readonly string _separatorForKeyValue = "|";
|
||||
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly string _separatorItems = ";";
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -83,4 +99,118 @@ public class StorageCollection<T>
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сохранение информации по автомобилям в хранилище в файл
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (_storages.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
|
||||
StringBuilder sb = new();
|
||||
|
||||
using (StreamWriter sw = new StreamWriter(filename))
|
||||
{
|
||||
sw.WriteLine(_collectionKey.ToString());
|
||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> kvpair in _storages)
|
||||
{
|
||||
// не сохраняем пустые коллекции
|
||||
if (kvpair.Value.Count == 0)
|
||||
continue;
|
||||
sb.Append(kvpair.Key);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
sb.Append(kvpair.Value.GetCollectionType);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
sb.Append(kvpair.Value.MaxCount);
|
||||
sb.Append(_separatorForKeyValue);
|
||||
foreach (T? item in kvpair.Value.GetItems())
|
||||
{
|
||||
string data = item?.GetDataForSave() ?? string.Empty;
|
||||
if (string.IsNullOrEmpty(data))
|
||||
continue;
|
||||
sb.Append(data);
|
||||
sb.Append(_separatorItems);
|
||||
}
|
||||
sw.WriteLine(sb.ToString());
|
||||
sb.Clear();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Загрузка информации по автомобилям в хранилище из файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using (StreamReader sr = new StreamReader(filename))
|
||||
{
|
||||
string? str;
|
||||
str = sr.ReadLine();
|
||||
if (str != _collectionKey.ToString())
|
||||
return false;
|
||||
_storages.Clear();
|
||||
while ((str = sr.ReadLine()) != null)
|
||||
{
|
||||
string[] record = str.Split(_separatorForKeyValue);
|
||||
if (record.Length != 4)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||
if (collection == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||
|
||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
if (elem?.CreateDrawningAirPlane() is T boat)
|
||||
{
|
||||
if (collection.Insert(boat) == -1)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_storages.Add(record[0], collection);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание коллекции по типу
|
||||
/// </summary>
|
||||
/// <param name="collectionType"></param>
|
||||
/// <returns></returns>
|
||||
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
|
||||
{
|
||||
return collectionType switch
|
||||
{
|
||||
CollectionType.Massive => new MassiveGenericObjects<T>(),
|
||||
CollectionType.List => new ListGenericObjects<T>(),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
}
|
@ -28,6 +28,14 @@ public class DrawningAirBomber : DrawningAirPlane
|
||||
EntityAirPlane = new EntityAirBomber(speed, weight, bodyColor, additionalColor, bombs, fuelTanks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор через сущность
|
||||
/// </summary>
|
||||
/// <param name="entityAirPlane"></param>
|
||||
public DrawningAirBomber(EntityAirPlane entityAirPlane) : base()
|
||||
{
|
||||
EntityAirPlane = entityAirPlane;
|
||||
}
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityAirPlane == null || EntityAirPlane is not EntityAirBomber airbomber || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
|
@ -69,7 +69,7 @@ public class DrawningAirPlane
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
///
|
||||
private DrawningAirPlane()
|
||||
protected DrawningAirPlane()
|
||||
{
|
||||
_pictureHeight = null;
|
||||
_pictureHeight = null;
|
||||
@ -92,11 +92,20 @@ public class DrawningAirPlane
|
||||
/// </summary>
|
||||
/// <param name="drawningAirBomberWidth">Ширина прорисовки самолета</param>
|
||||
/// <param name="drawningAirBomberWidth">Высота прорисовки самолета</param>
|
||||
protected DrawningAirPlane(int drawningAirBomberWidth, int drawningAirBomberHeight) : this()
|
||||
public DrawningAirPlane(int drawningAirBomberWidth, int drawningAirBomberHeight) : this()
|
||||
{
|
||||
_drawningAirPlaneHeight = drawningAirBomberHeight;
|
||||
_drawningAirPlaneWidth = drawningAirBomberWidth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор через сущность
|
||||
/// </summary>
|
||||
/// <param name="entityAirPlane"></param>
|
||||
public DrawningAirPlane(EntityAirPlane entityAirPlane) : base()
|
||||
{
|
||||
EntityAirPlane = entityAirPlane;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
|
56
AirBomber/Drawnings/ExtentionDrawningAirPlane.cs
Normal file
56
AirBomber/Drawnings/ExtentionDrawningAirPlane.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AirBomber.Entities;
|
||||
|
||||
namespace AirBomber.Drawnings;
|
||||
|
||||
public static class ExtentionDrawningAirPlane
|
||||
{
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly string _separatorForObject = ":";
|
||||
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info">Строка с данными для создания объекта</param>
|
||||
/// <returns>Объект</returns>
|
||||
public static DrawningAirPlane? CreateDrawningAirPlane(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
EntityAirPlane? airPlane = EntityAirBomber.CreateEntityAirBomber(strs);
|
||||
if (airPlane != null)
|
||||
{
|
||||
return new DrawningAirBomber(airPlane);
|
||||
}
|
||||
|
||||
airPlane = EntityAirPlane.CreateEntityAirPlane(strs);
|
||||
if (airPlane != null)
|
||||
{
|
||||
return new DrawningAirPlane(airPlane);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
/// <param name="drawningAirPlane">Сохраняемый объект</param>
|
||||
/// <returns>Строка с данными по объекту</returns>
|
||||
public static string GetDataForSave(this DrawningAirPlane drawningAirPlane)
|
||||
{
|
||||
string[]? array = drawningAirPlane?.EntityAirPlane?.GetStringRepresentation();
|
||||
|
||||
if (array == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return string.Join(_separatorForObject, array);
|
||||
}
|
||||
}
|
@ -4,38 +4,52 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber.Entities
|
||||
namespace AirBomber.Entities;
|
||||
|
||||
public class EntityAirBomber : EntityAirPlane
|
||||
{
|
||||
public class EntityAirBomber : EntityAirPlane
|
||||
public Color AdditionalColor { get; private set; }
|
||||
|
||||
public void SetAdditionalColor(Color color) => AdditionalColor = color;
|
||||
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия бомб
|
||||
/// </summary>
|
||||
public bool Bombs { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия дополнительных топливных баков
|
||||
/// </summary>
|
||||
public bool FuelTanks { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="bombs">Признак наличия бомб</param>
|
||||
/// <param name="fuelTanks">Признак наличия дополнительных топливных баков</param>
|
||||
public EntityAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, bool bombs, bool fuelTanks) : base(speed, weight, bodyColor)
|
||||
{
|
||||
public Color AdditionalColor { get; private set; }
|
||||
|
||||
public void SetAdditionalColor(Color color) => AdditionalColor = color;
|
||||
AdditionalColor = additionalColor;
|
||||
Bombs = bombs;
|
||||
FuelTanks = fuelTanks;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия бомб
|
||||
/// </summary>
|
||||
public bool Bombs { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия дополнительных топливных баков
|
||||
/// </summary>
|
||||
public bool FuelTanks { get; private set; }
|
||||
public override string[] GetStringRepresentation()
|
||||
{
|
||||
return new[] { nameof(EntityAirBomber), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Bombs.ToString(), FuelTanks.ToString() };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="bombs">Признак наличия бомб</param>
|
||||
/// <param name="fuelTanks">Признак наличия дополнительных топливных баков</param>
|
||||
public EntityAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, bool bombs, bool fuelTanks) : base(speed, weight, bodyColor)
|
||||
public static EntityAirBomber? CreateEntityAirBomber(string[] strs)
|
||||
{
|
||||
if (strs.Length != 7 || strs[0] != nameof(EntityAirBomber))
|
||||
{
|
||||
|
||||
AdditionalColor = additionalColor;
|
||||
Bombs = bombs;
|
||||
FuelTanks = fuelTanks;
|
||||
return null;
|
||||
}
|
||||
return new EntityAirBomber(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]),
|
||||
Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]));
|
||||
}
|
||||
}
|
||||
|
@ -42,4 +42,28 @@ public class EntityAirPlane
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение строк со значениями свойств объекта класса-сущности
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual string[] GetStringRepresentation()
|
||||
{
|
||||
return new[] { nameof(EntityAirPlane), Speed.ToString(), Weight.ToString(), BodyColor.Name };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание объекта из массива строк
|
||||
/// </summary>
|
||||
/// <param name="strs"></param>
|
||||
/// <returns></returns>
|
||||
public static EntityAirPlane? CreateEntityAirPlane(string[] strs)
|
||||
{
|
||||
if (strs.Length != 4 || strs[0] != nameof(EntityAirPlane))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new EntityAirPlane(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user