public ICommand Create(IPhonebookCommand commandInfo)
        {
            ICommand command = null;

            if (commandInfo.Name == AddPhoneCommandName && commandInfo.Arguments.Length >= 2)
            {
                command = this.addPhoneCommand.Value;
            }
            else if (commandInfo.Name == ChangePhoneCommandName && commandInfo.Arguments.Length == 2)
            {
                command = this.changePhoneCommand.Value;
            }
            else if (commandInfo.Name == DeletePhoneCommandName && commandInfo.Arguments.Length == 1)
            {
                command = this.deletePhoneCommand.Value;
            }
            else if (commandInfo.Name == ListPhonesCommandName && commandInfo.Arguments.Length == 2)
            {
                command = this.listPhonesCommand.Value;
            }
            else
            {
                throw new ArgumentException("Invalid command info parameters.");
            }

            return command;
        }
Esempio n. 2
0
        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);
        }
        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;
        }
        public static void Main()
        {
            var commandsFactory             = new PhonebookCommandsFactory();
            IPhonebookRepository repository = new PhonebookRepositoryWithDictionary();
            var parser = new CommandParser();

            // TODO: To extract Print class
            StringBuilder input = new StringBuilder();

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

                var commandInfo = parser.Parse(currentLine);

                IPhonebookCommand command = commandsFactory.GetCommand(commandInfo.CommandString);
                command.Execute(commandInfo.Arguments, repository);
            }

            Console.Write(input);
        }
Esempio n. 5
0
        public ICommand Create(IPhonebookCommand commandInfo)
        {
            ICommand command = null;

            if (commandInfo.Name == AddPhoneCommandName && commandInfo.Arguments.Length >= 2)
            {
                command = this.addPhoneCommand.Value;
            }
            else if (commandInfo.Name == ChangePhoneCommandName && commandInfo.Arguments.Length == 2)
            {
                command = this.changePhoneCommand.Value;
            }
            else if (commandInfo.Name == DeletePhoneCommandName && commandInfo.Arguments.Length == 1)
            {
                command = this.deletePhoneCommand.Value;
            }
            else if (commandInfo.Name == ListPhonesCommandName && commandInfo.Arguments.Length == 2)
            {
                command = this.listPhonesCommand.Value;
            }
            else
            {
                throw new ArgumentException("Invalid command info parameters.");
            }

            return(command);
        }
        public ICommand Create(IPhonebookCommand commandInfo)
        {
            if (commandInfo == null)
            {
                throw new NullReferenceException("Input command instance cannot be null.");
            }

            ICommand selectedCommand;
            this.commands.TryGetValue(commandInfo.Name, out selectedCommand);
            if (selectedCommand == null)
            {
                throw new ArgumentException("You have entered unknown command name.");
            }

            return selectedCommand;
        }
        public ICommand Create(IPhonebookCommand commandInfo)
        {
            if (commandInfo == null)
            {
                throw new NullReferenceException("Input command instance cannot be null.");
            }

            ICommand selectedCommand;

            this.commands.TryGetValue(commandInfo.Name, out selectedCommand);
            if (selectedCommand == null)
            {
                throw new ArgumentException("You have entered unknown command name.");
            }

            return(selectedCommand);
        }
        public IPhonebookCommand CreateCommand(string commandName, int argumentsCount)
        {
            IPhonebookCommand command;

            if (commandName.StartsWith("AddPhone") && argumentsCount >= 2)
            {
                if (this.addCommand == null)
                {
                    this.addCommand = new AddPhonebookCommand(this.consoleLogger, this.data, this.numberFormatter);
                }

                command = this.addCommand;
            }
            else if ((commandName == "ChangePhone") && (argumentsCount == 2))
            {
                if (this.changeCommand == null)
                {
                    this.changeCommand = new ChangePhonebookCommand(this.consoleLogger, this.data, this.numberFormatter);
                }

                command = this.changeCommand;
            }
            else if ((commandName == "List") && (argumentsCount == 2))
            {
                if (this.listCommand == null)
                {
                    this.listCommand = new ListPhonebookCommand(this.consoleLogger, this.data);
                }

                command = this.listCommand;
            }
            else
            {
                throw new StackOverflowException();
            }

            return command;
        }
Esempio n. 9
0
        internal static void Main()
        {
            IPhonebookRepository  data           = new PhonebookRepositoryWithDictionary();
            IPrinter              printer        = new StringBuilderPrinter();
            IPhoneNumberSanitizer sanitizer      = new PhoneNumberSanitizer();
            ICommandFactory       commandFactory = new CommandFactoryWithLazyLoading(data, printer, sanitizer);
            ICommandParser        commandParser  = new CommandParser();

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

                var commandInfo           = commandParser.Parse(userInput);
                IPhonebookCommand command = commandFactory.CreateCommand(commandInfo.CommandName, commandInfo.Arguments.Count());
                command.Execute(commandInfo.Arguments.ToArray());
            }

            printer.Accept(new ConsolePrinterVisitorWithNewLine());
        }