Третий этап

This commit is contained in:
Максим Егоров 2024-09-05 20:03:59 +04:00
parent 1dda5914de
commit 8b8c908d41
7 changed files with 58 additions and 135 deletions

View File

@ -7,6 +7,7 @@ using System.Runtime.Serialization;
namespace Sailboat.Exceptions
{
[Serializable]
internal class BoatNotFoundException : ApplicationException
{
public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }

View File

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

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
using Sailboat.MovementStrategy;
@ -119,7 +118,7 @@ namespace Sailboat.Generics
}
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write($"PlaneStorage{Environment.NewLine}{data}");
writer.Write($"SailboatStorage{Environment.NewLine}{data}");
}
return true;
}

View File

@ -1,5 +1,9 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Serilog;
namespace Sailboat
{
@ -25,7 +29,7 @@ namespace Sailboat
private static void ConfigureServices(ServiceCollection services)
{
object value = services.AddSingleton<FormBoatCollection>().AddLogging(option =>
services.AddSingleton<FormBoatCollection>().AddLogging(option =>
{
string[] path = Directory.GetCurrentDirectory().Split('\\');
string pathNeed = "";
@ -38,6 +42,7 @@ namespace Sailboat
option.SetMinimumLevel(LogLevel.Information);
option.AddSerilog(logger);
option.AddNLog("nlog.config");
});
}

View File

@ -9,8 +9,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.7" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -1,8 +1,10 @@
using System;
using Sailboat.Exceptions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sailboat.Generics
{
@ -38,7 +40,7 @@ namespace Sailboat.Generics
{
if (_places.Count == _maxCount)
{
return false;
throw new StorageOverflowException(_maxCount);
}
Insert(boat, 0);
return true;
@ -51,7 +53,10 @@ namespace Sailboat.Generics
/// <returns></returns>
public bool Insert(T boat, 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;
}
_places.Insert(position, boat);
@ -66,7 +71,7 @@ namespace Sailboat.Generics
{
if (position < 0 || position >= Count)
{
return false;
throw new BoatNotFoundException(position);
}
_places.RemoveAt(position);
return true;
@ -80,14 +85,16 @@ namespace Sailboat.Generics
{
get
{
if (position < 0 || position >= Count) {
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount)) {
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
_places.Insert(position, value);

View File

@ -1,113 +1,18 @@
using Sailboat.Exceptions;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.Generics
{
internal class SetGeneric<T> where T : class
namespace Sailboat.MovementStrategy
{
/// <summary>
/// Список объектов, которые храним
/// Статус выполнения операции перемещения
/// </summary>
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
public enum Status
{
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <returns></returns>
public bool Insert(T boat)
{
return Insert(boat, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T boat, int position)
{
if (position < 0 || position >= _maxCount)
throw new BoatNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
_places.Insert(0, boat);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position > _maxCount || position >= Count)
throw new BoatNotFoundException(position);
_places.RemoveAt(position);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
get
{
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetBoats(int? maxBoats = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxBoats.HasValue && i == maxBoats.Value)
{
yield break;
}
}
}
NotInit,
InProgress,
Finish
}
}