public void Execute(string input)
        {
            //Remove question mark
            input = input.Substring(0, input.Length - 1);

            string[] parts = input.Split(new string[] { " is " }, StringSplitOptions.RemoveEmptyEntries);

            string[] preIsWords  = parts[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string[] postIsWords = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            string sourceCommodity      = postIsWords.Skip(postIsWords.Length - 1).ToString();
            string destinationCommodity = preIsWords[2];

            string[] aliases = postIsWords.Take(postIsWords.Length - 1).ToArray();

            StringBuilder sb = new StringBuilder();

            //Create Roman Numeral from aliases
            for (int i = 0; i < aliases.Length - 1; i++)
            {
                sb.Append(aliasMap.GetValueForAlias(aliases[i]));
            }

            double sourceCommodityPrice      = commodityIndex.GetPriceByCommodity(sourceCommodity);
            double destinationCommodityPrice = commodityIndex.GetPriceByCommodity(destinationCommodity);

            //Convert Roman to Decimal
            double?totalUnits = converter.ToDecimal(sb.ToString());

            if (totalUnits.HasValue)
            {
                double totalSourceCommodity = sourceCommodityPrice * totalUnits.Value;
                Console.WriteLine(String.Format("{0} is {1} {2}", parts[1], (totalSourceCommodity / destinationCommodityPrice), destinationCommodity));
            }
        }
Exemplo n.º 2
0
        public void Execute(string input)
        {
            //Remove question mark
            input = input.Substring(0, input.Length - 1);

            string[] parts = input.Split(new string[] { " is " }, StringSplitOptions.RemoveEmptyEntries);
            string[] words = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            string        commodity = words[words.Length - 1];
            StringBuilder sb        = new StringBuilder();

            //Create Roman Numeral from aliases
            for (int i = 0; i < words.Length - 1; i++)
            {
                sb.Append(aliasMap.GetValueForAlias(words[i]));
            }

            //Convert Roman to Decimal
            double?totalUnits = converter.ToDecimal(sb.ToString());

            if (totalUnits.HasValue)
            {
                Console.WriteLine(String.Format("{0} is {1}", parts[1], totalUnits.Value * commodityIndex.GetPriceByCommodity(commodity)));
            }
        }
        public void Execute(string input)
        {
            string[] parts             = input.Split(new string[] { " is " }, StringSplitOptions.RemoveEmptyEntries);
            string[] wordsInFirstPart  = parts[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string[] wordsInSecondPart = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            double decimalPrice = 0;

            double.TryParse(wordsInSecondPart[0], out decimalPrice);

            string metal      = wordsInFirstPart[wordsInFirstPart.Length - 1];
            string AliasValue = string.Empty;

            //Create Roman Numeral from aliases
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < wordsInFirstPart.Length - 1; i++)
            {
                sb.Append(_aliasMap.GetValueForAlias(wordsInFirstPart[i]));
            }

            //Convert Roman to decimal
            double?totalUnits = _converter.ToDecimal(sb.ToString());

            //Calculate and store per unit price of commodity
            if (totalUnits.HasValue)
            {
                _metalMap.AddMetal(metal, decimalPrice / totalUnits.Value);
            }
            else
            {
                Console.WriteLine("Error occurred while calculating commodity price");
            }
        }
        public void Execute(string input)
        {
            //Remove question mark
            input = input.Substring(0, input.Length - 1);

            string[] parts = Regex.Split(input, Constant.ExpressionSplitter, RegexOptions.IgnoreCase);

            string[] preIsWords  = parts[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string[] postIsWords = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            string sourceMetal      = postIsWords.Skip(postIsWords.Length - 1).ToString();
            string destinationMetal = preIsWords[2];

            string[] aliases = postIsWords.Take(postIsWords.Length - 1).ToArray();

            StringBuilder sb = new StringBuilder();

            //Create Roman Numeral from aliases
            for (int i = 0; i < aliases.Length - 1; i++)
            {
                sb.Append(_aliasMap.GetValueForAlias(aliases[i]));
            }

            double sourceMetalPrice      = _metalMap.GetPriceByMetal(sourceMetal);
            double destinationMetalPrice = _metalMap.GetPriceByMetal(destinationMetal);

            //Convert Roman to Decimal
            double?totalUnits = _converter.ToDecimal(sb.ToString());

            if (totalUnits.HasValue)
            {
                double total = sourceMetalPrice * totalUnits.Value;
                Console.WriteLine(String.Format("{0} is {1} {2}", parts[1], (total / destinationMetalPrice), destinationMetal));
            }
        }
        public void AliasExpressionTest()
        {
            AliasMapper     aliasMap   = new AliasMapper();
            AliasExpression expression = new AliasExpression(aliasMap);

            Assert.IsTrue(expression.Match("glob is C"));
            Assert.IsFalse(expression.Match("glob is N"));
            expression.Execute("glob is C");
            Assert.IsTrue(aliasMap.Exists("glob"));
            Assert.IsTrue(String.Equals(aliasMap.GetValueForAlias("glob"), "C"));
        }
        public void AliasExpressionTest()
        {
            AliasMapper         aliasMap   = new AliasMapper();
            AliasExpressionType expression = new AliasExpressionType(aliasMap);

            Assert.True(expression.Match("glob is C"));
            Assert.False(expression.Match("glob is N"));
            expression.Execute("glob is C");
            Assert.True(aliasMap.Exists("glob"));
            Assert.Equal("C", aliasMap.GetValueForAlias("glob"));
        }
Exemplo n.º 7
0
        private string CreateRomanNumeralsFromAliases(string[] words)
        {
            StringBuilder sb = new StringBuilder();

            //Create Roman Numeral from aliases
            for (int i = 0; i < words.Length - 1; i++)
            {
                sb.Append(_aliasMap.GetValueForAlias(words[i]));
            }

            return(sb.ToString());
        }
Exemplo n.º 8
0
        public void Execute(string input)
        {
            //Remove question mark
            input = input.Substring(0, input.Length - 1);

            StringBuilder sb = new StringBuilder();

            string[] parts = input.Split(new string[] { " is " }, StringSplitOptions.RemoveEmptyEntries);
            string[] words = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string word in words)
            {
                if (!aliasMap.Exists(word))
                {
                    Console.WriteLine(String.Format("Error while processing this input: {0}", input));
                    return;
                }
                sb.Append(aliasMap.GetValueForAlias(word));
            }

            Console.WriteLine(String.Format("{0} is {1}", parts[1], converter.ConvertToDecimal(sb.ToString())));
        }
        public void Execute(string input)
        {
            //Remove question mark
            input = input.Substring(0, input.Length - 1);

            StringBuilder sb = new StringBuilder();

            string[] parts = Regex.Split(input, Constant.ExpressionSplitter, RegexOptions.IgnoreCase);
            string[] words = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string word in words)
            {
                if (!_aliasMap.Exists(word))
                {
                    Console.WriteLine(String.Format("Error while processing this input: {0}", input));
                    return;
                }
                sb.Append(_aliasMap.GetValueForAlias(word));
            }

            Console.WriteLine(String.Format("{0}{1}{2}", parts[1], Constant.ExpressionSplitter, _converter.ToDecimal(sb.ToString())));
        }