public void ConvertToIntTest()
        {
            var expected = 0;
            var actual   = 0;

            foreach (var item in _romanExpectedInt)
            {
                expected = item.Value;

                actual = _romanToIntConverter.ConvertToInt(item.Key);

                Assert.AreEqual(expected, actual);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// A method to fetch metal value.
        /// </summary>
        /// <param name="input">Input.</param>
        /// <returns>Metal Object.</returns>
        private Metal FetchMetal(string input, IDictionary <string, string> romanSymbolsValues)
        {
            string[] inputWords = input.Split(Constants.Space).ToArray();

            //input word list should contain Is word.
            if (inputWords.Any(value => string.IsNullOrEmpty(value)) ||
                !inputWords.Contains(Constants.Is))
            {
                return(null);
            }

            // Remove credits from last.
            inputWords = inputWords.Take(inputWords.Length - 1).ToArray();

            float metalVal = 0;

            if (!float.TryParse(inputWords[inputWords.Length - 1], out metalVal))
            {
                return(null);
            }

            var romanSequenceBeforeMetal = string.Empty;
            var metalName = string.Empty;

            // Extract Name of metal and Roman sequance before name of metal
            foreach (var word in inputWords)
            {
                if (word == Constants.Is)
                {
                    break;
                }

                if (romanSymbolsValues.ContainsKey(word))
                {
                    romanSequenceBeforeMetal = string.Concat(romanSequenceBeforeMetal, romanSymbolsValues[word]);
                    continue;
                }
                else
                {
                    metalName = string.Concat(metalName, Constants.Space, word);
                }
            }

            // If Roman Sequence is found then divide metal value with divisor
            if (!string.IsNullOrEmpty(romanSequenceBeforeMetal))
            {
                if (!_romanToIntConverter.IsValidRomanString(romanSequenceBeforeMetal))
                {
                    return(null);
                }

                float divisor = _romanToIntConverter.ConvertToInt(romanSequenceBeforeMetal);

                metalVal = metalVal / divisor;
            }

            return(new Metal {
                MetalName = metalName, Value = metalVal
            });
        }
        /// <summary>
        /// Method to generate answer from symbols of roman.
        /// </summary>
        /// <param name="inputSeq">Sequence of symbols.</param>
        /// <returns>String Answer.</returns>
        private string ConvertRomanSequenceToAnswer(string inputSeq, DataContract parsedData)
        {
            if (parsedData.RomanSymbolsValues == null || parsedData.RomanSymbolsValues.Count == 0)
            {
                return(Constants.NoSymbolsForRoman);
            }

            var inputs = inputSeq.Trim().Split(Constants.Space);

            var romanSeq = GenerateRomanSequenceFromSymbols(inputs, parsedData);

            if (!_romanToIntConverter.IsValidRomanString(romanSeq))
            {
                return(Constants.NoIdea);
            }

            var ans = _romanToIntConverter.ConvertToInt(romanSeq);

            return(string.Format(Constants.RomanSymbolAnswer, inputSeq, ans));
        }