Beispiel #1
0
        public override void Translate()
        {
            var value = GetValue();
            var parts = value.Split(' ');

            var roman      = String.Empty;
            var thingValue = 1m;
            var lastRoman  = parts.Length;

            for (var p = 0; p < parts.Length; p++)
            {
                var part = parts[p];

                if (Interpreter.ConversionDictionary.ContainsKey(part))
                {
                    roman += Interpreter.ConversionDictionary[part];
                }
                else
                {
                    lastRoman = p;
                    break;
                }
            }

            parts = parts.Skip(lastRoman).ToArray();

            if (parts.Any())
            {
                var thingName = String.Join(" ", parts);

                if (Interpreter.ThingValueDictionary.ContainsKey(thingName))
                {
                    thingValue *= Interpreter.ThingValueDictionary[thingName];
                }
                else
                {
                    var message = String.Format(Messages.UnknownAlienNumberOrThing, thingName);
                    Interpreter.AddError(message, Order);
                    return;
                }
            }

            var arabic          = RomanConversor.Convert(roman);
            var thingToBeValued = GetThing();

            var finalValue = getFinalValue(arabic, thingValue, thingToBeValued);

            if (finalValue.HasValue)
            {
                var message = String.Format(rightAnswer, value, finalValue, thingToBeValued);
                Interpreter.AddInfo(message, Order);
            }
            else
            {
                var message = String.Format(Messages.UnknownRomanNumber, roman);
                Interpreter.AddError(message, Order);
            }
        }
Beispiel #2
0
        public void SEVEN_TO_VII()
        {
            RomanConversor romanConversor = new RomanConversor();

            // Input
            int    number         = 7;
            string experatedValue = "VII";

            // Test
            string result = romanConversor.Convert(number);

            // Assert
            Assert.AreEqual(experatedValue, result);
        }
 private void AssertEqual(String roman, Int32?arabic)
 {
     Assert.AreEqual(arabic, RomanConversor.Convert(roman));
 }
        public override void Translate()
        {
            var    key   = Match.Groups[1].Value.Trim();
            var    value = (Decimal?)Decimal.Parse(Match.Groups[2].Value);
            String name;

            if (!key.Contains(" "))
            {
                name = key;
            }
            else
            {
                var numbers = key.Split(' ');
                var names   = new List <String>();

                for (var n = 0; n < numbers.Length; n++)
                {
                    if (numbers[n][0].ToString() == numbers[n][0].ToString().ToUpper())
                    {
                        names.Add(numbers[n]);
                        numbers[n] = String.Empty;
                    }
                }

                name = String.Join(" ", names);
                var romanDivisor = String.Empty;

                foreach (var number in numbers)
                {
                    if (String.IsNullOrEmpty(number))
                    {
                        continue;
                    }

                    if (!Interpreter.ConversionDictionary.ContainsKey(number))
                    {
                        var message = String.Format(Messages.UnknownAlienToRomanConversion, number);

                        Interpreter.AddError(message, Order);
                        return;
                    }

                    romanDivisor += Interpreter.ConversionDictionary[number];
                }

                var divisor = RomanConversor.Convert(romanDivisor);

                if (divisor == null)
                {
                    value = null;

                    var message = String.Format(Messages.UnknownRomanNumber, romanDivisor);
                    Interpreter.AddError(message, Order);
                }
                else if (divisor.Value != 0)
                {
                    value /= divisor.Value;
                }
            }

            if (value.HasValue)
            {
                var message = Interpreter.ThingValueDictionary.HandleKeyValue(name, value.Value);

                if (!String.IsNullOrEmpty(message))
                {
                    Interpreter.AddWarning(message, Order);
                }
            }
        }