Лабораторная работа №8

This commit is contained in:
xom9kxom9k 2024-05-14 19:49:41 +04:00
parent 2258c0b47c
commit a2301293b2
6 changed files with 79 additions and 62 deletions

View File

@ -61,7 +61,7 @@ public abstract class AbstractCompany
/// <returns></returns>
public static int operator +(AbstractCompany company, DrawningArmoredCar armoredCar)
{
return company._collection?.Insert(armoredCar, new DrawningArmoredCarEqutables()) ?? throw new DrawingEquitablesException();
return company._collection.Insert(armoredCar, new DrawningArmoredCarEqutables());
}

View File

@ -57,12 +57,9 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (comparer != null)
{
for (int i = 0; i < Count; i++)
if (_collection.Contains(obj, comparer))
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
throw new ObjectIsEqualException(obj);
}
}
_collection.Add(obj);
@ -71,17 +68,13 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
if (Count == _maxCount) throw new CollectionOverflowException();
if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
if (comparer != null)
{
for (int i = 0; i < Count; i++)
if (_collection.Contains(obj, comparer))
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
throw new ObjectIsEqualException(obj);
}
}
_collection.Insert(position, obj);

View File

@ -62,15 +62,14 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
{
if (comparer != null)
{
for (int i = 0; i < Count; i++)
foreach (T? i in _collection)
{
if (comparer.Equals(_collection[i], obj))
if (comparer.Equals(i, obj))
{
throw new CollectionInsertException(obj);
throw new ObjectIsEqualException(i);
}
}
}
for (int i = 0; i < Count; i++)
{
if (_collection[i] == null)
@ -79,22 +78,20 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return i;
}
}
throw new CollectionOverflowException(Count);
throw new CollectionOverflowException();
}
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position);
if (comparer != null)
{
for (int i = 0; i < Count; i++)
foreach (T? i in _collection)
{
if (comparer.Equals(_collection[i], obj))
if (comparer.Equals(i, obj))
{
throw new CollectionInsertException(obj);
throw new ObjectIsEqualException(i);
}
}
}
@ -104,27 +101,27 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
_collection[position] = obj;
return position;
}
int index = position + 1;
while (index < _collection.Length)
int temp = position + 1;
while (temp < Count)
{
if (_collection[index] == null)
if (_collection[temp] == null)
{
_collection[index] = obj;
return index;
_collection[temp] = obj;
return temp;
}
++index;
++temp;
}
index = position - 1;
while (index >= 0)
temp = position - 1;
while (temp >= 0)
{
if (_collection[index] == null)
if (_collection[temp] == null)
{
_collection[index] = obj;
return index;
_collection[temp] = obj;
return temp;
}
--index;
--temp;
}
throw new CollectionOverflowException(Count);
throw new CollectionOverflowException();
}
public T? Remove(int position)
@ -145,13 +142,8 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
void ICollectionGenericObjects<T>.CollectionSort(IComparer<T?> comparer)
{
List<T?> lst = [.._collection];
lst.Sort(comparer.Compare);
for (int i = 0; i < _collection.Length; ++i)
{
_collection[i] = lst[i];
}
Array.Sort(_collection, comparer);
}
}

View File

@ -3,6 +3,7 @@ using AntiAircraftGun.CollectionGenereticObject;
using AntiAircraftGun.Drawnings;
using AntiAircraftGun.Exceptions;
using System.Text;
using System.Xml.Linq;
namespace AntiAircraftGun.CollectionGenericObjects;
@ -51,15 +52,14 @@ public class StorageCollection<T>
/// </summary>
/// <param name="name">Название коллекции</param>
/// <param name="collectionType">тип коллекции</param>
public void AddCollection(CollectionInfo collectionInfo)
public void AddCollection(String name, CollectionType collectionType)
{
if (_storages.ContainsKey(collectionInfo)) throw new CollectionAlreadyExistsException(collectionInfo);
if (collectionInfo.CollectionType == CollectionType.None)
throw new CollectionTypeException("Пустой тип коллекции");
if (collectionInfo.CollectionType == CollectionType.Massive)
CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
if (_storages.ContainsKey(collectionInfo)) return;
if (collectionType == CollectionType.None) return;
else if (collectionType == CollectionType.Massive)
_storages[collectionInfo] = new MassiveGenericObjects<T>();
else if (collectionInfo.CollectionType == CollectionType.List)
else if (collectionType == CollectionType.List)
_storages[collectionInfo] = new ListGenericObjects<T>();
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace AntiAircraftGun.Exceptions;
/// <summary>
/// Класс, описывающий ошибку переполнения коллекции
/// </summary>
[Serializable]
public class ObjectIsEqualException : ApplicationException
{
public ObjectIsEqualException(object i) : base("В коллекции уже есть такой элемент " + i) { }
public ObjectIsEqualException() : base() { }
public ObjectIsEqualException(string message) : base(message) { }
public ObjectIsEqualException(string message, Exception exception) : base(message, exception)
{ }
protected ObjectIsEqualException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

View File

@ -69,20 +69,28 @@ public partial class FormArmoredCarCollection : Form
{
return;
}
if (_company + armoredCar != -1)
if (_company + armoredCar < 32)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
_logger.LogInformation("Добавлен объект: {0}", armoredCar.GetDataForSave());
_logger.LogInformation("Добавлен объект: " + armoredCar.GetDataForSave());
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
catch (ObjectNotFoundException) { }
catch (CollectionOverflowException ex)
{
MessageBox.Show("Не удалось добавить объект");
_logger.LogError("Ошибка: {Message}", ex.Message);
}
catch (ObjectIsEqualException ex)
{
MessageBox.Show("Не удалось добавить объект");
_logger.LogError("Ошибка: {Message}", ex.Message);
}
}
@ -192,10 +200,10 @@ public partial class FormArmoredCarCollection : Form
listBoxCollection.Items.Clear();
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
{
string? colName = _storageCollection.Keys?[i];
if (!string.IsNullOrEmpty(colName))
CollectionInfo? col = _storageCollection.Keys?[i];
if (!col!.IsEmpty())
{
listBoxCollection.Items.Add(colName);
listBoxCollection.Items.Add(col);
}
}
@ -245,14 +253,15 @@ public partial class FormArmoredCarCollection : Form
MessageBox.Show("Коллекция не выбрана");
return;
}
try
{
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RerfreshListBoxItems();
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(listBoxCollection.SelectedItem.ToString()!);
_storageCollection.DelCollection(collectionInfo!);
_logger.LogInformation("Коллекция: " + listBoxCollection.SelectedItem.ToString() + " удалена");
}
catch (Exception ex)
@ -273,7 +282,9 @@ public partial class FormArmoredCarCollection : Form
return;
}
ICollectionGenericObjects<DrawningArmoredCar>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
ICollectionGenericObjects<DrawningArmoredCar>? collection = _storageCollection[
CollectionInfo.GetCollectionInfo(listBoxCollection.SelectedItem.ToString()!) ??
new CollectionInfo("", CollectionType.None, "")];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");