-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegistrationNumber.cs
53 lines (44 loc) · 1.48 KB
/
RegistrationNumber.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
53
using System.Text.RegularExpressions;
namespace ParkingDeluxe;
public record RegistrationNumber
{
private readonly int _numbers;
private readonly string _letters;
public string RegNumber => $"{_letters}{_numbers:D3}";
public RegistrationNumber(string letters, int numbers)
{
_numbers = numbers;
_letters = letters.ToUpper();
}
public static RegistrationNumber GetRandom()
{
int numbers = Random.Shared.Next(1000);
string swedishAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ";
string letters = "";
for (int i = 0; i < 3; i++)
{
char RandomLetter = swedishAlphabet[Random.Shared.Next(swedishAlphabet.Length)];
letters = $"{letters}{RandomLetter}";
}
return new RegistrationNumber(letters, numbers);
}
internal static bool TryCreate(string userEnteredString, out RegistrationNumber? userEnteredRegNumber)
{
userEnteredRegNumber = null;
if (IsValid(userEnteredString))
{
userEnteredRegNumber = new RegistrationNumber(userEnteredString[..3], int.Parse(userEnteredString[3..]));
return true;
}
return false;
}
private static bool IsValid(string userEnteredString)
{
if (userEnteredString.Length != 6)
{
return false;
}
string regExPattern = @"^[A-Za-zåÅäÄöÖ]{3}\d{3}$";
return Regex.IsMatch(userEnteredString, regExPattern);
}
}