This commit is contained in:
Daniya_Youdakova 2022-12-24 22:59:12 +04:00
parent 382cf11d89
commit 910cc861f9
3 changed files with 93 additions and 13 deletions

View File

@ -111,6 +111,26 @@ namespace AircraftCarrier
} }
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var aircraftcarrier in _setAircraftCarriers.GetAircraftCarriers())
{
data += $"{aircraftcarrier.GetInfo()}{separatorData}";
}
return data;
}
/// <summary>
/// Загрузка списка из массива строк
/// </summary>
/// <param name="records"></param>
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setAircraftCarriers.Insert(DrawningObjectAircraftCarrier.Create(rec) as T);
}
}
/// <summary> /// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале /// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary> /// </summary>
@ -164,10 +184,8 @@ namespace AircraftCarrier
{ {
int yNumOfPlaces = _pictureHeight / _placeSizeHeight; int yNumOfPlaces = _pictureHeight / _placeSizeHeight;
int xNumOfPlaces = _pictureWidth / _placeSizeWidth; int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
int rowNum = yNumOfPlaces - 1; int rowNum = yNumOfPlaces - 1;
int columnNum = 0; int columnNum = 0;
for (int i = 0; i < _setAircraftCarriers.Count; i++) for (int i = 0; i < _setAircraftCarriers.Count; i++)
{ {
if (_setAircraftCarriers[i] != null) if (_setAircraftCarriers[i] != null)

View File

@ -11,7 +11,7 @@ namespace AircraftCarrier
/// <summary> /// <summary>
/// Словарь (хранилище) с картами /// Словарь (хранилище) с картами
/// </summary> /// </summary>
readonly Dictionary<string, MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, readonly Dictionary<string, MapWithSetAircraftCarriersGeneric<IDrawningObject,
AbstractMap>> _mapStorages; AbstractMap>> _mapStorages;
/// <summary> /// <summary>
/// Возвращение списка названий карт /// Возвращение списка названий карт
@ -30,10 +30,11 @@ namespace AircraftCarrier
/// </summary> /// </summary>
/// <param name="pictureWidth"></param> /// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param> /// <param name="pictureHeight"></param>
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight) public MapsCollection(int pictureWidth, int pictureHeight)
{ {
_mapStorages = new Dictionary<string, _mapStorages = new Dictionary<string,MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap>>();
MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
@ -63,7 +64,7 @@ namespace AircraftCarrier
/// </summary> /// </summary>
/// <param name="ind"></param> /// <param name="ind"></param>
/// <returns></returns> /// <returns></returns>
public MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap> this[string public MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap> this[string
ind] ind]
{ {
get get
@ -72,7 +73,7 @@ namespace AircraftCarrier
return mapWithSetAircraftCarriersGeneric; return mapWithSetAircraftCarriersGeneric;
} }
} }
public MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap> this[int index] public MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap> this[int index]
{ {
get get
{ {
@ -88,5 +89,66 @@ namespace AircraftCarrier
} }
} }
} }
/// <summary>
/// Сохранение информации по автомобилям в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns></returns>
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
/// <summary>
/// Загрузка нформации по автомобилям на парковках из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "HardMap":
map = new ComplexMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
} }
} }

View File

@ -55,6 +55,7 @@ namespace AircraftCarrier
_places.Insert(position, AircraftCarrier); _places.Insert(position, AircraftCarrier);
return position; return position;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
/// </summary> /// </summary>
@ -75,7 +76,6 @@ namespace AircraftCarrier
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public T this[int position] public T this[int position]
{ {
get get
@ -86,14 +86,15 @@ namespace AircraftCarrier
{ {
Insert(value, position); Insert(value, position);
} }
} }
public IEnumerable<T> GetCars() public IEnumerable<T> GetAircraftCarriers()
{ {
foreach (var car in _places) foreach (var aircraftcarrier in _places)
{ {
if (car != null) if (aircraftcarrier != null)
{ {
yield return car; yield return aircraftcarrier;
} }
else else
{ {
@ -101,6 +102,5 @@ namespace AircraftCarrier
} }
} }
} }
} }
} }