예제 #1
0
        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());
            }
        }
예제 #2
0
        public void Format()
        {
            var date = "20110911";
            var birthDate = new BirthDate(date);
            var pattern = new string[] { "yyyy-MM-dd" };
            var valid = birthDate.IsValid(pattern);
            var newDate = birthDate.Format("MM-dd-yyyy");

            Assert.AreNotEqual("09-11-2011", newDate);
        }
예제 #3
0
        private string ValidateDate(Parameters mandatoryAndOptional)
        {
            string inputDateValue = string.Empty;
            var pattern = "yyyy-MM-dd";

            if (mandatoryAndOptional.MandatoryLength() >= 3)
            {
                inputDateValue = mandatoryAndOptional.Mandatory()[2].Value;
                var birthDate = new BirthDate(inputDateValue);
                if (birthDate.IsValid(new string[] { pattern }))
                {
                    return birthDate.Format("yyyyMMdd");
                }

                Console.WriteLine("Can't convert '{0}' to YYYY-MM-DD.", inputDateValue);
            }

            return string.Empty;
        }