PIBD-14 Boyko_M.S, LabWork06 Simple #7

Closed
LivelyPuer wants to merge 2 commits from lab6-develop into lab5-develop
4 changed files with 118 additions and 143 deletions
Showing only changes of commit 4cab399d4b - Show all commits

View File

@ -1,5 +1,4 @@
 namespace ProjectElectroTrans.CollectionGenericObjects;
namespace ProjectElectroTrans.CollectionGenericObjects;
/// <summary> /// <summary>
/// Параметризованный набор объектов /// Параметризованный набор объектов
@ -22,7 +21,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
public int MaxCount public int MaxCount
{ {
get => _maxCount; get { return Count; }
set set
{ {
if (value > 0) if (value > 0)
@ -32,8 +31,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
} }
} }
public CollectionType GetCollectionType => CollectionType.List;
public CollectionType GetCollectionType => throw new NotImplementedException();
/// <summary> /// <summary>
/// Конструктор /// Конструктор
@ -43,48 +41,40 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
_collection = new(); _collection = new();
} }
public T? Get(int position) public T Get(int position)
{ {
if (position >= Count || position < 0) return null;
if (position < 0 || position > _collection.Count - 1)
{
return null;
}
return _collection[position]; return _collection[position];
} }
public int Insert(T obj) public int Insert(T obj)
{ {
if (_collection.Count + 1 > _maxCount) { return 0; } if (Count == _maxCount) return -1;
_collection.Add(obj); _collection.Add(obj);
return Count;
return 1;
} }
public int Insert(T obj, int position) public int Insert(T obj, int position)
{ {
if (_collection.Count + 1 < _maxCount) { return 0; } if (Count == _maxCount) return -1;
if (position > _collection.Count || position < 0) if (position >= Count || position < 0) return -1;
{
return 0;
}
_collection.Insert(position, obj); _collection.Insert(position, obj);
return 1; return position;
} }
public T Remove(int position) public T Remove(int position)
{ {
if (position > _collection.Count || position < 0) if (position >= _collection.Count || position < 0) return null;
{ T obj = _collection[position];
return null;
}
T temp = _collection[position];
_collection.RemoveAt(position); _collection.RemoveAt(position);
return temp; return obj;
} }
public IEnumerable<T?> GetItems() public IEnumerable<T?> GetItems()
{ {
throw new NotImplementedException(); 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; 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,11 +65,7 @@ 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))
{
return;
}
_storages.Remove(name); _storages.Remove(name);
} }
@ -92,12 +78,9 @@ public class StorageCollection<T>
{ {
get get
{ {
if (name == null || !_storages.ContainsKey(name)) if (_storages.ContainsKey(name))
{
return null;
}
return _storages[name]; return _storages[name];
return null;
} }
} }
@ -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);
Review

Записывать в файл можно сразу, без использования StringBuilder

Записывать в файл можно сразу, без использования StringBuilder
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;
} }

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 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);
} }
} }

View File

@ -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;
} }