From 2468f3950047b4f20ff8db8bf88fb2efa6785a14 Mon Sep 17 00:00:00 2001 From: dyakonovr <dyakonov.rr@bk.ru> Date: Sat, 2 Dec 2023 17:11:04 +0400 Subject: [PATCH] All done --- .../Exceptions/StorageOverlowException.cs | 15 ++++ .../Exceptions/TankNotFoundException.cs | 16 +++++ ProjectTank/FormTankCollection.Designer.cs | 62 ++++++++-------- ProjectTank/FormTankCollection.cs | 72 +++++++++++++------ ProjectTank/FormTankConfig.Designer.cs | 4 +- ProjectTank/Generics/SetGeneric.cs | 18 +++-- .../Generics/TanksGenericCollection.cs | 9 +-- ProjectTank/Generics/TanksGenericStorage.cs | 14 ++-- ProjectTank/Program.cs | 25 ++++++- ProjectTank/ProjectTank.csproj | 7 ++ 10 files changed, 166 insertions(+), 76 deletions(-) create mode 100644 ProjectTank/Exceptions/StorageOverlowException.cs create mode 100644 ProjectTank/Exceptions/TankNotFoundException.cs diff --git a/ProjectTank/Exceptions/StorageOverlowException.cs b/ProjectTank/Exceptions/StorageOverlowException.cs new file mode 100644 index 0000000..7d13725 --- /dev/null +++ b/ProjectTank/Exceptions/StorageOverlowException.cs @@ -0,0 +1,15 @@ +using System.Runtime.Serialization; +namespace ProjectTank.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) { } + } +} diff --git a/ProjectTank/Exceptions/TankNotFoundException.cs b/ProjectTank/Exceptions/TankNotFoundException.cs new file mode 100644 index 0000000..4b1e9c7 --- /dev/null +++ b/ProjectTank/Exceptions/TankNotFoundException.cs @@ -0,0 +1,16 @@ +using System.Runtime.Serialization; + +namespace ProjectTank.Exceptions +{ + [Serializable] + internal class TankNotFoundException : ApplicationException + { + public TankNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public TankNotFoundException() : base() { } + public TankNotFoundException(string message) : base(message) { } + public TankNotFoundException(string message, Exception exception) : + base(message, exception) { } + protected TankNotFoundException(SerializationInfo info, + StreamingContext contex) : base(info, contex) { } + } +} diff --git a/ProjectTank/FormTankCollection.Designer.cs b/ProjectTank/FormTankCollection.Designer.cs index 50bf659..00062aa 100644 --- a/ProjectTank/FormTankCollection.Designer.cs +++ b/ProjectTank/FormTankCollection.Designer.cs @@ -33,12 +33,12 @@ deleteTankButton = new Button(); addTankButton = new Button(); maskedTextBoxNumber = new TextBox(); - objectsLabel = new Label(); + label2 = new Label(); textBoxStorageName = new TextBox(); addObjectButton = new Button(); listBoxStorages = new ListBox(); deleteObjectButton = new Button(); - toolsGroupBox = new GroupBox(); + groupBox1 = new GroupBox(); menuStrip = new MenuStrip(); fileToolStripMenuItem = new ToolStripMenuItem(); loadToolStripMenuItem = new ToolStripMenuItem(); @@ -46,7 +46,7 @@ openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); - toolsGroupBox.SuspendLayout(); + groupBox1.SuspendLayout(); menuStrip.SuspendLayout(); SuspendLayout(); // @@ -95,14 +95,14 @@ maskedTextBoxNumber.Size = new Size(139, 27); maskedTextBoxNumber.TabIndex = 7; // - // objectsLabel + // label2 // - objectsLabel.AutoSize = true; - objectsLabel.Location = new Point(6, 30); - objectsLabel.Name = "objectsLabel"; - objectsLabel.Size = new Size(66, 20); - objectsLabel.TabIndex = 8; - objectsLabel.Text = "Наборы"; + label2.AutoSize = true; + label2.Location = new Point(6, 30); + label2.Name = "label2"; + label2.Size = new Size(66, 20); + label2.TabIndex = 8; + label2.Text = "Наборы"; // // textBoxStorageName // @@ -141,23 +141,23 @@ deleteObjectButton.UseVisualStyleBackColor = true; deleteObjectButton.Click += ButtonDelObject_Click; // - // toolsGroupBox + // groupBox1 // - toolsGroupBox.Controls.Add(objectsLabel); - toolsGroupBox.Controls.Add(refreshObjectsButton); - toolsGroupBox.Controls.Add(deleteTankButton); - toolsGroupBox.Controls.Add(maskedTextBoxNumber); - toolsGroupBox.Controls.Add(deleteObjectButton); - toolsGroupBox.Controls.Add(addTankButton); - toolsGroupBox.Controls.Add(textBoxStorageName); - toolsGroupBox.Controls.Add(listBoxStorages); - toolsGroupBox.Controls.Add(addObjectButton); - toolsGroupBox.Location = new Point(775, 32); - toolsGroupBox.Name = "toolsGroupBox"; - toolsGroupBox.Size = new Size(154, 472); - toolsGroupBox.TabIndex = 13; - toolsGroupBox.TabStop = false; - toolsGroupBox.Text = "Инструменты"; + groupBox1.Controls.Add(label2); + groupBox1.Controls.Add(refreshObjectsButton); + groupBox1.Controls.Add(deleteTankButton); + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(deleteObjectButton); + groupBox1.Controls.Add(addTankButton); + groupBox1.Controls.Add(textBoxStorageName); + groupBox1.Controls.Add(listBoxStorages); + groupBox1.Controls.Add(addObjectButton); + groupBox1.Location = new Point(775, 32); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(154, 472); + groupBox1.TabIndex = 13; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; // // menuStrip // @@ -204,15 +204,15 @@ AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(935, 516); - Controls.Add(toolsGroupBox); + Controls.Add(groupBox1); Controls.Add(pictureBoxCollection); Controls.Add(menuStrip); MainMenuStrip = menuStrip; Name = "FormTankCollection"; Text = "Набор танков"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); - toolsGroupBox.ResumeLayout(false); - toolsGroupBox.PerformLayout(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); menuStrip.ResumeLayout(false); menuStrip.PerformLayout(); ResumeLayout(false); @@ -226,12 +226,12 @@ private Button deleteTankButton; private Button addTankButton; private TextBox maskedTextBoxNumber; - private Label objectsLabel; + private Label label2; private TextBox textBoxStorageName; private Button addObjectButton; private ListBox listBoxStorages; private Button deleteObjectButton; - private GroupBox toolsGroupBox; + private GroupBox groupBox1; private MenuStrip menuStrip; private ToolStripMenuItem fileToolStripMenuItem; private ToolStripMenuItem loadToolStripMenuItem; diff --git a/ProjectTank/FormTankCollection.cs b/ProjectTank/FormTankCollection.cs index cd15bb9..da3860b 100644 --- a/ProjectTank/FormTankCollection.cs +++ b/ProjectTank/FormTankCollection.cs @@ -2,6 +2,10 @@ using ProjectTank.Generics; using ProjectTank.MovementStrategy; using System.Windows.Forms; +using Microsoft.VisualBasic.Logging; +using ProjectTank.Exceptions; +using Serilog; +using Log = Serilog.Log; namespace ProjectTank { @@ -22,17 +26,18 @@ namespace ProjectTank { int index = listBoxStorages.SelectedIndex; listBoxStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) { listBoxStorages.Items.Add(_storage.Keys[i]); } - if (listBoxStorages.Items.Count > 0 && (index == -1 || index - >= listBoxStorages.Items.Count)) + + if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) { listBoxStorages.SelectedIndex = 0; } - else if (listBoxStorages.Items.Count > 0 && index > -1 && - index < listBoxStorages.Items.Count) + + else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count) { listBoxStorages.SelectedIndex = index; } @@ -48,6 +53,7 @@ namespace ProjectTank } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); + Log.Information($"Добавлен набор: {textBoxStorageName.Text}"); } private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) @@ -66,9 +72,10 @@ namespace ProjectTank 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}"); } } @@ -87,15 +94,18 @@ namespace ProjectTank form.Show(); Action<DrawningTankBase>? tankDelegate = new((t) => { - if (obj + t) + try { + bool q = (obj + t); MessageBox.Show("Объект добавлен"); + Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); pictureBoxCollection.Image = obj.ShowTanks(); t.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); } - else + catch (StorageOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); + Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена"); + MessageBox.Show(ex.Message); } }); @@ -120,13 +130,25 @@ namespace ProjectTank { return; } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + + try { + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + var q = obj - pos; MessageBox.Show("Объект удален"); pictureBoxCollection.Image = obj.ShowTanks(); + Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); + } + catch (TankNotFoundException exception) + { + Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show(exception.Message); + } + catch (FormatException ex) + { + Log.Warning("Было введено не-число при удалении объекта из набора"); + MessageBox.Show("Введите число"); } - else MessageBox.Show("Не удалось удалить объект"); } /// <summary> @@ -148,15 +170,18 @@ namespace ProjectTank { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.SaveData(saveFileDialog.FileName)) + try { + _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", - "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен"); } - else + catch (Exception exception) { - MessageBox.Show("Не сохранилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось сохранить файл"); + MessageBox.Show($"Не сохранилось: {exception.Message}", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -169,19 +194,22 @@ namespace ProjectTank { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storage.LoadData(openFileDialog.FileName)) + try { + _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", - "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Файл {openFileDialog.FileName} успешно загружен"); foreach (var collection in _storage.Keys) { listBoxStorages.Items.Add(collection); } } - else + catch (Exception exception) { - MessageBox.Show("Не загрузилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Log.Warning("Не удалось загрузить"); + MessageBox.Show($"Не загрузилось: {exception.Message}", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/ProjectTank/FormTankConfig.Designer.cs b/ProjectTank/FormTankConfig.Designer.cs index 3265673..b9000f2 100644 --- a/ProjectTank/FormTankConfig.Designer.cs +++ b/ProjectTank/FormTankConfig.Designer.cs @@ -143,7 +143,7 @@ // // orangePanel // - orangePanel.BackColor = Color.FromArgb(255, 128, 0); + orangePanel.BackColor = Color.DarkOrange; orangePanel.Location = new Point(78, 84); orangePanel.Name = "orangePanel"; orangePanel.Size = new Size(49, 48); @@ -183,7 +183,7 @@ // // yellowPanel // - yellowPanel.BackColor = Color.FromArgb(255, 255, 128); + yellowPanel.BackColor = Color.Gold; yellowPanel.Location = new Point(12, 26); yellowPanel.Name = "yellowPanel"; yellowPanel.Size = new Size(49, 48); diff --git a/ProjectTank/Generics/SetGeneric.cs b/ProjectTank/Generics/SetGeneric.cs index f9d21e4..f402168 100644 --- a/ProjectTank/Generics/SetGeneric.cs +++ b/ProjectTank/Generics/SetGeneric.cs @@ -1,3 +1,5 @@ +using ProjectTank.Exceptions; + namespace ProjectTank.Generics { /// <summary> @@ -28,23 +30,27 @@ namespace ProjectTank.Generics /// <summary> /// Добавление объекта в набор /// </summary> - /// <param name="tank">Добавляемый автомобиль</param> + /// <param name="tank">Добавляемый танк</param> /// <returns></returns> public bool Insert(T tank) { if (_places.Count == _maxCount) - return false; - return Insert(tank, 0); + throw new StorageOverflowException(_maxCount); + + Insert(tank, 0); + return true; } /// <summary> /// Добавление объекта в набор на конкретную позицию /// </summary> - /// <param name="tank">Добавляемый автомобиль</param> + /// <param name="tank">Добавляемый танк</param> /// <param name="position">Позиция</param> /// <returns></returns> public bool Insert(T tank, int position) { - if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + if (!(position >= 0 && position <= Count)) return false; _places.Insert(position, tank); return true; @@ -57,7 +63,7 @@ namespace ProjectTank.Generics public bool Remove(int position) { if (!(position >= 0 && position < Count)) - return false; + throw new TankNotFoundException(position); _places.RemoveAt(position); return true; } diff --git a/ProjectTank/Generics/TanksGenericCollection.cs b/ProjectTank/Generics/TanksGenericCollection.cs index 7efbd01..7ea59ca 100644 --- a/ProjectTank/Generics/TanksGenericCollection.cs +++ b/ProjectTank/Generics/TanksGenericCollection.cs @@ -4,7 +4,7 @@ using ProjectTank.MovementStrategy; namespace ProjectTank.Generics { /// <summary> - /// Параметризованный класс для набора объектов DrawningCar + /// Параметризованный класс для набора объектов DrawningTankBase /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="U"></typeparam> @@ -70,14 +70,9 @@ namespace ProjectTank.Generics pos) { T? obj = collect._collection[pos]; - if (obj != null) - { - collect._collection.Remove(pos); - } + collect._collection.Remove(pos); return obj; } - - /// <summary> /// Получение объекта IMoveableObject /// </summary> diff --git a/ProjectTank/Generics/TanksGenericStorage.cs b/ProjectTank/Generics/TanksGenericStorage.cs index 0f98801..d14bb07 100644 --- a/ProjectTank/Generics/TanksGenericStorage.cs +++ b/ProjectTank/Generics/TanksGenericStorage.cs @@ -107,9 +107,10 @@ namespace ProjectTank.Generics } data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); } + if (data.Length == 0) { - return false; + throw new Exception("Невалиданя операция, нет данных для сохранения"); } string toWrite = $"TanksStorage{Environment.NewLine}{data}"; @@ -134,7 +135,7 @@ namespace ProjectTank.Generics { if (!File.Exists(filename)) { - return false; + throw new IOException("Файл не найден"); } using (StreamReader sr = new(filename)) @@ -143,11 +144,13 @@ namespace ProjectTank.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("TanksStorage")) { - return false; + //если нет такой записи, то это не те данные + throw new IOException("Неверный формат данных"); } _tankStorages.Clear(); @@ -161,7 +164,8 @@ namespace ProjectTank.Generics continue; } TanksGenericCollection<DrawningTankBase, DrawningObjectTank> collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { DrawningTankBase? tank = diff --git a/ProjectTank/Program.cs b/ProjectTank/Program.cs index 94ae4d7..f16c78b 100644 --- a/ProjectTank/Program.cs +++ b/ProjectTank/Program.cs @@ -1,3 +1,18 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; +using Microsoft.VisualBasic.Logging; +using Serilog; +using Serilog.Events; +using Serilog.Formatting.Json; +using Log = Serilog.Log; + namespace ProjectTank { internal static class Program @@ -8,9 +23,13 @@ namespace ProjectTank [STAThread] static void Main() { - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. - ApplicationConfiguration.Initialize(); + Log.Logger = new LoggerConfiguration() + .WriteTo.File("log.txt") + .MinimumLevel.Debug() + .CreateLogger(); + Application.SetHighDpiMode(HighDpiMode.SystemAware); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormTankCollection()); } } diff --git a/ProjectTank/ProjectTank.csproj b/ProjectTank/ProjectTank.csproj index b57c89e..2028c8e 100644 --- a/ProjectTank/ProjectTank.csproj +++ b/ProjectTank/ProjectTank.csproj @@ -8,4 +8,11 @@ <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> + <ItemGroup> + <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> + <PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" /> + <PackageReference Include="Serilog" Version="3.1.1" /> + <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> + </ItemGroup> + </Project> \ No newline at end of file