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()); }
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); } }