-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKata.cs
More file actions
39 lines (34 loc) · 1.71 KB
/
Kata.cs
File metadata and controls
39 lines (34 loc) · 1.71 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
using System.Text.RegularExpressions;
namespace RegexValidatePINcode;
/// <summary>
/// Contient des méthodes utilitaires pour la validation de chaînes représentant un code PIN.
/// </summary>
public static class Kata
{
/// <summary>
/// Valide si une chaîne donnée est un code PIN correct.
/// Un code PIN valide est constitué uniquement de chiffres et sa longueur est soit 4, soit 6 caractères.
/// </summary>
/// <param name="pin">La chaîne représentant le code PIN à valider.</param>
/// <returns><c>true</c> si le PIN est valide ; sinon <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">Lancé si la chaîne <paramref name="pin"/> est null.</exception>
public static bool ValidatePinLinq(string pin)
{
if (pin is null)
throw new ArgumentNullException(nameof(pin), "Le code PIN ne peut pas être null.");
return (pin.Length == 4 || pin.Length == 6) && pin.All(char.IsDigit);
}
/// <summary>
/// Valide si une chaîne donnée est un code PIN correct.
/// Un code PIN valide est constitué uniquement de chiffres et sa longueur est soit 4, soit 6 caractères.
/// </summary>
/// <param name="pin">La chaîne représentant le code PIN à valider.</param>
/// <returns><c>true</c> si le PIN est valide ; sinon <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">Lancé si la chaîne <paramref name="pin"/> est null.</exception>
public static bool ValidatePin(string pin)
{
if (pin is null)
throw new ArgumentNullException(nameof(pin), "Le code PIN ne peut pas être null.");
return Regex.IsMatch(pin, @"^\d{4}$|^\d{6}$") && (pin.Length == 4 || pin.Length == 6);
}
}