From 3cae6dc2f4344e8fb3fa467e7d7e879a063fc6c4 Mon Sep 17 00:00:00 2001 From: sqdselo <147947144+sqdselo@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:17:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=207=20(=D0=B2=D1=81=D0=B5=20=D0=BA=D1=80=D0=BE=D0=BC?= =?UTF-8?q?=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 2 +- .../CollectionGenericObjects/Garage.cs | 4 +- .../ListGenericObjects.cs | 68 +++++++++----- .../MassivGenericObjects.cs | 88 +++++++++++++------ .../StorageCollection.cs | 31 ++++--- .../Exceptions/CollectionOverflowException.cs | 19 ++++ .../Exceptions/ObjectNotFoundException.cs | 19 ++++ .../PositionOutOfCollectionException.cs | 19 ++++ .../HoistingCrane/FormCarCollection.cs | 49 ++++++++--- 9 files changed, 222 insertions(+), 77 deletions(-) create mode 100644 HoistingCrane/HoistingCrane/Exceptions/CollectionOverflowException.cs create mode 100644 HoistingCrane/HoistingCrane/Exceptions/ObjectNotFoundException.cs create mode 100644 HoistingCrane/HoistingCrane/Exceptions/PositionOutOfCollectionException.cs diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs index 98a10dc..1c50f33 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/AbstractCompany.cs @@ -31,7 +31,7 @@ namespace HoistingCrane.CollectionGenericObjects { get { - return (pictureWidth * pictureHeight) / (_placeSizeHeight * _placeSizeWidth)-3; + return (pictureWidth * pictureHeight) / (_placeSizeHeight * _placeSizeWidth)-2; } } public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects array) diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/Garage.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/Garage.cs index e2da629..0cf29d7 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/Garage.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/Garage.cs @@ -38,8 +38,9 @@ namespace HoistingCrane.CollectionGenericObjects { arr?.Get(i)?.SetPictureSize(pictureWidth, pictureHeight); arr?.Get(i)?.SetPosition(_placeSizeWidth * currentPosWidth + 25, _placeSizeHeight * currentPosHeight + 15); + } - + if (currentPosWidth > 0) currentPosWidth--; else @@ -51,7 +52,6 @@ namespace HoistingCrane.CollectionGenericObjects { break; } - } } } diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs index c92ca52..73f740c 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,5 @@ -using System; +using HoistingCrane.Exceptions; +using System; using System.CodeDom.Compiler; namespace HoistingCrane.CollectionGenericObjects @@ -44,47 +45,72 @@ namespace HoistingCrane.CollectionGenericObjects public T? Get(int position) { - // TODO проверка позиции - if (position >= Count || position < 0) return null; - return list[position]; + try + { + if (position >= Count || position < 0) + { + throw new PositionOutOfCollectionException(position); + } + return list[position]; + } + catch (PositionOutOfCollectionException ex) + { + MessageBox.Show(ex.Message); + return null; + } } - - + public int Insert(T obj) { - // TODO проверка, что не превышено максимальное количество элементов - // TODO вставка в конец набора - if (Count == _maxCount) + try { + if (Count == _maxCount) throw new CollectionOverflowException(Count); + list.Add(obj); + return Count; + } + catch(CollectionOverflowException ex) + { + MessageBox.Show(ex.Message); return -1; } - list.Add(obj); - return Count; } public int Insert(T obj, int position) { - // TODO проверка, что не превышено максимальное количество элементов - // TODO проверка позиции - // TODO вставка по позиции - if (position < 0 || position >= Count || Count == _maxCount) + try { + if (position < 0 || position >= _maxCount) throw new PositionOutOfCollectionException(position); + if (Count == _maxCount) throw new CollectionOverflowException(Count); + list.Insert(position, obj); + return position; + } + catch (CollectionOverflowException ex) + { + MessageBox.Show(ex.Message); return -1; } - list.Insert(position, obj); - return position; + catch (PositionOutOfCollectionException ex) + { + MessageBox.Show(ex.Message); + return -1; + } + } + public T? Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из списка - if (position >= 0 && position < list.Count) + try { + if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); T? temp = list[position]; list.RemoveAt(position); return temp; } - return null; + catch (PositionOutOfCollectionException ex) + { + MessageBox.Show(ex.Message); + return null; + } } public IEnumerable GetItems() diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs index afe3a36..7680ae0 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/MassivGenericObjects.cs @@ -1,4 +1,5 @@ -using System; +using HoistingCrane.Exceptions; +using System; namespace HoistingCrane.CollectionGenericObjects { @@ -39,11 +40,22 @@ namespace HoistingCrane.CollectionGenericObjects public T? Get(int position) { - if (position >= 0 && position < arr.Length) + try { + if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); + //if (arr[position] == null) throw new ObjectNotFoundException(position); return arr[position]; } - return null; + catch (PositionOutOfCollectionException ex) + { + MessageBox.Show(ex.Message); + return null; + } + //catch (ObjectNotFoundException ex) + //{ + // MessageBox.Show(ex.Message); + // return null; + //} } public IEnumerable GetItems() @@ -56,45 +68,69 @@ namespace HoistingCrane.CollectionGenericObjects public int Insert(T obj) { - return Insert(obj, 0); + try + { + if (arr.Count(x => x != null) == MaxCount) throw new CollectionOverflowException(Count); + return Insert(obj, 0); + } + catch(CollectionOverflowException ex) + { + MessageBox.Show(ex.Message); + return -1; + } } public int Insert(T obj, int position) { - //todo Проверка позиции - if (position < 0 || position > Count) + try { + if (position < 0 || position > Count) throw new PositionOutOfCollectionException(position); + + if (arr[position] == null) + { + arr[position] = obj; + return position; + } + else + { + if (Insert(obj, position + 1) != -1) + { + return position; + } + if (Insert(obj, position - 1) != -1) + { + return position; + } + return -1; + } + } + catch (PositionOutOfCollectionException ex) + { + MessageBox.Show(ex.Message); return -1; } - - if (arr[position] == null) - { - arr[position] = obj; - return position; - } - else - { - if (Insert(obj, position + 1) != -1) - { - return position; - } - if (Insert(obj, position - 1) != -1) - { - return position; - } - } - return -1; } public T? Remove(int position) { - if (position >= 0 && position < Count) + try { + if (position < 0 || position >= MaxCount) throw new PositionOutOfCollectionException(position); + if (arr[position] == null) throw new ObjectNotFoundException(position); T? temp = arr[position]; arr[position] = null; return temp; } - return null; + catch (ObjectNotFoundException ex) + { + MessageBox.Show(ex.Message); + return null; + } + catch (PositionOutOfCollectionException ex) + { + MessageBox.Show(ex.Message); + return null; + } } } } diff --git a/HoistingCrane/HoistingCrane/CollectionGenericObjects/StorageCollection.cs b/HoistingCrane/HoistingCrane/CollectionGenericObjects/StorageCollection.cs index d696adb..cb6a3f0 100644 --- a/HoistingCrane/HoistingCrane/CollectionGenericObjects/StorageCollection.cs +++ b/HoistingCrane/HoistingCrane/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,5 @@ using HoistingCrane.Drawning; +using HoistingCrane.Exceptions; using System; using System.Text; @@ -79,11 +80,11 @@ namespace HoistingCrane.CollectionGenericObjects /// /// Путь и имя файла /// true - сохранение прошло успешно, false - ошибка при сохранении данных - public bool SaveData(string filename) + public void SaveData(string filename) { if (dict.Count == 0) { - return false; + throw new Exception("В хранилище отсутствуют коллекции для сохранения"); } if (File.Exists(filename)) @@ -125,8 +126,6 @@ namespace HoistingCrane.CollectionGenericObjects } } - return true; - } /// @@ -134,22 +133,22 @@ namespace HoistingCrane.CollectionGenericObjects // /// // /// Путь и имя файла // /// true - загрузка прошла успешно, false - ошибка при загрузке данных - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не существует"); } using (StreamReader fs = File.OpenText(filename)) { string str = fs.ReadLine(); if (str == null || str.Length == 0) { - return false; + throw new Exception("В файле не присутствуют данные"); } if (!str.StartsWith(_collectionKey)) { - return false; + throw new Exception("В файле неверные данные"); } dict.Clear(); string strs = ""; @@ -164,23 +163,29 @@ namespace HoistingCrane.CollectionGenericObjects ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); if (collection == null) { - return false; + throw new Exception("Не удалось создать коллекцию"); } collection.MaxCount = Convert.ToInt32(record[2]); string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { - if (elem?.CreateDrawningTrackedVehicle() is T truck) + if (elem?.CreateDrawningTrackedVehicle() is T crane) { - if (collection.Insert(truck) == -1) + try { - return false; + if (collection.Insert(crane) == -1) + { + throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); + } + } + catch(CollectionOverflowException ex) + { + throw new Exception("Коллекция переполнена"); } } } dict.Add(record[0], collection); } - return true; } } /// diff --git a/HoistingCrane/HoistingCrane/Exceptions/CollectionOverflowException.cs b/HoistingCrane/HoistingCrane/Exceptions/CollectionOverflowException.cs new file mode 100644 index 0000000..da3c19f --- /dev/null +++ b/HoistingCrane/HoistingCrane/Exceptions/CollectionOverflowException.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 HoistingCrane.Exceptions +{ + [Serializable] + public class CollectionOverflowException : ApplicationException + { + public CollectionOverflowException(int count) : base("В коллекции превышено допустимое количество: count" + count) { } + public CollectionOverflowException() : base(){ } + public CollectionOverflowException(string message) : base(message) { } + public CollectionOverflowException(string message, Exception exception) : base(message, exception) { } + protected CollectionOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/HoistingCrane/HoistingCrane/Exceptions/ObjectNotFoundException.cs b/HoistingCrane/HoistingCrane/Exceptions/ObjectNotFoundException.cs new file mode 100644 index 0000000..c833095 --- /dev/null +++ b/HoistingCrane/HoistingCrane/Exceptions/ObjectNotFoundException.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 HoistingCrane.Exceptions +{ + [Serializable] + public class ObjectNotFoundException : ApplicationException + { + public ObjectNotFoundException(int i) :base("Не найден объект по позиции " + i){ } + public ObjectNotFoundException() : base() { } + public ObjectNotFoundException(string message) : base(message) { } + public ObjectNotFoundException(string message, Exception exception) : base(message, exception) { } + protected ObjectNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/HoistingCrane/HoistingCrane/Exceptions/PositionOutOfCollectionException.cs b/HoistingCrane/HoistingCrane/Exceptions/PositionOutOfCollectionException.cs new file mode 100644 index 0000000..e35263d --- /dev/null +++ b/HoistingCrane/HoistingCrane/Exceptions/PositionOutOfCollectionException.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 HoistingCrane.Exceptions +{ + [Serializable] + public class PositionOutOfCollectionException : ApplicationException + { + public PositionOutOfCollectionException(int i) : base("Выход за границы коллекции. Позиция " + i) { } + public PositionOutOfCollectionException() : base(){ } + public PositionOutOfCollectionException(string message) : base(message) { } + public PositionOutOfCollectionException(string message, Exception exception) : base(message, exception) { } + protected PositionOutOfCollectionException(SerializationInfo info, StreamingContext context) : base(info, context){ } + } +} diff --git a/HoistingCrane/HoistingCrane/FormCarCollection.cs b/HoistingCrane/HoistingCrane/FormCarCollection.cs index 978fbe7..7fb63e8 100644 --- a/HoistingCrane/HoistingCrane/FormCarCollection.cs +++ b/HoistingCrane/HoistingCrane/FormCarCollection.cs @@ -49,10 +49,10 @@ namespace HoistingCrane MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); } - else - { - MessageBox.Show("Не удалось добавить объект"); - } + //else + //{ + // MessageBox.Show("Не удалось добавить объект"); + //} } private static Color GetColor(Random random) { @@ -82,10 +82,10 @@ namespace HoistingCrane MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); } - else - { - MessageBox.Show("Не удалось добавить объект"); - } + //else + //{ + // MessageBox.Show("Не удалось добавить объект"); + //} } private void buttonDeleteCar_Click(object sender, EventArgs e) @@ -105,10 +105,10 @@ namespace HoistingCrane MessageBox.Show("Объект удален!"); pictureBox.Image = _company.Show(); } - else - { - MessageBox.Show("Не удалось удалить объект"); - } + //else + //{ + // MessageBox.Show("Не удалось удалить объект"); + //} } private void buttonRefresh_Click(object sender, EventArgs e) { @@ -214,7 +214,17 @@ namespace HoistingCrane { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_storageCollection.SaveData(saveFileDialog.FileName)) + bool saveSuccessful = false; + try + { + _storageCollection.SaveData(saveFileDialog.FileName); + saveSuccessful = true; + } + catch (Exception ex) + { + MessageBox.Show("Ошибка при сохранении данных: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + if (saveSuccessful) { MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } @@ -234,7 +244,18 @@ namespace HoistingCrane { if(openFileDialog.ShowDialog() == DialogResult.OK) { - if (_storageCollection.LoadData(openFileDialog.FileName)) + bool loadSuccessful = false; + try + { + _storageCollection.LoadData(openFileDialog.FileName); + loadSuccessful = true; + } + + catch(Exception ex) + { + MessageBox.Show("Ошибка при загрузке данных: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + if(loadSuccessful) { MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); RerfreshListBoxItems();