Лабораторная работа №6

This commit is contained in:
mar-va 2024-04-18 15:18:31 +04:00
parent 21741e196c
commit 2365620cf6
6 changed files with 87 additions and 95 deletions

View File

@ -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)

View File

@ -29,14 +29,14 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
bool Insert(T obj);
int Insert(T obj);
/// <summary>
/// Добавление объекта в коллекцию
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj, int position);
int Insert(T obj, int position);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции

View File

@ -40,26 +40,26 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
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)

View File

@ -62,7 +62,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
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<T> : ICollectionGenericObjects<T>
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<T> : ICollectionGenericObjects<T>
// Если пустого элемента нет, то выходим
if (nullIndex < 0)
{
return false;
return -1;
}
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
int j = nullIndex - 1;
@ -110,7 +110,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
}
_collection[position] = obj;
return true;
return 1;
}
public T? Remove(int position)

View File

@ -101,23 +101,23 @@ public class StorageCollection<T>
{
File.Delete(filename);
}
StringBuilder sb = new();
sb.Append(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in
_storages)
using FileStream fs = new(filename, FileMode.Create);
using StreamWriter sw = new StreamWriter(fs);
sw.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> 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,13 +125,10 @@ public class StorageCollection<T>
{
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;
}
@ -141,59 +138,55 @@ public class StorageCollection<T>
{
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
{
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)
using StreamReader sr = new StreamReader(fs);
string str = sr.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!strs[0].Equals(_collectionKey))
if (!str.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<T>? collection = StorageCollection<T>.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;
}
/// <summary>
/// Создание коллекции по типу
/// </summary>

View File

@ -36,6 +36,8 @@ public class DrawningAccordionBus : DrawningBus
Brush brushBlue = new SolidBrush(Color.Blue);
// отсек
if (accordionBus.Compartment)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value, 50, 30);
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20);
@ -67,4 +69,5 @@ public class DrawningAccordionBus : DrawningBus
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10);
}
}
}
}