Dodano Stepa na wysypywanie smieci

This commit is contained in:
Bartosz Chyzy 2019-03-13 16:55:34 +01:00
parent e9476f7b2d
commit e61eb1a87c
8 changed files with 86 additions and 20 deletions

View File

@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions.cs" />
<Compile Include="Interfaces\Enums\Directions.cs" />
<Compile Include="Interfaces\GarbageCollector\AGarbageCollector.cs" />
<Compile Include="Interfaces\GarbageCollector\IGarbageCollector.cs" />
@ -52,11 +53,12 @@
<Compile Include="Interfaces\RoutePlanningEngine\IRoutePlanningEngine.cs" />
<Compile Include="Interfaces\TrashCans\AGarbageCollectorContainer.cs" />
<Compile Include="Interfaces\TrashCans\ATrashCan.cs" />
<Compile Include="Interfaces\TrashCans\IStep.cs" />
<Compile Include="Interfaces\IStep.cs" />
<Compile Include="Models\Coords.cs" />
<Compile Include="Models\Map.cs" />
<Compile Include="Models\Steps\CollectStep.cs" />
<Compile Include="Models\Steps\MoveStep.cs" />
<Compile Include="Models\Steps\SpillStep.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CzokoŚmieciarka.DataModels
{
public class WrongPositionException : Exception
{
public WrongPositionException(string message) : base(message)
{
}
public WrongPositionException(string message,Exception innerException) : base(message,innerException)
{
}
}
public class TrashContainerNotFound : Exception
{
public TrashContainerNotFound(string message) : base(message)
{
}
public TrashContainerNotFound(string message, Exception innerException) : base(message, innerException)
{
}
}
}

View File

@ -0,0 +1,7 @@
namespace CzokoŚmieciarka.DataModels.Interfaces
{
public interface IStep
{
void Invoke();
}
}

View File

@ -15,6 +15,6 @@ namespace CzokoŚmieciarka.DataModels.Interfaces
this.Localization = localization;
}
Coords Localization { get; }
public Coords Localization { get; }
}
}

View File

@ -21,7 +21,7 @@ namespace CzokoŚmieciarka.DataModels.Interfaces.TrashCans
get { return Garbage.Volume / this.MaxVolume; }
}
public virtual bool AddGarbage(AGarbage garbage)
public virtual bool AddGarbage(IGarbage garbage)
{
if (this.TypeOfGarbage != garbage.TypeOfGarbage)
throw new Exception("You cannot add up different type garbage!");

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CzokoŚmieciarka.DataModels.Interfaces.TrashCans
{
public interface IStep
{
void Invoke();
}
}

View File

@ -24,15 +24,15 @@ namespace CzokoŚmieciarka.DataModels.Models.Steps
public void Invoke()
{
if(_garbageCollector.Position != _garbageLocalization.Coords)
throw new Exception("Śmieciarka nie jest w miejscu odebrania śmieci");
throw new WrongPositionException("Śmieciarka nie jest w miejscu oderbania śmieci");
var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage == _typeOfGarbage);
var garbage = trashCans.Select(t => t.TakeGarbage()).Aggregate((a,b)=>a+b);
if (!_garbageCollector.TrashContainers.Any(c => c.TypeOfGarbage == _typeOfGarbage))
throw new Exception("Ta śmieciarka nie może zebrać żądanych śmieci.");
if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage != _typeOfGarbage))
throw new TrashContainerNotFound($"Nie znaleziono kontenera na {_typeOfGarbage.GarbageType}.");
_garbageCollector.TrashContainers.FirstOrDefault(t => t.TypeOfGarbage == _typeOfGarbage).AddGarbage(garbage);
_garbageCollector.TrashContainers.First(t => t.TypeOfGarbage == _typeOfGarbage).AddGarbage(garbage);
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CzokoŚmieciarka.DataModels.Interfaces;
namespace CzokoŚmieciarka.DataModels.Models.Steps
{
public class SpillStep : IStep
{
public SpillStep(IGarbageCollector garbageCollector, ADump dump, ITypeOfGarbage typeOfGarbage)
{
this._garbageCollector = garbageCollector;
this._dump = dump;
this._typeOfGarbage = typeOfGarbage;
}
private IGarbageCollector _garbageCollector;
private ADump _dump;
private ITypeOfGarbage _typeOfGarbage;
public void Invoke()
{
if(_garbageCollector.Position != _dump.Localization)
throw new WrongPositionException("Śmieciarka nie na terenie podanego wyspiska");
if(_dump.TypeOfGarbage != _typeOfGarbage)
throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage.GarbageType}");
if(_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage != _typeOfGarbage))
throw new TrashContainerNotFound($"Smieciarka nie ma pojemnika na {_typeOfGarbage.GarbageType}!");
var garbage = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage == _typeOfGarbage)
.Select(t => t.TakeGarbage())
.Aggregate((a, b) => a + b);
_dump.AddGarbage(garbage);
}
}
}