Advertisement
Filkolev

PIN Validation

May 27th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class PinValidation
  5. {
  6.     public static void Main()
  7.     {
  8.         const string NamePattern = @"^[A-Z][a-z]+ [A-Z][a-z]+$";
  9.         const string PinPattern = @"^\d{10}$";
  10.  
  11.         int[] checksumMultipliers = { 2, 4, 8, 5, 10, 9, 7, 3, 6 };
  12.  
  13.         string name = Console.ReadLine();
  14.         string gender = Console.ReadLine();
  15.         string pin = Console.ReadLine();
  16.  
  17.         if (!Regex.IsMatch(name, NamePattern))
  18.         {
  19.             Console.WriteLine("<h2>Incorrect data</h2>");
  20.             return;
  21.         }
  22.  
  23.         if (gender != "male" && gender != "female")
  24.         {
  25.             Console.WriteLine("<h2>Incorrect data</h2>");
  26.             return;
  27.         }
  28.  
  29.         if (!Regex.IsMatch(pin, PinPattern))
  30.         {
  31.             Console.WriteLine("<h2>Incorrect data</h2>");
  32.             return;
  33.         }
  34.  
  35.         int year = int.Parse(pin.Substring(0, 2));
  36.         int month = int.Parse(pin.Substring(2, 2));
  37.         int day = int.Parse(pin.Substring(4, 2));
  38.  
  39.         int genderNum = pin[8] - '0';
  40.  
  41.         if (21 <= month && month <= 32)
  42.         {
  43.             year += 1800;
  44.             month -= 20;
  45.         }
  46.         else if (41 <= month && month < 52)
  47.         {
  48.             year += 2000;
  49.             month -= 40;
  50.         }
  51.         else if (1 <= month && month <= 12)
  52.         {
  53.             year += 1900;
  54.         }
  55.         else
  56.         {
  57.             Console.WriteLine("<h2>Incorrect data</h2>");
  58.             return;
  59.         }
  60.  
  61.         try
  62.         {
  63.             new DateTime(year, month, day);
  64.         }
  65.         catch (Exception ex)
  66.         {
  67.             Console.WriteLine("<h2>Incorrect data</h2>");
  68.             return;
  69.         }
  70.  
  71.         if (gender == "male" && genderNum % 2 != 0)
  72.         {
  73.             Console.WriteLine("<h2>Incorrect data</h2>");
  74.             return;
  75.         }
  76.  
  77.         if (gender == "female" && genderNum % 2 == 0)
  78.         {
  79.             Console.WriteLine("<h2>Incorrect data</h2>");
  80.             return;
  81.         }
  82.  
  83.         int checksum = 0;
  84.  
  85.         for (int i = 0; i < 9; i++)
  86.         {
  87.             checksum += checksumMultipliers[i] * (pin[i] - '0');
  88.         }
  89.  
  90.         int hash = checksum % 11;
  91.  
  92.         if (hash == 10)
  93.         {
  94.             hash = 0;
  95.         }
  96.  
  97.         if (hash != pin[9] - '0')
  98.         {
  99.             Console.WriteLine("<h2>Incorrect data</h2>");
  100.             return;
  101.         }
  102.  
  103.         Console.WriteLine(
  104.             "{{\"name\":\"{0}\",\"gender\":\"{1}\",\"pin\":\"{2}\"}}",
  105.             name,
  106.             gender,
  107.             pin);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement