Skip to content

Instantly share code, notes, and snippets.

@ahmetkucukoglu
Created November 3, 2023 19:32
Show Gist options
  • Save ahmetkucukoglu/e787bd28001cdb77d46ae94b5ec4bd88 to your computer and use it in GitHub Desktop.
Save ahmetkucukoglu/e787bd28001cdb77d46ae94b5ec4bd88 to your computer and use it in GitHub Desktop.
Shipment
public class Shipment
{
public Guid OrderId { get; private set; }
public string FromCountry { get; private set; }
public string ToAddress { get; private set; }
public ShipmentStatuses Status { get; private set; }
public int DeliveryAttempts { get; internal set; }
public Shipment(Guid orderId, string fromCountry, string toAddress)
{
OrderId = orderId;
FromCountry = fromCountry;
ToAddress = toAddress;
Status = ShipmentStatuses.OrderPlaced;
}
public void ToInTransit()
{
if (FromCountry == "TR")
{
Console.WriteLine(GetInvalidMessage(nameof(ToInTransit)));
return;
}
if (Status != ShipmentStatuses.OrderPlaced ||
Status == ShipmentStatuses.InTransit)
{
Console.WriteLine(GetInvalidMessage(nameof(ToInTransit)));
return;
}
Console.WriteLine("The cargo has been loaded onto the ship.");
Status = ShipmentStatuses.InTransit;
}
public void ToInCustoms()
{
if (Status != ShipmentStatuses.InTransit ||
Status == ShipmentStatuses.InCustoms)
{
Console.WriteLine(GetInvalidMessage(nameof(ToInCustoms)));
return;
}
Console.WriteLine("The cargo has arrived at customs.");
Status = ShipmentStatuses.InCustoms;
}
public void ToCustomsCleared()
{
if (Status != ShipmentStatuses.InCustoms ||
Status == ShipmentStatuses.CustomsCleared)
{
Console.WriteLine(GetInvalidMessage(nameof(ToCustomsCleared)));
return;
}
Console.WriteLine("Customs clearance procedures have been completed.");
Status = ShipmentStatuses.CustomsCleared;
}
public void ToOutForDelivery()
{
if (FromCountry != "TR" && Status != ShipmentStatuses.CustomsCleared)
{
Console.WriteLine(GetInvalidMessage(nameof(ToOutForDelivery)));
return;
}
if (Status == ShipmentStatuses.OutForDelivery)
{
Console.WriteLine(GetInvalidMessage(nameof(ToOutForDelivery)));
return;
}
Console.WriteLine("The cargo is out for delivery.");
Status = ShipmentStatuses.OutForDelivery;
}
public void ToDelivered()
{
if (Status != ShipmentStatuses.OutForDelivery ||
Status == ShipmentStatuses.NotDelivered)
{
Console.WriteLine(GetInvalidMessage(nameof(ToDelivered)));
return;
}
Console.WriteLine("The cargo has been delivered.");
Status = ShipmentStatuses.Delivered;
}
public void ToNotDelivered()
{
if (Status != ShipmentStatuses.OutForDelivery ||
Status == ShipmentStatuses.Delivered)
{
Console.WriteLine(GetInvalidMessage(nameof(ToNotDelivered)));
return;
}
if (DeliveryAttempts < 1)
{
DeliveryAttempts++;
Console.WriteLine("The cargo could not be delivered. We will attempt delivery again.");
return;
}
Console.WriteLine("The cargo could not be delivered. The return process has been initiated.");
Status = ShipmentStatuses.NotDelivered;
}
private string GetInvalidMessage(string newStatus) =>
$"{newStatus} cannot be performed in {Status}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment