Пример #1
0
        private static void ParseDateBetween(DateTime dateTimeStart, DateTime dateTimeEnd)
        {
            InvalidRangeException <DateTime> dateTimeRange = new InvalidRangeException <DateTime>()
            {
                RangeStart = dateTimeStart, RangeEnd = dateTimeEnd
            };

            try
            {
                Console.WriteLine($"Input a year");
                int year = ParseNumberBetween(1, 9999);

                Console.WriteLine($"Input a month");
                int month = ParseNumberBetween(1, 12);

                Console.WriteLine($"Input a day");
                int day = ParseNumberBetween(1, 31);

                DateTime date = new DateTime(year, month, day);
                if (date < dateTimeRange.RangeStart || date > dateTimeRange.RangeEnd)
                {
                    throw dateTimeRange;
                }
            }
            catch (InvalidRangeException <DateTime> e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
Пример #2
0
        static int ParseNumberBetween(int start, int end)
        {
            InvalidRangeException <int> intRange = new InvalidRangeException <int>()
            {
                RangeStart = start, RangeEnd = end
            };

            Console.WriteLine($"Input a number between {intRange.RangeStart} - {intRange.RangeEnd}");
            int number = Int32.Parse(Console.ReadLine());

            try
            {
                if (number < intRange.RangeStart || number > intRange.RangeEnd)
                {
                    throw intRange;
                }
            }
            catch (InvalidRangeException <int> e)
            {
                Console.WriteLine(e.Message);
                throw;
            }

            return(number);
        }
Пример #3
0
        public static void Main(string[] args)
        {
            DateTime date = DateTime.Now;

            Console.WriteLine(date);

            InvalidRangeException <int> intOutOfRangeException =
                new InvalidRangeException <int>("Enter number from 1 to 100!", 1, 100);

            for (int i = 0; i < 10; i++)
            {
                int number = int.Parse(Console.ReadLine());

                if (number < intOutOfRangeException.MinValue || number > intOutOfRangeException.MaxValue)
                {
                    throw intOutOfRangeException;
                }
                else
                {
                    Console.WriteLine("Correct number: {0}", number);
                }
            }

            string someDate  = "23/6/2008";
            string otherDate = "12/9/2012";

            InvalidRangeException <DateTime> outOfRangeDateTimeException = new InvalidRangeException <DateTime>(
                string.Format("Insert date from {0} to {1}", someDate, otherDate), DateTime.Parse(someDate), DateTime.Parse(otherDate));

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Insert date:");

                DateTime myDate = DateTime.Parse(Console.ReadLine());

                if (myDate.Year < outOfRangeDateTimeException.MinValue.Year ||
                    myDate.Year > outOfRangeDateTimeException.MaxValue.Year)
                {
                    throw outOfRangeDateTimeException;
                }
                else
                {
                    Console.WriteLine("Correct date!");
                }
            }
        }