Skip to content

Instantly share code, notes, and snippets.

@bilal68
Forked from KMetokhir/PaymentSystem
Created May 6, 2025 11:21
Show Gist options
  • Select an option

  • Save bilal68/ae45aa4b0ae2cc53b1d248276fe0db24 to your computer and use it in GitHub Desktop.

Select an option

Save bilal68/ae45aa4b0ae2cc53b1d248276fe0db24 to your computer and use it in GitHub Desktop.
PaymentSystem
namespace IMJunior
{
class Program
{
static void Main(string[] args)
{
OrderForm orderForm = new OrderForm();
PaymentSystemFactory factory = new PaymentSystemFactory();
PaymentHandler paymentHandler = new PaymentHandler(factory);
string systemId = orderForm.ShowForm();
if (systemId == "QIWI")
Console.WriteLine("Перевод на страницу QIWI...");
else if (systemId == "WebMoney")
Console.WriteLine("Вызов API WebMoney...");
else if (systemId == "Card")
Console.WriteLine("Вызов API банка эмитера карты Card...");
paymentHandler.ShowPaymentResult(systemId);
}
}
public class OrderForm
{
public string ShowForm()
{
Console.WriteLine("Мы принимаем: QIWI, WebMoney, Card");
//симуляция веб интерфейса
Console.WriteLine("Какое системой вы хотите совершить оплату?");
return Console.ReadLine();
}
}
public class PaymentHandler
{
private readonly PaymentSystemFactory _paymentFactory;
public PaymentHandler(PaymentSystemFactory paymentfactory)
{
_paymentFactory = paymentfactory ?? throw new ArgumentNullException(nameof(paymentfactory));
}
public void ShowPaymentResult(string systemId)
{
Console.WriteLine($"Вы оплатили с помощью {systemId}");
IPaymentSystem paymentSystem = _paymentFactory.GetPaymentSystem(systemId);
paymentSystem.ProcessRequest();
Console.WriteLine("Оплата прошла успешно!");
}
}
public interface IPaymentSystem
{
public void ProcessRequest();
}
public class Qiwi : IPaymentSystem
{
public void ProcessRequest()
{
Console.WriteLine("Проверка платежа через QIWI...");
}
}
public class WebMoney : IPaymentSystem
{
public void ProcessRequest()
{
Console.WriteLine("Проверка платежа через WebMoney...");
}
}
public class Card : IPaymentSystem
{
public void ProcessRequest()
{
Console.WriteLine("Проверка платежа через Card...");
}
}
public enum PaymentType
{
QIWI,
WebMoney,
Card
}
public class PaymentSystemFactory
{
private readonly Dictionary<PaymentType, IPaymentSystem> _paymentMap;
public PaymentSystemFactory()
{
_paymentMap = new Dictionary<PaymentType, IPaymentSystem>();
_paymentMap.Add(PaymentType.QIWI, new Qiwi());
_paymentMap.Add(PaymentType.WebMoney, new WebMoney());
_paymentMap.Add(PaymentType.Card, new Card());
}
public IPaymentSystem GetPaymentSystem(string typeName)
{
if (Enum.TryParse(typeName, out PaymentType paymentType))
if (Enum.IsDefined(typeof(PaymentType), paymentType))
if (_paymentMap.ContainsKey(paymentType))
return _paymentMap[paymentType];
throw new ArgumentOutOfRangeException(typeName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment