-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidation.cs
52 lines (46 loc) · 1.41 KB
/
Validation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Text.RegularExpressions;
namespace BirthFinder
{
class Validation
{
public delegate bool Validate(string line);
public bool ValidationChecks(string line)
{
Validate lengthDigitCheck = LengthDigitCheck;
Validate luhnCheck = LuhnCheck;
return lengthDigitCheck(line) ? (luhnCheck(line) ? true : false) : false;
}
private bool LengthDigitCheck(string line)
{
Regex regex = new Regex(@"^\d{13}$");
return regex.IsMatch(line);
}
private bool LuhnCheck(string line)
{
long sum = 0;
for (int i = 0; i < line.Length; i++)
{
var digit = line[line.Length - 1 - i] - '0';
sum += (i % 2 != 0) ? GetDouble(digit) : digit;
}
return sum % 10 == 0;
int GetDouble(long i)
{
switch (i)
{
case 0: return 0;
case 1: return 2;
case 2: return 4;
case 3: return 6;
case 4: return 8;
case 5: return 1;
case 6: return 3;
case 7: return 5;
case 8: return 7;
case 9: return 9;
default: return 0;
}
}
}
}
}