-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (96 loc) · 2.78 KB
/
Program.cs
File metadata and controls
101 lines (96 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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;
}
}