Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions Examples/E1/CargoService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
namespace Examples.E1;

public class CargoService
{
public ICargo BookProduct(byte type, string? origin = null, string? destination = null)
{
CargoFactory? factory = null;
if (type == (byte)CargoType.Air)
{
factory = new AirFactory();
}
if (type == (byte)CargoType.Ship)
{
if (string.IsNullOrWhiteSpace(origin))
throw new ArgumentException("Origin should not be null or empty.", nameof(origin));

if (string.IsNullOrWhiteSpace(destination))
throw new ArgumentException("Destination should not be null or empty.", nameof(destination));
factory = new ShipFactory(origin, destination);

}
if (type == (byte)CargoType.Air)
{
factory = new TrainFactory();

}
if (factory is null)
throw new Exception("cargotype is wrong please enter a new type");
return factory.Create(origin, destination);
}
}

public abstract class CargoFactory
{
public abstract ICargo Create(string? origin = null, string? destination = null);
}

public class TrainFactory : CargoFactory
{
public override ICargo Create(string? origin = null, string? destination = null)
{
var cargo = new Train();
TrainMethod();
return cargo;
}

private void TrainMethod() => Console.WriteLine("train is new");
}

public class ShipFactory : CargoFactory
{
public ShipFactory(string origin, string destination)
{
Origin = origin;
Destination = destination;
}

public string Origin { get; set; }
public string Destination { get; set; }
public override ICargo Create(string? origin, string? destination)
{
return new Ship(origin, destination);
}
}

public class AirFactory : CargoFactory
{
private static readonly Lazy<Air> _air = new Lazy<Air>(() => new Air());

public override ICargo Create(string? origin, string? destination)
{
return _air.Value;
}
}

public class Train : ICargo
{
public decimal SetPayment()
{
return 500;
}
}

public class Ship : ICargo
{
public Ship(string origin, string destination)
{
Origin = origin;
Destination = destination;
}

public string Origin { get; set; }
public string Destination { get; set; }
public decimal SetPayment()
{
return 2000;
}
}

public class Air : ICargo
{
public decimal SetPayment()
{
return 1000;
}
}

public interface ICargo
{
public decimal SetPayment();
}
public enum CargoType
{
NotSet = 0,
Air = 1,
Ship = 2,
Train = 3,
}

103 changes: 101 additions & 2 deletions Payment/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,101 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using Payment.ThirdParty;

namespace Payment;
public interface IPaymentProcessor
{
decimal Amount;
void CalculatePayment(decimal amount);
void SendMessage();
}
public class CreditCardProcessor : IPaymentProcessor
{
public decimal Amount { get; set; }
public void CalculatePayment(decimal amount)
{
Amount= amount + (0 / 02 * amount)
}
public void SendMessage()
{
Console.WriteLine("Applying 2% fee for credit card payment.");
Console.WriteLine($"Processing credit card payment of $" +{ Amount});
}
}

public class PayPalProcessor : IPaymentProcessor
{
public decimal Amount { get; set; }
public void CalculatePayment(decimal amount)
{
Amount= amount;
}
public void SendMessage()
{
Console.WriteLine($"Processing PayPal payment of $" +{ Amount});
}
}

public class NullPaymentProcessor : IPaymentProcessor
{
public decimal Amount { get; set; }
public void CalculatePayment(decimal amount)
{
Amount = amount;
}
public void SendMessage()
{
Console.WriteLine("No adjustment for the selected payment method.");
Console.WriteLine($"Payment method not supported. Unable to process $" +{ Amount});
}
}
public class CryptoCurrencyProcessorAdopter: IPaymentProcessor
{
public CryptoCurrencyProcessor _cryptoCurrencyProcessor = new CryptoCurrencyProcessor();
public decimal Amount { get; set; }
public void CalculatePayment(decimal amount)
{
Amount = _cryptoCurrencyProcessor.SetPaymentAmount(amount);
}
public void SendMessage()
{
Console.WriteLine("No adjustment for the selected payment method.");
Console.WriteLine($"Processing cryptocurrency payment of $" +{ Amount}+" via third-party library");
}
}

public static class PaymentProcessorFactory
{
public static IPaymentProcessor GetPaymentProcessor(string paymentMethod)
{
return paymentMethod.ToLower() switch
{
"creditcard" => new CreditCardPaymentProcessor(),
"paypal" => new PayPalPaymentProcessor(),
"cryptocurrency" => new CryptoCurrencyPaymentProcessor(),
_ => new NullPaymentProcessor(),
};
}
}
class Program
{
static void Main(string[] args)
{
ProcessOrder("CreditCard", 100);
ProcessOrder("CryptoCurrency", 200);
ProcessOrder("Unsupported", 150);
}

static void ProcessOrder(string paymentMethod, decimal amount)
{
var processor = PaymentProcessorFactory.GetPaymentProcessor(paymentMethod);
processor.CalculatePayment(amount);
processor.SendMessage();
}
}
namespace ThirdParty;
public class CryptoCurrencyProcessor
{
public decimal SetPaymentAmount(decimal amount)
{
return (decimal)amount;
}
}