Advertisement
G_Burlakova

EGN

Feb 20th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ValidatonNumberCopy1
  7. {
  8.     class Program
  9.     {
  10.         public static int RemainingDaysToBirthday(DateTime birthday)
  11.         {
  12.             var nextBirthday = birthday.AddYears(DateTime.Today.Year - birthday.Year);
  13.             if (nextBirthday < DateTime.Today)
  14.             {
  15.                 nextBirthday = nextBirthday.AddYears(1);
  16.             }
  17.             return (nextBirthday - DateTime.Today).Days;
  18.         }
  19.         static void Main(string[] args)
  20.         {
  21.             try
  22.             {
  23.  
  24.                 bool correctCount = true;
  25.                 bool correctMonth = true;
  26.                 bool correctSixthSeventh = true;
  27.                 bool correctValidationNumber = true;
  28.                 bool correctDayOfBirth = true;
  29.                 string monthStr = "";
  30.                 int month = 0;
  31.                 string yearLastDigitsStr = "";
  32.                 int yearLastDigits = 0;
  33.                 string dateOfBirthStr = "";
  34.                 int dayOfBirth = 0;
  35.                 long[] arrayDigits = new long[0];
  36.                 do
  37.                 {
  38.                     Console.Write("Enter EGN: ");
  39.                     string EGNStr = Console.ReadLine();
  40.                     //string EGNStr = "9203127630";
  41.                     int digitsCount = EGNStr.Length;
  42.  
  43.                     // Check for exactly 10 digits
  44.                     correctCount = true;
  45.                     if (digitsCount != 10)
  46.                     {
  47.                         correctCount = false;
  48.                         continue;
  49.                     }
  50.  
  51.                     long EGN = long.Parse(EGNStr);
  52.                     arrayDigits = new long[digitsCount];
  53.                     for (int i = 0; i < arrayDigits.Length; i++)
  54.                     {
  55.                         arrayDigits[(arrayDigits.Length - 1) - i] = EGN % 10;
  56.                         EGN = EGN / 10;
  57.                     }
  58.  
  59.  
  60.                     // Check for correct month
  61.                     monthStr = EGNStr.Substring(2, 2);
  62.                     month = int.Parse(monthStr);
  63.                     correctMonth = ((month > 0 && month < 13) || (month > 20 && month < 33) || (month > 40 && month < 53));
  64.                     if (!correctMonth)
  65.                     {
  66.                         correctMonth = false;
  67.                         continue;
  68.                     }
  69.  
  70.                     // Check for correct date of birth
  71.                     dateOfBirthStr = EGNStr.Substring(4, 2);
  72.                     dayOfBirth = int.Parse(dateOfBirthStr);
  73.                     if (!(dayOfBirth > 0 && dayOfBirth < 32))
  74.                     {
  75.                         correctDayOfBirth = false;
  76.                         continue;
  77.                     }
  78.  
  79.                     // Check for sixth and seventh digit
  80.                     string sixthAndSeventhDigitStr = EGNStr.Substring(6, 2);
  81.                     int sixthAndSeventhDigit = int.Parse(sixthAndSeventhDigitStr);
  82.                     correctSixthSeventh = true;
  83.                     if (!(sixthAndSeventhDigit != 0 && sixthAndSeventhDigit != 88))
  84.                     {
  85.                         correctSixthSeventh = false;
  86.                         continue;
  87.                     }
  88.  
  89.                     // Check validation number
  90.                     int[] validationDigits = { 2, 4, 8, 5, 10, 9, 7, 3, 6 };
  91.                     long validationSum = 0;
  92.                     for (int i = 0; i < validationDigits.Length; i++)
  93.                     {
  94.                         validationSum = validationSum + (arrayDigits[i] * validationDigits[i]);
  95.                     }
  96.                     long validationNumber;
  97.                     long residue = validationSum / 11;
  98.                     if (residue > 10)
  99.                     {
  100.                         validationNumber = 0;
  101.                     }
  102.                     else
  103.                     {
  104.                         validationNumber = validationSum / 11;
  105.                     }
  106.                     correctValidationNumber = (validationNumber == arrayDigits[9]);
  107.                     yearLastDigitsStr = EGNStr.Substring(0, 2);
  108.                     yearLastDigits = int.Parse(yearLastDigitsStr);
  109.                 } while (!(correctCount && correctMonth && correctSixthSeventh && correctValidationNumber && correctDayOfBirth));
  110.  
  111.                 Console.WriteLine("Personal information: ");
  112.                 Console.Write("- Gender: ");
  113.                 string gender = "";
  114.                 if (arrayDigits[8] % 2 == 0)
  115.                 {
  116.                     gender = "male";
  117.                 }
  118.                 else
  119.                 {
  120.                     gender = "female";
  121.                 }
  122.                 Console.WriteLine(gender);
  123.  
  124.                 int yearOfBirth = 0;
  125.                 if (month > 0 && month < 13)
  126.                 {
  127.                     yearOfBirth = 1900 + yearLastDigits;
  128.                 }
  129.                 else if (month > 20 && month < 33)
  130.                 {
  131.                     yearOfBirth = 1800 + yearLastDigits;
  132.                     month = month - 20;
  133.                 }
  134.                 else
  135.                 {
  136.                     yearOfBirth = 2000 + yearLastDigits;
  137.                     month = month - 40;
  138.                 }
  139.                 DateTime date = DateTime.Now;
  140.                 int currentYear = date.Year;
  141.                 int age = currentYear - yearOfBirth;
  142.                 Console.Write("- Age: ");
  143.                 Console.WriteLine(age);
  144.  
  145.                 Console.Write("- Date of birth: ");
  146.                 DateTime birthday = new DateTime(yearOfBirth, month, dayOfBirth);
  147.                 string format = "dd MMMMMMMMMM yyyy";
  148.                 Console.WriteLine(birthday.Date.ToString(format));
  149.  
  150.                 Console.Write("- Days remaining to the next birthday: ");
  151.                 int remainingDays = RemainingDaysToBirthday(birthday);
  152.                 Console.WriteLine(remainingDays);
  153.             }
  154.             catch (FormatException)
  155.             {
  156.                 Console.WriteLine("You have entered a number containing some characters.");
  157.             }
  158.  
  159.  
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement