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);
        }
Beispiel #2
0
        public void Exchange_ShouldThrow_WhenCurrencyIsNotFound(FxExchange exchange, IExchangeRateHolder rateHolder)
        {
            // arrange
            var sut = new CurrencyExchanger(rateHolder);

            // act
            // assert
            Should.Throw <InvalidCurrencyException>(() => sut.Exchange(exchange));
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        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));
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        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));
        }
Beispiel #8
0
        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));
        }