Приведено к рабочему виду из лекции
This commit is contained in:
parent
d0d784e33e
commit
f2a405c743
@ -41,7 +41,9 @@ namespace Trolleybus
|
||||
|
||||
void IDrawningObject.DrawningObject(Graphics g)
|
||||
{
|
||||
// TODO
|
||||
_trolleybus.DrawTransport(g);
|
||||
}
|
||||
public string GetInfo() => _trolleybus?.GetDataForSave();
|
||||
public static IDrawningObject Create(string data) => new DrawningObjectTrolleybus(data.CreateDrawingTrolleybus());
|
||||
}
|
||||
}
|
||||
|
@ -42,5 +42,12 @@ namespace Trolleybus
|
||||
{
|
||||
_trolleybus.DrawTransport(g);
|
||||
}
|
||||
|
||||
public string GetInfo() => _trolleybus?.GetDataForSave();
|
||||
|
||||
public static IDrawningObject Create(string data)
|
||||
{
|
||||
return new DrawningObjectTrolleybus(data.CreateDrawingTrolleybus());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
43
Trolleybus/Trolleybus/ExtentionTrolleybus.cs
Normal file
43
Trolleybus/Trolleybus/ExtentionTrolleybus.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Trolleybus
|
||||
{
|
||||
internal static class ExtentionTrolleybus
|
||||
{
|
||||
private static readonly char _separatorForObject = ':';
|
||||
public static DrawingTrolleybus CreateDrawingTrolleybus(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawingTrolleybus(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawningSmallTrolleybus(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 DrawingTrolleybus drawningTrolleybus)
|
||||
{
|
||||
var trolleybus = drawningTrolleybus.Trolleybus;
|
||||
var str = $"{trolleybus.Speed}{_separatorForObject}{trolleybus.Weight}{_separatorForObject}{trolleybus.BodyColor.Name}";
|
||||
if (trolleybus is not EntitySmallTrolleybus smallTrolleybus)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return $"{str}{_separatorForObject}{smallTrolleybus.DopColor.Name}{_separatorForObject}{smallTrolleybus.Horns}{_separatorForObject}{smallTrolleybus.Battary}";
|
||||
}
|
||||
}
|
||||
}
|
@ -38,5 +38,6 @@ namespace Trolleybus
|
||||
/// <returns></returns>
|
||||
void nothing();
|
||||
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
|
||||
string GetInfo();
|
||||
}
|
||||
}
|
||||
|
@ -112,6 +112,24 @@ namespace Trolleybus
|
||||
}
|
||||
return new(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
|
||||
public string GetData(char separatorType, char separatorData)
|
||||
{
|
||||
string data = $"{_map.GetType().Name}{separatorType}";
|
||||
foreach (var tractor in _setTrolleybus.GetTrolleybus())
|
||||
{
|
||||
data += $"{tractor.GetInfo()}{separatorData}";
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public void LoadData(string[] records)
|
||||
{
|
||||
foreach (var rec in records)
|
||||
{
|
||||
_setTrolleybus.Insert(DrawningObjectTrolleybus.Create(rec) as T);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||||
/// </summary>
|
||||
|
@ -1,15 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace Trolleybus
|
||||
{
|
||||
internal class MapsCollection
|
||||
{
|
||||
/// Словарь (хранилище) с картами
|
||||
readonly Dictionary<string, MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus,
|
||||
readonly Dictionary<string, MapWithSetTrolleybusGeneric<IDrawningObject,
|
||||
AbstractMap>> _mapStorages;
|
||||
/// Возвращение списка названий карт
|
||||
public List<string> Keys => _mapStorages.Keys.ToList();
|
||||
@ -17,13 +19,15 @@ namespace Trolleybus
|
||||
private readonly int _pictureWidth;
|
||||
/// Высота окна отрисовки
|
||||
private readonly int _pictureHeight;
|
||||
private readonly char separatorDict = '|';
|
||||
private readonly char separatorData = ';';
|
||||
/// Конструктор
|
||||
/// <param name="pictureWidth"></param>
|
||||
/// <param name="pictureHeight"></param>
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new Dictionary<string,
|
||||
MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap>>();
|
||||
MapWithSetTrolleybusGeneric<IDrawningObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -45,13 +49,67 @@ namespace Trolleybus
|
||||
/// Доступ к депо
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap> this[string ind]
|
||||
public MapWithSetTrolleybusGeneric<IDrawningObject, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
_mapStorages.TryGetValue(ind, out var mapWithSetTractorsGeneric);
|
||||
return mapWithSetTractorsGeneric;
|
||||
_mapStorages.TryGetValue(ind, out var mapWithSetTrolleybusGeneric);
|
||||
return mapWithSetTrolleybusGeneric;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 AutoStopMap();
|
||||
break;
|
||||
}
|
||||
_mapStorages.Add(elem[0], new MapWithSetTrolleybusGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, (char)StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,13 +68,13 @@ namespace Trolleybus
|
||||
Insert(value, position);
|
||||
}
|
||||
}
|
||||
public IEnumerable<T> GetTractors()
|
||||
public IEnumerable<T> GetTrolleybus()
|
||||
{
|
||||
foreach (var tractor in _places)
|
||||
foreach (var trolleybus in _places)
|
||||
{
|
||||
if (tractor != null)
|
||||
if (trolleybus != null)
|
||||
{
|
||||
yield return tractor;
|
||||
yield return trolleybus;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -55,6 +55,7 @@
|
||||
<Compile Include="DrawningObjectTrolleybus.cs" />
|
||||
<Compile Include="EntitySmallTrolleybus.cs" />
|
||||
<Compile Include="EntityTrolleybus.cs" />
|
||||
<Compile Include="ExtentionTrolleybus.cs" />
|
||||
<Compile Include="Form1.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
Loading…
x
Reference in New Issue
Block a user