public static void Main()
        {
            IPrinter printer = new StringBuilderPrinter();
            ISanitizer sanitizer = new PhoneSanitizer();

            while (true)
            {
                string currentCommand = Console.ReadLine();
                if (currentCommand == "End" || currentCommand == null)
                {
                    break;
                }

                int indexOfFirstOpeningBracket = currentCommand.IndexOf('(');
                if (indexOfFirstOpeningBracket == -1)
                {
                    throw new ArgumentException("Invalid command. It should have '(' for parameters!");
                }

                if (!currentCommand.EndsWith(")"))
                {
                    throw new ArgumentException("Invalid command. It should have ')' for parameters!");
                }

                string commandName = currentCommand.Substring(0, indexOfFirstOpeningBracket);

                string parameters = currentCommand.Substring(indexOfFirstOpeningBracket + 1, currentCommand.Length - indexOfFirstOpeningBracket - 2);
                var strings = parameters.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(m => m.Trim()).ToList();

                ICommand command;
                if (commandName.StartsWith("AddPhone") && strings.Count >= 2)
                {
                    command = new AddPhoneCommand(printer, repository, sanitizer);
                }
                else if (commandName == "ChangePhone" && strings.Count == 2)
                {
                    command = new ChangePhoneCommand(printer, repository, sanitizer);
                }
                else if (commandName == "List" && strings.Count == 2)
                {
                    command = new ListPhoneCommand(printer, repository);
                }
                else
                {
                    throw new InvalidOperationException("Invalid command name!");
                }

                command.Execute(strings);
            }

            Console.Write(printer.GetAllText());
        }
Esempio n. 2
0
        public ICommand CreateCommand(string commandName, string[] arguments)
        {
            ICommand command;
            if (commandName.StartsWith("AddPhone") && (arguments.Length >= 2))
            {
                command = new AddPhoneCommand(arguments, this.sanitizer, this.phonebookDatabase, this.printer);
            }
            else if ((commandName == "ChangePhone") && (arguments.Length == 2))
            {
                command = new ChangePhoneCommand(arguments, this.sanitizer, this.phonebookDatabase, this.printer);
            }
            else if ((commandName == "List") && (arguments.Length == 2))
            {
                command = new ListCommand(arguments, this.phonebookDatabase, this.printer);
            }
            else
            {
                throw new ArgumentException();
            }

            return command;
        }
Esempio n. 3
0
        public string ProceedCommand(Command command)
        {
            string result = string.Empty;
            IPhonebookCommand cmd = null;

            if (command.Name == "AddPhone")
            {
                cmd = new AddCommand(this.Printer, this.PhonebookRepository, this.PhoneConverter);
            }
            else if (command.Name == "ChangePhone")
            {
                cmd = new ChangePhoneCommand(this.Printer, this.PhonebookRepository, this.PhoneConverter);
            }
            else if (command.Name == "List")
            {
                cmd = new ListCommand(this.Printer, this.PhonebookRepository);
            }

            cmd.Execute(command);

            return result;
        }