public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var minimumParameters = parameters.MandatoryLength(); var maximumParameters = parameters.ParametersLength(); Parameters mandatoryAndOptional = new Parameters(consoleInput.Parameters); if (consoleInput.Parameters.Length >= minimumParameters && consoleInput.Parameters.Length <= maximumParameters) { var name = consoleInput[0]; var phone = consoleInput[1]; var born = ValidateDate(mandatoryAndOptional); var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); if (phoneBook.Add(name: name, phone: phone, birthdate: born)) { fileIO.WriteFile(ref phoneBook); if (born != string.Empty) { var bornT = new BirthDate(born); bornT.IsValid(new string[] { "yyyyMMdd" }); born = bornT.Format("yyyy-MM-dd"); } message = string.Format("Added: {0} => {1} => {2}", name, phone, born); } else { message = string.Format("Cannot add." + System.Environment.NewLine + "Reason: Duplicate name/phone for {0} => {1}", name, phone); } ////Console.WriteLine("Total phone numbers: {0}", phoneBook.Count()); message += System.Environment.NewLine; message += string.Format("Total phone numbers: {0}", phoneBook.Count()); } else { message += string.Format("This command requires:"); message += System.Environment.NewLine; message += string.Format("- mandatory parameters: {0}, {1}", parameters.Mandatory()[0].Name, parameters.Mandatory()[1].Name); message += System.Environment.NewLine; message += string.Format("- optional parameters: {0}", parameters.Optional()[0].Name); message += System.Environment.NewLine; message += string.Format("Command format: add {0}", string.Join(" ", parameters.Original)); } Console.WriteLine(Message()); } }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var minimumParameters = parameters.MandatoryLength(); var maximumParameters = parameters.ParametersLength(); if (consoleInput.Parameters.Length >= minimumParameters && consoleInput.Parameters.Length <= maximumParameters) { var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); string index = consoleInput[0]; var currentItem = new KeyValuePair<string, Contact>(); if (phoneBook.RemoveAt(index, out currentItem)) { // KeyValuePair<string, string> currentItem = phoneBook.GetElementTo(index); // phoneBook.Remove(currentItem); fileIO.WriteFile(ref phoneBook); ////Console.WriteLine("The contact '{0} => {1}: {2}' has been deleted.", new object[] { currentItem.Key, currentItem.Value.Name, currentItem.Value.PhoneNumber }); message = string.Format("The contact '{0} => {1}: {2}' has been deleted.", currentItem.Key, currentItem.Value.Name, currentItem.Value.PhoneNumber); } else ////Console.WriteLine("Your input must be an integer between 0 and {0}", new object[] { phoneBook.Count() }); message = string.Format("Your input '{0}' is not valid.", index); } else { message = string.Format("This command requires 1 mandatory parameter"); message += System.Environment.NewLine; message += string.Format("{0}", string.Join(" ", parameters.Mandatory()[0].Name)); } Console.WriteLine(Message()); } }