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