示例#1
0
        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";
            }
        }
示例#2
0
        public void ListBirthdayTomorrow()
        {
            PhoneBook phoneBook = new PhoneBook();
            phoneBook.Add("John Smith", "0745516", "20120327", "rty");
            phoneBook.Add("Nick", "0744596866", "20120326", "gyu");
            phoneBook.Add("Samuel", "444", "20120327", "fgh");

            phoneBook.Backup();
            DateTime today = new DateTime(2016, 03, 26);
            var startRange = today.AddDays(1);
            var endRange = today.AddDays(1);
            DateTime[] birthdayRange;
            birthdayRange = new DateTime[] { startRange, endRange };
            phoneBook.BirthdayRange(birthdayRange);

            Assert.AreEqual(2, phoneBook.Count());
        }
示例#3
0
        public void ListBirthdayNextWeek()
        {
            PhoneBook phoneBook = new PhoneBook();
            phoneBook.Add("John Smith 2", "0745516", "20160329", "bnm"); // Tuesday
            phoneBook.Add("Nickk", "0744596866", "20120328", "bbb"); // Monday
            phoneBook.Add("Samuelo", "44334", "20120403", "vdf"); // Sunday => next week

            phoneBook.Backup();
            DateTime today = new DateTime(2016, 03, 28);
            var dayOfWeek = (int)today.DayOfWeek;
            var startRange = today.AddDays(0 - dayOfWeek + 7);
            var endRange = today.AddDays(6 - dayOfWeek + 7);
            DateTime[] birthdayRange;
            birthdayRange = new DateTime[] { startRange, endRange };
            phoneBook.BirthdayRange(birthdayRange);

            Assert.AreEqual(1, phoneBook.Count());
        }
示例#4
0
        public void ListBirthdayThisWeek()
        {
            PhoneBook phoneBook = new PhoneBook();
            phoneBook.Add("John Smith", "0745516", "20160329", "rty"); // Tuesday
            phoneBook.Add("Nick", "0744596866", "20120328", "gyu"); // Monday
            phoneBook.Add("Samuel", "444", "20100326", "iop"); // Sunday

            phoneBook.Backup();
            DateTime today = new DateTime(2016, 04, 02);
            var dayOfWeek = (int)today.DayOfWeek;
            var startRange = today.AddDays(0 - dayOfWeek);
            var endRange = today.AddDays(6 - dayOfWeek);
            DateTime[] birthdayRange;
            birthdayRange = new DateTime[] { startRange, endRange };
            phoneBook.BirthdayRange(birthdayRange);

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