-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c53dca7
commit 5f4c5df
Showing
5 changed files
with
27 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
using System.Numerics; | ||
|
||
namespace Funcky.Test; | ||
|
||
// Bank which returns a 1:1 exchange rate for every pair of currencies. | ||
internal sealed class OneToOneBank : IBank | ||
internal sealed class OneToOneBank<TUnderlyingType> : IBank<TUnderlyingType> | ||
where TUnderlyingType : INumberBase<TUnderlyingType> | ||
{ | ||
public static readonly IBank Instance = new OneToOneBank(); | ||
public static readonly IBank<TUnderlyingType> Instance = new OneToOneBank<TUnderlyingType>(); | ||
|
||
public decimal ExchangeRate(Currency source, Currency target) | ||
=> 1m; | ||
public TUnderlyingType ExchangeRate(Currency source, Currency target) | ||
=> TUnderlyingType.One; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
using System.Collections.Immutable; | ||
using System.Numerics; | ||
using Funcky.Extensions; | ||
|
||
namespace Funcky; | ||
|
||
internal sealed class DefaultBank : IBank | ||
internal sealed class DefaultBank<TUnderlyingType> : IBank<TUnderlyingType> | ||
where TUnderlyingType : INumberBase<TUnderlyingType> | ||
{ | ||
internal static readonly DefaultBank Empty = new(); | ||
internal static readonly DefaultBank<TUnderlyingType> Empty = new(); | ||
|
||
public DefaultBank(ImmutableDictionary<(Currency Source, Currency Target), decimal> exchangeRates) | ||
public DefaultBank(ImmutableDictionary<(Currency Source, Currency Target), TUnderlyingType> exchangeRates) | ||
{ | ||
ExchangeRates = exchangeRates; | ||
} | ||
|
||
private DefaultBank() | ||
{ | ||
ExchangeRates = ImmutableDictionary<(Currency Source, Currency Target), decimal>.Empty; | ||
ExchangeRates = ImmutableDictionary<(Currency Source, Currency Target), TUnderlyingType>.Empty; | ||
} | ||
|
||
public ImmutableDictionary<(Currency Source, Currency Target), decimal> ExchangeRates { get; } | ||
public ImmutableDictionary<(Currency Source, Currency Target), TUnderlyingType> ExchangeRates { get; } | ||
|
||
public decimal ExchangeRate(Currency source, Currency target) | ||
public TUnderlyingType ExchangeRate(Currency source, Currency target) | ||
=> ExchangeRates | ||
.GetValueOrNone(key: (source, target)) | ||
.GetOrElse(() => throw new MissingExchangeRateException($"No exchange rate for {source.AlphabeticCurrencyCode} => {target.AlphabeticCurrencyCode}.")); | ||
|
||
internal DefaultBank AddExchangeRate(Currency source, Currency target, decimal sellRate) | ||
internal DefaultBank<TUnderlyingType> AddExchangeRate(Currency source, Currency target, TUnderlyingType sellRate) | ||
=> new(ExchangeRates.Add((source, target), sellRate)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace Funcky; | ||
|
||
public interface IBank | ||
public interface IBank<TUnderlyingType> | ||
{ | ||
decimal ExchangeRate(Currency source, Currency target); | ||
TUnderlyingType ExchangeRate(Currency source, Currency target); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters