示例#1
0
        public FormatterResult <int> GetInt(string input)
        {
            FormatterResult <int> res            = new FormatterResult <int>();
            FormatterError        formatterError = new FormatterError();

            if (input == null)
            {
                res.Errors.Add(formatterError.Create("String is null"));
                return(res);
            }

            if (input.Length == 0)
            {
                res.Errors.Add(formatterError.Create("String is empty"));
                return(res);
            }

            int val = 0;

            if (!int.TryParse(input, out val))
            {
                res.Errors.Add(formatterError.Create("Cant transform the string to int number"));
                return(res);
            }
            if (val <= 0)
            {
                res.Errors.Add(formatterError.Create("Can be only positive number"));
                return(res);
            }
            res.Value = val;

            return(res);
        }
示例#2
0
        public FormatterResult <string> GetString(string input)
        {
            FormatterResult <string> res            = new FormatterResult <string>();
            FormatterError           formatterError = new FormatterError();

            if (input == null)
            {
                res.Errors.Add(formatterError.Create("String is null"));
                return(res);
            }

            if (input.Length == 0)
            {
                res.Errors.Add(formatterError.Create("String is empty"));
                return(res);
            }

            Regex regex = new Regex("[a-zA-Z]");

            if (!regex.IsMatch(input))
            {
                res.Errors.Add(formatterError.Create("String hasn't any word"));
                return(res);
            }

            res.Value = input;

            return(res);
        }
示例#3
0
        public FormatterResult <DateTime> GetDateTime(string input)
        {
            FormatterResult <DateTime> res            = new FormatterResult <DateTime>();
            FormatterError             formatterError = new FormatterError();

            if (input == null)
            {
                res.Errors.Add(formatterError.Create("String is null"));
                return(res);
            }

            if (input.Length == 0)
            {
                res.Errors.Add(formatterError.Create("String is empty"));
                return(res);
            }

            DateTime val;

            if (!DateTime.TryParse(input, out val))
            {
                res.Errors.Add(formatterError.Create("Cant transform the string to datetime format"));
                return(res);
            }

            res.Value = val;

            return(res);
        }
示例#4
0
        private static DateTime GetDateTime()
        {
            FormatterResult <DateTime> res             = null;
            StringFormatter            stringFormatter = new StringFormatter();

            do
            {
                string input = Console.ReadLine();
                res = stringFormatter.GetDateTime(input);

                if (res.IsValid)
                {
                    Console.WriteLine("All right");
                }
                else
                {
                    Console.WriteLine("Errors:");
                    string s = string.Join("\n", res.Errors.Select(str => str.ErrorMessage));
                    Console.WriteLine(s);
                }
            }while (!res.IsValid);

            return(res.Value);
        }