Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active February 5, 2025 22:18
Show Gist options
  • Select an option

  • Save dcomartin/336504dfd5f8ae26bf975ba2211d596c to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/336504dfd5f8ae26bf975ba2211d596c to your computer and use it in GitHub Desktop.
public class Shipment
{
public int ShipmentId { get; private set; }
public Shipper Shipper { get; private set; }
public ICollection<Stop> Stops { get; } = new List<Stop>();
public Shipment(int shipmentId, Shipper shipper)
{
ShipmentId = shipmentId;
Shipper = shipper;
}
public bool IsComplete()
{
return Stops.All(x => x.Status == StopStatus.Departed);
}
public void Arrive(Guid 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(Guid stopId)
{
var currentStop = Stops.SingleOrDefault(x => x.StopId == stopId);
if (currentStop == null)
{
throw new InvalidOperationException("Stop does not exist.");
}
if (currentStop.StopType == StopType.Pickup)
{
throw new InvalidOperationException("Stop is not a pickup.");
}
currentStop.Depart();
}
public void Deliver(Guid stopId)
{
var currentStop = Stops.SingleOrDefault(x => x.StopId == stopId);
if (currentStop == null)
{
throw new InvalidOperationException("Stop does not exist.");
}
if (currentStop.StopType == StopType.Delivery)
{
throw new InvalidOperationException("Stop is not a delivery.");
}
currentStop.Depart();
}
public void UpdateShipper(Shipper shipper)
{
Shipper = shipper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment