Example #1
0
        public void ExecuteCommand(string command)
        {
            if (command == null)
            {
                ThrowArgumentNullExceptionOnNullArgument(nameof(command), nameof(ExecuteCommand));
            }

            //Ignore not relevant spaces and case
            command = command.Trim().ToUpperInvariant();

            if (command.StartsWith(SimulatorCommands.Place))
            {
                var parts = command.Split(' ');
                if (parts.Length == 2 && parts[0] == SimulatorCommands.Place)
                {
                    var parameters = parts[1].Split(',');
                    if (parameters.Length != 3)
                    {
                        ThrowArgumentExceptionOnInvalidCommand(command, nameof(ExecuteCommand), null);
                    }
                    else
                    {
                        try {
                            int x         = int.Parse(parameters[0].Trim());
                            int y         = int.Parse(parameters[1].Trim());
                            var position  = new Point.Point(x, y);
                            var direction = DirectionExtensions.ConvertStringToDirection(parameters[2].Trim());

                            Robot.Place(Table, position, direction);
                        } catch (InvalidOperationException ioe) {
                            // Handle exceptions from Robot.Place
                            LogNotAllowedCommand(command, ioe);
                        } catch (Exception e) when(e is FormatException || e is OverflowException || e is ArgumentOutOfRangeException)
                        {
                            // Handle exceptions from parameters parsing
                            ThrowArgumentExceptionOnInvalidCommand(command, nameof(ExecuteCommand), e);
                        }
                    }
                }
                else
                {
                    ThrowArgumentExceptionOnInvalidCommand(command, nameof(ExecuteCommand), null);
                }
            }
            else
            {
                try {
                    switch (command)
                    {
                    case SimulatorCommands.Move:
                        Robot.Move();
                        break;

                    case SimulatorCommands.Left:
                        Robot.RotateToTheLeft();
                        break;

                    case SimulatorCommands.Right:
                        Robot.RotateToTheRight();
                        break;

                    case SimulatorCommands.Report:
                        var robotStatus = Robot.GetStatus();
                        MainTextWriter.WriteLine($"{robotStatus.Position.x},{robotStatus.Position.y},{robotStatus.Direction.ToString().ToUpperInvariant()}");
                        break;

                    default:
                        ThrowArgumentExceptionOnInvalidCommand(command, nameof(ExecuteCommand), null);
                        break;
                    }
                } catch (InvalidOperationException ioe) {
                    LogNotAllowedCommand(command, ioe);
                }
            }
        }