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); } }
private static void Main() { ISanitizer sanitizer = new Sanitizer(); IPhonebookRepository phonebookDatabase = new PhonebookRepository(); IPrinter printer = new StringBuilderPrinter(); ILineParser lineParser = new LineParser(sanitizer, phonebookDatabase, printer); while (true) { string inputLine = Console.ReadLine(); if (inputLine == "End" || inputLine == null) { break; } lineParser.ParseLine(inputLine); } Console.Write(printer.GetOutput()); }
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()); }