Example #1
0
        private static void HandleOutput(CliOptions options, FibonacciResultSet resultSet)
        {
            IFibonacciOutput outputMethod;

            if (options.OutputFile == null)
            {
                outputMethod = new DirectFibonacciOutput();
            }
            else if (Path.GetExtension(options.OutputFile) == ".xml")
            {
                outputMethod = new XMLFibonacciOutput(options.OutputFile);
            }
            else
            {
                outputMethod = new PlainTextFibonacciOutput(options.OutputFile);
            }

            outputMethod.WriteResult(resultSet);
        }
Example #2
0
        private static void Main(string[] args)
        {
            var options = new CliOptions();

            // parser fail case
            if (!Parser.Default.ParseArguments(args, options))
            {
                Console.Write(options.GetUsage());
                Environment.Exit(1);
            }

            // no input case
            if (args.Length == 0 || (options.InputFile == null && options.InputNumber == null && options.InteractiveMode == false))
            {
                Console.Write(options.GetUsage());
                Environment.Exit(1);
            }

            int numVal = 0;
            try
            {
                numVal = GetInputValue(options);
            }
            catch (InvalidFibonacciInputException e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }

            // negative number case
            if (numVal < 0)
            {
                Console.WriteLine("Number must be positive.");
                Environment.Exit(1);
            }

            FibonacciCalculator.Calculator calc = new FibonacciCalculator.Calculator();
            FibonacciResultSet results = calc.Compute(numVal);

            HandleOutput(options, results);
        }
Example #3
0
        private static int GetInputValue(CliOptions options)
        {
            IFibonacciInput inputMethod;

            if (options.InteractiveMode)
            {
                inputMethod = new InteractiveFibonacciInput();
            }
            else if (options.InputFile == null)
            {
                // attempt to parse InputNumber
                return BaseFibonacciInput.ParseStringArgument(options.InputNumber);
            }
            else if (Path.GetExtension(options.InputFile) == ".xml")
            {
                inputMethod = new XMLFibonacciInput(options.InputFile);
            }
            else
            {
                inputMethod = new PlainTextFibonacciInput(options.InputFile);
            }

            return inputMethod.GetValue();
        }