Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (61 loc) · 2.21 KB
/
Copy pathProgram.cs
File metadata and controls
70 lines (61 loc) · 2.21 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Linq;
namespace Person
{
public class IdUtils
{
/// <summary>
/// Valida un número de cédula de identidad; el número puede tener o no puntos y el guión pero debe tener el dígito verificador. Adaptado
/// de https://es.wikipedia.org/wiki/C%C3%A9dula_de_Identidad_de_Uruguay#Match_on_Card.
/// </summary>
/// <param name="id">El número de cédula</param>
/// <returns>Retorna true si el número de cédula es válido y false en caso contrario.</returns>
public static bool IdIsValid(string id)
{
long tempOut;
// Quitar puntos y guiones
id = id.Replace(".", "");
id = id.Replace("-", "");
// Validar largo
if (id.Length == 8 && long.TryParse(id, out tempOut))
{
var idAsCharArray = id.ToArray();
var idAsIntArray = idAsCharArray.Select(c => int.Parse(c.ToString())).ToArray();
var referenceArray = "2987634".ToArray().Select(c => int.Parse(c.ToString())).ToArray();
var inputCheckDigit = idAsIntArray[7];
// Calcular número verificador
int checkSum = 0;
for (int i = 0; i < referenceArray.Length; i++)
{
checkSum = checkSum + (idAsIntArray[i] * referenceArray[i]);
}
int checkDigit = 10 - (checkSum % 10);
if (checkDigit == 10)
{
checkDigit = 0;
}
if (checkDigit != inputCheckDigit)
{
/// Número verificador ingresado inválido
return false;
}
}
else
{
// Formato de cédula de identidad inválido
return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
Person john = new Person("John Doe", "1.234.567-2");
Person jane = new Person("Jane Doe", "8.765.432-7");
john.IntroduceYourself();
jane.IntroduceYourself();
}
}
}