готовая 8 лаб работа

This commit is contained in:
cyxaruk 2024-05-20 13:12:41 +04:00
parent e8b3ec816b
commit 642e19f10d
8 changed files with 47 additions and 41 deletions

View File

@ -1,6 +1,5 @@
using ProjectAircraftCarrier_.Exceptions;
using ProjectGasMachine.Exceptions;
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;

View File

@ -1,12 +1,11 @@
using ProjectGasMachine.CollectionGenericObjects;
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.Drawnings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectGasMachine;
namespace ProjectGasMachine.CollectionGenericObjects;
/// <summary>
/// Интерфейс описания действий для набора хранимых объектов
@ -31,7 +30,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="obj">Добавляемый объект</param>
/// <param name="comparer">Сравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, IEqualityComparer<DrawningMachine?>? comparer = null);
int Insert(T obj, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Добавление объекта в коллекцию на конкретную позицию
@ -40,7 +39,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="position">Позиция</param>
/// <param name="comparer">Сравнение двух объектов</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
int Insert(T obj, int position, IEqualityComparer<DrawningMachine?>? comparer = null);
int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции

View File

@ -1,7 +1,7 @@

using ProjectAircraftCarrier_.Exceptions;
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.Exceptions;
using System.Linq;
namespace ProjectGasMachine.CollectionGenericObjects;
@ -48,14 +48,14 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position];
}
public int Insert(T obj, IEqualityComparer<DrawningMachine?>? comparer = null)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если переполнение
for (int i = 0; i < Count; i++)
if (comparer != null)
{
if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine)))
if (_collection.Contains(obj, comparer))
{
throw new ObjectAlreadyInCollectionException(i);
throw new ObjectAlreadyInCollectionException();
}
}
if (Count == _maxCount)
@ -67,15 +67,15 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection.Count;
}
public int Insert(T obj, int position, IEqualityComparer<DrawningMachine?>? comparer = null)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если выход за границы списка
// TODO выброс ошибки, если переполнение
for (int i = 0; i < Count; i++)
if (comparer != null)
{
if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine)))
if (_collection.Contains(obj, comparer))
{
throw new ObjectAlreadyInCollectionException(i);
throw new ObjectAlreadyInCollectionException();
}
}
if (position < 0 || position > Count)

View File

@ -1,6 +1,4 @@

using ProjectAircraftCarrier_.Exceptions;
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.Exceptions;
using System.Linq;
@ -71,18 +69,21 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
public int Insert(T obj, IEqualityComparer<DrawningMachine?>? comparer = null)
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если переполнение
for (int i = 0; i < Count; i++)
if (comparer != null)
{
if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine)))
foreach (T? item in _collection)
{
throw new ObjectAlreadyInCollectionException(i);
if ((comparer as IEqualityComparer<DrawningMachine>).Equals(obj as DrawningMachine, item as DrawningMachine))
{
throw new ObjectAlreadyInCollectionException();
}
}
}
for (int i = 0; i < Count; i++)
{
if (_collection[i] == null)
@ -95,15 +96,18 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position, IEqualityComparer<DrawningMachine?>? comparer = null)
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
// TODO выброс ошибки, если выход за границы массива
// TODO выброс ошибки, если переполнение
for (int i = 0; i < Count; i++)
if (comparer != null)
{
if (comparer.Equals((_collection[i] as DrawningMachine), (obj as DrawningMachine)))
foreach (T? item in _collection)
{
throw new ObjectAlreadyInCollectionException(i);
if ((comparer as IEqualityComparer<DrawningMachine>).Equals(obj as DrawningMachine, item as DrawningMachine))
{
throw new ObjectAlreadyInCollectionException();
}
}
}

View File

@ -1,5 +1,4 @@
using ProjectAircraftCarrier_.Exceptions;
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.Exceptions;
namespace ProjectGasMachine.CollectionGenericObjects;

View File

@ -23,10 +23,6 @@ public class DrawningMachineCompareByColor : IComparer<DrawningMachine?>
{
return -1;
}
if (x.GetType().Name != y.GetType().Name)
{
return y.GetType().Name.CompareTo(x.GetType().Name);
}
var bodyColorCompare = y.EntityGas.BodyColor.Name.CompareTo(x.EntityGas.BodyColor.Name);
if (bodyColorCompare != 0)
{

View File

@ -1,14 +1,14 @@
using System.Runtime.Serialization;
namespace ProjectAircraftCarrier_.Exceptions;
namespace ProjectGasMachine.Exceptions;
/// <summary>
/// Класс, описывающий ошибку переполнения коллекции
/// </summary>
[Serializable]
internal class ObjectAlreadyInCollectionException : ApplicationException
public class ObjectAlreadyInCollectionException : ApplicationException
{
public ObjectAlreadyInCollectionException(int index) : base("Такой объект уже присутствует в коллекции. Позиция " + index) { }
public ObjectAlreadyInCollectionException(int count) : base("Такой объект уже присутствует в коллекции. Позиция " + count) { }
public ObjectAlreadyInCollectionException() : base() { }

View File

@ -51,18 +51,22 @@ namespace ProjectGasMachine
/// <param name="machine"></param>
private void SetMachine(DrawningMachine? machine)
{
if (_company == null || machine == null)
{
return;
}
try
{
if (_company == null || machine == null)
{
return;
}
if (_company + machine != -1)
{
MessageBox.Show("Объект добавлен");
_logger.LogInformation($"Добавлен объект {machine.GetDataForSave()}");
pictureBox.Image = _company.Show();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
catch (CollectionOverflowException ex)
@ -70,6 +74,11 @@ namespace ProjectGasMachine
MessageBox.Show(ex.Message);
_logger.LogWarning($"Ошибка: {ex.Message}");
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Ошибка: {ex.Message}");
}
}