Example #1
0
        private static void ProcessCommand(string inputCommand, Toy robot)
        {
            string message = string.Empty;

            bool result = CommandProcessor.ProcessCommand(robot, inputCommand, out message);

            if (result)
            {
                Console.ForegroundColor = ConsoleColor.Green;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
            }

            Console.WriteLine(message);
            Console.ResetColor();
        }
Example #2
0
 public ReportCommand(Toy toy)
     : base(toy)
 {
 }
Example #3
0
 public PlaceCommand(Toy toy, Position placePosition, MoveDirection moveDirection)
     : base(toy)
 {
     this._toy.CurrentPosition = placePosition;
     this._toy.Facing = moveDirection;
 }
Example #4
0
 public TurnCommand(Toy toy, TurnDirection turnDirection)
     : base(toy)
 {
     this._toy.TurnToDirection = turnDirection;
 }
Example #5
0
        public static bool ProcessCommand(Toy robot, string inputCommand, out string output)
        {
            //Toy robot = new Robot(;
             ToyArgs inputArgs;

            if (CommandValidations.ValidateCommand(inputCommand, out inputArgs))
            {
                ToyCommand toyCommand = null;
                CommandInvoker commandInvoker = null;

                switch (inputArgs.ActionArg)
                {
                    case ToyActions.Invalid:
                        output = "Invalid command";
                        return false;
                    case ToyActions.Place:
                        {
                            toyCommand = new PlaceCommand(robot, inputArgs.PositionArg, inputArgs.MoveDirectionArg);
                            commandInvoker = new CommandInvoker(toyCommand);
                        }
                        break;
                    case ToyActions.Move:
                        {
                            toyCommand = new MoveCommand(robot);
                            commandInvoker = new CommandInvoker(toyCommand);
                        }
                        break;
                    case ToyActions.Right:
                        {
                            toyCommand = new TurnCommand(robot, TurnDirection.Right);
                            commandInvoker = new CommandInvoker(toyCommand);
                        }
                        break;
                    case ToyActions.Left:
                        {
                            toyCommand = new TurnCommand(robot, TurnDirection.Left);
                            commandInvoker = new CommandInvoker(toyCommand);
                        }
                        break;
                    case ToyActions.Report:
                        {
                            toyCommand = new ReportCommand(robot);
                            commandInvoker = new CommandInvoker(toyCommand);
                        }
                        break;
                    default:
                        output = "Invalid command";
                        return false;
                }

                bool isCommandSuccessful = false;
                if (commandInvoker != null)
                {
                    isCommandSuccessful = commandInvoker.ExecuteCommand();
                }
                output = robot.Message;
                return isCommandSuccessful;
            }
            else
            {
                output = "Invalid command";
                return false;
            }
        }
Example #6
0
 public MoveCommand(Toy toy)
     : base(toy)
 {
 }
Example #7
0
        private static void ProcessCommand_1(string inputCommand, Toy robot, ToyArgs inputArgs)
        {
            try
            {
                if (CommandValidations.ValidateCommand(inputCommand, out inputArgs))
                {
                    ToyCommand toyCommand = null;
                    CommandInvoker commandInvoker = null;

                    switch (inputArgs.ActionArg)
                    {
                        case ToyActions.Invalid:
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Invalid command");
                            return;
                        case ToyActions.Place:
                            {
                                toyCommand = new PlaceCommand(robot, inputArgs.PositionArg, inputArgs.MoveDirectionArg);
                                commandInvoker = new CommandInvoker(toyCommand);
                            }
                            break;
                        case ToyActions.Move:
                            {
                                toyCommand = new MoveCommand(robot);
                                commandInvoker = new CommandInvoker(toyCommand);
                            }
                            break;
                        case ToyActions.Right:
                            {
                                toyCommand = new TurnCommand(robot, TurnDirection.Right);
                                commandInvoker = new CommandInvoker(toyCommand);
                            }
                            break;
                        case ToyActions.Left:
                            {
                                toyCommand = new TurnCommand(robot, TurnDirection.Left);
                                commandInvoker = new CommandInvoker(toyCommand);
                            }
                            break;
                        case ToyActions.Report:
                            {
                                toyCommand = new ReportCommand(robot);
                                commandInvoker = new CommandInvoker(toyCommand);
                            }
                            break;
                        default:
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Invalid command");
                            return;
                    }

                    if (commandInvoker != null && commandInvoker.ExecuteCommand())
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }

                    Console.WriteLine(robot.Message);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Invalid command");
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Exception Occured: " + ex.Message);
            }

            Console.ResetColor();
        }