lab6-ready

This commit is contained in:
Kankistodor 2024-04-21 13:18:45 +04:00
parent bca0b7ff6e
commit 4cab399d4b
4 changed files with 118 additions and 143 deletions

View File

@ -1,90 +1,80 @@

namespace ProjectElectroTrans.CollectionGenericObjects;
namespace ProjectElectroTrans.CollectionGenericObjects;
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
where T : class
{
/// <summary>
/// Список объектов, которые храним
/// </summary>
private readonly List<T?> _collection;
/// <summary>
/// Список объектов, которые храним
/// </summary>
private readonly List<T?> _collection;
/// <summary>
/// Максимально допустимое число объектов в списке
/// </summary>
private int _maxCount;
/// <summary>
/// Максимально допустимое число объектов в списке
/// </summary>
private int _maxCount;
public int Count => _collection.Count;
public int Count => _collection.Count;
public int MaxCount
{
get => _maxCount;
set
{
if (value > 0)
{
_maxCount = value;
}
}
}
public int MaxCount
{
get { return Count; }
set
{
if (value > 0)
{
_maxCount = value;
}
}
}
public CollectionType GetCollectionType => CollectionType.List;
public CollectionType GetCollectionType => throw new NotImplementedException();
/// <summary>
/// Конструктор
/// </summary>
public ListGenericObjects()
{
_collection = new();
}
/// <summary>
/// Конструктор
/// </summary>
public ListGenericObjects()
{
_collection = new();
}
public T Get(int position)
{
if (position >= Count || position < 0) return null;
return _collection[position];
}
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)
{
return null;
}
return _collection[position];
}
public int Insert(T obj, int position)
{
if (Count == _maxCount) return -1;
if (position >= Count || position < 0) return -1;
_collection.Insert(position, obj);
return position;
}
public int Insert(T obj)
{
if (_collection.Count + 1 > _maxCount) { return 0; }
_collection.Add(obj);
public T Remove(int position)
{
if (position >= _collection.Count || position < 0) return null;
T obj = _collection[position];
_collection.RemoveAt(position);
return obj;
}
return 1;
}
public int Insert(T obj, int position)
{
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();
}
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < Count; ++i)
{
yield return _collection[i];
}
}
}

View File

@ -1,5 +1,6 @@
using System.Text;
using ProjectElectroTrans.Drawnings;
using ProjectElectroTrans.Drawnings;
using System.Text;
using ProjectElectroTrans.CollectionGenericObjects;
namespace ProjectElectroTrans.CollectionGenericObjects;
@ -50,23 +51,12 @@ public class StorageCollection<T>
/// <param name="collectionType">тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType)
{
if (name == null || _storages.ContainsKey(name))
{
return;
}
switch (collectionType)
{
case CollectionType.None:
return;
case CollectionType.Massive:
_storages.Add(name, new MassiveGenericObjects<T> { });
return;
case CollectionType.List:
_storages.Add(name, new ListGenericObjects<T> { });
return;
}
if (_storages.ContainsKey(name)) return;
if (collectionType == CollectionType.None) return;
else if (collectionType == CollectionType.Massive)
_storages[name] = new MassiveGenericObjects<T>();
else if (collectionType == CollectionType.List)
_storages[name] = new ListGenericObjects<T>();
}
/// <summary>
@ -75,12 +65,8 @@ public class StorageCollection<T>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
if (name == null || !_storages.ContainsKey(name))
{
return;
}
_storages.Remove(name);
if (_storages.ContainsKey(name))
_storages.Remove(name);
}
/// <summary>
@ -92,12 +78,9 @@ public class StorageCollection<T>
{
get
{
if (name == null || !_storages.ContainsKey(name))
{
return null;
}
return _storages[name];
if (_storages.ContainsKey(name))
return _storages[name];
return null;
}
}
@ -118,18 +101,25 @@ public class StorageCollection<T>
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)
{
sw.WriteLine(_collectionKey);
sw.WriteLine(value.Key);
sw.Write(_separatorForKeyValue);
sw.WriteLine(value.Value.GetCollectionType);
sw.Write(_separatorForKeyValue);
sw.WriteLine(value.Value.MaxCount);
sw.Write(_separatorForKeyValue);
StringBuilder sb = new();
sb.Append(Environment.NewLine);
// не сохраняем пустые коллекции
if (value.Value.Count == 0)
{
continue;
}
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())
{
string data = item?.GetDataForSave() ?? string.Empty;
@ -138,8 +128,11 @@ public class StorageCollection<T>
continue;
}
sw.WriteLine(data);
sb.Append(data);
sb.Append(_separatorItems);
}
writer.Write(sb);
}
}
@ -158,30 +151,24 @@ public class StorageCollection<T>
return false;
}
using (StreamReader sr = new StreamReader(filename))
using (StreamReader fs = File.OpenText(filename))
{
string bufferTextFromFile = "";
while (sr.Peek() >= 0)
{
bufferTextFromFile += sr.ReadLine() + "\n";
}
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
string str = fs.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!strs[0].Equals(_collectionKey))
if (!str.StartsWith(_collectionKey))
{
//если нет такой записи, то это не те данные
return false;
}
_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)
{
continue;
@ -195,13 +182,12 @@ public class StorageCollection<T>
}
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
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;
}

View File

@ -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
{
/// <summary>
@ -20,16 +18,16 @@ public static class ExtentionDrawningTrans
public static DrawingTrans? CreateDrawningTrans(this string info)
{
string[] strs = info.Split(_separatorForObject);
EntityTrans? car = EntityElectroTrans.CreateEntityElectroTrans(strs);
if (car != null)
EntityTrans? airplan = EntityElectroTrans.CreateEntityElectroTrans(strs);
if (airplan != null)
{
return new DrawingElectroTrans(car);
return new DrawingElectroTrans((EntityElectroTrans)airplan);
}
car = EntityTrans.CreateEntityTrans(strs);
if (car != null)
airplan = EntityTrans.CreateEntityTrans(strs);
if (airplan != null)
{
return new DrawingTrans(car);
return new DrawingTrans(airplan);
}
return null;
@ -38,11 +36,11 @@ public static class ExtentionDrawningTrans
/// <summary>
/// Получение данных для сохранения в файл
/// </summary>
/// <param name="drawningCar">Сохраняемый объект</param>
/// <param name="DrawingTrans">Сохраняемый объект</param>
/// <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)
{
@ -51,4 +49,5 @@ public static class ExtentionDrawningTrans
return string.Join(_separatorForObject, array);
}
}

View File

@ -69,7 +69,7 @@ internal class EntityElectroTrans : EntityTrans
/// <returns></returns>
public static EntityTrans? CreateEntityElectroTrans(string[] strs)
{
if (strs.Length != 7 || strs[0] != nameof(EntityTrans))
if (strs.Length != 7 || strs[0] != nameof(EntityElectroTrans))
{
return null;
}