PIBD-14 Boyko_M.S, LabWork06 Simple #7
@ -1,90 +1,80 @@
|
|||||||
|
namespace ProjectElectroTrans.CollectionGenericObjects;
|
||||||
namespace ProjectElectroTrans.CollectionGenericObjects;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Параметризованный набор объектов
|
/// Параметризованный набор объектов
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
|
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
|
||||||
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Список объектов, которые храним
|
/// Список объектов, которые храним
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly List<T?> _collection;
|
private readonly List<T?> _collection;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Максимально допустимое число объектов в списке
|
/// Максимально допустимое число объектов в списке
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int _maxCount;
|
private int _maxCount;
|
||||||
|
|
||||||
public int Count => _collection.Count;
|
public int Count => _collection.Count;
|
||||||
|
|
||||||
public int MaxCount
|
public int MaxCount
|
||||||
{
|
{
|
||||||
get => _maxCount;
|
get { return Count; }
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (value > 0)
|
if (value > 0)
|
||||||
{
|
{
|
||||||
_maxCount = value;
|
_maxCount = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CollectionType GetCollectionType => CollectionType.List;
|
||||||
|
|
||||||
public CollectionType GetCollectionType => throw new NotImplementedException();
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
public ListGenericObjects()
|
||||||
|
{
|
||||||
|
_collection = new();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
public T Get(int position)
|
||||||
/// Конструктор
|
{
|
||||||
/// </summary>
|
if (position >= Count || position < 0) return null;
|
||||||
public ListGenericObjects()
|
return _collection[position];
|
||||||
{
|
}
|
||||||
_collection = new();
|
|
||||||
}
|
|
||||||
|
|
||||||
public T? Get(int position)
|
public int Insert(T obj)
|
||||||
{
|
{
|
||||||
|
if (Count == _maxCount) return -1;
|
||||||
|
_collection.Add(obj);
|
||||||
|
return Count;
|
||||||
|
}
|
||||||
|
|
||||||
if (position < 0 || position > _collection.Count - 1)
|
public int Insert(T obj, int position)
|
||||||
{
|
{
|
||||||
return null;
|
if (Count == _maxCount) return -1;
|
||||||
}
|
if (position >= Count || position < 0) return -1;
|
||||||
return _collection[position];
|
_collection.Insert(position, obj);
|
||||||
}
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
public int Insert(T obj)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (_collection.Count + 1 > _maxCount) { return 0; }
|
if (position >= _collection.Count || position < 0) return null;
|
||||||
_collection.Add(obj);
|
T obj = _collection[position];
|
||||||
|
_collection.RemoveAt(position);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
public IEnumerable<T?> GetItems()
|
||||||
}
|
{
|
||||||
|
for (int i = 0; i < Count; ++i)
|
||||||
public int Insert(T obj, int position)
|
{
|
||||||
{
|
yield return _collection[i];
|
||||||
if (_collection.Count + 1 < _maxCount) { return 0; }
|
}
|
||||||
if (position > _collection.Count || position < 0)
|
}
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
_collection.Insert(position, obj);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T Remove(int position)
|
|
||||||
{
|
|
||||||
if (position > _collection.Count || position < 0)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
T temp = _collection[position];
|
|
||||||
_collection.RemoveAt(position);
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<T?> GetItems()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System.Text;
|
using ProjectElectroTrans.Drawnings;
|
||||||
using ProjectElectroTrans.Drawnings;
|
using System.Text;
|
||||||
|
using ProjectElectroTrans.CollectionGenericObjects;
|
||||||
|
|
||||||
namespace ProjectElectroTrans.CollectionGenericObjects;
|
namespace ProjectElectroTrans.CollectionGenericObjects;
|
||||||
|
|
||||||
@ -50,23 +51,12 @@ public class StorageCollection<T>
|
|||||||
/// <param name="collectionType">тип коллекции</param>
|
/// <param name="collectionType">тип коллекции</param>
|
||||||
public void AddCollection(string name, CollectionType collectionType)
|
public void AddCollection(string name, CollectionType collectionType)
|
||||||
{
|
{
|
||||||
if (name == null || _storages.ContainsKey(name))
|
if (_storages.ContainsKey(name)) return;
|
||||||
{
|
if (collectionType == CollectionType.None) return;
|
||||||
return;
|
else if (collectionType == CollectionType.Massive)
|
||||||
}
|
_storages[name] = new MassiveGenericObjects<T>();
|
||||||
|
else if (collectionType == CollectionType.List)
|
||||||
switch (collectionType)
|
_storages[name] = new ListGenericObjects<T>();
|
||||||
|
|
||||||
{
|
|
||||||
case CollectionType.None:
|
|
||||||
return;
|
|
||||||
case CollectionType.Massive:
|
|
||||||
_storages.Add(name, new MassiveGenericObjects<T> { });
|
|
||||||
return;
|
|
||||||
case CollectionType.List:
|
|
||||||
_storages.Add(name, new ListGenericObjects<T> { });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -75,12 +65,8 @@ public class StorageCollection<T>
|
|||||||
/// <param name="name">Название коллекции</param>
|
/// <param name="name">Название коллекции</param>
|
||||||
public void DelCollection(string name)
|
public void DelCollection(string name)
|
||||||
{
|
{
|
||||||
if (name == null || !_storages.ContainsKey(name))
|
if (_storages.ContainsKey(name))
|
||||||
{
|
_storages.Remove(name);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_storages.Remove(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -92,12 +78,9 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (name == null || !_storages.ContainsKey(name))
|
if (_storages.ContainsKey(name))
|
||||||
{
|
return _storages[name];
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
return _storages[name];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,18 +101,25 @@ public class StorageCollection<T>
|
|||||||
File.Delete(filename);
|
File.Delete(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
using (StreamWriter sw = new StreamWriter(filename))
|
using (StreamWriter writer = new StreamWriter(filename))
|
||||||
{
|
{
|
||||||
|
writer.Write(_collectionKey);
|
||||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||||
{
|
{
|
||||||
sw.WriteLine(_collectionKey);
|
StringBuilder sb = new();
|
||||||
sw.WriteLine(value.Key);
|
sb.Append(Environment.NewLine);
|
||||||
|
|||||||
sw.Write(_separatorForKeyValue);
|
// не сохраняем пустые коллекции
|
||||||
sw.WriteLine(value.Value.GetCollectionType);
|
if (value.Value.Count == 0)
|
||||||
sw.Write(_separatorForKeyValue);
|
{
|
||||||
sw.WriteLine(value.Value.MaxCount);
|
continue;
|
||||||
sw.Write(_separatorForKeyValue);
|
}
|
||||||
|
|
||||||
|
sb.Append(value.Key);
|
||||||
|
sb.Append(_separatorForKeyValue);
|
||||||
|
sb.Append(value.Value.GetCollectionType);
|
||||||
|
sb.Append(_separatorForKeyValue);
|
||||||
|
sb.Append(value.Value.MaxCount);
|
||||||
|
sb.Append(_separatorForKeyValue);
|
||||||
foreach (T? item in value.Value.GetItems())
|
foreach (T? item in value.Value.GetItems())
|
||||||
{
|
{
|
||||||
string data = item?.GetDataForSave() ?? string.Empty;
|
string data = item?.GetDataForSave() ?? string.Empty;
|
||||||
@ -138,8 +128,11 @@ public class StorageCollection<T>
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
sw.WriteLine(data);
|
sb.Append(data);
|
||||||
|
sb.Append(_separatorItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writer.Write(sb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,30 +151,24 @@ public class StorageCollection<T>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
using (StreamReader sr = new StreamReader(filename))
|
using (StreamReader fs = File.OpenText(filename))
|
||||||
{
|
{
|
||||||
string bufferTextFromFile = "";
|
string str = fs.ReadLine();
|
||||||
while (sr.Peek() >= 0)
|
if (str == null || str.Length == 0)
|
||||||
{
|
|
||||||
bufferTextFromFile += sr.ReadLine() + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
if (strs == null || strs.Length == 0)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strs[0].Equals(_collectionKey))
|
if (!str.StartsWith(_collectionKey))
|
||||||
{
|
{
|
||||||
//если нет такой записи, то это не те данные
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_storages.Clear();
|
_storages.Clear();
|
||||||
foreach (string data in strs)
|
string strs = "";
|
||||||
|
while ((strs = fs.ReadLine()) != null)
|
||||||
{
|
{
|
||||||
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (record.Length != 4)
|
if (record.Length != 4)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@ -195,13 +182,12 @@ public class StorageCollection<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||||
|
|
||||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||||
foreach (string elem in set)
|
foreach (string elem in set)
|
||||||
{
|
{
|
||||||
if (elem?.CreateDrawningTrans() is T car)
|
if (elem?.CreateDrawningTrans() is T ship)
|
||||||
{
|
{
|
||||||
if (collection.Insert(car) != 1)
|
if (collection.Insert(ship) == -1)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
using ProjectElectroTrans.Entities;
|
using ProjectElectroTrans.Drawnings;
|
||||||
|
using ProjectElectroTrans.Entities;
|
||||||
|
|
||||||
namespace ProjectElectroTrans.Drawnings;
|
namespace ProjectElectroTrans;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Расширение для класса EntityCar
|
|
||||||
/// </summary>
|
|
||||||
public static class ExtentionDrawningTrans
|
public static class ExtentionDrawningTrans
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -20,16 +18,16 @@ public static class ExtentionDrawningTrans
|
|||||||
public static DrawingTrans? CreateDrawningTrans(this string info)
|
public static DrawingTrans? CreateDrawningTrans(this string info)
|
||||||
{
|
{
|
||||||
string[] strs = info.Split(_separatorForObject);
|
string[] strs = info.Split(_separatorForObject);
|
||||||
EntityTrans? car = EntityElectroTrans.CreateEntityElectroTrans(strs);
|
EntityTrans? airplan = EntityElectroTrans.CreateEntityElectroTrans(strs);
|
||||||
if (car != null)
|
if (airplan != null)
|
||||||
{
|
{
|
||||||
return new DrawingElectroTrans(car);
|
return new DrawingElectroTrans((EntityElectroTrans)airplan);
|
||||||
}
|
}
|
||||||
|
|
||||||
car = EntityTrans.CreateEntityTrans(strs);
|
airplan = EntityTrans.CreateEntityTrans(strs);
|
||||||
if (car != null)
|
if (airplan != null)
|
||||||
{
|
{
|
||||||
return new DrawingTrans(car);
|
return new DrawingTrans(airplan);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@ -38,11 +36,11 @@ public static class ExtentionDrawningTrans
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получение данных для сохранения в файл
|
/// Получение данных для сохранения в файл
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="drawningCar">Сохраняемый объект</param>
|
/// <param name="DrawingTrans">Сохраняемый объект</param>
|
||||||
/// <returns>Строка с данными по объекту</returns>
|
/// <returns>Строка с данными по объекту</returns>
|
||||||
public static string GetDataForSave(this DrawingTrans drawningCar)
|
public static string GetDataForSave(this DrawingTrans DrawingTrans)
|
||||||
{
|
{
|
||||||
string[]? array = drawningCar?.EntityTrans?.GetStringRepresentation();
|
string[]? array = DrawingTrans?.EntityTrans?.GetStringRepresentation();
|
||||||
|
|
||||||
if (array == null)
|
if (array == null)
|
||||||
{
|
{
|
||||||
@ -51,4 +49,5 @@ public static class ExtentionDrawningTrans
|
|||||||
|
|
||||||
return string.Join(_separatorForObject, array);
|
return string.Join(_separatorForObject, array);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -69,7 +69,7 @@ internal class EntityElectroTrans : EntityTrans
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static EntityTrans? CreateEntityElectroTrans(string[] strs)
|
public static EntityTrans? CreateEntityElectroTrans(string[] strs)
|
||||||
{
|
{
|
||||||
if (strs.Length != 7 || strs[0] != nameof(EntityTrans))
|
if (strs.Length != 7 || strs[0] != nameof(EntityElectroTrans))
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user
Записывать в файл можно сразу, без использования StringBuilder