Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Examples/CryptoCurrencyExternalLibrary/CryptoCurrency.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
12 changes: 9 additions & 3 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
246 changes: 205 additions & 41 deletions UserCode/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
// }
//}



//#endregion

//#region 1.2
Expand Down Expand Up @@ -530,109 +532,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
// // <xml>
// // <node>............</node>
// //</xml>
// _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 TruckAdapter()
public CryptoCurrencyAdapter()
{
_truck = new Truck(5,"THR");
_cryptoCurrency = new CryptoCurrency();
}

public void Send()
public decimal AdjustAmount(decimal amount)
{
// Delegate the call to the Truck class's Deliver method
// string -> xml nodes
// <xml>
// <node>............</node>
//</xml>
_truck.Deliver();
return _cryptoCurrency.ExternalProcessPayment(amount);
}

public void ProcessPayment(decimal amount)
{
throw new NotImplementedException();
}
}

public interface IPaymentFactory
{
IPayment CreateInstance();
}

public interface ITransportFactory
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