Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace OCP
- {
- internal class Program
- {
- private static void Main()
- {
- PaymentSystemFactory paymentSystemFactory = new PaymentSystemFactory();
- var orderForm = new OrderForm();
- try
- {
- IEnumerable<string> availableSystems = paymentSystemFactory.AvailablePaymentSystemIds;
- string systemId = orderForm.ShowForm(availableSystems);
- Func<IPaymentSystem> paymentSystemFactoryMethod = paymentSystemFactory.GetPaymentSystemFactory(systemId);
- var paymentHandler = new PaymentService(paymentSystemFactoryMethod);
- paymentHandler.ProcessPayment();
- paymentHandler.ShowPaymentResult();
- }
- catch (Exception exception)
- {
- Console.WriteLine($"Ошибка: {exception.Message}");
- }
- }
- }
- public class PaymentSystemFactory
- {
- private readonly Dictionary<string, Func<IPaymentSystem>> _paymentSystemFactories;
- public PaymentSystemFactory()
- {
- _paymentSystemFactories = new Dictionary<string, Func<IPaymentSystem>>();
- InitializePaymentSystemFactories();
- }
- public IEnumerable<string> AvailablePaymentSystemIds
- {
- get { return _paymentSystemFactories.Keys; }
- }
- public Func<IPaymentSystem> GetPaymentSystemFactory(string systemId)
- {
- if (string.IsNullOrWhiteSpace(systemId))
- {
- throw new ArgumentException("Идентификатор платежной системы не может быть пустым или содержать только пробелы.", nameof(systemId));
- }
- if (_paymentSystemFactories.TryGetValue(systemId, out Func<IPaymentSystem> factory))
- {
- return factory;
- }
- throw new NotSupportedException($"Платежная система с идентификатором '{systemId}' не поддерживается.");
- }
- private void InitializePaymentSystemFactories()
- {
- _paymentSystemFactories.Add(PaymentSystemIdentifiers.Qiwi, () => new QiwiPaymentSystem());
- _paymentSystemFactories.Add(PaymentSystemIdentifiers.WebMoney, () => new WebMoneyPaymentSystem());
- _paymentSystemFactories.Add(PaymentSystemIdentifiers.Card, () => new CardPaymentSystem());
- _paymentSystemFactories.Add(PaymentSystemIdentifiers.PayPal, () => new PayPalPaymentSystem());
- }
- }
- public static class PaymentSystemIdentifiers
- {
- public const string Qiwi = "qiwi";
- public const string WebMoney = "webmoney";
- public const string Card = "card";
- public const string PayPal = "paypal";
- }
- public interface IPaymentSystem
- {
- public string Id { get; }
- public void ProcessPayment();
- public void CheckPayment();
- }
- public class QiwiPaymentSystem : IPaymentSystem
- {
- public string Id => PaymentSystemIdentifiers.Qiwi;
- public void ProcessPayment()
- {
- Console.WriteLine("Перевод на страницу QIWI...");
- }
- public void CheckPayment()
- {
- Console.WriteLine("Проверка платежа через QIWI...");
- }
- }
- public class WebMoneyPaymentSystem : IPaymentSystem
- {
- public string Id => PaymentSystemIdentifiers.WebMoney;
- public void ProcessPayment()
- {
- Console.WriteLine("Вызов API WebMoney...");
- }
- public void CheckPayment()
- {
- Console.WriteLine("Проверка платежа через WebMoney...");
- }
- }
- public class CardPaymentSystem : IPaymentSystem
- {
- public string Id => PaymentSystemIdentifiers.Card;
- public void ProcessPayment()
- {
- Console.WriteLine("Вызов API банка эмитента карты...");
- }
- public void CheckPayment()
- {
- Console.WriteLine("Проверка платежа через карту...");
- }
- }
- public class PayPalPaymentSystem : IPaymentSystem
- {
- public string Id => PaymentSystemIdentifiers.PayPal;
- public void CheckPayment()
- {
- Console.WriteLine("Перевод на страницу PayPal...");
- }
- public void ProcessPayment()
- {
- Console.WriteLine("Проверка платежа через PayPal...");
- }
- }
- public class PaymentService
- {
- private readonly IPaymentSystem _paymentSystem;
- public PaymentService(Func<IPaymentSystem> paymentSystemFactory)
- {
- if (paymentSystemFactory == null)
- {
- throw new ArgumentNullException("Фабрика платёжных систем не может быть null.", nameof(paymentSystemFactory));
- }
- _paymentSystem = paymentSystemFactory();
- }
- public void ProcessPayment()
- {
- _paymentSystem.ProcessPayment();
- }
- public void ShowPaymentResult()
- {
- Console.WriteLine($"Вы оплатили с помощью {_paymentSystem.Id}");
- _paymentSystem.CheckPayment();
- Console.WriteLine("Оплата прошла успешно!");
- }
- }
- public class OrderForm
- {
- public string ShowForm(IEnumerable<string> paymentSystemId)
- {
- if (paymentSystemId == null || paymentSystemId.Any() == false)
- {
- throw new ArgumentException("Список платёжных систем не может быть пустым.", nameof(paymentSystemId));
- }
- Console.WriteLine("Мы принимаем: " + string.Join(", ", paymentSystemId));
- Console.WriteLine("Какой системой вы хотите совершить оплату?");
- string systemId = Console.ReadLine();
- if (string.IsNullOrWhiteSpace(systemId))
- {
- throw new ArgumentException("Идентификатор платежной системы не может быть пустым.", nameof(systemId));
- }
- return systemId.Trim().ToLower();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment