From fd3e52eb37a5b495ce41dd827ce68e24405e083c Mon Sep 17 00:00:00 2001 From: Zusefi Date: Fri, 29 Nov 2024 20:14:28 +0330 Subject: [PATCH 1/3] CargoService --- Examples/E1/CargoService.cs | 122 ++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 Examples/E1/CargoService.cs diff --git a/Examples/E1/CargoService.cs b/Examples/E1/CargoService.cs new file mode 100644 index 0000000..deeaa1f --- /dev/null +++ b/Examples/E1/CargoService.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Examples.E1 +{ + public class CargoService + { + private CargoFactory CargoFactory { get; set; } + private string Origin =string.Empty; + private string Destination = string.Empty; + public ICargo BookProduct(string type) + { + + return CargoFactory.CreateCargoFactory(type,Origin,Destination); + + } + } + + internal abstract class CargoFactory + { + public ICargo CreateCargoFactory(string type,string origin,string destination) + { + CargoFactory factory =null ; + if (type == "Air") + { + factory= new AirFactory(); + } + if (type == "Ship") + { + factory = new ShipFactory(origin,destination); + + } + if (type == "Train") + { + factory = new TrainFactory(); + + } + if (factory is null) + throw new Exception("type is wrong"); + + return factory.CreateCargo(); + } + + public abstract ICargo CreateCargo(); + + } + + internal class TrainFactory : CargoFactory + { + public override ICargo CreateCargo() + { + var cargo = new Train(); + TrainMethod(); + return cargo; + } + + private void TrainMethod() + { + throw new NotImplementedException(); + } + } + + internal class ShipFactory : CargoFactory + { + public ShipFactory(string origin, string destination) + { + Origin = origin; + Destination = destination; + } + + public string Origin { get; set; } + public string Destination { get; set; } + public override ICargo CreateCargo() + { + return new Ship(Origin, Destination); + } + } + + internal class AirFactory : CargoFactory + { + private static Air Air { get; set; } + public override ICargo CreateCargo() + { + if (Air is null) + { + Air = new Air(); + + } + return Air; + } + } + + internal class Train : ICargo + { + + } + + internal class Ship : ICargo + { + public Ship(string origin, string destination) + { + Origin = origin; + Destination = destination; + } + + public string Origin { get; } + public string Destination { get; } + } + + internal class Air : ICargo + { + + } + + public interface ICargo + { + + } +} From 50fae450a608f4596c141d8efac4a6913e59d47b Mon Sep 17 00:00:00 2001 From: Zahra Usefi Date: Mon, 2 Dec 2024 11:28:21 +0330 Subject: [PATCH 2/3] fixCargoService --- Examples/E1/CargoService.cs | 175 ++++++++++++++++++------------------ UserCode/Program.cs | 33 ++++++- 2 files changed, 118 insertions(+), 90 deletions(-) diff --git a/Examples/E1/CargoService.cs b/Examples/E1/CargoService.cs index deeaa1f..7c96f93 100644 --- a/Examples/E1/CargoService.cs +++ b/Examples/E1/CargoService.cs @@ -1,122 +1,119 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Examples.E1; -namespace Examples.E1 +public class CargoService { - public class CargoService + public ICargo BookProduct(byte type, string? origin = null, string? destination = null) { - private CargoFactory CargoFactory { get; set; } - private string Origin =string.Empty; - private string Destination = string.Empty; - public ICargo BookProduct(string type) + CargoFactory? factory = null; + if (type == (byte)CargoType.Air) { - - return CargoFactory.CreateCargoFactory(type,Origin,Destination); - + factory = new AirFactory(); } - } - - internal abstract class CargoFactory - { - public ICargo CreateCargoFactory(string type,string origin,string destination) + if (type == (byte)CargoType.Ship) { - CargoFactory factory =null ; - if (type == "Air") - { - factory= new AirFactory(); - } - if (type == "Ship") - { - factory = new ShipFactory(origin,destination); - - } - if (type == "Train") - { - factory = new TrainFactory(); - - } - if (factory is null) - throw new Exception("type is wrong"); - - return factory.CreateCargo(); - } + if (string.IsNullOrWhiteSpace(origin)) + throw new ArgumentException("Origin should not be null or empty.", nameof(origin)); - public abstract ICargo CreateCargo(); - - } + if (string.IsNullOrWhiteSpace(destination)) + throw new ArgumentException("Destination should not be null or empty.", nameof(destination)); + factory = new ShipFactory(origin, destination); - internal class TrainFactory : CargoFactory - { - public override ICargo CreateCargo() - { - var cargo = new Train(); - TrainMethod(); - return cargo; } - - private void TrainMethod() + if (type == (byte)CargoType.Air) { - throw new NotImplementedException(); + factory = new TrainFactory(); + } + if (factory is null) + throw new Exception("cargotype is wrong please enter a new type"); + return factory.Create(origin, destination); } +} - internal class ShipFactory : CargoFactory - { - public ShipFactory(string origin, string destination) - { - Origin = origin; - Destination = destination; - } +public abstract class CargoFactory +{ + public abstract ICargo Create(string? origin = null, string? destination = null); +} - public string Origin { get; set; } - public string Destination { get; set; } - public override ICargo CreateCargo() - { - return new Ship(Origin, Destination); - } +public class TrainFactory : CargoFactory +{ + public override ICargo Create(string? origin = null, string? destination = null) + { + var cargo = new Train(); + TrainMethod(); + return cargo; } - internal class AirFactory : CargoFactory + private void TrainMethod() => Console.WriteLine("train is new"); +} + +public class ShipFactory : CargoFactory +{ + public ShipFactory(string origin, string destination) { - private static Air Air { get; set; } - public override ICargo CreateCargo() - { - if (Air is null) - { - Air = new Air(); - - } - return Air; - } + Origin = origin; + Destination = destination; } - internal class Train : ICargo + public string Origin { get; set; } + public string Destination { get; set; } + public override ICargo Create(string? origin, string? destination) { - + return new Ship(origin, destination); } +} - internal class Ship : ICargo - { - public Ship(string origin, string destination) - { - Origin = origin; - Destination = destination; - } +public class AirFactory : CargoFactory +{ + private static readonly Lazy _air = new Lazy(() => new Air()); - public string Origin { get; } - public string Destination { get; } + public override ICargo Create(string? origin, string? destination) + { + return _air.Value; } +} - internal class Air : ICargo +public class Train : ICargo +{ + public decimal SetPayment() { + return 500; + } +} +public class Ship : ICargo +{ + public Ship(string origin, string destination) + { + Origin = origin; + Destination = destination; } - public interface ICargo + public string Origin { get; set; } + public string Destination { get; set; } + public decimal SetPayment() { + return 2000; + } +} +public class Air : ICargo +{ + public decimal SetPayment() + { + return 1000; } } + +public interface ICargo +{ + public decimal SetPayment(); +} +public enum CargoType +{ + NotSet = 0, + Air = 1, + Ship = 2, + Train = 3, +} + diff --git a/UserCode/Program.cs b/UserCode/Program.cs index 50e4fd0..dd02c41 100644 --- a/UserCode/Program.cs +++ b/UserCode/Program.cs @@ -1 +1,32 @@ -namespace UserCode; +using Examples.E1; + +namespace UserCode; +class Program +{ + static void Main() + { + var cargoService = new CargoService(); + Console.WriteLine("please enter cargo type "); + Console.WriteLine($"air = " + CargoType.Air); + Console.WriteLine($"ship = " + CargoType.Ship); + Console.WriteLine($"train = " + CargoType.Train); + byte type; + Console.WriteLine("Please enter a number between 0 and 255:"); + while (!byte.TryParse(Console.ReadLine(), out type)) + { + Console.WriteLine("Invalid input. Please enter a valid byte value (0-255):"); + } + string? origin = null; + string? destination = null; + if (type == (byte)CargoType.Ship) + { + Console.Write("please enter origin "); + origin = Console.ReadLine(); + Console.Write("please enter destination "); + destination = Console.ReadLine(); + } + + + cargoService.BookProduct(type, origin, destination); + } +} \ No newline at end of file From cd559193d5b6f20afe9963c072f8cc0339a5fe60 Mon Sep 17 00:00:00 2001 From: Zusefi Date: Mon, 9 Dec 2024 02:58:36 +0330 Subject: [PATCH 3/3] PaymentProcessor --- Payment/Program.cs | 103 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/Payment/Program.cs b/Payment/Program.cs index 3751555..a7e57ec 100644 --- a/Payment/Program.cs +++ b/Payment/Program.cs @@ -1,2 +1,101 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +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; + } +} \ No newline at end of file