Guest User

Untitled

a guest
Jul 5th, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ExcerciseForJohny
  8. {
  9.     class Program
  10.     {
  11.         public string[] CorrectnessOfDate(string word)
  12.         {
  13.             Console.WriteLine("Enter a date of {0} (DD-MM-YYYY):", word);
  14.             var date = Console.ReadLine();
  15.             string[] dateArray = date.Split('-');
  16.  
  17.             int check = 0;
  18.  
  19.             if (dateArray.Length == 3)
  20.                 check++;
  21.             else
  22.             {
  23.                 Console.Clear();
  24.                 Console.WriteLine("A Date is invalid! Try again.");
  25.                 CorrectnessOfDate(word);
  26.             }
  27.  
  28.             if (Convert.ToInt32(dateArray[0]) <= 31) check++;
  29.             if (Convert.ToInt32(dateArray[1]) <= 12) check++;
  30.  
  31.             if (check != 3)
  32.             {
  33.                 Console.Clear();
  34.                 Console.WriteLine("A Date is invalid! Try again.");
  35.                 CorrectnessOfDate(word);
  36.             }
  37.             return dateArray;
  38.         }
  39.         public void DateRange()
  40.         {
  41.             string[] start = CorrectnessOfDate("start");
  42.             string[] end = CorrectnessOfDate("end");
  43.  
  44.             int dayStart = Convert.ToInt32(start[0]);
  45.             int monthStart = Convert.ToInt32(start[1]);
  46.             int yearStart = Convert.ToInt32(start[2]);
  47.             int dayEnd = Convert.ToInt32(end[0]);
  48.             int monthEnd = Convert.ToInt32(end[1]);
  49.             int yearEnd = Convert.ToInt32(end[2]);
  50.  
  51.             Console.Clear();
  52.             Console.WriteLine("The date of start: {0}.{1}.{2}", dayStart, monthStart, yearStart);
  53.             Console.WriteLine("The date of end: {0}.{1}.{2}", dayEnd, monthEnd, yearEnd);
  54.  
  55.             Console.WriteLine("Data range:");
  56.  
  57.             if (dayStart == dayEnd && monthStart == monthEnd && yearStart == yearEnd)
  58.                 Console.WriteLine("There is only one day");
  59.             else if (yearStart < yearEnd)
  60.                 Console.WriteLine("{0}.{1}.{2} - {3}.{4}.{5}", dayStart, monthStart, yearStart, dayEnd, monthEnd, yearEnd);
  61.             else if (monthStart < monthEnd)
  62.                 Console.WriteLine("{0}.{1} - {2}.{3}.{4}", dayStart, monthStart, dayEnd, monthEnd, yearStart);
  63.             else if (dayStart < dayEnd)
  64.                 Console.WriteLine("{0} - {1}.{2}.{3}", dayStart, dayEnd, monthStart, yearStart);
  65.             else
  66.                 Console.WriteLine("the end date can not be before the start date.");
  67.         }
  68.         static void Main(string[] args)
  69.         {
  70.             var program = new Program();
  71.             program.DateRange();
  72.  
  73.             Console.ReadKey();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment