ПИбд-23 Салин Олег Алексеевич Лабораторная работа №7 #17

Closed
Oleja123 wants to merge 5 commits from Lab7 into Lab6
6 changed files with 74 additions and 27 deletions
Showing only changes of commit cb5c91f884 - Show all commits

View File

@ -1,4 +1,5 @@
using Monorail.DrawningObjects; using Monorail.DrawningObjects;
using Monorail.Exceptions;
using Monorail.Generics; using Monorail.Generics;
using Monorail.MovementStrategy; using Monorail.MovementStrategy;
using System; using System;
@ -63,16 +64,16 @@ namespace Monorail
form.Show(); form.Show();
Action<DrawningMonorail>? monorailDelegate = new((m) => Action<DrawningMonorail>? monorailDelegate = new((m) =>
{ {
bool q = (obj + m); try
if (q)
{ {
bool q = obj + m;
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
m.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); m.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height);
pictureBoxCollection.Image = obj.ShowMonorails(); pictureBoxCollection.Image = obj.ShowMonorails();
} }
else catch (StorageOverflowException ex)
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show(ex.Message);
} }
}); });
form.AddEvent(monorailDelegate); form.AddEvent(monorailDelegate);
@ -96,14 +97,15 @@ namespace Monorail
return; return;
} }
int pos = Convert.ToInt32(maskedTextBox.Text); int pos = Convert.ToInt32(maskedTextBox.Text);
if (obj - pos != null) try
{ {
var q = obj - pos;
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowMonorails(); pictureBoxCollection.Image = obj.ShowMonorails();
} }
else catch (MonorailNotFoundException ex)
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show(ex.Message);
} }
} }
@ -161,15 +163,17 @@ MessageBoxIcon.Question) == DialogResult.Yes)
{ {
if (saveFileDialog.ShowDialog() == DialogResult.OK) if (saveFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_storage.SaveData(saveFileDialog.FileName)) try
{ {
_storage.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно", MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
else catch (Exception ex)
{ {
MessageBox.Show("Не сохранилось", "Результат", MessageBox.Show($"Не сохранилось: {ex.Message}",
MessageBoxButtons.OK, MessageBoxIcon.Error); "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
@ -179,8 +183,8 @@ MessageBoxIcon.Question) == DialogResult.Yes)
{ {
if (openFileDialog.ShowDialog() == DialogResult.OK) if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_storage.LoadData(openFileDialog.FileName)) try {
{ _storage.LoadData(openFileDialog.FileName);
MessageBox.Show("Загрузка прошла успешно", MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach(var collection in _storage.Keys) foreach(var collection in _storage.Keys)
@ -188,10 +192,11 @@ MessageBoxIcon.Question) == DialogResult.Yes)
listBoxStorages.Items.Add(collection); listBoxStorages.Items.Add(collection);
} }
} }
else catch (Exception ex)
{ {
MessageBox.Show("Не загрузилось", "Результат", MessageBox.Show($"Не загрузилось: {ex.Message}",
MessageBoxButtons.OK, MessageBoxIcon.Error); "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }

View File

@ -45,8 +45,7 @@ namespace Monorail.Generics
pos) pos)
{ {
T? obj = collect._collection[pos]; T? obj = collect._collection[pos];
if (obj != null) collect._collection.Remove(pos);
collect._collection.Remove(pos);
return obj; return obj;
} }

View File

@ -50,7 +50,7 @@ MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail>> record in _
if (data.Length == 0) if (data.Length == 0)
{ {
return false; throw new Exception("Невалиданя операция, нет данных для сохранения");
} }
string toWrite = $"MonorailStorage{Environment.NewLine}{data}"; string toWrite = $"MonorailStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' }, var strs = toWrite.Split(new char[] { '\n', '\r' },
@ -70,7 +70,7 @@ StringSplitOptions.RemoveEmptyEntries);
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
return false; throw new Exception("Файл не найден");
} }
using (StreamReader sr = new(filename)) using (StreamReader sr = new(filename))
{ {
@ -79,11 +79,11 @@ StringSplitOptions.RemoveEmptyEntries);
StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0) if (strs == null || strs.Length == 0)
{ {
return false; throw new Exception("Нет данных для загрузки");
} }
if (!strs[0].StartsWith("MonorailStorage")) if (!strs[0].StartsWith("MonorailStorage"))
{ {
return false; throw new Exception("Неверный формат данных");
} }
_monorailStorages.Clear(); _monorailStorages.Clear();
do do
@ -107,7 +107,7 @@ StringSplitOptions.RemoveEmptyEntries);
{ {
if (!(collection + monorail)) if (!(collection + monorail))
{ {
return false; throw new Exception("Ошибка добавления в коллекцию");
} }
} }
} }

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Monorail.Exceptions
{
[Serializable]
internal class MonorailNotFoundException : ApplicationException
{
public MonorailNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
public MonorailNotFoundException() : base () { }
public MonorailNotFoundException(string message) : base(message) { }
public MonorailNotFoundException(string message, Exception exception) : base(message, exception) { }
protected MonorailNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}

View File

@ -1,8 +1,10 @@
using System; using Monorail.Exceptions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms;
namespace Monorail.Generics namespace Monorail.Generics
{ {
@ -23,7 +25,7 @@ namespace Monorail.Generics
public bool Insert(T monorail) public bool Insert(T monorail)
{ {
if (_places.Count == _maxCount) if (_places.Count == _maxCount)
return false; throw new StorageOverflowException(_maxCount);
Insert(monorail, 0); Insert(monorail, 0);
return true; return true;
@ -31,7 +33,9 @@ namespace Monorail.Generics
public bool Insert(T monorail, int position) public bool Insert(T monorail, 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; return false;
_places.Insert(position, monorail); _places.Insert(position, monorail);
return true; return true;
@ -39,7 +43,7 @@ namespace Monorail.Generics
public bool Remove(int position) public bool Remove(int position)
{ {
if (!(position >= 0 && position < Count)) if (!(position >= 0 && position < Count))
return false; throw new MonorailNotFoundException(position);
_places.RemoveAt(position); _places.RemoveAt(position);
return true; return true;
} }

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Monorail.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 context) : base(info, context) { }
}
}