VodVas

OCP.cs

Dec 18th, 2024 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.49 KB | Spirit | 0 0
  1. namespace OCP
  2. {
  3.     internal class Program
  4.     {
  5.         private static void Main()
  6.         {
  7.             PaymentSystemFactory paymentSystemFactory = new PaymentSystemFactory();
  8.  
  9.             var orderForm = new OrderForm();
  10.  
  11.             try
  12.             {
  13.                 IEnumerable<string> availableSystems = paymentSystemFactory.AvailablePaymentSystemIds;
  14.  
  15.                 string systemId = orderForm.ShowForm(availableSystems);
  16.  
  17.                 Func<IPaymentSystem> paymentSystemFactoryMethod = paymentSystemFactory.GetPaymentSystemFactory(systemId);
  18.  
  19.                 var paymentHandler = new PaymentService(paymentSystemFactoryMethod);
  20.  
  21.                 paymentHandler.ProcessPayment();
  22.                 paymentHandler.ShowPaymentResult();
  23.             }
  24.             catch (Exception exception)
  25.             {
  26.                 Console.WriteLine($"Ошибка: {exception.Message}");
  27.             }
  28.         }
  29.     }
  30.  
  31.     public class PaymentSystemFactory
  32.     {
  33.         private readonly Dictionary<string, Func<IPaymentSystem>> _paymentSystemFactories;
  34.  
  35.         public PaymentSystemFactory()
  36.         {
  37.             _paymentSystemFactories = new Dictionary<string, Func<IPaymentSystem>>();
  38.  
  39.             InitializePaymentSystemFactories();
  40.         }
  41.  
  42.         public IEnumerable<string> AvailablePaymentSystemIds
  43.         {
  44.             get { return _paymentSystemFactories.Keys; }
  45.         }
  46.  
  47.         public Func<IPaymentSystem> GetPaymentSystemFactory(string systemId)
  48.         {
  49.             if (string.IsNullOrWhiteSpace(systemId))
  50.             {
  51.                 throw new ArgumentException("Идентификатор платежной системы не может быть пустым или содержать только пробелы.", nameof(systemId));
  52.             }
  53.  
  54.             if (_paymentSystemFactories.TryGetValue(systemId, out Func<IPaymentSystem> factory))
  55.             {
  56.                 return factory;
  57.             }
  58.  
  59.             throw new NotSupportedException($"Платежная система с идентификатором '{systemId}' не поддерживается.");
  60.         }
  61.  
  62.         private void InitializePaymentSystemFactories()
  63.         {
  64.             _paymentSystemFactories.Add(PaymentSystemIdentifiers.Qiwi, () => new QiwiPaymentSystem());
  65.             _paymentSystemFactories.Add(PaymentSystemIdentifiers.WebMoney, () => new WebMoneyPaymentSystem());
  66.             _paymentSystemFactories.Add(PaymentSystemIdentifiers.Card, () => new CardPaymentSystem());
  67.             _paymentSystemFactories.Add(PaymentSystemIdentifiers.PayPal, () => new PayPalPaymentSystem());
  68.         }
  69.     }
  70.  
  71.     public static class PaymentSystemIdentifiers
  72.     {
  73.         public const string Qiwi = "qiwi";
  74.         public const string WebMoney = "webmoney";
  75.         public const string Card = "card";
  76.         public const string PayPal = "paypal";
  77.     }
  78.  
  79.     public interface IPaymentSystem
  80.     {
  81.         public string Id { get; }
  82.         public void ProcessPayment();
  83.         public void CheckPayment();
  84.     }
  85.  
  86.     public class QiwiPaymentSystem : IPaymentSystem
  87.     {
  88.         public string Id => PaymentSystemIdentifiers.Qiwi;
  89.  
  90.         public void ProcessPayment()
  91.         {
  92.             Console.WriteLine("Перевод на страницу QIWI...");
  93.         }
  94.  
  95.         public void CheckPayment()
  96.         {
  97.             Console.WriteLine("Проверка платежа через QIWI...");
  98.         }
  99.     }
  100.  
  101.     public class WebMoneyPaymentSystem : IPaymentSystem
  102.     {
  103.         public string Id => PaymentSystemIdentifiers.WebMoney;
  104.  
  105.         public void ProcessPayment()
  106.         {
  107.             Console.WriteLine("Вызов API WebMoney...");
  108.         }
  109.  
  110.         public void CheckPayment()
  111.         {
  112.             Console.WriteLine("Проверка платежа через WebMoney...");
  113.         }
  114.     }
  115.  
  116.     public class CardPaymentSystem : IPaymentSystem
  117.     {
  118.         public string Id => PaymentSystemIdentifiers.Card;
  119.  
  120.         public void ProcessPayment()
  121.         {
  122.             Console.WriteLine("Вызов API банка эмитента карты...");
  123.         }
  124.  
  125.         public void CheckPayment()
  126.         {
  127.             Console.WriteLine("Проверка платежа через карту...");
  128.         }
  129.     }
  130.  
  131.     public class PayPalPaymentSystem : IPaymentSystem
  132.     {
  133.         public string Id => PaymentSystemIdentifiers.PayPal;
  134.  
  135.         public void CheckPayment()
  136.         {
  137.             Console.WriteLine("Перевод на страницу PayPal...");
  138.         }
  139.  
  140.         public void ProcessPayment()
  141.         {
  142.             Console.WriteLine("Проверка платежа через PayPal...");
  143.         }
  144.     }
  145.  
  146.     public class PaymentService
  147.     {
  148.         private readonly IPaymentSystem _paymentSystem;
  149.  
  150.         public PaymentService(Func<IPaymentSystem> paymentSystemFactory)
  151.         {
  152.             if (paymentSystemFactory == null)
  153.             {
  154.                 throw new ArgumentNullException("Фабрика платёжных систем не может быть null.", nameof(paymentSystemFactory));
  155.             }
  156.  
  157.             _paymentSystem = paymentSystemFactory();
  158.         }
  159.  
  160.         public void ProcessPayment()
  161.         {
  162.             _paymentSystem.ProcessPayment();
  163.         }
  164.  
  165.         public void ShowPaymentResult()
  166.         {
  167.             Console.WriteLine($"Вы оплатили с помощью {_paymentSystem.Id}");
  168.  
  169.             _paymentSystem.CheckPayment();
  170.  
  171.             Console.WriteLine("Оплата прошла успешно!");
  172.         }
  173.     }
  174.  
  175.     public class OrderForm
  176.     {
  177.         public string ShowForm(IEnumerable<string> paymentSystemId)
  178.         {
  179.             if (paymentSystemId == null || paymentSystemId.Any() == false)
  180.             {
  181.                 throw new ArgumentException("Список платёжных систем не может быть пустым.", nameof(paymentSystemId));
  182.             }
  183.  
  184.             Console.WriteLine("Мы принимаем: " + string.Join(", ", paymentSystemId));
  185.             Console.WriteLine("Какой системой вы хотите совершить оплату?");
  186.  
  187.             string systemId = Console.ReadLine();
  188.  
  189.             if (string.IsNullOrWhiteSpace(systemId))
  190.             {
  191.                 throw new ArgumentException("Идентификатор платежной системы не может быть пустым.", nameof(systemId));
  192.             }
  193.  
  194.             return systemId.Trim().ToLower();
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment