-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.cs
More file actions
93 lines (90 loc) · 2.54 KB
/
Copy pathGenerator.cs
File metadata and controls
93 lines (90 loc) · 2.54 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
namespace CustomerIdGenerator
{
public class Generator
{
List<string> context = new List<string>() { "DX645789", "AC125489", "KL985632" }; //replace this list with a Database!
private int Length { get; set; }
public Generator(List<string> context)
{
this.context = context;
}
public Generator(int length, List<string> context)
{
this.context = context;
this.Length = length;
}
private string GetLetters()
{
Random rnd = new Random();
string customerId = "";
int ascii_index = 0;
for (int i = 0; i < 2; i++)
{
ascii_index = rnd.Next(65, 91);
char upperCaseLetter = Convert.ToChar(ascii_index);
customerId += upperCaseLetter;
}
return customerId;
}
private string GetNumbers()
{
Random rnd = new Random();
string numbers = "";
if (Length == 0)
{
int[] num = new int[6];
for (int i = 0; i < num.Length; i++)
{
num[i] = rnd.Next(0, 10);
numbers += num[i].ToString();
}
}
else
{
int[] num = new int[Length];
for (int i = 0; i < num.Length; i++)
{
num[i] = rnd.Next(0, 10);
numbers += num[i].ToString();
}
}
return numbers;
}
private string CreateCUID()
{
string customerId = "";
string letters = GetLetters();
string numbers = GetNumbers();
customerId = letters + numbers;
if (CheckCUID(customerId) == true)
{
CreateCUID();
}
return customerId;
}
private bool CheckCUID(string id)
{
bool isTaken = false;
string customerId = id;
var cuidList = context;
foreach (var item in cuidList)
{
if (item == customerId)
{
isTaken = true;
}
else
{
isTaken = false;
}
}
return isTaken;
}
public string NewCuid()
{
return CreateCUID();
}
}
}