Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created April 28, 2021 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/29d1d299dfebb8eccb2748263417eacd to your computer and use it in GitHub Desktop.
Save dcomartin/29d1d299dfebb8eccb2748263417eacd to your computer and use it in GitHub Desktop.
public class Shipment
{
private readonly IList<Stop> _stops;
public Shipment(IList<Stop> stops)
{
_stops = stops;
}
public void Arrive(int stopId)
{
var currentStop = _stops.SingleOrDefault(x => x.StopId == stopId);
if (currentStop == null)
{
throw new InvalidOperationException("Stop does not exist.");
}
var previousStopsAreNotDeparted = _stops.Any(x => x.Sequence < currentStop.Sequence && x.Status != StopStatus.Departed);
if (previousStopsAreNotDeparted)
{
throw new InvalidOperationException("Previous stops have not departed.");
}
currentStop.Arrive();
}
public void Pickup(int stopId)
{
var currentStop = _stops.SingleOrDefault(x => x.StopId == stopId);
if (currentStop == null)
{
throw new InvalidOperationException("Stop does not exist.");
}
if (currentStop is PickupStop == false)
{
throw new InvalidOperationException("Stop is not a pickup.");
}
currentStop.Depart();
}
public void Deliver(int stopId)
{
var currentStop = _stops.SingleOrDefault(x => x.StopId == stopId);
if (currentStop == null)
{
throw new InvalidOperationException("Stop does not exist.");
}
if (currentStop is DeliveryStop == false)
{
throw new InvalidOperationException("Stop is not a delivery.");
}
currentStop.Depart();
}
public bool IsComplete()
{
return _stops.All(x => x.Status == StopStatus.Departed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment