7 лабораторная работа
This commit is contained in:
parent
89b32379e6
commit
bf12e1a9bf
@ -1,4 +1,5 @@
|
|||||||
using ProjectSeaplane.Drawnings;
|
using ProjectSeaplane.Drawnings;
|
||||||
|
using ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
namespace ProjectSeaplane.CollectionGenericObjects;
|
namespace ProjectSeaplane.CollectionGenericObjects;
|
||||||
|
|
||||||
@ -104,8 +105,15 @@ public abstract class AbstractCompany
|
|||||||
SetObjectsPosition();
|
SetObjectsPosition();
|
||||||
for (int i = 0; i < (_collection?.Count ?? 0); ++i)
|
for (int i = 0; i < (_collection?.Count ?? 0); ++i)
|
||||||
{
|
{
|
||||||
DrawningPlane? obj = _collection?.Get(i);
|
try
|
||||||
obj?.DrawTransport(graphics);
|
{
|
||||||
|
DrawningPlane? obj = _collection?.Get(i);
|
||||||
|
obj?.DrawTransport(graphics);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace ProjectSeaplane.CollectionGenericObjects;
|
using ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.CollectionGenericObjects;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Параметризованный набор объектов
|
/// Параметризованный набор объектов
|
||||||
@ -42,11 +44,15 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
}
|
}
|
||||||
public T? Get(int position)
|
public T? Get(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position > _collection.Count)
|
try
|
||||||
{
|
{
|
||||||
return null;
|
if (_collection[position] == null) throw new ObjectNotFoundException(position);
|
||||||
|
return _collection[position];
|
||||||
|
}
|
||||||
|
catch (IndexOutOfRangeException)
|
||||||
|
{
|
||||||
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
return _collection[position];
|
|
||||||
}
|
}
|
||||||
public int Insert(T obj)
|
public int Insert(T obj)
|
||||||
{
|
{
|
||||||
@ -54,22 +60,25 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
}
|
}
|
||||||
public int Insert(T obj, int position)
|
public int Insert(T obj, int position)
|
||||||
{
|
{
|
||||||
if (_maxCount == _collection.Count || position < 0 || position > _collection.Count)
|
if (position > MaxCount) throw new CollectionOverflowException(position);
|
||||||
{
|
|
||||||
return -1;
|
if (obj == null) throw new ArgumentNullException(nameof(obj));
|
||||||
}
|
|
||||||
_collection.Insert(position, obj);
|
_collection.Insert(position, obj);
|
||||||
return _collection.Count;
|
return _collection.Count;
|
||||||
}
|
}
|
||||||
public T? Remove(int position)
|
public T? Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position > _collection.Count)
|
try
|
||||||
{
|
{
|
||||||
return null;
|
T obj = _collection[position];
|
||||||
|
_collection.RemoveAt(position);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
catch (IndexOutOfRangeException)
|
||||||
|
{
|
||||||
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
T obj = _collection[position];
|
|
||||||
_collection.RemoveAt(position);
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T?> GetItems()
|
public IEnumerable<T?> GetItems()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using ProjectSeaplane.Drawnings;
|
using ProjectSeaplane.Drawnings;
|
||||||
|
using ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
namespace ProjectSeaplane.CollectionGenericObjects;
|
namespace ProjectSeaplane.CollectionGenericObjects;
|
||||||
|
|
||||||
@ -51,13 +52,14 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
|
|
||||||
public T? Get(int position)
|
public T? Get(int position)
|
||||||
{
|
{
|
||||||
if (position >= 0 && position < Count)
|
try
|
||||||
{
|
{
|
||||||
|
if (_collection[position] == null) throw new ObjectNotFoundException(position);
|
||||||
return _collection[position];
|
return _collection[position];
|
||||||
}
|
}
|
||||||
else
|
catch (IndexOutOfRangeException)
|
||||||
{
|
{
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,23 +96,22 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
throw new CollectionOverflowException(Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? Remove(int position)
|
public T? Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _collection.Count())
|
try
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (_collection[position] != null)
|
|
||||||
{
|
{
|
||||||
T obj = _collection[position];
|
T obj = _collection[position];
|
||||||
|
if (obj == null) throw new ObjectNotFoundException(position);
|
||||||
_collection[position] = null;
|
_collection[position] = null;
|
||||||
return obj;
|
return obj;
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
catch (IndexOutOfRangeException)
|
||||||
|
{
|
||||||
|
throw new PositionOutOfCollectionException(position);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T?> GetItems()
|
public IEnumerable<T?> GetItems()
|
||||||
|
@ -24,6 +24,7 @@ public class PlaneSharingService : AbstractCompany
|
|||||||
protected override void DrawBackgound(Graphics g)
|
protected override void DrawBackgound(Graphics g)
|
||||||
{
|
{
|
||||||
Pen pen = new Pen(Color.Brown);
|
Pen pen = new Pen(Color.Brown);
|
||||||
|
int max_count = 0;
|
||||||
int x = 1, y = 0;
|
int x = 1, y = 0;
|
||||||
while (y + _placeSizeHeight <= _pictureHeight)
|
while (y + _placeSizeHeight <= _pictureHeight)
|
||||||
{
|
{
|
||||||
@ -31,6 +32,7 @@ public class PlaneSharingService : AbstractCompany
|
|||||||
while (x + _placeSizeWidth <= _pictureWidth)
|
while (x + _placeSizeWidth <= _pictureWidth)
|
||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
|
max_count++;
|
||||||
g.DrawLine(pen, x, y, x + _placeSizeWidth, y);
|
g.DrawLine(pen, x, y, x + _placeSizeWidth, y);
|
||||||
g.DrawLine(pen, x, y, x, y + _placeSizeHeight);
|
g.DrawLine(pen, x, y, x, y + _placeSizeHeight);
|
||||||
g.DrawLine(pen, x, y + _placeSizeHeight, x + _placeSizeWidth, y + _placeSizeHeight);
|
g.DrawLine(pen, x, y + _placeSizeHeight, x + _placeSizeWidth, y + _placeSizeHeight);
|
||||||
@ -43,6 +45,7 @@ public class PlaneSharingService : AbstractCompany
|
|||||||
y += _placeSizeHeight + 5;
|
y += _placeSizeHeight + 5;
|
||||||
countRow++;
|
countRow++;
|
||||||
}
|
}
|
||||||
|
_collection.MaxCount = max_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void SetObjectsPosition()
|
protected override void SetObjectsPosition()
|
||||||
@ -54,12 +57,19 @@ public class PlaneSharingService : AbstractCompany
|
|||||||
int row = countRow, col = 1;
|
int row = countRow, col = 1;
|
||||||
for (int i = 0; i < _collection?.Count; i++, col++)
|
for (int i = 0; i < _collection?.Count; i++, col++)
|
||||||
{
|
{
|
||||||
_collection?.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight);
|
try
|
||||||
_collection?.Get(i)?.SetPosition(locCoord[row * countInRow - col].Item1 + 5, locCoord[row * countInRow - col].Item2 + 5);
|
|
||||||
if (col == countInRow)
|
|
||||||
{
|
{
|
||||||
col = 0;
|
_collection?.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight);
|
||||||
row--;
|
_collection?.Get(i)?.SetPosition(locCoord[row * countInRow - col].Item1 + 5, locCoord[row * countInRow - col].Item2 + 5);
|
||||||
|
if (col == countInRow)
|
||||||
|
{
|
||||||
|
col = 0;
|
||||||
|
row--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using ProjectSeaplane.Drawnings;
|
using ProjectSeaplane.Drawnings;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
namespace ProjectSeaplane.CollectionGenericObjects;
|
namespace ProjectSeaplane.CollectionGenericObjects;
|
||||||
|
|
||||||
@ -105,8 +106,7 @@ public class StorageCollection<T>
|
|||||||
/// Сохранение информации по самолётам в хранилище в файл
|
/// Сохранение информации по самолётам в хранилище в файл
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">Путь и имя файла</param>
|
/// <param name="filename">Путь и имя файла</param>
|
||||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
public void SaveData(string filename)
|
||||||
public bool SaveData(string filename)
|
|
||||||
{
|
{
|
||||||
if (File.Exists(filename))
|
if (File.Exists(filename))
|
||||||
{
|
{
|
||||||
@ -115,7 +115,7 @@ public class StorageCollection<T>
|
|||||||
|
|
||||||
if (_storage.Count == 0)
|
if (_storage.Count == 0)
|
||||||
{
|
{
|
||||||
return false;
|
throw new NoCollectionException("В хранилище отсутствуют коллекции для сохранения");
|
||||||
}
|
}
|
||||||
|
|
||||||
using (StreamWriter writer = new StreamWriter(filename))
|
using (StreamWriter writer = new StreamWriter(filename))
|
||||||
@ -133,26 +133,31 @@ public class StorageCollection<T>
|
|||||||
writer.Write(data + _separatorItems);
|
writer.Write(data + _separatorItems);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.WriteLine();
|
|
||||||
}
|
}
|
||||||
|
writer.WriteLine();
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Загрузка информации по автомобилям в хранилище из файла
|
/// Загрузка информации по автомобилям в хранилище из файла
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">Путь и имя файла</param>
|
/// <param name="filename">Путь и имя файла</param>
|
||||||
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
public void LoadData(string filename)
|
||||||
public bool LoadData(string filename)
|
|
||||||
{
|
{
|
||||||
if (!File.Exists(filename)) return false;
|
if (!File.Exists(filename))
|
||||||
|
{
|
||||||
|
throw new FileNotFoundException("Файл не существует");
|
||||||
|
}
|
||||||
using (StreamReader reader = new StreamReader(filename))
|
using (StreamReader reader = new StreamReader(filename))
|
||||||
{
|
{
|
||||||
string line = reader.ReadLine();
|
string line = reader.ReadLine();
|
||||||
if (line == null || !line.Equals(_collectionKey))
|
if (line == null)
|
||||||
{
|
{
|
||||||
return false;
|
throw new FileIsEmptyException(filename);
|
||||||
|
}
|
||||||
|
if (!line.Equals(_collectionKey))
|
||||||
|
{
|
||||||
|
throw new FileHasWrongDataException(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
_storage.Clear();
|
_storage.Clear();
|
||||||
@ -169,7 +174,7 @@ public class StorageCollection<T>
|
|||||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
{
|
||||||
return false;
|
throw new NullCollectionException("Не удалось создать коллекцию");
|
||||||
}
|
}
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||||
|
|
||||||
@ -179,17 +184,21 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
if (elem?.CreateDrawningPlane() is T plane)
|
if (elem?.CreateDrawningPlane() is T plane)
|
||||||
{
|
{
|
||||||
if (collection.Insert(plane) == -1)
|
try
|
||||||
{
|
{
|
||||||
return false;
|
collection.Insert(plane);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new CollectionOverflowException("Коллекция переполнена", ex);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_storage.Add(record[0], collection);
|
_storage.Add(record[0], collection);
|
||||||
line = reader.ReadLine();
|
line = reader.ReadLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку переполнения коллекции
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class CollectionOverflowException : ApplicationException
|
||||||
|
{
|
||||||
|
public CollectionOverflowException(int count) : base("В коллекции превышено допустимое количество: " + count) { }
|
||||||
|
public CollectionOverflowException() : base() { }
|
||||||
|
public CollectionOverflowException(string message) : base(message) { }
|
||||||
|
public CollectionOverflowException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
protected CollectionOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий переполнение коллекции
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
|
||||||
|
internal class FileHasWrongDataException : ApplicationException
|
||||||
|
{
|
||||||
|
public FileHasWrongDataException() : base() { }
|
||||||
|
|
||||||
|
public FileHasWrongDataException(string message) : base("Файл имеет неверные данные: " + message) { }
|
||||||
|
|
||||||
|
public FileHasWrongDataException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
|
||||||
|
public FileHasWrongDataException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку, что по указанной позиции нет элемента
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class FileIsEmptyException : ApplicationException
|
||||||
|
{
|
||||||
|
|
||||||
|
public FileIsEmptyException() : base() { }
|
||||||
|
|
||||||
|
public FileIsEmptyException(string message) : base("Файл пустой: " + message) { }
|
||||||
|
|
||||||
|
public FileIsEmptyException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
|
||||||
|
public FileIsEmptyException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
internal class NoCollectionException : ApplicationException
|
||||||
|
{
|
||||||
|
public NoCollectionException() : base() { }
|
||||||
|
|
||||||
|
public NoCollectionException(string message) : base(message) { }
|
||||||
|
|
||||||
|
public NoCollectionException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
|
||||||
|
public NoCollectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
internal class NullCollectionException : ApplicationException
|
||||||
|
{
|
||||||
|
public NullCollectionException() : base() { }
|
||||||
|
|
||||||
|
public NullCollectionException(string message) : base(message) { }
|
||||||
|
|
||||||
|
public NullCollectionException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
|
||||||
|
public NullCollectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.VisualBasic.ApplicationServices;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку, что по указанной позиции нет элемента
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class ObjectNotFoundException : ApplicationException
|
||||||
|
{
|
||||||
|
public ObjectNotFoundException(int i) : base("Не найден объект по позиции " + i) { }
|
||||||
|
public ObjectNotFoundException() : base() { }
|
||||||
|
public ObjectNotFoundException(string message) : base(message) { }
|
||||||
|
public ObjectNotFoundException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
protected ObjectNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectSeaplane.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку выхода за границы коллекции
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class PositionOutOfCollectionException : ApplicationException
|
||||||
|
{
|
||||||
|
public PositionOutOfCollectionException(int i) : base("Выход за границы коллекции. Позиция " + i) { }
|
||||||
|
public PositionOutOfCollectionException() : base() { }
|
||||||
|
public PositionOutOfCollectionException(string message) : base(message) { }
|
||||||
|
public PositionOutOfCollectionException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
protected PositionOutOfCollectionException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
using ProjectSeaplane.CollectionGenericObjects;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using ProjectSeaplane.CollectionGenericObjects;
|
||||||
using ProjectSeaplane.Drawnings;
|
using ProjectSeaplane.Drawnings;
|
||||||
|
|
||||||
namespace ProjectSeaplane;
|
namespace ProjectSeaplane;
|
||||||
@ -18,13 +20,19 @@ public partial class FormPlaneCollection : Form
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private AbstractCompany? _company = null;
|
private AbstractCompany? _company = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Логер
|
||||||
|
/// </summary>
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public FormPlaneCollection()
|
public FormPlaneCollection(ILogger<FormPlaneCollection> logger)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_storageCollection = new();
|
_storageCollection = new();
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -53,21 +61,25 @@ public partial class FormPlaneCollection : Form
|
|||||||
/// Добавление самолёта в коллекцию
|
/// Добавление самолёта в коллекцию
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="plane"></param>
|
/// <param name="plane"></param>
|
||||||
private void SetPlane(DrawningPlane? plane)
|
private void SetPlane(DrawningPlane plane)
|
||||||
{
|
{
|
||||||
if (_company == null || plane == null)
|
if (_company == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try
|
||||||
if ((_company + plane) != -1)
|
|
||||||
{
|
{
|
||||||
MessageBox.Show("Объект добавлен");
|
if ((_company + plane) != -1)
|
||||||
pictureBox.Image = _company.Show();
|
{
|
||||||
|
MessageBox.Show("Объект добавлен");
|
||||||
|
_logger.LogInformation("Добавлен объект: {entity}", plane.GetDataForSave());
|
||||||
|
pictureBox.Image = _company.Show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось добавить объект");
|
MessageBox.Show("Объект не был добавлен");
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,14 +101,17 @@ public partial class FormPlaneCollection : Form
|
|||||||
}
|
}
|
||||||
|
|
||||||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||||||
if ((_company - pos) != null)
|
try
|
||||||
{
|
{
|
||||||
|
DrawningPlane plane = _company - pos;
|
||||||
|
_logger.LogInformation("Объект по позиции {pos} удаден", pos);
|
||||||
MessageBox.Show("Объект удален");
|
MessageBox.Show("Объект удален");
|
||||||
pictureBox.Image = _company.Show();
|
pictureBox.Image = _company.Show();
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось удалить объект");
|
MessageBox.Show("Не удалось удалить объект");
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -174,6 +189,8 @@ public partial class FormPlaneCollection : Form
|
|||||||
}
|
}
|
||||||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
|
|
||||||
|
_logger.LogInformation("Добавлена коллекция: {CollectionName} типа: {Type}", textBoxCollectionName.Text, collectionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -192,6 +209,7 @@ public partial class FormPlaneCollection : Form
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
_logger.LogInformation("Коллекция успешно удалена: {collectionName}", listBoxCollection.SelectedIndex.ToString());
|
||||||
_storageCollection.DelCollection(listBoxCollection.SelectedItem?.ToString() ?? string.Empty);
|
_storageCollection.DelCollection(listBoxCollection.SelectedItem?.ToString() ?? string.Empty);
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
@ -230,6 +248,7 @@ public partial class FormPlaneCollection : Form
|
|||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Коллекция не проинициализирована");
|
MessageBox.Show("Коллекция не проинициализирована");
|
||||||
|
_logger.LogInformation("Коллекция не проиннициализирована");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,8 +256,10 @@ public partial class FormPlaneCollection : Form
|
|||||||
{
|
{
|
||||||
case "Хранилище":
|
case "Хранилище":
|
||||||
_company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, collection);
|
_company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, collection);
|
||||||
|
_logger.LogInformation("Создана компания типа плейншейринг, коллекция: {CollectionName}", listBoxCollection.SelectedItem);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
_logger.LogInformation("Создана компания на коллекции : {CollectionName}", listBoxCollection.SelectedItem);
|
||||||
panelCompanyTools.Enabled = true;
|
panelCompanyTools.Enabled = true;
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
@ -252,13 +273,16 @@ public partial class FormPlaneCollection : Form
|
|||||||
{
|
{
|
||||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.SaveData(saveFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
||||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -272,15 +296,18 @@ public partial class FormPlaneCollection : Form
|
|||||||
{
|
{
|
||||||
if (loadFileDialog.ShowDialog() == DialogResult.OK)
|
if (loadFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.LoadData(loadFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
_storageCollection.LoadData(loadFileDialog.FileName);
|
||||||
RefreshListBoxItems();
|
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Загрузка прошла успешно из файла, {filename}", loadFileDialog.FileName);
|
||||||
}
|
}
|
||||||
else
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogError("Ошибка {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,11 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
using Serilog.Events;
|
||||||
|
using Serilog.Sinks.File;
|
||||||
|
using Serilog.Configuration;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
namespace ProjectSeaplane
|
namespace ProjectSeaplane
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
@ -11,7 +19,31 @@ namespace ProjectSeaplane
|
|||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
Application.Run(new FormPlaneCollection());
|
|
||||||
|
ServiceCollection services = new();
|
||||||
|
ConfigureServices(services);
|
||||||
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||||
|
Application.Run(serviceProvider.GetRequiredService<FormPlaneCollection>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Êîíôèãóðàöèÿ ñåðâèñà DI
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
private static void ConfigureServices(ServiceCollection services)
|
||||||
|
{
|
||||||
|
var configuration = new ConfigurationBuilder()
|
||||||
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
|
.AddJsonFile("Settings.json")
|
||||||
|
.Build();
|
||||||
|
services.AddSingleton<FormPlaneCollection>()
|
||||||
|
.AddLogging(builder =>
|
||||||
|
{
|
||||||
|
builder.AddSerilog(new LoggerConfiguration()
|
||||||
|
.ReadFrom.Configuration(configuration)
|
||||||
|
.CreateLogger());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,6 +8,18 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||||
|
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.11" />
|
||||||
|
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||||
|
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Update="Properties\Resources.Designer.cs">
|
<Compile Update="Properties\Resources.Designer.cs">
|
||||||
<DesignTime>True</DesignTime>
|
<DesignTime>True</DesignTime>
|
||||||
@ -23,4 +35,10 @@
|
|||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="nlog.config">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
16
ProjectSeaplane/ProjectSeaplane/Settings.json
Normal file
16
ProjectSeaplane/ProjectSeaplane/Settings.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/planeLog.log",
|
||||||
|
"rollingInterval": "Day",
|
||||||
|
"outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
15
ProjectSeaplane/ProjectSeaplane/nlog.config
Normal file
15
ProjectSeaplane/ProjectSeaplane/nlog.config
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<configuration>
|
||||||
|
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
autoReload="true" internalLogLevel="Info">
|
||||||
|
|
||||||
|
<targets>
|
||||||
|
<target xsi:type="File" name="tofile" fileName="carlog-${shortdate}.log" />
|
||||||
|
</targets>
|
||||||
|
|
||||||
|
<rules>
|
||||||
|
<logger name="*" minlevel="Debug" writeTo="tofile" />
|
||||||
|
</rules>
|
||||||
|
</nlog>
|
||||||
|
</configuration>
|
Loading…
Reference in New Issue
Block a user