public IExecutable ExecuteCommand(string line)
        {
            string[] inputArgs = line.Split(new[] { '(', ',', ')' }, StringSplitOptions.RemoveEmptyEntries);
            string command = inputArgs[0];
            string[] parameters = inputArgs.Skip(1).Select(p => p.Trim()).ToArray();
            IExecutable commandResult = null;

            switch (command)
            {
                case "AddTheatre":
                    commandResult = new AddTheatreCommand(parameters, this.performanceDatabase);
                    break;
                case "PrintAllTheatres":
                    commandResult = new PrintAllTheatresCommand(this.performanceDatabase);
                    break;
                case "AddPerformance":
                    commandResult = new AddPerformanceCommand(parameters, this.performanceDatabase);
                    break;
                case "PrintAllPerformances":
                    commandResult = new PrintAllPerformancesCommand(this.performanceDatabase);
                    break;
                case "PrintPerformances":
                    commandResult = new PrintPerformancesCommand(parameters, this.performanceDatabase);
                    break;
                default:
                    commandResult = new InvalidResultCommand(this.performanceDatabase);
                    break;
            }

            return commandResult;
        }
Example #2
0
        public virtual ICommand CreateCommand(string commandName, IPerformanceDatabase performanceDatabase, IRenderer renderer)
        {
            ICommand command = null;

            switch (commandName.ToLower())
            {
            case "addperformance":
                command = new AddPerformanceCommand(performanceDatabase, renderer);
                break;

            case "addtheatre":
                command = new AddTheatreCommand(performanceDatabase, renderer);
                break;

            case "printallperformances":
                command = new PrintAllPerformancesCommand(performanceDatabase, renderer);
                break;

            case "printalltheatres":
                command = new PrintAllTheatresCommand(performanceDatabase, renderer);
                break;

            case "printperformances":
                command = new PrintPerformancesCommand(performanceDatabase, renderer);
                break;

            default:
                throw new ArgumentException("Invalid command!");
            }

            return(command);
        }
        public void ExecuteCommand(string inputLine)
        {
            string[] commandLine = inputLine.Split('(');
            string commandName = commandLine[0];

            string[] commandArgumentsArray =
                commandLine[1]
                .Split(
                    new[] { '(', ',', ')' },
                    StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => p.Trim())
                    .ToArray();

            ICommand command = null;
            switch (commandName)
            {
                case "AddTheatre":
                    command = new AddTheatreCommand(commandArgumentsArray, this.performanceDatabase);
                    break;
                case "PrintAllTheatres":
                    command = new PrintAllTheatersCommand(commandArgumentsArray, this.performanceDatabase);
                    break;
                case "AddPerformance":
                    command = new AddPerformanceCommand(commandArgumentsArray, this.performanceDatabase);
                    break;
                case "PrintAllPerformances":
                    command = new PrintAllPerformancesCommand(commandArgumentsArray, this.performanceDatabase);
                    break;
                case "PrintPerformances":
                    command = new PrintPerformancesCommand(commandArgumentsArray, this.performanceDatabase);
                    break;
                default:
                    throw new NotImplementedException("The command with name " + commandName + " is not defined/implemented.");
            }

            string commandResult;
            try
            {
                commandResult = command.Execute();
            }
            catch (Exception ex)
            {
                commandResult = "Error: " + ex.Message;
            }

            Console.WriteLine(commandResult);
        }