Exemplo n.º 1
0
        private List <ConversionPath> AddPathsFromMatches(List <ConversionPath> paths, ConversionPath currentAnalyzedPath, List <ExchangeRate> matchingRates)
        {
            var lastNode         = currentAnalyzedPath.Nodes.Last();
            var currentPathState = new ConversionPath {
                Nodes = currentAnalyzedPath.Nodes.ToList()
            };

            // add a node to the iterated path
            paths.First(p => p == currentAnalyzedPath)
            .Nodes.Add(
                GetNewNode(matchingRates.First(), lastNode.Key
                           ));

            // multiple matches, create new possible paths
            if (matchingRates.Count > 1)
            {
                int remainingCount = matchingRates.Count - 1;
                var duplicates     = Enumerable.Repeat(currentPathState, remainingCount).ToList();
                for (int i = 0; i < remainingCount; i++)
                {
                    duplicates[i].Nodes.Add(GetNewNode(matchingRates[i + 1], lastNode.Key));
                }
                paths.AddRange(duplicates);
            }

            return(paths);
        }
Exemplo n.º 2
0
 SearchResult(IImplementation feature, ConversionPath converterPath)
     : base(_nextObjectId++)
 {
     Feature = feature;
     ConverterPath = converterPath;
     StopByObjectIds();
 }
Exemplo n.º 3
0
 /// <summary>
 /// Removes a conversion path from the <see cref="ConversionPaths"/> list
 /// </summary>
 /// <param name="path">The conversion path to remove</param>
 /// <returns>True if the conversion path was found and removed, false otherwise</returns>
 public static bool RemoveConversionPath(ConversionPath path)
 {
     if (path == null)
     {
         throw new ArgumentNullException(nameof(path));
     }
     return(_ConversionPaths.Remove(path));
 }
Exemplo n.º 4
0
        public double ConvertCurrency(ConversionPath path)
        {
            float amount = path.Nodes[0].Value;

            foreach (var node in path.Nodes.Skip(1))
            {
                amount *= node.Value;
            }
            return(amount);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Adds a conversion path to the <see cref="ConversionPaths"/> list
 /// </summary>
 /// <param name="path">The conversion path to add</param>
 public static void AddConversionPath(ConversionPath path)
 {
     if (path == null)
     {
         throw new ArgumentNullException(nameof(path));
     }
     if (!_ConversionPaths.Contains(path))
     {
         _ConversionPaths.Add(path);
     }
 }
Exemplo n.º 6
0
        public void Run(string filePath)
        {
            if (!File.Exists(filePath))
            {
                Console.Error.WriteLine("File path doesn't exist");
                Environment.Exit(1);
            }

            var lines = File.ReadLines(filePath).ToList();

            try
            {
                var instructions = ExchangeRatesTableParser.GetConversionInstructions(lines[0], lines[1]);

                ToConvert toConvert          = instructions.Item1;
                int       exchangeRatesCount = instructions.Item2;

                var exchangeRates = ExchangeRatesTableParser.ParseRates(lines, exchangeRatesCount);

                ConversionPath shortestPath = GetShortestPath(toConvert, exchangeRates.ToList());

                if (shortestPath == null)
                {
                    Console.Error.WriteLine("Incorrect file: no solution");
                    Environment.Exit(1);
                }

                Console.WriteLine(Math.Round(ConvertCurrency(shortestPath)));
            }
            catch (IndexOutOfRangeException)
            {
                Console.Error.WriteLine("Incorrect file: doesn't respect the exchange rates format");
                Environment.Exit(1);
            }
            catch (FormatException)
            {
                Console.Error.WriteLine("Incorrect file: number of lines not provided");
                Environment.Exit(1);
            }
            catch (IncorrectRateCountException)
            {
                Console.Error.WriteLine("Incorrect file: expected number of exchange rates differs from content");
                return;
            }
        }
Exemplo n.º 7
0
 internal SearchResult(SearchResult searchResult, ConversionPath relativeConversion)
     : this(searchResult.Feature, relativeConversion + searchResult.ConverterPath)
 {
     Tracer.Assert(searchResult.Source == relativeConversion.Destination);
 }