Bonjour,
Ci-joint, une petite participation avec une classe qui teste la validité d'un NIR (numéro d'inscription au répertoire des personnes physiques / le numéro de sécurité sociale) ou qui en calcule la clef.
NIR.cs
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 using System; using System.Linq; using System.Text; namespace Developpez.Dotnet.Algorithms { /// <summary> /// Outil de vérification des NIR (numéro d'inscription au répertoire des personnes physiques) /// usuellement dénommé "numéro de sécurité sociale en France" /// http://fr.wikipedia.org/wiki/Num%C3%A9ro_de_s%C3%A9curit%C3%A9_sociale_en_France /// </summary> public static class NIR { /// <summary> /// Calcule la clé d'un NIR /// </summary> /// <param name="nir">NIR dont on désire calculer la clef</param> /// <returns>La clef calculé.</returns> /// <exception cref="ArgumentException">Le NIR est invalide</exception> /// <exception cref="ArgumentNullException">Le NIR est null</exception> public static string CalcKey(string nir) { if (nir.Count() != 13) { throw new ArgumentException("Le NIR doit être d'une longueur de 13 caractères"); } string dep = nir.Substring(5, 2); string newDep = ""; if (dep == "2A") { newDep = "19"; } else if (dep == "2B") { newDep = "18"; } if (newDep != "") { nir = nir.Left(5) + newDep + nir.Right(6); } decimal nirAsDecimal; if (!decimal.TryParse(nir, out nirAsDecimal)) { throw new ArgumentException("Le NIR contient des caractères invalide." + " Il doit être composé de 13 chiffres à l'exception du département qui peut être 2A ou 2B."); } return (97 - (nirAsDecimal % 97)).ToString("00"); } /// <summary> /// Vérifie la validité d'un couple NIR / Clef /// </summary> /// <param name="nir">NIR à controller.</param> /// <param name="nirKey">Clef à controller.</param> /// <returns>Vrai si la clef est valide pour ce NIR, faux sinon.</returns> /// <exception cref="ArgumentException">Le NIR ou la clef sont invalides</exception> /// <exception cref="ArgumentNullException">Le NIR ou la clef sont null</exception> public static bool Check(string nir, string nirKey) { int dummyInt; if (nirKey.Count() != 2 || !int.TryParse(nirKey, out dummyInt)) { throw new ArgumentException("La clef doit être une chaine de 2 chiffres"); } try { return CalcKey(nir) == nirKey; } catch { throw; } } } }
NIRTests.cs
Si pour intégrer cette classe dans le projet, il est nécessaire de suivre une procédure différente, indiquez moi le.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
71
72
73
74
75 using System; using Developpez.Dotnet.Algorithms; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Algorithms.NIRTests { [TestFixture] class NIRTests { [Test] public void Test_CalcNIRKey() { string nir = "1750175137262"; string key = NIR.CalcKey(nir); Assert.AreEqual(key, "79"); } [Test] public void Test_CalcNIRKey2A() { string nir = "175012A137262"; string key = NIR.CalcKey(nir); Assert.AreEqual(key, "39"); } [Test] public void Test_CalcNIRKey2B() { string nir = "175012B137262"; string key = NIR.CalcKey(nir); Assert.AreEqual(key, "66"); } [Test] public void Test_KeyMustBeTwoCharacter() { string nir = "1640183137080"; string key = NIR.CalcKey(nir); Assert.AreEqual(key, "02"); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void Test_CheckNull() { string nir = null; string key = NIR.CalcKey(nir); Assert.AreEqual(key, "66"); } [Test] [ExpectedException(typeof(ArgumentException))] public void Test_BadLength() { string nir = "12345"; string key = NIR.CalcKey(nir); Assert.AreEqual(key, "66"); } [Test] [ExpectedException(typeof(ArgumentException))] public void Test_BadCharacterInNIR() { string nir = "175012C137262"; string key = NIR.CalcKey(nir); Assert.AreEqual(key, "66"); } [Test] public void Test_CheckNIRAndKey() { string nir = "1750175137262"; string key = "79"; Assert.IsTrue(NIR.Check(nir, key)); } [Test] [ExpectedException(typeof(ArgumentException))] public void Test_CheckNIRBadKeyLength() { string nir = "1750175137262"; string key = "1"; bool res = NIR.Check(nir, key); } [Test] [ExpectedException(typeof(ArgumentException))] public void Test_CheckNIRKeyMustBeTwoNumbers() { string nir = "1750175137262"; string key = "A"; bool res = NIR.Check(nir, key); } } }
Vu que je ne suis pas habitué à participer à des projets publics, n'hésitez pas à faire tout commentaires nécessaire.
Partager