Beispiel #1
0
        public void Run()
        {
            var numbersToSum = new List <double> ();

            CurrentConsole.WriteLine("Please enter numbers you want me to add.  When you have finished just enter 'done'.");

            while (true)
            {
                CurrentConsole.Write("Enter a number or type 'done': ");
                var inputString = Console.ReadLine();

                var inputNumber = 0.0;
                if (double.TryParse(inputString, out inputNumber))
                {
                    numbersToSum.Add(inputNumber);
                }
                else
                {
                    if (inputString.ToLowerInvariant() == "done")
                    {
                        break;
                    }
                    CurrentConsole.WriteLine(string.Format("I'm sorry, '{0}' is not a number or the word done.", inputString));
                }
            }

            var sum = Summarizer.Sum(numbersToSum.ToArray());

            CurrentConsole.WriteLine(string.Format("The sum of the numbers {0} is {1}", string.Join(", ", numbersToSum.Select(n => n.ToString())), sum));
        }