Advertisement
dimipan80

PIN Validation

May 27th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | None | 0 0
  1. /* Your task is to write a program that reads data about the voters in an electronic poll: first and last name, gender and PIN (EGN in Bulgarian) and then verifies the PIN. The program should generate a JSON string for DB insert if the data is correct, or print "Incorrect data" in <h2></h2> heading tags.
  2.  * The PIN is a 10-digit number, which consists of the following: First 6 digits are the date of birth of the citizen in format yymmdd; if the person is born before 1900, the mm digits are +20. If the person is born after 2000, the mm digits are +40; Next 3 digits show the region, based on the regional city of birth; The last of the above 3 digits shows the gender – even for male and odd for female; One digit for checksum. In order to get the correct checksum you need to multiply each of the first 9 digits with [2,4,8,5,10,9,7,3,6] respectively, sum all and then divide by 11. The remainder is the checksum.
  3.  * NOTE: if the remainder is 10, then the checksum is 0!
  4.  * The first and last name will be received on the first input line. The gender will be received on the second line. The PIN will be received on the third line.
  5.  * If the PIN is not correct or the data is not in the format described, "Incorrect data" should be printed. Otherwise, print a JSON string with all the data. */
  6.  
  7. namespace PIN_Validation
  8. {
  9.     using System;
  10.     using System.Globalization;
  11.     using System.Text.RegularExpressions;
  12.     using System.Threading;
  13.  
  14.     class PINValidation
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  19.  
  20.             string names = Console.ReadLine().Trim();
  21.             string gender = Console.ReadLine().Trim();
  22.             string pinNum = Console.ReadLine().Trim();
  23.  
  24.             if (!Regex.IsMatch(names, @"^[A-Z][a-z]+\s[A-Z][a-z]+$") ||
  25.                 (gender != "male" && gender != "female") ||
  26.                 !Regex.IsMatch(pinNum, @"^\d{10}$"))
  27.             {
  28.                 Console.WriteLine("<h2>Incorrect data</h2>");
  29.                 return;
  30.             }
  31.  
  32.             int sexDigit = int.Parse(pinNum[8].ToString()) % 2;
  33.             if ((gender == "male" && sexDigit != 0) ||
  34.                 (gender == "female" && sexDigit == 0))
  35.             {
  36.                 Console.WriteLine("<h2>Incorrect data</h2>");
  37.                 return;
  38.             }
  39.  
  40.             string year = pinNum.Substring(0, 2);
  41.             string month = pinNum.Substring(2, 2);
  42.             string dayOfMonth = pinNum.Substring(4, 2);
  43.  
  44.             if (int.Parse(month) > 40)
  45.             {
  46.                 year = "20" + year;
  47.                 month = (int.Parse(month) - 40).ToString();
  48.             }
  49.             else if (int.Parse(month) > 20)
  50.             {
  51.                 year = "18" + year;
  52.                 month = (int.Parse(month) - 20).ToString();
  53.             }
  54.             else
  55.             {
  56.                 year = "19" + year;
  57.             }
  58.  
  59.             string dateTimeStr = string.Format("{0}-{1}-{2}", year, month, dayOfMonth);
  60.             DateTime parsedDate = new DateTime();
  61.             if (!DateTime.TryParse(dateTimeStr, out parsedDate))
  62.             {
  63.                 Console.WriteLine("<h2>Incorrect data</h2>");
  64.                 return;
  65.             }
  66.  
  67.             int[] multipliers = { 2, 4, 8, 5, 10, 9, 7, 3, 6 };
  68.             int totalSum = 0;
  69.             for (int i = 0; i < multipliers.Length; i++)
  70.             {
  71.                 int digit = int.Parse(pinNum[i].ToString());
  72.                 totalSum += digit * multipliers[i];
  73.             }
  74.  
  75.             totalSum = (totalSum % 11) % 10;
  76.             int checksum = int.Parse(pinNum[9].ToString());
  77.             if (totalSum != checksum)
  78.             {
  79.                 Console.WriteLine("<h2>Incorrect data</h2>");
  80.             }
  81.             else
  82.             {
  83.                 Console.WriteLine(@"{{""name"":""{0}"",""gender"":""{1}"",""pin"":""{2}""}}", names, gender, pinNum);
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement