diff --git a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/AbstractCompany.cs b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/AbstractCompany.cs index 2aca4f1..f236836 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/AbstractCompany.cs @@ -33,11 +33,7 @@ public abstract class AbstractCompany public static int operator +(AbstractCompany company, DrawningBus bus) { - if (company._collection.Insert(bus)) - { - return 1; - } - return 0; + return company._collection.Insert(bus); } public static DrawningBus operator -(AbstractCompany company, int position) diff --git a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ICollectionGenericObjects.cs index 387ab6d..927d76b 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -29,14 +29,14 @@ public interface ICollectionGenericObjects /// /// /// - bool Insert(T obj); + int Insert(T obj); /// /// Добавление объекта в коллекцию /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj, int position); + int Insert(T obj, int position); /// /// Удаление объекта из коллекции с конкретной позиции diff --git a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ListGenericObjects.cs b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ListGenericObjects.cs index 34b7dc6..087e88b 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/ListGenericObjects.cs @@ -40,26 +40,26 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public bool Insert(T obj) + public int Insert(T obj) { if (Count == _maxCount) { - return false; + return -1; } _collection.Add(obj); - return true; + return Count; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { if (Count == _maxCount || position < 0 || position > Count) { - return false; + return -1; } _collection.Insert(position, obj); - return true; + return 1; } public T? Remove(int position) diff --git a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/MassiveGenericObjects.cs index ed6d261..c94d1cb 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -62,7 +62,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public bool Insert(T obj) + public int Insert(T obj) { int index = 0; while (index < _collection.Length) @@ -70,18 +70,18 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[index] = obj; - return true; + return 1; } index++; } - return false; + return -1; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { if (position >= _collection.Length || position < 0) - return false; + return -1; if (_collection[position] != null) @@ -99,7 +99,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects // Если пустого элемента нет, то выходим if (nullIndex < 0) { - return false; + return -1; } // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента int j = nullIndex - 1; @@ -110,7 +110,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } _collection[position] = obj; - return true; + return 1; } public T? Remove(int position) diff --git a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs index 6580b17..f8a3f22 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs @@ -101,23 +101,23 @@ public class StorageCollection { File.Delete(filename); } - StringBuilder sb = new(); - sb.Append(_collectionKey); - foreach (KeyValuePair> value in - _storages) + using FileStream fs = new(filename, FileMode.Create); + using StreamWriter sw = new StreamWriter(fs); + sw.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) { - sb.Append(Environment.NewLine); + sw.Write(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); + sw.Write(value.Key); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.GetCollectionType); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.MaxCount); + sw.Write(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) { string data = item?.GetDataForSave() ?? string.Empty; @@ -125,81 +125,74 @@ public class StorageCollection { continue; } - sb.Append(data); - sb.Append(_separatorItems); + sw.Write(data); + sw.Write(_separatorItems); } } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); - fs.Write(info, 0, info.Length); return true; } public bool LoadData(string filename) + { + if (!File.Exists(filename)) { - if (!File.Exists(filename)) + return false; + } + + using (FileStream fs = new(filename, FileMode.Open)) + { + using StreamReader sr = new StreamReader(fs); + + string str = sr.ReadLine(); + if (str == null || str.Length == 0) { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + + if (!str.Equals(_collectionKey)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) - { - bufferTextFromFile += temp.GetString(b); - } - } - string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - if (!strs[0].Equals(_collectionKey)) - { - //если нет такой записи, то это не те данные return false; } _storages.Clear(); - foreach (string data in strs) + + while (!sr.EndOfStream) { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); + string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 4) { continue; } + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); if (collection == null) { return false; } + 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) { if (elem?.CreateDrawningBus() is T bus) { - if (!collection.Insert(bus)) - { + if (collection.Insert(bus) == -1) return false; - } } } _storages.Add(record[0], collection); } - return true; } - /// - /// Создание коллекции по типу - /// - /// - /// - private static ICollectionGenericObjects? + return true; + } + + /// + /// Создание коллекции по типу + /// + /// + /// + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) { return collectionType switch diff --git a/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningAccordionBus.cs b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningAccordionBus.cs index b08d394..078d863 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningAccordionBus.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningAccordionBus.cs @@ -36,35 +36,38 @@ public class DrawningAccordionBus : DrawningBus Brush brushBlue = new SolidBrush(Color.Blue); // отсек - g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value, 50, 30); - g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20); - - g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value, 50, 30); - g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20); - - // колеса - g.FillEllipse(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10); - - g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10); - - if (accordionBus.Entrance) + if (accordionBus.Compartment) { - g.FillRectangle(additionalBrush, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20); + g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value, 50, 30); + g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20); - g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20); - } + g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value, 50, 30); + g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20); - if (accordionBus.Windows) - { - g.FillEllipse(brushBlue, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10); - g.FillEllipse(brushBlue, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10); - g.FillEllipse(brushBlue, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10); + // колеса + g.FillEllipse(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10); - g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10); - g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10); + g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10); + + if (accordionBus.Entrance) + { + g.FillRectangle(additionalBrush, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20); + + g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20); + } + + if (accordionBus.Windows) + { + g.FillEllipse(brushBlue, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10); + g.FillEllipse(brushBlue, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10); + g.FillEllipse(brushBlue, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10); + + g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10); + g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10); + g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10); + } } } } \ No newline at end of file