Phonebook.CommandFactoryWithLazyLoading.CreateCommand C# (CSharp) Method

CreateCommand() public method

public CreateCommand ( string commandName, int argumentsCount ) : IPhonebookCommand
commandName string
argumentsCount int
return IPhonebookCommand
        public IPhonebookCommand CreateCommand(string commandName, int argumentsCount)
        {
            IPhonebookCommand command;
            if ((commandName.StartsWith("AddPhone")) && (argumentsCount >= 2))
            {
                if (this.addCommand == null)
                {
                    this.addCommand = new AddPhoneCommand(this.data, this.printer, this.sanitizer);
                }
                command = this.addCommand;

            }
            else if ((commandName == "ChangePhone") && (argumentsCount == 2))
            {
                if (this.changeCommand == null)
                {
                    this.changeCommand = new ChangePhoneCommand(this.data, this.printer, this.sanitizer);
                }
                command = this.changeCommand;
            }
            else if ((commandName == "List") && (argumentsCount == 2))
            {
                if (this.listCommand == null)
                {
                    this.listCommand = new ListPhonesCommand(this.data, this.printer);
                }
                command = this.listCommand;
            }
            else
            {
                throw new ArgumentException("Invalid command!");
            }
            return command;
        }
    }

Usage Example

        public static void Main()
        {
            IPhonebookRepository data = new AdvancedRepository();
            IPrinter printer = new StringBuilderPrinter();
            IPhoneNumberSanitizer sanitizer = new PhonebookSanitizer();
            ICommandFactory commandFactory = new CommandFactoryWithLazyLoading(data, printer, sanitizer);
           

            while (true)
            {
                string currentCommand = Console.ReadLine();
                if (currentCommand == "End" || currentCommand == null)
                {
                    Console.Write(printer.GetAllText());
                    return;
                }

                int openingBracketIndex = currentCommand.IndexOf('('); 
                
                if (openingBracketIndex == -1) 
                {
                    throw new ArgumentException("Invalid command. The command must have an opening bracket.");
                }

                if (!currentCommand.EndsWith(")"))
                {
                    throw new ArgumentException("Invalid command. The command must end with a closing bracket.");
                }

                string commandText = currentCommand.Substring(0, openingBracketIndex);
                string parametersString = currentCommand.Substring(openingBracketIndex + 1, currentCommand.Length - openingBracketIndex - 2);

                string[] parameters = parametersString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                for (int j = 0; j < parameters.Length; j++)
                {
                    parameters[j] = parameters[j].Trim();
                }

                IPhoneBookCommand command = commandFactory.CreateCommand(commandText, parameters.Length);

                command.Execute(parameters);
            }
         }
All Usage Examples Of Phonebook.CommandFactoryWithLazyLoading::CreateCommand