diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Exceptions/BusNotFoundException.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Exceptions/BusNotFoundException.cs new file mode 100644 index 0000000..8ec2fc4 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Exceptions/BusNotFoundException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Exceptions +{ + [Serializable] + internal class BusNotFoundException : ApplicationException + { + public BusNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public BusNotFoundException() : base() { } + public BusNotFoundException(string message) : base(message) { } + public BusNotFoundException(string message, Exception exception) : base(message, exception) { } + protected BusNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Exceptions/StorageOverflowException.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Exceptions/StorageOverflowException.cs new file mode 100644 index 0000000..ec84ce6 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Exceptions/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Exceptions +{ + [Serializable] + internal class StorageOverflowException : ApplicationException + { + public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { } + public StorageOverflowException() : base() { } + public StorageOverflowException(string message) : base(message) { } + public StorageOverflowException(string message, Exception exception) : base(message, exception) { } + protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs index 35272b7..d3fc657 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs @@ -10,6 +10,10 @@ using System.Threading.Tasks; using System.Windows.Forms; using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.MovementStrategy; +using Microsoft.Extensions.Logging; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Exceptions; +using System.Xml.Linq; +using Serilog; namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base { @@ -70,6 +74,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); + Log.Information($"Добавлен набор: {textBoxStorageName.Text}"); } /// @@ -96,9 +101,10 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base if (MessageBox.Show($"Удалить объект{listBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(listBoxObjects.SelectedItem.ToString() - ?? string.Empty); + string name = (listBoxObjects.SelectedItem.ToString() ?? string.Empty); + _storage.DelSet(name); ReloadObjects(); + Log.Information($"Удален набор: {name}"); } } @@ -121,18 +127,20 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base FormBusConfig form = new(); form.Show(); - Action? busDelegate = new((m) => + Action? busDelegate = new((bus) => { - bool isAdditionSuccessful = (obj + m); - if (isAdditionSuccessful) + try { + bool isAdditionSuccessful = obj + bus; MessageBox.Show("Объект добавлен"); - m.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); + bus.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); pictureBoxCollection.Image = obj.ShowTheBuses(); + Log.Information($"Добавлен объект в коллекцию {listBoxObjects.SelectedItem.ToString() ?? string.Empty}"); } - else + catch (StorageOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); + Log.Warning($"Коллекция {listBoxObjects.SelectedItem.ToString() ?? string.Empty} переполнена"); + MessageBox.Show(ex.Message); } }); form.AddEvent(busDelegate); @@ -160,15 +168,23 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base { return; } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + try { + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + var isAdditionSuccessful = obj - pos; MessageBox.Show("Объект удален"); + Log.Information($"Удален объект из коллекции {listBoxObjects.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); pictureBoxCollection.Image = obj.ShowTheBuses(); } - else + catch (BusNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + Log.Warning($"Не получилось удалить объект из коллекции {listBoxObjects.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show(ex.Message); + } + catch (FormatException) + { + Log.Warning($"Было введено не число"); + MessageBox.Show("Введите число"); } } @@ -201,13 +217,16 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { + _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен"); } - else + catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось сохранить"); + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -221,17 +240,21 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { + _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Файл {openFileDialog.FileName} успешно загружен"); foreach (var collection in _storage.Keys) { listBoxObjects.Items.Add(collection); } + ReloadObjects(); } - else + catch (Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось загрузить"); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs index 36b2cc6..c8b77f0 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/SetGeneric.cs @@ -1,4 +1,5 @@ -using System; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Exceptions; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -43,15 +44,13 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// Добавляемый автобус /// - public bool Insert(T bus) + public void Insert(T bus) { if (_places.Count == _maxCount) { - return false; + throw new StorageOverflowException(_maxCount); } - Insert(bus, 0); - return true; } /// @@ -60,15 +59,17 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// Добавляемый автобус /// Позиция /// - public bool Insert(T bus, int position) + public void Insert(T bus, int position) { - if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + if (_places.Count == _maxCount) { - return false; + throw new StorageOverflowException(_maxCount); + } + if (!(position >= 0 && position <= Count)) + { + throw new Exception("Неверная позиция для вставки"); } - _places.Insert(position, bus); - return true; } /// @@ -76,15 +77,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// /// - public bool Remove(int position) + public void Remove(int position) { if (position < 0 || position >= Count) { - return false; + throw new BusNotFoundException(position); } _places.RemoveAt(position); - return true; } /// diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs index 35b7fbd..1588bf8 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs @@ -70,12 +70,13 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// public static bool operator +(TheBusesGenericCollection collect, T? obj) { - if (obj == null) + if (obj == null || collect == null) { return false; } - return collect?._collection.Insert(obj) ?? false; + collect?._collection.Insert(obj); + return true; } /// @@ -87,11 +88,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics public static T? operator -(TheBusesGenericCollection collect, int pos) { T? obj = collect._collection[pos]; - if (obj != null) - { - collect._collection.Remove(pos); - } - + collect._collection.Remove(pos); return obj; } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs index fdc04cc..5784951 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs @@ -64,7 +64,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// Путь и имя файла /// true - сохранение прошло успешно, false - ошибка при сохранении данных - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -83,7 +83,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics if (data.Length == 0) { - return false; + throw new IOException("Невалидная операция, нет данных для сохранения"); } string toWrite = $"BusStorage{Environment.NewLine}{data}"; var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); @@ -95,7 +95,6 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics sw.WriteLine(str); } } - return true; } /// @@ -103,11 +102,11 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// Путь и имя файла /// true - загрузка прошла успешно, false - ошибка при загрузке данных - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new IOException("Файл не найден"); } using (StreamReader sr = new(filename)) { @@ -115,11 +114,11 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); if (strs == null || strs.Length == 0) { - return false; + throw new IOException("Нет данных для загрузки"); } if (!strs[0].StartsWith("BusStorage")) { - return false; + throw new IOException("Неверный формат данных"); } _busStorages.Clear(); do @@ -140,7 +139,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics { if (!(collection + bus)) { - return false; + throw new IOException("Ошибка добавления в коллекцию"); } } } @@ -149,7 +148,6 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics str = sr.ReadLine(); } while (str != null); } - return true; } /// diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base.csproj b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base.csproj index 1e2d961..ff6ab76 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base.csproj +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base.csproj @@ -9,6 +9,18 @@ enable + + + + + + + + + + + + True diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs index 03f361e..01a8fb2 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Program.cs @@ -1,3 +1,11 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Events; +using Serilog.Formatting.Json; +using Serilog.Configuration; + namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base { internal static class Program @@ -8,9 +16,23 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base [STAThread] static void Main() { - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); + 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}appsettings.json", optional: false, reloadOnChange: true) + .Build(); + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + Application.SetHighDpiMode(HighDpiMode.SystemAware); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormBusCollection()); } } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/appsettings.json b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/appsettings.json new file mode 100644 index 0000000..21a6582 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { "path": "log.log" } + } + ], + "Properties": { + "Application": "Sample" + } + } +} \ No newline at end of file diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/nlog.config b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/nlog.config new file mode 100644 index 0000000..f46f823 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/nlog.config @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file