From a545ae31394984ae111f17aea827eae079453265 Mon Sep 17 00:00:00 2001 From: danaei Date: Sun, 8 Dec 2024 22:53:30 +0330 Subject: [PATCH 1/3] --- UserCode/Program.cs | 244 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 203 insertions(+), 41 deletions(-) diff --git a/UserCode/Program.cs b/UserCode/Program.cs index 1dd825f..379faa6 100644 --- a/UserCode/Program.cs +++ b/UserCode/Program.cs @@ -530,109 +530,271 @@ //#endregion -#region 1.8 +//#region 1.8 + +//using CryptoCurrencyExternalLibrary; +//using ExternalClassLibrary; +//using System.ComponentModel; + +//public class Program +//{ +// public static void Main() +// { +// var type = Console.ReadLine(); +// ITransport transport = TransportService.Create(type); +// transport.Send(); +// } +//} + +//public class TransportService +//{ +// public static ITransport Create(string type) +// { +// switch (type) +// { +// case "Air": +// { +// var factory = new AirFactory(); +// return factory.CreateInstance(); +// } +// case "Train": +// { +// var factory = new TrainFactory(); +// return factory.CreateInstance(); +// } + +// case "Truck": +// { +// return new TruckAdapter(); +// } +// default: +// throw new NotImplementedException(); +// } +// } +//} + +//public interface ITransport +//{ +// void Send(); +//} + +//public class Air : ITransport +//{ +// public void Send() +// { +// Console.WriteLine("Send package by air ..."); +// } +//} + +//public class Train : ITransport +//{ +// public void Send() +// { +// Console.WriteLine("Send package by Train ..."); +// } +//} + +//public class TruckAdapter : ITransport +//{ +// private readonly Truck _truck; + +// public TruckAdapter() +// { +// _truck = new Truck(5, "THR"); +// } + +// public void Send() +// { +// // Delegate the call to the Truck class's Deliver method +// // string -> xml nodes +// // +// // ............ +// // +// _truck.Deliver(); +// } +//} + +//public interface ITransportFactory +//{ +// ITransport CreateInstance(); +//} + +//public class AirFactory : ITransportFactory +//{ +// public ITransport CreateInstance() +// { +// //manage instance +// return new Air(); +// } +//} + +//public class TrainFactory : ITransportFactory +//{ +// public ITransport CreateInstance() +// { +// //manage instance +// return new Train(); +// } +//} + +//#endregion +#region 1.9 + +using CryptoCurrencyExternalLibrary; -using ExternalClassLibrary; public class Program { public static void Main() { var type = Console.ReadLine(); - ITransport transport = TransportService.Create(type); - transport.Send(); + Console.WriteLine("Please enter the amount:"); + + string input = Console.ReadLine(); + if (decimal.TryParse(input, out decimal amount)) + { + Console.WriteLine($"The entered amount is: {amount}"); // You can now use the 'amount' variable as needed + + } + else + { + Console.WriteLine("Invalid input, please enter a valid decimal number."); + } + + + IPayment payment = PaymentService.Create(type); + payment.ProcessPayment(amount); + amount = payment.AdjustAmount(amount); } } -public class TransportService +public class PaymentService { - public static ITransport Create(string type) + public static IPayment Create(string type) { switch (type) { - case "Air": + case "CreditCard": { - var factory = new AirFactory(); + var factory = new CreditCardFactory(); return factory.CreateInstance(); } - case "Train": + case "PayPal": { - var factory = new TrainFactory(); + var factory = new PayPalFactory(); return factory.CreateInstance(); } - case "Truck": + case "CryptoCurrency": { - return new TruckAdapter(); + return new CryptoCurrencyAdapter(); } default: - throw new NotImplementedException(); + { + var factory = new NullPaymentFactory(); + return factory.CreateInstance(); + } } } } -public interface ITransport +public interface IPayment { - void Send(); -} + void ProcessPayment(decimal amount); + decimal AdjustAmount(decimal amount); -public class Air : ITransport +} +public class CreditCard : IPayment { - public void Send() + private const decimal FeeRate = 0.02m; + public void ProcessPayment(decimal amount) { - Console.WriteLine("Send package by air ..."); + + } + public decimal AdjustAmount(decimal amount) + { + return amount + (amount * FeeRate); } + } -public class Train : ITransport +public class PayPal : IPayment { - public void Send() + private const decimal DiscountRate = 0.03m; + public void ProcessPayment(decimal amount) { - Console.WriteLine("Send package by Train ..."); + Console.WriteLine(); + } + public decimal AdjustAmount(decimal amount) + { + return amount - (amount * DiscountRate); } } -public class TruckAdapter : ITransport +public class CryptoCurrencyAdapter : IPayment { - private readonly Truck _truck; + private readonly CryptoCurrency _cryptoCurrency; + + public CryptoCurrencyAdapter() + { + _cryptoCurrency = new CryptoCurrency(); + } - public TruckAdapter() + public decimal AdjustAmount(decimal amount) { - _truck = new Truck(5,"THR"); + return _cryptoCurrency.ExternalProcessPayment(amount); } - public void Send() + public void ProcessPayment(decimal amount) { - // Delegate the call to the Truck class's Deliver method - // string -> xml nodes - // - // ............ - // - _truck.Deliver(); + throw new NotImplementedException(); } } -public interface ITransportFactory +public interface IPaymentFactory +{ + IPayment CreateInstance(); +} + +public class CreditCardFactory : IPaymentFactory { - ITransport CreateInstance(); + public IPayment CreateInstance() + { + //manage instance + return new CreditCard(); + } } -public class AirFactory : ITransportFactory +public class PayPalFactory : IPaymentFactory { - public ITransport CreateInstance() + public IPayment CreateInstance() { //manage instance - return new Air(); + return new PayPal(); } } -public class TrainFactory : ITransportFactory +public class NullPayment : IPayment +{ + public decimal AdjustAmount(decimal amount) + { + return amount; + } + + public void ProcessPayment(decimal amount) + { + Console.WriteLine($"Payment method not supported. Unable to process ${amount:F2}."); + } + +} +public class NullPaymentFactory : IPaymentFactory { - public ITransport CreateInstance() + public IPayment CreateInstance() { //manage instance - return new Train(); + return new NullPayment(); } } + #endregion \ No newline at end of file From 4adeb18102167aeacfcffdd5842f8c6b51b7534c Mon Sep 17 00:00:00 2001 From: danaei Date: Sun, 8 Dec 2024 22:55:09 +0330 Subject: [PATCH 2/3] Add PaymentService --- .../CryptoCurrency.cs | 17 +++++++++++++++++ .../CryptoCurrencyExternalLibrary.csproj | 9 +++++++++ Examples/Examples.sln | 12 +++++++++--- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 Examples/CryptoCurrencyExternalLibrary/CryptoCurrency.cs create mode 100644 Examples/CryptoCurrencyExternalLibrary/CryptoCurrencyExternalLibrary.csproj diff --git a/Examples/CryptoCurrencyExternalLibrary/CryptoCurrency.cs b/Examples/CryptoCurrencyExternalLibrary/CryptoCurrency.cs new file mode 100644 index 0000000..09c68c7 --- /dev/null +++ b/Examples/CryptoCurrencyExternalLibrary/CryptoCurrency.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CryptoCurrencyExternalLibrary +{ + public class CryptoCurrency + { + public decimal ExternalProcessPayment(decimal amount) + { + return amount; + } + + } +} diff --git a/Examples/CryptoCurrencyExternalLibrary/CryptoCurrencyExternalLibrary.csproj b/Examples/CryptoCurrencyExternalLibrary/CryptoCurrencyExternalLibrary.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/Examples/CryptoCurrencyExternalLibrary/CryptoCurrencyExternalLibrary.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/Examples/Examples.sln b/Examples/Examples.sln index 0fd3d0a..21724c1 100644 --- a/Examples/Examples.sln +++ b/Examples/Examples.sln @@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.35430.204 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserCode", "..\UserCode\UserCode.csproj", "{03E39C4B-5B4C-4FD2-A79D-AC86704AF3DC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserCode", "..\UserCode\UserCode.csproj", "{03E39C4B-5B4C-4FD2-A79D-AC86704AF3DC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExternalClassLibrary", "..\ExternalClassLibrary\ExternalClassLibrary.csproj", "{B77A43A4-BF8D-44E1-B3E8-8F6DD365DEAB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExternalClassLibrary", "..\ExternalClassLibrary\ExternalClassLibrary.csproj", "{B77A43A4-BF8D-44E1-B3E8-8F6DD365DEAB}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RealExample", "RealExample", "{BEFC8EDE-A568-4AC3-B3EA-B68427103370}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Payment", "..\Payment\Payment.csproj", "{E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Payment", "..\Payment\Payment.csproj", "{E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CryptoCurrencyExternalLibrary", "CryptoCurrencyExternalLibrary\CryptoCurrencyExternalLibrary.csproj", "{CEE91381-0168-4615-9DFA-0440BF0A4085}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -29,6 +31,10 @@ Global {E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}.Debug|Any CPU.Build.0 = Debug|Any CPU {E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}.Release|Any CPU.ActiveCfg = Release|Any CPU {E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}.Release|Any CPU.Build.0 = Release|Any CPU + {CEE91381-0168-4615-9DFA-0440BF0A4085}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CEE91381-0168-4615-9DFA-0440BF0A4085}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CEE91381-0168-4615-9DFA-0440BF0A4085}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CEE91381-0168-4615-9DFA-0440BF0A4085}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From aef44154f70c5795da10c8a70205ee5ee03c4011 Mon Sep 17 00:00:00 2001 From: danaei Date: Sun, 8 Dec 2024 22:59:54 +0330 Subject: [PATCH 3/3] vhjh --- UserCode/Program.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/UserCode/Program.cs b/UserCode/Program.cs index 379faa6..492dc0c 100644 --- a/UserCode/Program.cs +++ b/UserCode/Program.cs @@ -70,6 +70,8 @@ // } //} + + //#endregion //#region 1.2