예제 #1
0
        public void FilterByPartialName_TwoResults()
        {
            PhoneBook phoneBook = new PhoneBook();
            phoneBook.Add("Nick", "0744596866");
            phoneBook.Add("John Smith", "0745516", "3gd");
            phoneBook.Add("Samuel", "444");

            string partialName = "i";
            string pattern = @"(" + partialName + @")";
            phoneBook.FilterBy(pattern, "name");

            Assert.AreEqual(2, phoneBook.Count());
        }
예제 #2
0
        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";
            }
        }