コード例 #1
0
        public void produce_6_two_way_currency_pairs_when_3_exchange_rate_lines_with_no_common_tail_or_head_are_read()
        {
            Tuple <string, string, decimal> exchangeRateLineElements = new Tuple <string, string, decimal>("AUD", "CHF", 0.9661m);

            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            exchangeRateLineElements = new Tuple <string, string, decimal>("EUR", "JPY", 1.2053m);

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            exchangeRateLineElements = new Tuple <string, string, decimal>("HUF", "USD", 7.5498m);

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            currencyConversionPaths.ShouldSatisfyAllConditions
            (
                () => currencyConversionPaths.CurrencyPaths.Count.ShouldBe(6),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("AUD->CHF"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("CHF->AUD"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("EUR->JPY"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("JPY->EUR"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("HUF->USD"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("USD->HUF")
            );
        }
コード例 #2
0
        public void return_the_shortest_path_among_all_paths_for_given_start_and_end_currencies()
        {
            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths.CurrencyPaths.Add("CHF->EUR->USD->JPY->AUD");
            currencyConversionPaths.CurrencyPaths.Add("CHF->EUR->USD");
            currencyConversionPaths.CurrencyPaths.Add("CHF->EUR->USD->JPY->BOB->COP->AUD");
            currencyConversionPaths.CurrencyPaths.Add("EUR->USD->JPY->BOB->COP->AUD");
            currencyConversionPaths.CurrencyPaths.Add("CHF->EUR->BRL->AUD");

            currencyConversionPaths.FindShortestPath("CHF", "AUD").First().ShouldBe("CHF->EUR->BRL->AUD");
        }
コード例 #3
0
        public void return_the_right_rate_among_all_rates_for_given_currency_pair()
        {
            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths.ExchangeRatePaths.Add("CHF->EUR:0,2522");
            currencyConversionPaths.ExchangeRatePaths.Add("EUR->USD:1,3456");
            currencyConversionPaths.ExchangeRatePaths.Add("EUR->CHF:2,4656");
            currencyConversionPaths.ExchangeRatePaths.Add("EUR->JPY:0,4567");
            currencyConversionPaths.ExchangeRatePaths.Add("CHF->AUD:1,3876");

            currencyConversionPaths.GetRate("CHF->AUD").First().ShouldBe("1,3876");
        }
コード例 #4
0
        public void return_the_right_rate_among_all_rates_for_given_currency_pair()
        {
            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths.ExchangeRatePaths.Add ("CHF->EUR:0,2522");
            currencyConversionPaths.ExchangeRatePaths.Add ("EUR->USD:1,3456");
            currencyConversionPaths.ExchangeRatePaths.Add ("EUR->CHF:2,4656");
            currencyConversionPaths.ExchangeRatePaths.Add ("EUR->JPY:0,4567");
            currencyConversionPaths.ExchangeRatePaths.Add ("CHF->AUD:1,3876");

            currencyConversionPaths.GetRate("CHF->AUD").First().ShouldBe("1,3876");
        }
コード例 #5
0
        public void return_the_shortest_path_among_all_paths_for_given_start_and_end_currencies()
        {
            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths.CurrencyPaths.Add ("CHF->EUR->USD->JPY->AUD");
            currencyConversionPaths.CurrencyPaths.Add ("CHF->EUR->USD");
            currencyConversionPaths.CurrencyPaths.Add ("CHF->EUR->USD->JPY->BOB->COP->AUD");
            currencyConversionPaths.CurrencyPaths.Add ("EUR->USD->JPY->BOB->COP->AUD");
            currencyConversionPaths.CurrencyPaths.Add ("CHF->EUR->BRL->AUD");

            currencyConversionPaths.FindShortestPath("CHF","AUD").First().ShouldBe("CHF->EUR->BRL->AUD");
        }
コード例 #6
0
        public static CurrencyConversionPaths ConvertToCurrencyConversionPaths(Tuple<string, string, decimal> lineElements, CurrencyConversionPaths currencyConversionPaths)
        {
            string newHeadCurrency = lineElements.Item1;
            string newTailCurrency = lineElements.Item2;
            decimal newExchangeRate = lineElements.Item3;

            if ((!currencyConversionPaths.CurrencyPaths.Contains(newHeadCurrency + "->" + newTailCurrency)) && (!currencyConversionPaths.CurrencyPaths.Contains(newHeadCurrency + "->" + newTailCurrency)))
            {
                List<string> newPaths = new List<string>();

                if (currencyConversionPaths.CurrencyPaths.Count > 0)
                {
                    newPaths.AddRange(
                        from path in currencyConversionPaths.CurrencyPaths
                        where path.StartsWith(newHeadCurrency)
                        select newTailCurrency + "->" + path
                        );

                    newPaths.AddRange(
                       from path in currencyConversionPaths.CurrencyPaths
                       where path.EndsWith(newHeadCurrency)
                       select path + "->" + newTailCurrency
                       );

                    newPaths.AddRange(
                        from path in currencyConversionPaths.CurrencyPaths
                        where path.StartsWith(newTailCurrency)
                        select newHeadCurrency + "->" + path
                        );

                    newPaths.AddRange(
                        from path in currencyConversionPaths.CurrencyPaths
                        where path.EndsWith(newTailCurrency)
                        select path + "->" + newHeadCurrency
                        );

                    currencyConversionPaths.CurrencyPaths.AddRange(newPaths);
                }

                currencyConversionPaths.CurrencyPaths.Add(newHeadCurrency + "->" + newTailCurrency);
                currencyConversionPaths.ExchangeRatePaths.Add(newHeadCurrency + "->" + newTailCurrency + ":" + newExchangeRate);

                currencyConversionPaths.CurrencyPaths.Add(newTailCurrency + "->" + newHeadCurrency);
                currencyConversionPaths.ExchangeRatePaths.Add(newTailCurrency + "->" + newHeadCurrency + ":" + decimal.Round(1 / newExchangeRate, 4));

            }

            return currencyConversionPaths;
        }
コード例 #7
0
        public void produce_a_two_way_currency_pairs_when_one_exchange_rate_line_is_read()
        {
            Tuple <string, string, decimal> exchangeRateLineElements = new Tuple <string, string, decimal> ("AUD", "CHF", 0.9661m);

            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();


            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            currencyConversionPaths.ShouldSatisfyAllConditions
            (
                () => currencyConversionPaths.CurrencyPaths.First().ShouldBe("AUD->CHF"),
                () => currencyConversionPaths.CurrencyPaths.Count.ShouldBe(2),
                () => currencyConversionPaths.CurrencyPaths.ElementAt(1).ShouldBe("CHF->AUD")
            );
        }
コード例 #8
0
        public void produce_4_currency_rate_pairs_when_2_different_exchange_rate_lines_are_read()
        {
            Tuple <string, string, decimal> exchangeRateLineElements = new Tuple <string, string, decimal>("AUD", "CHF", 0.9661m);

            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            exchangeRateLineElements = new Tuple <string, string, decimal>("EUR", "CHF", 1.2053m);

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            currencyConversionPaths.ShouldSatisfyAllConditions
            (
                () => currencyConversionPaths.ExchangeRatePaths.Count.ShouldBe(4),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("AUD->CHF:0,9661"),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("CHF->AUD:1,0351"),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("EUR->CHF:1,2053"),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("CHF->EUR:0,8297")
            );
        }
コード例 #9
0
        public void produce_4_two_way_currency_pairs_when_2_exchange_rate_lines_with_no_common_tail_or_head_are_read()
        {
            Tuple<string, string, decimal> exchangeRateLineElements = new Tuple<string, string, decimal>("AUD", "CHF", 0.9661m);

            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            exchangeRateLineElements = new Tuple<string, string, decimal>("EUR", "JPY", 2.5487m);

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            currencyConversionPaths.ShouldSatisfyAllConditions
            (
                () => currencyConversionPaths.CurrencyPaths.Count.ShouldBe(4),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("AUD->CHF"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("CHF->AUD"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("EUR->JPY"),
                () => currencyConversionPaths.CurrencyPaths.ShouldContain("JPY->EUR")
            );
        }
コード例 #10
0
        public void produce_4_currency_rate_pairs_when_2_different_exchange_rate_lines_are_read()
        {
            Tuple<string, string, decimal> exchangeRateLineElements = new Tuple<string, string, decimal>("AUD", "CHF", 0.9661m);

            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            exchangeRateLineElements = new Tuple<string, string, decimal>("EUR", "CHF", 1.2053m);

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            currencyConversionPaths.ShouldSatisfyAllConditions
            (
                () => currencyConversionPaths.ExchangeRatePaths.Count.ShouldBe(4),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("AUD->CHF:0,9661"),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("CHF->AUD:1,0351"),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("EUR->CHF:1,2053"),
                () => currencyConversionPaths.ExchangeRatePaths.ShouldContain("CHF->EUR:0,8297")
            );
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: Max-Git/TestLucca
        public static void Main(string[] args)
        {
            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();
            decimal convertedAmount = 0;

            Console.WriteLine ("Welcome to this brilliant currency converter! ;-)");
            Console.WriteLine ("=================================================");
            Tuple<string, int, string> firstLineElements = ConsoleParser.ReadFirstLine(Console.ReadLine());
            Decimal.TryParse(firstLineElements.Item2.ToString(CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture, out convertedAmount);

            if (firstLineElements != null)
            {
                int nbrExchangeRates = ConsoleParser.ReadSecondLine(Console.ReadLine());

                if (nbrExchangeRates > 0)
                {
                    for (int i = 0; i < nbrExchangeRates; i++)
                    {
                        Tuple<string, string, decimal> exchangeRateLineElements = ConsoleParser.ReadExchangeRatesLine(Console.ReadLine());

                        if (exchangeRateLineElements != null)
                        {
                            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);
                        }
                        else
                        {
                            Console.WriteLine("Incorrect exchange rate input!");
                            Console.ReadLine();
                            return;
                        }
                    }

                    List<string> shortestPath = currencyConversionPaths.FindShortestPath (firstLineElements.Item1, firstLineElements.Item3);

                    if (shortestPath.Count() > 0)
                    {
                        List<string> shortestPathPairs = CurrenciesPathManager.splitPathIntoPairs(shortestPath.First());

                        var rates =
                            from pair in shortestPathPairs
                            let rate = currencyConversionPaths.GetRate (pair)
                            let pairRate = Decimal.Parse(rate.First(), NumberStyles.AllowDecimalPoint, CultureInfo.CreateSpecificCulture("fr-FR"))
                            select pairRate;

                        convertedAmount = rates.Aggregate(convertedAmount, (x, y) => x * y);

                        Console.WriteLine("" + (int)decimal.Round(convertedAmount, MidpointRounding.AwayFromZero));
                    }
                    else
                    {
                        Console.WriteLine("No enough rates to convert! :-(");
                    }
                }
                else
                {
                    Console.WriteLine("Incorrect number of exchange rates!");
                }
            }
            else
            {
                Console.WriteLine("Incorrect first line input!");
            }

            Console.ReadLine();
        }
コード例 #12
0
        public void produce_a_two_way_currency_pairs_when_one_exchange_rate_line_is_read()
        {
            Tuple<string, string, decimal> exchangeRateLineElements = new Tuple<string, string, decimal> ("AUD","CHF", 0.9661m);

            CurrencyConversionPaths currencyConversionPaths = new CurrencyConversionPaths();

            currencyConversionPaths = CurrenciesPathManager.ConvertToCurrencyConversionPaths(exchangeRateLineElements, currencyConversionPaths);

            currencyConversionPaths.ShouldSatisfyAllConditions
            (
                () => currencyConversionPaths.CurrencyPaths.First().ShouldBe("AUD->CHF"),
                () => currencyConversionPaths.CurrencyPaths.Count.ShouldBe(2),
                () => currencyConversionPaths.CurrencyPaths.ElementAt(1).ShouldBe("CHF->AUD")
            );
        }