From 62a9296290b20eb92aa1e8352e77ef1c37229bb8 Mon Sep 17 00:00:00 2001
From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com>
Date: Sat, 26 Nov 2022 20:09:04 +0400
Subject: [PATCH] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?=
=?UTF-8?q?=D0=B8=D1=8F=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?=
=?UTF-8?q?=D0=B8=D0=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Boats/Boats/BoatNotFoundException.cs | 19 +++++++++++++
Boats/Boats/FormMapWithSetBoats.cs | 37 +++++++++++++++++--------
Boats/Boats/MapWithSetBoatsGeneric.cs | 3 +-
Boats/Boats/MapsCollection.cs | 10 +++----
Boats/Boats/SetBoatsGeneric.cs | 9 ++++--
Boats/Boats/StorageOverflowException.cs | 19 +++++++++++++
6 files changed, 75 insertions(+), 22 deletions(-)
create mode 100644 Boats/Boats/BoatNotFoundException.cs
create mode 100644 Boats/Boats/StorageOverflowException.cs
diff --git a/Boats/Boats/BoatNotFoundException.cs b/Boats/Boats/BoatNotFoundException.cs
new file mode 100644
index 0000000..5465af8
--- /dev/null
+++ b/Boats/Boats/BoatNotFoundException.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 Boats
+{
+ [Serializable]
+ internal class BoatNotFoundException : ApplicationException
+ {
+ public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
+ public BoatNotFoundException() : base() { }
+ public BoatNotFoundException(string message) : base(message) { }
+ public BoatNotFoundException(string message, Exception exception) : base(message, exception) { }
+ protected BoatNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
+ }
+}
diff --git a/Boats/Boats/FormMapWithSetBoats.cs b/Boats/Boats/FormMapWithSetBoats.cs
index 908f95c..5c4fb06 100644
--- a/Boats/Boats/FormMapWithSetBoats.cs
+++ b/Boats/Boats/FormMapWithSetBoats.cs
@@ -124,16 +124,27 @@ namespace Boats
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
- pos -= 1;
- 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 (BoatNotFoundException ex)
{
- MessageBox.Show("Не удалось удалить объект");
+ MessageBox.Show($"Ошибка удаления: {ex.Message}");
}
+ catch (Exception ex)
+ {
+ MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
+ }
+
}
///
/// Вывод набора
@@ -270,13 +281,14 @@ namespace Boats
{
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);
}
}
}
@@ -289,15 +301,16 @@ namespace Boats
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
- if (_mapsCollection.LoadData(openFileDialog.FileName))
+ try
{
+ _mapsCollection.LoadData(openFileDialog.FileName);
ReloadMaps();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
- else
+ catch (Exception ex)
{
- MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
diff --git a/Boats/Boats/MapWithSetBoatsGeneric.cs b/Boats/Boats/MapWithSetBoatsGeneric.cs
index 9c989cd..9d7e474 100644
--- a/Boats/Boats/MapWithSetBoatsGeneric.cs
+++ b/Boats/Boats/MapWithSetBoatsGeneric.cs
@@ -91,6 +91,7 @@ namespace Boats
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics g = Graphics.FromImage(bmp);
DrawBackground(g);
+ Shaking();
DrawBoats(g);
return bmp;
}
@@ -135,8 +136,8 @@ namespace Boats
var boat = _setBoats[j];
if (boat != null)
{
+ boat = _setBoats.Remove(j);
_setBoats.Insert(boat, i);
- _setBoats.Remove(j);
break;
}
}
diff --git a/Boats/Boats/MapsCollection.cs b/Boats/Boats/MapsCollection.cs
index d818b99..b9c40b4 100644
--- a/Boats/Boats/MapsCollection.cs
+++ b/Boats/Boats/MapsCollection.cs
@@ -93,7 +93,7 @@ namespace Boats
///
/// Путь и имя файла
///
- public bool SaveData(string filename)
+ public void SaveData(string filename)
{
if (File.Exists(filename))
{
@@ -107,18 +107,17 @@ namespace Boats
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
}
}
- return true;
}
///
/// Загрузка нформации по лодкам в гавани из файла
///
///
///
- public bool LoadData(string filename)
+ public void LoadData(string filename)
{
if (!File.Exists(filename))
{
- return false;
+ throw new Exception("Файл не найден");
}
using (StreamReader sr = File.OpenText(filename))
{
@@ -126,7 +125,7 @@ namespace Boats
if (currentLine == null || !currentLine.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
- return false;
+ throw new Exception("Формат данных в файле не правильный");
}
//очищаем записи
_mapStorages.Clear();
@@ -156,7 +155,6 @@ namespace Boats
currentLine = sr.ReadLine();
}
}
- return true;
}
}
}
diff --git a/Boats/Boats/SetBoatsGeneric.cs b/Boats/Boats/SetBoatsGeneric.cs
index 4097ce3..c1d5002 100644
--- a/Boats/Boats/SetBoatsGeneric.cs
+++ b/Boats/Boats/SetBoatsGeneric.cs
@@ -42,8 +42,9 @@ namespace Boats
public int Insert(T boat)
{
// Проверка на _maxCount
+ // Если достигли максимального значения - выбрасываем исключение
if (Count == _maxCount)
- return -1;
+ throw new StorageOverflowException(_maxCount);
// Вставка в начало набора
return Insert(boat, 0);
}
@@ -69,9 +70,11 @@ namespace Boats
public T Remove(int position)
{
// Проверка позиции
- if (Count == 0 || position < 0 || position >= _maxCount)
- return null;
+ // Если позиция уже пустая выбрасываем исключение
+ if (Count == 0 || position < 0 || position >= _maxCount || _places[position] == null)
+ throw new BoatNotFoundException(position);
T boat = _places[position];
+
_places[position] = null;
return boat;
}
diff --git a/Boats/Boats/StorageOverflowException.cs b/Boats/Boats/StorageOverflowException.cs
new file mode 100644
index 0000000..0eac646
--- /dev/null
+++ b/Boats/Boats/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 Boats
+{
+ [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) { }
+ }
+}