public string ExecuteCommand(string commandName, string[] commandArguments)
        {
            ICommand command = null;
            if (commandName == "AddPhone" && commandArguments.Length >= 2)
            {
                command = new AddPhoneCommand(this.phonebookRepository, commandArguments);
            }
            else if (commandName == "ChangePhone" && commandArguments.Length == 2)
            {
                command = new ChangePhoneCommand(this.phonebookRepository, commandArguments);
            }
            else if (commandName == "List" && commandArguments.Length == 2)
            {
                command = new ListCommand(this.phonebookRepository, commandArguments);
            }
            else
            {
                return "Invalid command";
            }

            string commandResult;
            try
            {
                commandResult = command.Execute();
            }
            catch (Exception e)
            {
                commandResult = e.Message;

            }

            return commandResult;
        }
        public Phone Update(ChangePhoneCommand command)
        {
            var phone = _repository.GetOne(command.Id);

            if (!string.IsNullOrEmpty(command.DDD))
            {
                phone.ChangeDDD(command.DDD);
            }
            if (!string.IsNullOrEmpty(command.Number))
            {
                phone.ChangeNumber(command.Number);
            }
            if (!string.IsNullOrEmpty(command.Description))
            {
                phone.ChangeDescription(command.Description);
            }

            _repository.Update(phone);

            if (Commit())
            {
                return(phone);
            }

            return(null);
        }
        public IPhoneBookCommand CreateCommand(string commandName, int argumentsCount)
        {
            IPhoneBookCommand command;
            if (commandName.StartsWith("AddPhone") && (argumentsCount >= 2))
            {
                command = new AddPhoneCommand(data, printer, sanitizer);
            }
            else if ((commandName == "ChangePhone") && (argumentsCount == 2))
            {
                command = new ChangePhoneCommand(data, printer, sanitizer);
            }
            else if ((commandName == "List") && (argumentsCount == 2))
            {
                command = new ListPhonesCommand(data, printer);
            }
            else
            {
                throw new ArgumentException("Invalid command");
            }

            return command;
        }
Esempio n. 4
0
        public IPhoneBookCommand CreateCommand(string commandName, int argumentsCount)
        {
            IPhoneBookCommand command;

            if (commandName.StartsWith("AddPhone") && (argumentsCount >= 2))
            {
                command = new AddPhoneCommand(data, printer, sanitizer);
            }
            else if ((commandName == "ChangePhone") && (argumentsCount == 2))
            {
                command = new ChangePhoneCommand(data, printer, sanitizer);
            }
            else if ((commandName == "List") && (argumentsCount == 2))
            {
                command = new ListPhonesCommand(data, printer);
            }
            else
            {
                throw new ArgumentException("Invalid command");
            }

            return(command);
        }