diff --git a/ArmoredCar/ArmoredCar/ArmoredCarNotFoundException.cs b/ArmoredCar/ArmoredCar/ArmoredCarNotFoundException.cs
new file mode 100644
index 0000000..77c9bde
--- /dev/null
+++ b/ArmoredCar/ArmoredCar/ArmoredCarNotFoundException.cs
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ArmoredCar
+{
+ [Serializable]
+ internal class ArmoredCarNotFoundException : ApplicationException
+ {
+ public ArmoredCarNotFoundException(int i) : base($"Не найден объект по позиции{i}") { }
+ public ArmoredCarNotFoundException() : base() { }
+ public ArmoredCarNotFoundException(string message) : base(message) { }
+ public ArmoredCarNotFoundException(string message, Exception exception) :
+ base(message, exception)
+ { }
+ protected ArmoredCarNotFoundException(SerializationInfo info, StreamingContext
+ contex) : base(info, contex) { }
+ }
+}
diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs
index c99cb31..3306a27 100644
--- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs
+++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs
@@ -229,14 +229,15 @@ namespace ArmoredCar
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
- if (_mapsCollection.SaveData(saveFileDialog.FileName))
+ try
{
+ _mapsCollection.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно", "Результат",
- MessageBoxButtons.OK, MessageBoxIcon.Information);
+ MessageBoxButtons.OK, MessageBoxIcon.Information);
}
- else
+ catch (Exception ex)
{
- MessageBox.Show("Не сохранилось", "Результат",
+ MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
@@ -250,15 +251,16 @@ namespace ArmoredCar
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
- if (_mapsCollection.LoadData(openFileDialog.FileName))
+ try
{
- MessageBox.Show("Загрузка прошла успешно", "Результат",
- MessageBoxButtons.OK, MessageBoxIcon.Information);
+ _mapsCollection.LoadData(openFileDialog.FileName);
+ MessageBox.Show("Загрузка прошла успешно", "Результат",
+ MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadMaps();
}
- else
+ catch (Exception ex)
{
- MessageBox.Show("Не загрузилось", "Результат",
+ MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs
index 19195c8..63c2468 100644
--- a/ArmoredCar/ArmoredCar/MapsCollection.cs
+++ b/ArmoredCar/ArmoredCar/MapsCollection.cs
@@ -83,7 +83,7 @@ namespace ArmoredCar
///
/// Путь и имя файла
///
- public bool SaveData(string filename)
+ public void SaveData(string filename)
{
if (File.Exists(filename))
{
@@ -99,14 +99,13 @@ namespace ArmoredCar
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))
{
@@ -138,8 +137,7 @@ namespace ArmoredCar
}
_mapStorages.Add(elem[0], new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
- }
- return true;
+ }
}
}
}
diff --git a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs
index c24a911..bd7f915 100644
--- a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs
+++ b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs
@@ -33,10 +33,8 @@ namespace ArmoredCar
/// Добавляемый автомобиль
///
public int Insert(T armoredCar)
- {
- if (Count < _maxCount)
- return Insert(armoredCar, 0);
- return -1;
+ {
+ return Insert(armoredCar, 0);
}
///
/// Добавление объекта в набор на конкретную позицию
@@ -46,6 +44,9 @@ namespace ArmoredCar
///
public int Insert(T armoredCar, int position)
{
+ if (Count >= _maxCount)
+ throw new StorageOverflowException();
+
if (position < 0 || position >= _maxCount)
return -1;
@@ -62,6 +63,8 @@ namespace ArmoredCar
if (position < 0 || position >= Count)
return null;
T armoredCar = _places[position];
+ if (armoredCar == null)
+ throw new ArmoredCarNotFoundException();
_places.RemoveAt(position);
return armoredCar;
}
diff --git a/ArmoredCar/ArmoredCar/StorageOverflowException.cs b/ArmoredCar/ArmoredCar/StorageOverflowException.cs
new file mode 100644
index 0000000..7738cd4
--- /dev/null
+++ b/ArmoredCar/ArmoredCar/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 ArmoredCar
+{
+
+ [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) { }
+ }
+}