Advertisement
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
- {
- string systemId = orderForm.ShowForm();
- IPaymentSystem paymentSystem = paymentSystemFactory.CreatePaymentSystem(systemId);
- var paymentHandler = new PaymentService(paymentSystem);
- paymentHandler.ProcessPayment();
- paymentHandler.ShowPaymentResult();
- }
- catch (Exception exception)
- {
- Console.WriteLine($"Ошибка: {exception.Message}");
- }
- }
- }
- public class PaymentSystemFactory
- {
- private readonly Dictionary<string, IPaymentSystem> PaymentSystems = new Dictionary<string, IPaymentSystem>
- {
- { PaymentSystemIdentifiers.Qiwi, new QiwiPaymentSystem() },
- { PaymentSystemIdentifiers.WebMoney, new WebMoneyPaymentSystem() },
- { PaymentSystemIdentifiers.Card, new CardPaymentSystem() },
- { PaymentSystemIdentifiers.PayPal, new PayPalPaymentSystem() },
- };
- public IPaymentSystem CreatePaymentSystem(string systemId)
- {
- if (string.IsNullOrWhiteSpace(systemId))
- {
- throw new ArgumentException("Идентификатор платежной системы не может быть пустым или содержать только пробелы.", nameof(systemId));
- }
- if (PaymentSystems.TryGetValue(systemId, out IPaymentSystem paymentSystem))
- {
- return paymentSystem;
- }
- throw new NotSupportedException($"Платежная система с идентификатором '{systemId}' не поддерживается.");
- }
- }
- 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(IPaymentSystem paymentSystem)
- {
- if (paymentSystem == null)
- {
- throw new ArgumentNullException("Платежная система не может быть null.", nameof(paymentSystem));
- }
- _paymentSystem = paymentSystem;
- }
- public void ProcessPayment()
- {
- _paymentSystem.ProcessPayment();
- }
- public void ShowPaymentResult()
- {
- Console.WriteLine($"Вы оплатили с помощью {_paymentSystem.Id}");
- _paymentSystem.CheckPayment();
- Console.WriteLine("Оплата прошла успешно!");
- }
- }
- public class OrderForm
- {
- public string ShowForm()
- {
- Console.WriteLine($"Мы принимаем: {PaymentSystemIdentifiers.Qiwi}, {PaymentSystemIdentifiers.WebMoney}, {PaymentSystemIdentifiers.Card}, {PaymentSystemIdentifiers.PayPal}");
- 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
Advertisement