public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); var maximumParameters = parameters.OptionalLength(); Parameters mandatoryAndOptional = new Parameters(consoleInput.Parameters); inputParameters = mandatoryAndOptional.Mandatory(); if (HasMaximumRequiredParameters(maximumParameters)) { var po = parameters.ParametersAreValid("optional", inputParameters); if (!po) Console.WriteLine("OPTIONAL parameters are not ok!"); if (!po) { Console.WriteLine("Command usage: " + Help()); } else { phoneBook.Backup(); string filterType = string.Empty; if (inputParameters.Length > 0) { DateTime[] birthdayRange; DateTime today = DateTime.UtcNow.ToLocalTime(); var dayOfWeek = (int)today.DayOfWeek; var startRange = today; var endRange = today; filterType = inputParameters[0].Value; ////Console.WriteLine("UTC: " + today); ////Console.WriteLine("Local: " + today.ToLocalTime()); switch (filterType) { case "tomorrow": startRange = today.AddDays(1); endRange = today.AddDays(1); break; case "thisweek": startRange = today.AddDays(0 - dayOfWeek); endRange = today.AddDays(6 - dayOfWeek); break; case "nextweek": startRange = today.AddDays(0 - dayOfWeek + 7); endRange = today.AddDays(6 - dayOfWeek + 7); break; default: filterType = "today"; break; } birthdayRange = new DateTime[] { startRange, endRange }; phoneBook.BirthdayRange(birthdayRange); } if (phoneBook.Count() == 0) { if (filterType != string.Empty) Console.WriteLine(string.Format("There is not contact which has the birthday {0}.", new object[] { filterType })); else Console.WriteLine("The phonebook is empty."); } else new ListConsole(phoneBook, filterType); phoneBook.Restore(); } } message = "done"; } }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var minimumParameters = parameters.MandatoryLength(); Parameters mandatoryAndOptional = new Parameters(consoleInput.Parameters); inputParameters = mandatoryAndOptional.Mandatory(); if (inputParameters.Length >= minimumParameters) { string findWord = inputParameters[0].Value; string pattern = @"(" + findWord + @")"; var design = new TableDesign(); var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); phoneBook.Backup(); phoneBook.FilterBy(pattern, "phone"); int counted = phoneBook.Count(); if (counted == 0) { Console.WriteLine(string.Format("No result.")); } else { Console.WriteLine(string.Format("INDEX" + design.SpaceLength(8, 5) + "| NAME" + design.SpaceLength(30, 4) + "| PHONE")); Console.WriteLine(string.Format(design.SpaceLength(50, 0, "-"))); var contactNo = 0; foreach (KeyValuePair<string, Contact> currentItem in phoneBook) { if (contactNo > 0) { Console.WriteLine(); } string index = currentItem.Key; string name = currentItem.Value.Name; string phone = currentItem.Value.PhoneNumber; string lineString = string.Format("{0}" + design.SpaceLength(8, index.ToString().Length) + "| {1}" + design.SpaceLength(30, name.Length) + "| {2}", index, name, phone); string[] splittedLine = lineString.Split(new string[] { findWord }, StringSplitOptions.None); var wordNo = 0; foreach (string split in splittedLine) { if (wordNo > 0) { Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.White; Console.Write(findWord); Console.ResetColor(); } Console.Write(split); wordNo++; } contactNo++; } } phoneBook.Restore(); } else Console.WriteLine(Help()); message += "it's done"; } }