From 78c5d65929d0c21a554b0371a0cf9517bead0953 Mon Sep 17 00:00:00 2001 From: Alenka Date: Wed, 6 Dec 2023 19:29:04 +0400 Subject: [PATCH] Start --- Cruiser/Cruiser/Cruiser.csproj | 5 ++ Cruiser/Cruiser/CruiserNotFoundException.cs | 18 ++++ Cruiser/Cruiser/ExtentionDrawningCruiser.cs | 53 ++++++++++++ Cruiser/Cruiser/FormCruiserCollection.cs | 92 +++++++++++++++------ Cruiser/Cruiser/Program.cs | 25 +++++- Cruiser/Cruiser/StorageOverflowException.cs | 20 +++++ Cruiser/Cruiser/nlog.config | 14 ++++ 7 files changed, 201 insertions(+), 26 deletions(-) create mode 100644 Cruiser/Cruiser/CruiserNotFoundException.cs create mode 100644 Cruiser/Cruiser/ExtentionDrawningCruiser.cs create mode 100644 Cruiser/Cruiser/StorageOverflowException.cs create mode 100644 Cruiser/Cruiser/nlog.config diff --git a/Cruiser/Cruiser/Cruiser.csproj b/Cruiser/Cruiser/Cruiser.csproj index 13ee123..33fa7a5 100644 --- a/Cruiser/Cruiser/Cruiser.csproj +++ b/Cruiser/Cruiser/Cruiser.csproj @@ -8,6 +8,11 @@ enable + + + + + True diff --git a/Cruiser/Cruiser/CruiserNotFoundException.cs b/Cruiser/Cruiser/CruiserNotFoundException.cs new file mode 100644 index 0000000..044e968 --- /dev/null +++ b/Cruiser/Cruiser/CruiserNotFoundException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Cruiser +{ + internal class CruiserNotFoundException : ApplicationException + { + public CruiserNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public CruiserNotFoundException() : base() { } + public CruiserNotFoundException(string message) : base(message) { } + public CruiserNotFoundException(string message, Exception exception) : base(message, exception) { } + protected CruiserNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/Cruiser/Cruiser/ExtentionDrawningCruiser.cs b/Cruiser/Cruiser/ExtentionDrawningCruiser.cs new file mode 100644 index 0000000..1762e73 --- /dev/null +++ b/Cruiser/Cruiser/ExtentionDrawningCruiser.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Cruiser.DrawningObjects; +using Cruiser.Entities; + +namespace Cruiser +{ + public static class ExtentionDrawningCruiser + { + public static DrawningCruiser? CreateDrawningMonorail(this string info, char separatorForObject, + int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 5) + { + return new DrawningCruiser(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 9) + { + return new DrawningAdvancedCruiser(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5]), + width, height); + } + return null; + } + + public static string GetDataForSave(this DrawningCruiser drawningMonorail, char separatorForObject) + { + var monorail = drawningMonorail.EntityCruiser; + if (monorail == null) + { + return string.Empty; + } + var str = $"{monorail.Speed}{separatorForObject}{monorail.Weight}{separatorForObject}{monorail.BodyColor.Name}{separatorForObject}" + + $"{monorail.BodyColor.Name}{separatorForObject}"; + if (monorail is not EntityAdvancedCruiser locomotive) + { + return str; + } + return + $"{str}{separatorForObject}{locomotive.BodyColor}{separatorForObject}{locomotive.AdditionalColor.Name}" + + $"{separatorForObject}{locomotive.HelicopterPad}{separatorForObject}{locomotive.Coating}"; + } + + + } +} + diff --git a/Cruiser/Cruiser/FormCruiserCollection.cs b/Cruiser/Cruiser/FormCruiserCollection.cs index 8b2a637..051ca81 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.cs +++ b/Cruiser/Cruiser/FormCruiserCollection.cs @@ -9,6 +9,16 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Microsoft.VisualBasic.Logging; +using System.Diagnostics.Metrics; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Xml.Linq; +using Serilog; +using Log = Serilog.Log; namespace Cruiser { @@ -45,11 +55,13 @@ namespace Cruiser { if (string.IsNullOrEmpty(textBoxSet.Text)) { - MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _storage.AddSet(textBoxSet.Text); ReloadObjects(); + Log.Information($"Добавлен набор: {textBoxSet.Text}"); } private void listBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { @@ -61,13 +73,16 @@ namespace Cruiser { return; } - if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, - MessageBoxIcon.Question) == DialogResult.Yes) + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, +MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(listBoxStorages.SelectedItem.ToString() - ?? string.Empty); + string name = listBoxStorages.SelectedItem.ToString() + ?? string.Empty; + _storage.DelSet(name); ReloadObjects(); + Log.Information($"Удален набор: {name}"); } + } private void ButtonAddCruiser_Click(object sender, EventArgs e) { @@ -75,29 +90,33 @@ namespace Cruiser { return; } - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; if (obj == null) { return; } FormCruiserConfig form = new(); form.Show(); - Action? cruiserDelegate = new((m) => + Action? monorailDelegate = new((m) => { - bool isAdditionSuccessful = (obj + m); - if (isAdditionSuccessful) + try { + bool q = obj + m; MessageBox.Show("Объект добавлен"); + Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); m.ChangePictureBoxSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height); pictureBoxCruiser.Image = obj.ShowCruisers(); } - else + catch (StorageOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); + Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена"); + MessageBox.Show(ex.Message); } }); - form.AddEvent(cruiserDelegate); + form.AddEvent(monorailDelegate); } + private void ButtonRemoveCruiser_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) @@ -115,17 +134,27 @@ namespace Cruiser { return; } - int pos = Convert.ToInt32(textBoxCruiser.Text); - if (obj - pos != null) + try { + + int pos = Convert.ToInt32(textBoxCruiser.Text); + var q = obj - pos; MessageBox.Show("Объект удален"); + Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); pictureBoxCruiser.Image = obj.ShowCruisers(); } - else + catch (CruiserNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show(ex.Message); + } + catch (FormatException ex) + { + Log.Warning($"Было введено не число"); + MessageBox.Show("Введите число"); } } + private void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) @@ -144,32 +173,47 @@ namespace Cruiser { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { - MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _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); + } } + } + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { - MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _storage.LoadData(openFileDialog.FileName); + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Файл {openFileDialog.FileName} успешно загружен"); foreach (var collection in _storage.Keys) { listBoxStorages.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/Cruiser/Cruiser/Program.cs b/Cruiser/Cruiser/Program.cs index 2672862..e801b53 100644 --- a/Cruiser/Cruiser/Program.cs +++ b/Cruiser/Cruiser/Program.cs @@ -1,3 +1,7 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; namespace Cruiser { internal static class Program @@ -11,7 +15,24 @@ namespace Cruiser // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormCruiserCollection()); + ApplicationConfiguration.Initialize(); + var services = new ServiceCollection(); + ConfigureServices(services); + using (ServiceProvider serviceProvider = + services.BuildServiceProvider()) + { + + Application.Run(serviceProvider.GetRequiredService()); + } + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton() + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); } } -} \ No newline at end of file +} diff --git a/Cruiser/Cruiser/StorageOverflowException.cs b/Cruiser/Cruiser/StorageOverflowException.cs new file mode 100644 index 0000000..e602daa --- /dev/null +++ b/Cruiser/Cruiser/StorageOverflowException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Cruiser +{ + [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 context) : base(info, context) { } + } +} + diff --git a/Cruiser/Cruiser/nlog.config b/Cruiser/Cruiser/nlog.config new file mode 100644 index 0000000..6518387 --- /dev/null +++ b/Cruiser/Cruiser/nlog.config @@ -0,0 +1,14 @@ + + + + + + + + + + +