Exemplo n.º 1
0
        public void MoveForward()
        {
            if (this.Status != RobotStatus.On)
            {
                _exceptionFactory.GenerateSafeException($"please place this robot first");
            }

            if (!MovableResponses.MoveForwardOneStep(this.Position, _surface))
            {
                _exceptionFactory.GenerateSafeException($"the robot cannot move to expected postion");
            }
        }
Exemplo n.º 2
0
        public async Task <float> GetAverageCubicWeightByCategory(string category)
        {
            var cubicWeights = await _fetchService.GetProductInfoByCategory <float>(WeightCalculator, category);

            if (!(cubicWeights?.Any() ?? false))
            {
                _exceptionFactory.GenerateSafeException("No products found in the remote api!");
            }

            return(cubicWeights.Average());
        }
Exemplo n.º 3
0
        /// <summary>
        /// get a command instance by a given command string
        /// </summary>
        /// <returns>The command instance</returns>
        /// <param name="commandLine">a string of command line</param>
        public ICommand GetCommand(string commandLine, IRobot robot)
        {
            ICommand cmd = null;

            //C# 7 new feature Tuples
            var(action, parameters) = getCommandInfo();

            try
            {
                switch (action)
                {
                case RobotCommand.PLACE:
                    cmd = new PlaceCommand(robot, parameters, _exceptionFactory);
                    break;

                case RobotCommand.MOVE:
                    cmd = new MoveCommand(robot);
                    break;

                case RobotCommand.LEFT:
                    cmd = new LeftCommand(robot);
                    break;

                case RobotCommand.RIGHT:
                    cmd = new RightCommand(robot);
                    break;

                case RobotCommand.REPORT:
                    cmd = new ReportCommand(robot);
                    break;

                default:
                    _exceptionFactory.GenerateWarningException($"Unkown command {commandLine}");
                    break;
                }
            }
            catch (InvalidCastException)
            {
                _exceptionFactory.GenerateSafeException($"The current robot is not able to execute the {action.ToString()} command");
            }
            return(cmd);

            //C# 7 new feature local function
            (RobotCommand action, string parameters) getCommandInfo()
            {
                commandLine = commandLine.Trim();
                int index         = commandLine.IndexOf(" ", StringComparison.CurrentCulture);
                var robotCommand  = (RobotCommand)Enum.Parse(typeof(RobotCommand), (index > 0 ? commandLine.Substring(0, index) : commandLine).ToUpper());
                var commandParams = index > 0 ? commandLine.Substring(index + 1) : string.Empty;

                return(robotCommand, commandParams);
            }
        }
        public PlaceCommand(IRobot robot, string parameters, IExceptionFactory exceptionFactory) : base(robot)
        {
            var parameterArray = parameters.Trim().Split(' ');

            try
            {
                _position           = new RobotPosition();
                _position.X         = int.Parse(parameterArray[0]);
                _position.Y         = int.Parse(parameterArray[1]);
                _position.Direction = (CompassDirections)Enum.Parse(typeof(CompassDirections), parameterArray[2].ToUpper());
            }
            catch
            {
                exceptionFactory.GenerateSafeException($"invalid parameters [{parameters}] for the {Command.ToString()} command");
            }
        }