Правки

This commit is contained in:
Glliza 2024-06-13 11:46:29 +04:00
parent 438b1653bc
commit 7624745b9d
6 changed files with 97 additions and 69 deletions

View File

@ -41,8 +41,8 @@ public abstract class AbstractCompany
/// <summary>
/// Вычисление максимального количества элементов, который можно разместить в окне
/// </summary>
private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
//private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
private int GetMaxCount => 36;
/// <summary>
/// Конструктор
/// </summary>
@ -98,8 +98,12 @@ public abstract class AbstractCompany
SetObjectsPosition();
for (int i = 0; i < (_collection?.Count ?? 0); ++i)
{
DrawningBus? obj = _collection?.Get(i);
obj?.DrawTransport(graphics);
try
{
DrawningBus? obj = _collection?.Get(i);
obj?.DrawTransport(graphics);
}
catch (Exception) { }
}
return bitmap;
}

View File

@ -9,19 +9,19 @@ namespace ProjectAirbus.CollectionGenericObjects;
public class BusSharingService : AbstractCompany
{
public BusSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningBus> collection) : base(picWidth, picHeight, collection)
public BusSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningBus> collection) : base(picWidth, picHeight, collection)
{
}
protected override void DrawBackgound(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
Pen pen = new(Color.Black, 2);
for (int i = 0; i < width; i++)
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height + 1; j++)
for (int j = 0; j < height + 1; j++)
{
//if (j + 1 > height)
@ -36,23 +36,24 @@ public class BusSharingService : AbstractCompany
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int BusWidth = width - 1;
int BusHeight = height - 1;
int BusWidth = 0;
int BusHeight = 0;
for (int i = 0; i < (_collection?.Count ?? 0); i++)
{
if (_collection?.Get(i) != null)
try
{
_collection.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i)?.SetPosition(_placeSizeWidth * BusWidth + 20, BusHeight * _placeSizeHeight + 20);
}
catch (Exception) { }
if (BusWidth > 0)
BusWidth--;
if (BusWidth < width - 1)
BusWidth++;
else
{
BusWidth = width - 1;
BusHeight--;
BusWidth = 0;
BusHeight++;
}
if (BusHeight > height)
{

View File

@ -56,6 +56,10 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
{
throw new PositionOutOfCollectionException(position);
}
if (_collection[position] == null)
{
throw new ObjectNotFoundException(position);
}
return _collection[position];
}

View File

@ -99,6 +99,7 @@ public class StorageCollection<T>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
public void SaveData(string filename)
{
if (_storages.Count == 0)
{
throw new Exception("В хранилище отсутствуют коллекции для сохранения");
@ -150,63 +151,67 @@ public class StorageCollection<T>
{
throw new Exception("Файл не существует");
}
using (StreamReader fs = File.OpenText(filename))
using StreamReader rd = new StreamReader(filename);
UTF8Encoding temp = new(true);
string str = rd.ReadLine();
if (str == null)
{
string str = fs.ReadLine();
if (str == null || str.Length == 0)
throw new Exception("В файле нет данных");
}
if (!str.StartsWith(_collectionKey))
{
throw new Exception("В файле неверные данные");
}
_storages.Clear();
string strs = "";
while ((strs = rd.ReadLine()) != null)
{
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{
throw new Exception("В файле нет данных");
continue;
}
if (!str.StartsWith(_collectionKey))
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{
throw new Exception("В файле неверные данные");
throw new Exception("Не удалось создать коллекции");
}
_storages.Clear();
string strs = "";
while ((strs = fs.ReadLine()) != null)
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
if (elem?.CreateDrawningBus() is T bus)
{
continue;
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{
throw new Exception("Не удалось определить тип коллекции:" + record[1]);
}
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
if (elem?.CreateDrawningBus() is T bus)
try
{
try
if (collection.Insert(bus) < 0)
{
if (collection.Insert(bus) == -1)
{
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
}
}
catch (CollectionOverflowException ex)
{
throw new Exception("Коллекция переполнена", ex);
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
}
}
catch (CollectionOverflowException ex)
{
throw new Exception("Коллекция переполнена", ex);
}
}
_storages.Add(record[0], collection);
}
_storages.Add(record[0], collection);
}
}
/// <summary>
/// Создание коллекции по типу
/// </summary>
/// <param name="collectionType"></param>
/// <returns></returns>
private static ICollectionGenericObjects<T>?
CreateCollection(CollectionType collectionType)
/// <summary>
/// Создание коллекции по типу
/// </summary>
/// <param name="collectionType"></param>
/// <returns></returns>
private static ICollectionGenericObjects<T>?
CreateCollection(CollectionType collectionType)
{
return collectionType switch
{

View File

@ -38,6 +38,7 @@ namespace ProjectAirbus
InitializeComponent();
_storageCollection = new();
_logger = logger;
_logger.LogInformation("Форма загрузилась");
}
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
@ -54,18 +55,24 @@ namespace ProjectAirbus
private void SetBus(DrawningBus? bus)
{
if (_company == null || bus == null)
try
{
return;
if (_company == null || bus == null)
{
return;
}
if (_company + bus >= 0)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
_logger.LogInformation("Объект добавлен");
}
}
if (_company + bus >= 0)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
}
else
catch (Exception ex)
{
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning($"Ошибка: {ex.Message}");
}
}
/// <summary>
@ -85,14 +92,19 @@ namespace ProjectAirbus
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_company - pos != null)
try
{
MessageBox.Show("Объект удален");
pictureBox.Image = _company.Show();
if (_company - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _company.Show();
_logger.LogInformation("Объект удален");
}
}
else
catch (Exception ex)
{
MessageBox.Show("Не удалось удалить объект");
_logger.LogError("Ошибка: {Message}", ex.Message);
}
}
@ -165,6 +177,7 @@ namespace ProjectAirbus
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
_logger.LogInformation("Коллекция добавлена " + textBoxCollectionName.Text);
RerfreshListBoxItems();
}
@ -206,6 +219,7 @@ namespace ProjectAirbus
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
_logger.LogInformation("Коллекция с название: " + listBoxCollection.SelectedItem.ToString() + " удалена");
RerfreshListBoxItems();
}

View File

@ -5,7 +5,7 @@
autoReload="true" internalLogLevel="Info">
<targets>
<target xsi:type="File" name="tofile" fileName="carlog-${shortdate}.log" />
<target xsi:type="File" name="tofile" fileName="buslog-${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="tofile" />