diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs
index db58c0f..5fc2f29 100644
--- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs
+++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs
@@ -125,22 +125,28 @@ namespace AircraftCarrier
///
private void AddWarshipOnForm(DrawingWarship drawningWarship)
{
- if (listBoxMaps.SelectedIndex == -1)
+ try
{
- return;
+ if (listBoxMaps.SelectedIndex == -1)
+ {
+ MessageBox.Show("Перед добавлением объекта необходимо создать карту");
+ }
+ DrawingObjectWarship warship = new(drawningWarship);
+ if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship >= 0)
+ {
+ MessageBox.Show("Объект добавлен");
+ pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
+ }
+ else
+ {
+ MessageBox.Show("Не удалось добавить объект");
+ }
}
-
- DrawingObjectWarship warship = new(drawningWarship);
- if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship >= 0)
- {
- MessageBox.Show("Объект добавлен");
- pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
- }
- else
+ catch(StorageOverflowException ex)
{
MessageBox.Show("Не удалось добавить объект");
+ MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
-
}
///
/// Удаление объекта
@@ -149,27 +155,27 @@ namespace AircraftCarrier
///
private void ButtonRemoveWarship_Click(object sender, EventArgs e)
{
- if (listBoxMaps.SelectedIndex == -1)
- {
- return;
- }
- if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
- {
- return;
- }
- if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
+ if (listBoxMaps.SelectedIndex == -1 || string.IsNullOrEmpty(maskedTextBoxPosition.Text) ||
+ MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
- if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
+ try
{
- MessageBox.Show("Объект удален");
- pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
+ if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
+ {
+ MessageBox.Show("Объект удален");
+ pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
+ }
+ else
+ {
+ MessageBox.Show("Не удалось удалить объект");
+ }
}
- else
+ catch (WarshipNotFoundException ex)
{
- MessageBox.Show("Не удалось удалить объект");
+ MessageBox.Show($"Ошибка удаления: {ex.Message}");
}
}
///
@@ -238,13 +244,14 @@ namespace AircraftCarrier
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
- if (_mapsCollection.SaveData(saveFileDialog.FileName))
+ try
{
+ _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);
}
}
}
@@ -257,14 +264,15 @@ namespace AircraftCarrier
{
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
- if (_mapsCollection.LoadData(openFileDialog.FileName))
+ try
{
+ _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/AircraftCarrier/AircraftCarrier/MapsCollection.cs b/AircraftCarrier/AircraftCarrier/MapsCollection.cs
index 618549b..1eb5145 100644
--- a/AircraftCarrier/AircraftCarrier/MapsCollection.cs
+++ b/AircraftCarrier/AircraftCarrier/MapsCollection.cs
@@ -74,21 +74,11 @@ namespace AircraftCarrier
}
}
///
- /// Метод записи информации в файл
- ///
- /// Строка, которую следует записать
- /// Поток для записи
- private static void WriteToFile(string text, FileStream stream)
- {
- byte[] info = new UTF8Encoding(true).GetBytes(text);
- stream.Write(info, 0, info.Length);
- }
- ///
/// Сохранение информации по кораблям в хранилище в файл
///
/// Путь и имя файла
///
- public bool SaveData(string filename)
+ public void SaveData(string filename)
{
if (File.Exists(filename))
{
@@ -102,7 +92,6 @@ namespace AircraftCarrier
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
- return true;
}
///
@@ -110,18 +99,18 @@ namespace AircraftCarrier
///
///
///
- public bool LoadData(string filename)
+ public void LoadData(string filename)
{
if (!File.Exists(filename))
{
- return false;
+ throw new FileNotFoundException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
- return false;
+ throw new FileFormatException("Формат данных в файле не правильный");
}
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
@@ -141,7 +130,6 @@ namespace AircraftCarrier
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
- return true;
}
}
}
diff --git a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs
index 5aba52c..538aad5 100644
--- a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs
+++ b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs
@@ -49,6 +49,9 @@ namespace AircraftCarrier
///
public int Insert(T warship, int position)
{
+ if (Count == _maxCount)
+ throw new StorageOverflowException(_maxCount);
+
if (position >= _maxCount || position < 0) return -1;
_places.Insert(position, warship);
return 1;
@@ -64,6 +67,8 @@ namespace AircraftCarrier
return null;
var result = _places[position];
+ if(result == null)
+ throw new WarshipNotFoundException(position);
_places.RemoveAt(position);
return result;
}
diff --git a/AircraftCarrier/AircraftCarrier/StorageOverflowException.cs b/AircraftCarrier/AircraftCarrier/StorageOverflowException.cs
new file mode 100644
index 0000000..d6f6a32
--- /dev/null
+++ b/AircraftCarrier/AircraftCarrier/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 AircraftCarrier
+{
+ [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/AircraftCarrier/AircraftCarrier/WarshipNotFoundException.cs b/AircraftCarrier/AircraftCarrier/WarshipNotFoundException.cs
new file mode 100644
index 0000000..40638c3
--- /dev/null
+++ b/AircraftCarrier/AircraftCarrier/WarshipNotFoundException.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 AircraftCarrier
+{
+ [Serializable]
+ internal class WarshipNotFoundException : ApplicationException
+ {
+ public WarshipNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
+ public WarshipNotFoundException() : base() { }
+ public WarshipNotFoundException(string message) : base(message) { }
+ public WarshipNotFoundException(string message, Exception exception) : base(message, exception) { }
+ protected WarshipNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
+ }
+}