From bacf66aad807af2672f216077f362f3997d6d742 Mon Sep 17 00:00:00 2001 From: ikswi Date: Sun, 12 May 2024 02:46:08 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D1=88=D0=B0=D0=B3=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D1=8B=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ListGenericObjects.cs | 20 ++++--- .../MassiveGenericObjects.cs | 60 +++++++++---------- .../Exceptions/CollectionOverflowException.cs | 20 +++++++ .../Exceptions/ObjectNotFoundException.cs | 20 +++++++ .../PositionOutOfCollectionException.cs | 20 +++++++ 5 files changed, 100 insertions(+), 40 deletions(-) create mode 100644 AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs create mode 100644 AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs create mode 100644 AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs diff --git a/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs index 64884c3..14c9a6d 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,6 @@ -namespace ProjectAirFighter.CollectionGenericObjects; +using ProjectSportCar.Exceptions; + +namespace ProjectAirFighter.CollectionGenericObjects; /// /// Параметризованный набор объектов @@ -46,31 +48,31 @@ public class ListGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position >= Count || position < 0) return null; + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); return _collection[position]; } public int Insert(T obj) { - if (Count + 1 > _maxCount) return -1; + if (Count + 1 > _maxCount) throw new CollectionOverflowException(); _collection.Add(obj); return Count; } public int Insert(T obj, int position) { - if (Count + 1 > _maxCount) return -1; - if (position < 0 || position > Count) return -1; + if (Count + 1 > _maxCount) throw new CollectionOverflowException(); + if (position < 0 || position > Count) throw new PositionOutOfCollectionException(); _collection.Insert(position, obj); - return 1; + return position; } public T? Remove(int position) { - if (position < 0 || position > Count) return null; - T? pos = _collection[position]; + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); + T temp = _collection[position]; _collection.RemoveAt(position); - return pos; + return temp; } public IEnumerable GetItems() diff --git a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs index ccda1cf..33df30e 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,6 @@ -namespace ProjectAirFighter.CollectionGenericObjects; +using ProjectSportCar.Exceptions; + +namespace ProjectAirFighter.CollectionGenericObjects; /// /// Параметризованный набор объектов @@ -48,66 +50,62 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position >= _collection.Length || position < 0) - { return null; } + if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(); + if (_collection[position] == null) throw new ObjectNotFoundException(); return _collection[position]; } public int Insert(T obj) { - int index = 0; - while (index < _collection.Length) + for (int i = 0; i < Count; i++) { - if (_collection[index] == null) + if (_collection[i] == null) { - _collection[index] = obj; - return index; + _collection[i] = obj; + return i; } - - index++; } - return -1; + throw new CollectionOverflowException(); } public int Insert(T obj, int position) { - if (position >= _collection.Length || position < 0) - { return -1; } - + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); if (_collection[position] == null) { _collection[position] = obj; return position; } - int index; - - for (index = position + 1; index < _collection.Length; ++index) + int temp = position + 1; + while (temp < Count) { - if (_collection[index] == null) + if (_collection[temp] == null) { - _collection[position] = obj; - return position; + _collection[temp] = obj; + return temp; } + ++temp; } - - for (index = position - 1; index >= 0; --index) + temp = position - 1; + while (temp >= 0) { - if (_collection[index] == null) + if (_collection[temp] == null) { - _collection[position] = obj; - return position; + _collection[temp] = obj; + return temp; } + --temp; } - return -1; + throw new CollectionOverflowException(); } public T? Remove(int position) { - if (position >= _collection.Length || position < 0) - { - return null; - } - T obj = _collection[position]; + if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); + + if (_collection[position] == null) throw new ObjectNotFoundException(position); + + T? obj = _collection[position]; _collection[position] = null; return obj; } diff --git a/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs b/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs new file mode 100644 index 0000000..e331bc7 --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace ProjectSportCar.Exceptions; + +/// +/// Класс, описывающий ошибку переполнения коллекции +/// +[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) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs b/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs new file mode 100644 index 0000000..4704965 --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace ProjectSportCar.Exceptions; + +/// +/// Класс, описывающий ошибку, что по указанной позиции нет элемента +/// +[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) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs b/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs new file mode 100644 index 0000000..ae01475 --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace ProjectSportCar.Exceptions; + +/// +/// Класс, описывающий ошибку выхода за границы коллекции +/// +[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) { } +} \ No newline at end of file -- 2.25.1 From 696f879bad40ebbff07c3c0c3175486c81c3e701 Mon Sep 17 00:00:00 2001 From: ikswi Date: Mon, 13 May 2024 17:08:23 +0400 Subject: [PATCH 2/4] =?UTF-8?q?7=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 11.txt | 3 - 111.txt | 2 + 12.txt | 3 - .../AbstractCompany.cs | 12 +++- .../CollectionGenericObjects/Angar.cs | 7 +- .../ListGenericObjects.cs | 18 ++--- .../MassiveGenericObjects.cs | 68 +++++++++++-------- .../StorageCollection.cs | 32 +++++---- .../Exceptions/CollectionOverflowException.cs | 4 +- .../Exceptions/ObjectNotFoundException.cs | 2 +- .../PositionOutOfCollectionException.cs | 2 +- .../FormMilitaryAircraftCollection.cs | 65 +++++++++++++----- AirFighter/AirFighter/Program.cs | 57 ++++++++++++---- .../AirFighter/ProjectAirFighter.csproj | 10 +++ AirFighter/AirFighter/serilogConfig.json | 20 ++++++ тренировочный.txt | 2 + 16 files changed, 213 insertions(+), 94 deletions(-) delete mode 100644 11.txt create mode 100644 111.txt delete mode 100644 12.txt create mode 100644 AirFighter/AirFighter/serilogConfig.json create mode 100644 тренировочный.txt diff --git a/11.txt b/11.txt deleted file mode 100644 index db43816..0000000 --- a/11.txt +++ /dev/null @@ -1,3 +0,0 @@ -CollectionsStorage -11|Massive|45|EntityMilitaryAircraft:100:100:Gray;EntityAirFighter:100:100:Gray:True:True:Gray;EntityAirFighter:100:100:Gray:True:True:Gray; -12|Massive|45|EntityAirFighter:100:100:BlueViolet:True:True:BlueViolet;EntityMilitaryAircraft:100:100:BlueViolet;EntityAirFighter:100:100:BlueViolet:True:True:BlueViolet; \ No newline at end of file diff --git a/111.txt b/111.txt new file mode 100644 index 0000000..0b76977 --- /dev/null +++ b/111.txt @@ -0,0 +1,2 @@ +CollectionsStorage +111|Massive|45|EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black; \ No newline at end of file diff --git a/12.txt b/12.txt deleted file mode 100644 index 80b13fc..0000000 --- a/12.txt +++ /dev/null @@ -1,3 +0,0 @@ -CollectionsStorage -11|Massive|45|EntityMilitaryAircraft:100:100:BlueViolet;EntityMilitaryAircraft:100:100:BlueViolet;EntityMilitaryAircraft:100:100:BlueViolet; -12|Massive|45|EntityMilitaryAircraft:100:100:Yellow;EntityMilitaryAircraft:100:100:Yellow;EntityMilitaryAircraft:100:100:Yellow; \ No newline at end of file diff --git a/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs b/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs index 1e6c381..5513f24 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs @@ -1,4 +1,5 @@ using ProjectAirFighter.Drawnings; +using ProjectAirFighter.Exceptions; namespace ProjectAirFighter.CollectionGenericObjects; @@ -95,9 +96,18 @@ public abstract class AbstractCompany SetObjectsPosition(); for (int i = 0; i < (_collection?.Count ?? 0); ++i) - { + { + try + { DrawningMilitaryAircraft? obj = _collection?.Get(i); obj?.DrawTransport(graphics); + } + catch (ObjectNotFoundException e) + { } + catch (PositionOutOfCollectionException e) + { } + catch (CollectionOverflowException e) + { } } return bitmap; diff --git a/AirFighter/AirFighter/CollectionGenericObjects/Angar.cs b/AirFighter/AirFighter/CollectionGenericObjects/Angar.cs index bd921bb..029f7b5 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/Angar.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/Angar.cs @@ -1,4 +1,5 @@ using ProjectAirFighter.Drawnings; +using ProjectAirFighter.Exceptions; namespace ProjectAirFighter.CollectionGenericObjects; @@ -42,11 +43,13 @@ public class Angar : AbstractCompany 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 * curWidth + 10, curHeight * _placeSizeHeight + 10); } + catch (ObjectNotFoundException) { } + catch (PositionOutOfCollectionException e) { } if (curWidth > 0) curWidth--; @@ -55,7 +58,7 @@ public class Angar : AbstractCompany curWidth = width - 1; curHeight--; } - if (curHeight > height) + if (curHeight >= height) { return; } diff --git a/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs index 14c9a6d..0e8e5fa 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,4 @@ -using ProjectSportCar.Exceptions; +using ProjectAirFighter.Exceptions; namespace ProjectAirFighter.CollectionGenericObjects; @@ -48,36 +48,36 @@ public class ListGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); return _collection[position]; } public int Insert(T obj) { - if (Count + 1 > _maxCount) throw new CollectionOverflowException(); + if (Count == _maxCount) throw new CollectionOverflowException(Count); _collection.Add(obj); return Count; } public int Insert(T obj, int position) { - if (Count + 1 > _maxCount) throw new CollectionOverflowException(); - if (position < 0 || position > Count) throw new PositionOutOfCollectionException(); + if (Count == _maxCount) throw new CollectionOverflowException(Count); + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); _collection.Insert(position, obj); return position; } public T? Remove(int position) { - if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); - T temp = _collection[position]; + if (position >= _collection.Count || position < 0) throw new PositionOutOfCollectionException(position); + T obj = _collection[position]; _collection.RemoveAt(position); - return temp; + return obj; } public IEnumerable GetItems() { - for (int i = 0; i < _collection.Count; ++i) + for (int i = 0; i < Count; ++i) { yield return _collection[i]; } diff --git a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs index 33df30e..59818b3 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,4 @@ -using ProjectSportCar.Exceptions; +using ProjectAirFighter.Exceptions; namespace ProjectAirFighter.CollectionGenericObjects; @@ -50,14 +50,14 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(); - if (_collection[position] == null) throw new ObjectNotFoundException(); + if (position < 0 || position >= Count - 3) throw new PositionOutOfCollectionException(position); + if (_collection[position] == null) throw new ObjectNotFoundException(position); return _collection[position]; } public int Insert(T obj) { - for (int i = 0; i < Count; i++) + for (int i = 0; i < Count - 3; i++) { if (_collection[i] == null) { @@ -65,47 +65,55 @@ public class MassiveGenericObjects : ICollectionGenericObjects return i; } } - throw new CollectionOverflowException(); + throw new CollectionOverflowException(Count); } public int Insert(T obj, int position) { - if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); - if (_collection[position] == null) + if (position < 0 || position >= Count - 3) throw new PositionOutOfCollectionException(position); + + if (_collection[position] != null) { - _collection[position] = obj; - return position; - } - int temp = position + 1; - while (temp < Count) - { - if (_collection[temp] == null) + bool pushed = false; + for (int index = position + 1; index < Count; index++) { - _collection[temp] = obj; - return temp; + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } } - ++temp; - } - temp = position - 1; - while (temp >= 0) - { - if (_collection[temp] == null) + + if (!pushed) { - _collection[temp] = obj; - return temp; + for (int index = position - 1; index >= 0; index--) + { + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } + } + } + + if (!pushed) + { + throw new CollectionOverflowException(Count); } - --temp; } - throw new CollectionOverflowException(); + + // вставка + _collection[position] = obj; + return position; } public T? Remove(int position) { - if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); - + if (position >= Count - 3 || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) throw new ObjectNotFoundException(position); - - T? obj = _collection[position]; + T obj = _collection[position]; _collection[position] = null; return obj; } diff --git a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs index f457172..3ed993b 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,5 @@ using ProjectAirFighter.Drawnings; +using ProjectAirFighter.Exceptions; using System.Text; namespace ProjectAirFighter.CollectionGenericObjects; @@ -39,7 +40,7 @@ public class StorageCollection /// public StorageCollection() { - _storages = new Dictionary>(); //вот тут какое-то свойство + _storages = new Dictionary>(); } /// @@ -94,11 +95,11 @@ public class StorageCollection /// Путь и имя файла /// true - сохранение прошло успешно, false - ошибка при сохранении данных - public bool SaveData(string filename) + public void SaveData(string filename) { if (_storages.Count == 0) { - return false; + throw new InvalidOperationException("В хранилище отсутствуют коллекции для сохранения"); } if (File.Exists(filename)) @@ -141,10 +142,7 @@ public class StorageCollection } writer.Write(sb); } - } - - return true; } /// @@ -152,11 +150,11 @@ public class StorageCollection /// /// Путь и имя файла /// true - загрузка прошла успешно, false - ошибка при загрузке данных - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new FileNotFoundException("Файл не существует"); } using (StreamReader fs = File.OpenText(filename)) { @@ -164,12 +162,12 @@ public class StorageCollection if (str == null || str.Length == 0) { - return false; + throw new FormatException("В файле неверные данные"); } if (!str.StartsWith(_collectionKey)) { - return false; + throw new FormatException("В файле неверные данные"); } _storages.Clear(); @@ -190,7 +188,7 @@ public class StorageCollection if (collection == null) { - return false; + throw new InvalidOperationException("Не удалось определить тип коллекции:" + record[1]); } collection.MaxCount = Convert.ToInt32(record[2]); @@ -201,15 +199,21 @@ public class StorageCollection { if (elem?.CreateDrawningMilitaryAircraft() is T militaryAircraft) { - if (collection.Insert(militaryAircraft) == -1) + try { - return false; + if (collection.Insert(militaryAircraft) == -1) + { + throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]); + } + } + catch (CollectionOverflowException ex) + { + throw new CollectionOverflowException("Коллекция переполнена", ex); } } } _storages.Add(record[0], collection); } - return true; } } diff --git a/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs b/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs index e331bc7..45735a2 100644 --- a/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs +++ b/AirFighter/AirFighter/Exceptions/CollectionOverflowException.cs @@ -1,12 +1,12 @@ using System.Runtime.Serialization; -namespace ProjectSportCar.Exceptions; +namespace ProjectAirFighter.Exceptions; /// /// Класс, описывающий ошибку переполнения коллекции /// [Serializable] -internal class CollectionOverflowException : ApplicationException +public class CollectionOverflowException : ApplicationException { public CollectionOverflowException(int count) : base("В коллекции превышено допустимое количество: " + count) { } diff --git a/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs b/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs index 4704965..eba0aae 100644 --- a/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs +++ b/AirFighter/AirFighter/Exceptions/ObjectNotFoundException.cs @@ -1,6 +1,6 @@ using System.Runtime.Serialization; -namespace ProjectSportCar.Exceptions; +namespace ProjectAirFighter.Exceptions; /// /// Класс, описывающий ошибку, что по указанной позиции нет элемента diff --git a/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs b/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs index ae01475..13db41a 100644 --- a/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs +++ b/AirFighter/AirFighter/Exceptions/PositionOutOfCollectionException.cs @@ -1,6 +1,6 @@ using System.Runtime.Serialization; -namespace ProjectSportCar.Exceptions; +namespace ProjectAirFighter.Exceptions; /// /// Класс, описывающий ошибку выхода за границы коллекции diff --git a/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs index a0dc7cd..094a01d 100644 --- a/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs +++ b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs @@ -1,5 +1,7 @@ -using ProjectAirFighter.CollectionGenericObjects; +using Microsoft.Extensions.Logging; +using ProjectAirFighter.CollectionGenericObjects; using ProjectAirFighter.Drawnings; +using ProjectAirFighter.Exceptions; namespace ProjectAirFighter; @@ -15,13 +17,19 @@ public partial class FormMilitaryAircraftCollection : Form /// private AbstractCompany? _company = null; + /// + /// Логер + /// + private readonly ILogger _logger; + /// /// Конструктор /// - public FormMilitaryAircraftCollection() + public FormMilitaryAircraftCollection(ILogger logger) { InitializeComponent(); _storageCollection = new(); + _logger = logger; } /// @@ -58,14 +66,18 @@ public partial class FormMilitaryAircraftCollection : Form return; } - if (_company + militaryAircraft != -1) + try { + var res = _company + militaryAircraft; MessageBox.Show("Объект добавлен"); + _logger.LogInformation($"Объект добавлен под индексом {res}"); pictureBox.Image = _company.Show(); } - else + catch (Exception ex) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show($"Объект не добавлен: {ex.Message}", "Результат", MessageBoxButtons.OK, + MessageBoxIcon.Error); + _logger.LogError($"Ошибка: {ex.Message}", ex.Message); } } @@ -88,14 +100,18 @@ public partial class FormMilitaryAircraftCollection : Form } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_company - pos != null) + try { + var res = _company - pos; MessageBox.Show("Объект удален"); + _logger.LogInformation($"Объект удален под индексом {pos}"); pictureBox.Image = _company.Show(); } - else + catch (Exception ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show(ex.Message, "Не удалось удалить объект", + MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError($"Ошибка: {ex.Message}", ex.Message); } } @@ -168,8 +184,17 @@ public partial class FormMilitaryAircraftCollection : Form collectionType = CollectionType.List; } - _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); - RerfreshListBoxItems(); + try + { + _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + _logger.LogInformation("Добавление коллекции"); + RerfreshListBoxItems(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError($"Ошибка: {ex.Message}", ex.Message); + } } private void buttonCollectionDel_Click(object sender, EventArgs e) @@ -184,6 +209,7 @@ public partial class FormMilitaryAircraftCollection : Form return; } _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + _logger.LogInformation("Коллекция удалена"); RerfreshListBoxItems(); } @@ -220,6 +246,7 @@ public partial class FormMilitaryAircraftCollection : Form { case "Хранилище": _company = new Angar(pictureBox.Width, pictureBox.Height, collection); + _logger.LogInformation("Компания создана"); break; } @@ -231,13 +258,16 @@ public partial class FormMilitaryAircraftCollection : Form { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storageCollection.SaveData(saveFileDialog.FileName)) + try { + _storageCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName); } - else + catch (Exception ex) { MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } } @@ -246,14 +276,17 @@ public partial class FormMilitaryAircraftCollection : Form { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storageCollection.LoadData(openFileDialog.FileName)) + try { - MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _storageCollection.LoadData(openFileDialog.FileName); RerfreshListBoxItems(); + MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName); } - else + catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Загрузка не выполнена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } } diff --git a/AirFighter/AirFighter/Program.cs b/AirFighter/AirFighter/Program.cs index 9d818ea..7ff1c04 100644 --- a/AirFighter/AirFighter/Program.cs +++ b/AirFighter/AirFighter/Program.cs @@ -1,17 +1,50 @@ -namespace ProjectAirFighter +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; + +namespace ProjectAirFighter; + +internal static class Program { - internal static class Program + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() { - /// - /// The main entry point for the application. - /// - [STAThread] - static void Main() + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + + var services = new ServiceCollection(); + ConfigureServices(services); + using ServiceProvider serviceProvider = services.BuildServiceProvider(); + Application.Run(serviceProvider.GetRequiredService ()); + + } + + /// + /// DI + /// + /// + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton().AddLogging(option => { - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. - ApplicationConfiguration.Initialize(); - Application.Run(new FormMilitaryAircraftCollection()); - } + string[] path = Directory.GetCurrentDirectory().Split('\\'); + string pathNeed = ""; + for (int i = 0; i < path.Length - 3; i++) + { + pathNeed += path[i] + "\\"; + } + + var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile(path: $"{pathNeed}serilogConfig.json", optional: false, reloadOnChange: true) + .Build(); + var logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger(); + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(logger); + }); } } \ No newline at end of file diff --git a/AirFighter/AirFighter/ProjectAirFighter.csproj b/AirFighter/AirFighter/ProjectAirFighter.csproj index 244387d..9067329 100644 --- a/AirFighter/AirFighter/ProjectAirFighter.csproj +++ b/AirFighter/AirFighter/ProjectAirFighter.csproj @@ -8,6 +8,16 @@ enable + + + + + + + + + + True diff --git a/AirFighter/AirFighter/serilogConfig.json b/AirFighter/AirFighter/serilogConfig.json new file mode 100644 index 0000000..d97e26c --- /dev/null +++ b/AirFighter/AirFighter/serilogConfig.json @@ -0,0 +1,20 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Information", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/log_.log", + "rollingInterval": "Day", + "outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}" + } + } + ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], + "Properties": { + "Application": "Stormtrooper" + } + } +} diff --git a/тренировочный.txt b/тренировочный.txt new file mode 100644 index 0000000..a8a4f70 --- /dev/null +++ b/тренировочный.txt @@ -0,0 +1,2 @@ +CollectionsStorage +11|Massive|45|EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black; \ No newline at end of file -- 2.25.1 From 7fd93c6c32b95ce69fba6c1bd5aa0520e44a6a7e Mon Sep 17 00:00:00 2001 From: ikswi Date: Thu, 16 May 2024 03:51:54 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirFighter/AirFighter/serilogConfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AirFighter/AirFighter/serilogConfig.json b/AirFighter/AirFighter/serilogConfig.json index d97e26c..4ca9b41 100644 --- a/AirFighter/AirFighter/serilogConfig.json +++ b/AirFighter/AirFighter/serilogConfig.json @@ -14,7 +14,7 @@ ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "Properties": { - "Application": "Stormtrooper" + "Application": "AirFighter" } } } -- 2.25.1 From 3ed0fd1ce0dd573fbd06e1a326b55f6c96d77f1f Mon Sep 17 00:00:00 2001 From: ikswi Date: Thu, 16 May 2024 09:19:52 +0400 Subject: [PATCH 4/4] =?UTF-8?q?7=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 111.txt | 2 -- AirFighter/AirFighter/FormMilitaryAircraftCollection.cs | 1 + тренировочный.txt | 2 -- 3 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 111.txt delete mode 100644 тренировочный.txt diff --git a/111.txt b/111.txt deleted file mode 100644 index 0b76977..0000000 --- a/111.txt +++ /dev/null @@ -1,2 +0,0 @@ -CollectionsStorage -111|Massive|45|EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black; \ No newline at end of file diff --git a/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs index 094a01d..92506fe 100644 --- a/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs +++ b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs @@ -30,6 +30,7 @@ public partial class FormMilitaryAircraftCollection : Form InitializeComponent(); _storageCollection = new(); _logger = logger; + _logger.LogInformation("Форма загрузилась"); } /// diff --git a/тренировочный.txt b/тренировочный.txt deleted file mode 100644 index a8a4f70..0000000 --- a/тренировочный.txt +++ /dev/null @@ -1,2 +0,0 @@ -CollectionsStorage -11|Massive|45|EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black;EntityMilitaryAircraft:100:100:Black; \ No newline at end of file -- 2.25.1