-
-
Save dcomartin/29d1d299dfebb8eccb2748263417eacd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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