Лабораторная работа №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) public static int operator +(AbstractCompany company, DrawningBus bus)
{ {
if (company._collection.Insert(bus)) return company._collection.Insert(bus);
{
return 1;
}
return 0;
} }
public static DrawningBus operator -(AbstractCompany company, int position) public static DrawningBus operator -(AbstractCompany company, int position)

View File

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

View File

@ -40,26 +40,26 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position]; return _collection[position];
} }
public bool Insert(T obj) public int Insert(T obj)
{ {
if (Count == _maxCount) if (Count == _maxCount)
{ {
return false; return -1;
} }
_collection.Add(obj); _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) if (Count == _maxCount || position < 0 || position > Count)
{ {
return false; return -1;
} }
_collection.Insert(position, obj); _collection.Insert(position, obj);
return true; return 1;
} }
public T? Remove(int position) public T? Remove(int position)

View File

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

View File

@ -101,23 +101,23 @@ public class StorageCollection<T>
{ {
File.Delete(filename); File.Delete(filename);
} }
StringBuilder sb = new(); using FileStream fs = new(filename, FileMode.Create);
sb.Append(_collectionKey); using StreamWriter sw = new StreamWriter(fs);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in sw.Write(_collectionKey);
_storages) foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
sb.Append(Environment.NewLine); sw.Write(Environment.NewLine);
// не сохраняем пустые коллекции // не сохраняем пустые коллекции
if (value.Value.Count == 0) if (value.Value.Count == 0)
{ {
continue; continue;
} }
sb.Append(value.Key); sw.Write(value.Key);
sb.Append(_separatorForKeyValue); sw.Write(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType); sw.Write(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue); sw.Write(_separatorForKeyValue);
sb.Append(value.Value.MaxCount); sw.Write(value.Value.MaxCount);
sb.Append(_separatorForKeyValue); sw.Write(_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;
@ -125,81 +125,74 @@ public class StorageCollection<T>
{ {
continue; continue;
} }
sb.Append(data); sw.Write(data);
sb.Append(_separatorItems); 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; return true;
} }
public bool LoadData(string filename) 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; 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; return false;
} }
_storages.Clear(); _storages.Clear();
foreach (string data in strs)
while (!sr.EndOfStream)
{ {
string[] record = data.Split(_separatorForKeyValue, string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4) if (record.Length != 4)
{ {
continue; continue;
} }
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType); ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null) if (collection == null)
{ {
return false; return false;
} }
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?.CreateDrawningBus() is T bus) if (elem?.CreateDrawningBus() is T bus)
{ {
if (!collection.Insert(bus)) if (collection.Insert(bus) == -1)
{
return false; return false;
}
} }
} }
_storages.Add(record[0], collection); _storages.Add(record[0], collection);
} }
return true;
} }
/// <summary> return true;
/// Создание коллекции по типу }
/// </summary>
/// <param name="collectionType"></param> /// <summary>
/// <returns></returns> /// Создание коллекции по типу
private static ICollectionGenericObjects<T>? /// </summary>
/// <param name="collectionType"></param>
/// <returns></returns>
private static ICollectionGenericObjects<T>?
CreateCollection(CollectionType collectionType) CreateCollection(CollectionType collectionType)
{ {
return collectionType switch return collectionType switch

View File

@ -36,35 +36,38 @@ public class DrawningAccordionBus : DrawningBus
Brush brushBlue = new SolidBrush(Color.Blue); Brush brushBlue = new SolidBrush(Color.Blue);
// отсек // отсек
g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value, 50, 30); if (accordionBus.Compartment)
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)
{ {
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(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10); g.FillEllipse(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 25, 10, 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 + 65, _startPosY.Value + 25, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10); g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 5, 5, 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);
}
} }
} }
} }