public ICommandLineParser Parse(string[] args) { if (ArgsAreNotValid(args)) { return(this); } var currencyPair = args[0].Split("/"); if (currencyPair.Length != 2) { Error = Constants.CurrencyPairIncorrectFormat; return(this); } if (!int.TryParse(args[1], out var result)) { Error = Constants.UnableToParseAmount; return(this); } ParsedObject = new FxExchange { CurrencyPair = new CurrencyPair { From = currencyPair[0], To = currencyPair[1] }, Amount = result }; return(this); }
public void Exchange_ShouldThrow_WhenCurrencyIsNotFound(FxExchange exchange, IExchangeRateHolder rateHolder) { // arrange var sut = new CurrencyExchanger(rateHolder); // act // assert Should.Throw <InvalidCurrencyException>(() => sut.Exchange(exchange)); }
public void Exchange_ShouldReturnTheSameAmount_AsRequested(FxExchange exchange, IExchangeRateHolder rateHolder) { // arrange var sut = new CurrencyExchanger(rateHolder); exchange.CurrencyPair.From = exchange.CurrencyPair.To; // act var result = sut.Exchange(exchange); // assert result.ShouldBe(exchange.Amount); }
private static void ExecuteExchange(FxExchange fxExchange) { var exchanger = new CurrencyExchanger(new ExchangeRateHolder(100)); try { Console.WriteLine(exchanger.Exchange(fxExchange)); } catch (Exception e) { Console.WriteLine(e.Message); } }
public void Exchange_IsCase_Insensitive(string from, string to) { // arrange var exchange = new FxExchange { Amount = 1, CurrencyPair = new CurrencyPair { From = from, To = to } }; var sut = new CurrencyExchanger(new ExchangeRateHolder(100)); // act // assert Should.NotThrow(() => sut.Exchange(exchange)); }
public void Exchange_ShouldExchangeTheAmount_AsRequested(string from, string to, int ammount, double expected) { // arrange var exchange = new FxExchange { Amount = ammount, CurrencyPair = new CurrencyPair { From = from, To = to } }; var sut = new CurrencyExchanger(new ExchangeRateHolder(100)); // act var result = sut.Exchange(exchange); // assert result.ShouldBe(expected); }
public void Exchange_ShouldThrow_WithMessage(string currency, string expected) { // arrange var exchange = new FxExchange { Amount = 1, CurrencyPair = new CurrencyPair { From = currency, To = "USD" } }; var sut = new CurrencyExchanger(new ExchangeRateHolder(100)); // act // assert var exception = Should.Throw <InvalidCurrencyException>(() => sut.Exchange(exchange)); exception.Message.ShouldBe(string.Format(expected, currency)); }
public double Exchange(FxExchange exchange) { var currencyPair = exchange.CurrencyPair; if (currencyPair.From == currencyPair.To) { return(exchange.Amount); } var hasFrom = _exchangeRateHolder.ExchangeRates.TryGetValue(currencyPair.From.ToUpper(), out var from); var hasTo = _exchangeRateHolder.ExchangeRates.TryGetValue(currencyPair.To.ToUpper(), out var to); if (hasFrom && hasTo) { return(Math.Round(from * exchange.Amount / to, 4)); } throw new InvalidCurrencyException(string.Format(Constants.CurrencyNotFound, hasFrom ? currencyPair.To : currencyPair.From)); }