public void ExecuteCommand(PhonebookRepository phonebookRepository, Command command, StringBuilder output) { switch (command.Type) { case CommandTypes.AddPhone: AddPhoneCommand(phonebookRepository, command, output); break; case CommandTypes.ChangePhone: ChangePhoneCommand(phonebookRepository, command, output); break; case CommandTypes.List: ListCommand(phonebookRepository, command, output); break; default: throw new ArgumentException("Unknown command: " + command.Type.ToString()); } }
public static Command Parse(string commandString) { int braketsStartIndex = commandString.IndexOf('('); string commandName = commandString.Substring(0, braketsStartIndex); string commandBody = commandString.Substring(braketsStartIndex + 1, commandString.Length - braketsStartIndex - 2); string[] commandArguments = commandBody.Split(','); for (int j = 0; j < commandArguments.Length; j++) { commandArguments[j] = commandArguments[j].Trim(); } Command command = new Command(commandName, commandArguments); return command; }
private static void AddPhoneCommand(PhonebookRepository phonebookRepository, Command command, StringBuilder output) { var name = command.Parameters[0]; var numbers = command.Parameters.Skip(1).ToList(); for (int i = 0; i < numbers.Count; i++) { numbers[i] = Convertnumber(numbers[i]); } var isExist = phonebookRepository.AddPhone(name, numbers); switch (isExist) { case true: output.AppendLine("Phone entry merged"); break; default: output.AppendLine("Phone entry created"); break; } }
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; }
private static void ChangePhoneCommand(PhonebookRepository phonebookRepository, Command command, StringBuilder output) { var changeResult = phonebookRepository.ChangePhone(Convertnumber(command.Parameters[0]), Convertnumber(command.Parameters[1])); output.AppendLine("" + changeResult + " numbers changed"); }
private static void ListCommand(PhonebookRepository phonebookRepository, Command command, StringBuilder output) { try { var entries = phonebookRepository.ListEntries(int.Parse(command.Parameters[0]), int.Parse(command.Parameters[1])); foreach (var entry in entries) { output.AppendLine(entry.ToString()); } } catch (ArgumentOutOfRangeException) { output.AppendLine("Invalid range"); } }