From 8ed442d581ba7b0f1efdeab7c07f5c285dcea10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Wed, 23 Nov 2022 21:04:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotives.Designer.cs | 12 +-- .../Locomotives/FormMapWithSetLocomotives.cs | 73 ++++++++++++------- .../LocomotiveNotFoundException.cs | 14 ++++ Locomotives/Locomotives/MapsCollection.cs | 10 +-- .../Locomotives/SetLocomotivesGeneric.cs | 8 +- .../Locomotives/StorageOverflowException.cs | 14 ++++ 6 files changed, 90 insertions(+), 41 deletions(-) create mode 100644 Locomotives/Locomotives/LocomotiveNotFoundException.cs create mode 100644 Locomotives/Locomotives/StorageOverflowException.cs diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs index 02f8491..5126946 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs @@ -70,7 +70,7 @@ this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddCar); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(950, 24); + this.groupBoxTools.Location = new System.Drawing.Point(462, 24); this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Size = new System.Drawing.Size(223, 652); this.groupBoxTools.TabIndex = 0; @@ -243,7 +243,7 @@ this.pictureBoxLocomotives.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxLocomotives.Location = new System.Drawing.Point(0, 24); this.pictureBoxLocomotives.Name = "pictureBoxLocomotives"; - this.pictureBoxLocomotives.Size = new System.Drawing.Size(950, 652); + this.pictureBoxLocomotives.Size = new System.Drawing.Size(462, 652); this.pictureBoxLocomotives.TabIndex = 1; this.pictureBoxLocomotives.TabStop = false; // @@ -253,7 +253,7 @@ this.FileToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(1173, 24); + this.menuStrip1.Size = new System.Drawing.Size(685, 24); this.menuStrip1.TabIndex = 2; this.menuStrip1.Text = "menuStrip"; // @@ -269,14 +269,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -292,7 +292,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1173, 676); + this.ClientSize = new System.Drawing.Size(685, 676); this.Controls.Add(this.pictureBoxLocomotives); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip1); diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs index 7154750..a61e58b 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs @@ -111,19 +111,30 @@ private void ButtonAddLocomotive_Click(object sender, EventArgs e) { FormLocomotiveConfig formLocomotiveConfig = new(); - formLocomotiveConfig.AddEvent(new (AddLocomotive)); + formLocomotiveConfig.AddEvent(new(AddLocomotive)); formLocomotiveConfig.Show(); } private void AddLocomotive(DrawningLocomotive locomotive) { - if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive)) > -1) + try { - MessageBox.Show("Объект добавлен"); - pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive)) > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } - else + catch (StorageOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show($"Ошибка добавления: {ex.Message}"); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } } /// @@ -146,16 +157,26 @@ return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) > -1) + try { - MessageBox.Show("Объект удален"); - pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) > -1) + { + MessageBox.Show("Объект удален"); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } } - else + catch (LocomotiveNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } - } /// /// Вывод набора @@ -223,15 +244,16 @@ { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { - MessageBox.Show("Сохранение прошло успешно", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Information); + _mapsCollection.SaveData(saveFileDialog.FileName); + MessageBox.Show("Сохранение прошло успешно", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch(Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -244,16 +266,17 @@ { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { - MessageBox.Show("Загрузка прошла успешно", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Information); - ReloadMaps(); + _mapsCollection.LoadData(openFileDialog.FileName); + MessageBox.Show("Загрузка прошла успешно", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); } - else + catch (Exception ex) { - MessageBox.Show("Не удалось загрузить файл", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не удалось загрузить файл: {ex.Message}", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/Locomotives/Locomotives/LocomotiveNotFoundException.cs b/Locomotives/Locomotives/LocomotiveNotFoundException.cs new file mode 100644 index 0000000..38c3942 --- /dev/null +++ b/Locomotives/Locomotives/LocomotiveNotFoundException.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization; + +namespace Locomotives +{ + [Serializable] + internal class LocomotiveNotFoundException : ApplicationException + { + public LocomotiveNotFoundException(int i) : base($"Не наден объект по позиции {i}") { } + public LocomotiveNotFoundException() : base() { } + public LocomotiveNotFoundException(string message) : base(message) { } + public LocomotiveNotFoundException(string message, Exception Exception) : base(message, Exception) { } + protected LocomotiveNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/Locomotives/Locomotives/MapsCollection.cs b/Locomotives/Locomotives/MapsCollection.cs index 0f6e083..fa5ee4c 100644 --- a/Locomotives/Locomotives/MapsCollection.cs +++ b/Locomotives/Locomotives/MapsCollection.cs @@ -71,7 +71,7 @@ namespace Locomotives return _mapStorages[ind]; } } - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -86,13 +86,12 @@ namespace Locomotives } sw.Close(); } - return true; } - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найдён"); } using (StreamReader sr = new(filename)) { @@ -100,7 +99,7 @@ namespace Locomotives if (firstStr == null || !firstStr.Contains("MapsCollection")) { //если нет такой записи, то это не те данные - return false; + throw new Exception("Формат данных в файле неправильный"); } string? currentString; while ((currentString = sr.ReadLine()) != null) @@ -123,7 +122,6 @@ namespace Locomotives _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } sr.Close(); - return true; } } } diff --git a/Locomotives/Locomotives/SetLocomotivesGeneric.cs b/Locomotives/Locomotives/SetLocomotivesGeneric.cs index 96c7182..987227c 100644 --- a/Locomotives/Locomotives/SetLocomotivesGeneric.cs +++ b/Locomotives/Locomotives/SetLocomotivesGeneric.cs @@ -43,7 +43,7 @@ _places.Insert(0, locomotive); return 0; } - return -1; + throw new StorageOverflowException(_places.Count); } /// /// Добавление объекта в набор на конкретную позицию @@ -62,7 +62,7 @@ _places.Insert(position, locomotive); return position; } - return -1; + throw new StorageOverflowException(_places.Count); } /// /// Удаление объекта из набора с конкретной позиции @@ -71,9 +71,9 @@ /// public int Remove(int position) { - if (position > Count || _places[position] == null) + if (position >= Count || _places[position] == null) { - return -1; + throw new LocomotiveNotFoundException(position); } _places.RemoveAt(position); return position; diff --git a/Locomotives/Locomotives/StorageOverflowException.cs b/Locomotives/Locomotives/StorageOverflowException.cs new file mode 100644 index 0000000..5900987 --- /dev/null +++ b/Locomotives/Locomotives/StorageOverflowException.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization; + +namespace Locomotives +{ + [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) { } + } +}