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

            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 sourceWord      = postIsWords.Skip(postIsWords.Length - 1).ToString();
            string destinationWord = 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(_pseudonymMap.GetValueForPseudonym(aliases[i]));
            }

            double sourceWordPrice      = _wordMap.GetPriceByWord(sourceWord);
            double destinationWordPrice = _wordMap.GetPriceByWord(destinationWord);

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

            if (totalUnits.HasValue)
            {
                double totalSourceCommodity = sourceWordPrice * totalUnits.Value;
                Console.WriteLine(String.Format("{0} is {1} {2}", parts[1], (totalSourceCommodity / destinationWordPrice), destinationWord));
            }
        }
Exemplo n.º 2
0
        public void UnitExpressionTest()
        {
            RomanPseudonymMapper pseudonymMap = new RomanPseudonymMapper();
            RomanConverter       converter    = new RomanConverter();
            WordMapper           wordMap      = new WordMapper();

            pseudonymMap.AddPseudonym("glob", "I");
            pseudonymMap.AddPseudonym("pish", "X");
            ExpressionValidationHelper helper     = new ExpressionValidationHelper(pseudonymMap, wordMap);
            UnitExpression             expression = new UnitExpression(pseudonymMap, wordMap, converter, helper);

            expression.Execute("pish glob Iron is 110 Credits");
            Assert.True(wordMap.Exists("Iron"));
            Assert.Equal <double>(10, wordMap.GetPriceByWord("Iron"));
            expression.Execute("glob pish Iron is 6300 Credits");
            Assert.Equal <double>(700, wordMap.GetPriceByWord("Iron"));
        }
        public void Execute(string input)
        {
            //Remove question mark
            input = input.Substring(0, input.Length - 1).ToLower();

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

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

            //Create Roman Numeral from aliases
            for (int i = 0; i < words.Length - 1; i++)
            {
                sb.Append(_pseudonymMap.GetValueForPseudonym(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 * _wordMap.GetPriceByWord(word)));
            }
        }