Третий этап

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 namespace Sailboat.Exceptions
{ {
[Serializable]
internal class BoatNotFoundException : ApplicationException internal class BoatNotFoundException : ApplicationException
{ {
public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }

View File

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

View File

@ -3,7 +3,6 @@ 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 Sailboat.DrawingObjects; using Sailboat.DrawingObjects;
using Sailboat.MovementStrategy; using Sailboat.MovementStrategy;
@ -110,7 +109,7 @@ namespace Sailboat.Generics
foreach (DrawingBoat? elem in record.Value.GetBoats) foreach (DrawingBoat? elem in record.Value.GetBoats)
{ {
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
} }
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
} }
if (data.Length == 0) if (data.Length == 0)
@ -119,7 +118,7 @@ namespace Sailboat.Generics
} }
using (StreamWriter writer = new StreamWriter(filename)) using (StreamWriter writer = new StreamWriter(filename))
{ {
writer.Write($"PlaneStorage{Environment.NewLine}{data}"); writer.Write($"SailboatStorage{Environment.NewLine}{data}");
} }
return true; return true;
} }
@ -164,23 +163,23 @@ namespace Sailboat.Generics
if (record.Length != 2) if (record.Length != 2)
{ {
continue; continue;
} }
BoatsGenericCollection<DrawingBoat, DrawingObjectBoat> collection = new(_pictureWidth, _pictureHeight); BoatsGenericCollection<DrawingBoat, DrawingObjectBoat> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{
DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight);
if (boat != null)
{ {
if (!(collection + boat)) DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight);
if (boat != null)
{ {
throw new Exception("Ошибка добавления в коллекцию"); if (!(collection + boat))
{
throw new Exception("Ошибка добавления в коллекцию");
}
} }
} }
_boatStorages.Add(record[0], collection);
} }
_boatStorages.Add(record[0], collection);
}
} }
} }
} }

View File

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

View File

@ -9,8 +9,14 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" 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.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>
<ItemGroup> <ItemGroup>
@ -27,5 +33,5 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,10 @@
using System; using System;
using Sailboat.Exceptions;
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 Sailboat.Generics namespace Sailboat.Generics
{ {
@ -11,7 +13,7 @@ namespace Sailboat.Generics
/// <summary> /// <summary>
/// Список объектов, которые храним /// Список объектов, которые храним
/// </summary> /// </summary>
private readonly List <T?> _places; private readonly List<T?> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в массиве
/// </summary> /// </summary>
@ -36,9 +38,9 @@ namespace Sailboat.Generics
/// <returns></returns> /// <returns></returns>
public bool Insert(T boat) public bool Insert(T boat)
{ {
if (_places.Count == _maxCount) if (_places.Count == _maxCount)
{ {
return false; throw new StorageOverflowException(_maxCount);
} }
Insert(boat, 0); Insert(boat, 0);
return true; return true;
@ -51,9 +53,12 @@ namespace Sailboat.Generics
/// <returns></returns> /// <returns></returns>
public bool Insert(T boat, int position) public bool Insert(T boat, int position)
{ {
if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) { if (_places.Count == _maxCount)
return false; throw new StorageOverflowException(_maxCount);
} if (!(position >= 0 && position <= Count))
{
return false;
}
_places.Insert(position, boat); _places.Insert(position, boat);
return true; return true;
} }
@ -66,7 +71,7 @@ namespace Sailboat.Generics
{ {
if (position < 0 || position >= Count) if (position < 0 || position >= Count)
{ {
return false; throw new BoatNotFoundException(position);
} }
_places.RemoveAt(position); _places.RemoveAt(position);
return true; return true;
@ -80,15 +85,17 @@ namespace Sailboat.Generics
{ {
get get
{ {
if (position < 0 || position >= Count) { if (position < 0 || position >= Count)
return null; {
return null;
} }
return _places[position]; return _places[position];
} }
set set
{ {
if (!(position >= 0 && position < Count && _places.Count < _maxCount)) { if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return; {
return;
} }
_places.Insert(position, value); _places.Insert(position, value);
return; return;
@ -110,4 +117,4 @@ namespace Sailboat.Generics
} }
} }
} }
} }

View File

@ -1,113 +1,18 @@
using Sailboat.Exceptions; using System;
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;
namespace Sailboat.Generics namespace Sailboat.MovementStrategy
{ {
/// <summary>
internal class SetGeneric<T> where T : class /// Статус выполнения операции перемещения
/// </summary>
public enum Status
{ {
/// <summary> NotInit,
/// Список объектов, которые храним InProgress,
/// </summary> Finish
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)
{
_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;
}
}
}
} }
} }