コード例 #1
0
ファイル: CommandProcessor.cs プロジェクト: mbittal5/ToyRobot
        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;
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: mbittal5/ToyRobot
        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();
        }