исправления в лабе 6
This commit is contained in:
parent
65cbde716f
commit
3ee520eb15
@ -60,11 +60,10 @@ public abstract class AbstractCompany
|
||||
/// <param name="company">Компания</param>
|
||||
/// <param name="car">Добавляемый объект</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator +(AbstractCompany company, DrawningTrackedCar trackedCar)
|
||||
public static int operator +(AbstractCompany company, DrawningTrackedCar trackedCar)
|
||||
{
|
||||
return company._collection?.Insert(trackedCar) ?? false;
|
||||
return company._collection.Insert(trackedCar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перегрузка оператора удаления для класса
|
||||
/// </summary>
|
||||
|
@ -21,7 +21,7 @@ public interface ICollectionGenericObjects<T>
|
||||
/// </summary>
|
||||
/// <param name="obj">Добавляемый объект</param>
|
||||
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
|
||||
bool Insert(T obj);
|
||||
int Insert(T obj);
|
||||
|
||||
/// <summary>
|
||||
/// Добавление объекта в коллекцию на конкретную позицию
|
||||
|
@ -49,16 +49,13 @@ public class ListGenericObjects<T>: ICollectionGenericObjects<T>
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
public bool Insert(T obj)
|
||||
public int Insert(T obj)
|
||||
{
|
||||
// TODO проверка, что не превышено максимальное количество элементов
|
||||
// TODO вставка в конец набора
|
||||
if (Count != _maxCount)
|
||||
{
|
||||
if (Count == _maxCount) return -1;
|
||||
_collection.Add(obj);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return Count;
|
||||
}
|
||||
|
||||
public int Insert(T obj, int position)
|
||||
|
@ -53,17 +53,19 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
return _collection[position];
|
||||
}
|
||||
|
||||
public bool Insert(T obj)
|
||||
public int Insert(T obj)
|
||||
{
|
||||
for (int i = 0; i < _collection.Length; i++)
|
||||
int index = 0;
|
||||
while (index < _collection.Length)
|
||||
{
|
||||
if (_collection[i] == null)
|
||||
if (_collection[index] == null)
|
||||
{
|
||||
_collection[i] = obj;
|
||||
return true;
|
||||
_collection[index] = obj;
|
||||
return index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int Insert(T obj, int position)
|
||||
|
@ -89,6 +89,11 @@ public class StorageCollection<T>
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Сохранение информации по автомобилям в хранилище в файл
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
||||
/// <summary>
|
||||
/// Сохранение информации по автомобилям в хранилище в файл
|
||||
/// </summary>
|
||||
@ -100,31 +105,28 @@ public class StorageCollection<T>
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
|
||||
StringBuilder sb = new();
|
||||
|
||||
sb.Append(_collectionKey);
|
||||
using (StreamWriter writer = new StreamWriter(filename))
|
||||
{
|
||||
writer.Write(_collectionKey);
|
||||
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||
{
|
||||
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;
|
||||
@ -132,18 +134,60 @@ public class StorageCollection<T>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
sb.Append(data);
|
||||
sb.Append(_separatorItems);
|
||||
}
|
||||
writer.Write(sb);
|
||||
}
|
||||
|
||||
using FileStream fs = new(filename, FileMode.Create);
|
||||
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
|
||||
fs.Write(info, 0, info.Length);
|
||||
}
|
||||
//if (_storages.Count == 0)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
//if (File.Exists(filename))
|
||||
//{
|
||||
// File.Delete(filename);
|
||||
//}
|
||||
//StringBuilder sb = new();
|
||||
//sb.Append(_collectionKey);
|
||||
//foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||
//{
|
||||
// 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;
|
||||
// if (string.IsNullOrEmpty(data))
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
// sb.Append(data);
|
||||
// sb.Append(_separatorItems);
|
||||
// }
|
||||
//}
|
||||
//using FileStream fs = new(filename, FileMode.Create);
|
||||
//byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
|
||||
//fs.Write(info, 0, info.Length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Загрузка информации по автомобилям в хранилище из файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
||||
/// <summary>
|
||||
/// Загрузка информации по автомобилям в хранилище из файла
|
||||
/// </summary>
|
||||
@ -155,64 +199,103 @@ public class StorageCollection<T>
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string bufferTextFromFile = "";
|
||||
using (FileStream fs = new(filename, FileMode.Open))
|
||||
using (StreamReader fs = File.OpenText(filename))
|
||||
{
|
||||
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)
|
||||
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);
|
||||
//по идее этого произойти не должно
|
||||
//if (strs == null)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
string[] record = strs.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);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
if (elem?.CreateDrawningTrackedCar() is T car)
|
||||
if (elem?.CreateDrawningTrackedCar() is T TrackedCar)
|
||||
{
|
||||
if (!collection.Insert(car))
|
||||
if (collection.Insert(TrackedCar) == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_storages.Add(record[0], collection);
|
||||
}
|
||||
|
||||
return true;
|
||||
//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)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
//if (!strs[0].Equals(_collectionKey))
|
||||
//{
|
||||
// //если нет такой записи, то это не те данные
|
||||
// return false;
|
||||
//}
|
||||
//_storages.Clear();
|
||||
//foreach (string data in strs)
|
||||
//{
|
||||
// string[] record = data.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);
|
||||
// foreach (string elem in set)
|
||||
// {
|
||||
// if (elem?.CreateDrawningShip() is T ship)
|
||||
// {
|
||||
// if (collection.Insert(ship) == -1)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// _storages.Add(record[0], collection);
|
||||
//}
|
||||
//return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -231,3 +314,4 @@ public class StorageCollection<T>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -60,16 +60,14 @@ public partial class FormCarCollection : Form
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_company + Trackedcar)
|
||||
if (_company + Trackedcar != -1)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBox.Image = _company.Show();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("не удалось добавить объект");
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ public partial class FormCarConfig : Form
|
||||
/// <summary>
|
||||
/// Событие для передачи объекта
|
||||
/// </summary>
|
||||
private event TrackedCarDelegate? TrackedCarDelegate;
|
||||
private event Action<DrawningTrackedCar>? TrackedCarDelegate;
|
||||
/// <summary>
|
||||
/// конструктор
|
||||
/// </summary>
|
||||
@ -36,7 +36,7 @@ public partial class FormCarConfig : Form
|
||||
/// Привязка внешнего метода к событию
|
||||
/// </summary>
|
||||
/// <param name="TrackedcarDelegate"></param>
|
||||
public void AddEvent(TrackedCarDelegate TrackedcarDelegate)
|
||||
public void AddEvent(Action<DrawningTrackedCar> TrackedcarDelegate)
|
||||
{
|
||||
TrackedCarDelegate += TrackedcarDelegate;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user