/
3.cs
154 lines (128 loc) · 4.76 KB
/
3.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Text;
using System.Collections.Generic;
using Bogus;
namespace RandomData
{
public class Person
{
public string Address { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
class Program
{
static void Main(string[] args)
{
string language = mapLanguage(args[0]);
int personsCount = Convert.ToInt32(args[1]);
double errorCount = getErrorCount(args);
printPersons(generatePersons(language, personsCount), errorCount, language);
Console.ReadKey();
}
private static string mapLanguage(string language)
{
Dictionary<string, string> languageMap = new Dictionary<string, string>
{
["en_US"] = "en_US",
["ru_RU"] = "ru",
["be_BY"] = "uk",
};
return languageMap[language];
}
private static double getErrorCount(string[] args)
{
try
{
return Convert.ToDouble(args[2]);
}
catch
{
return 0.0;
}
}
private static List<Person> generatePersons(string language, int personsCount)
{
Faker<Person> generatorPerson = getGeneratorPerson(language);
return generatorPerson.Generate(personsCount);
}
private static Faker<Person> getGeneratorPerson(string language)
{
return new Faker<Person>(language)
.RuleFor(x => x.Name, f => f.Name.FullName())
.RuleFor(x => x.Address, f => f.Address.FullAddress())
.RuleFor(x => x.Phone, f => f.Phone.PhoneNumber());
}
private static string createRecord(string cleanRecord, double errorCount, string language)
{
StringBuilder record = new StringBuilder(cleanRecord);
ErrorDelegate[] ErrorFunctions = new ErrorDelegate[3] { insertSymbol, removeSymbol, replaceSymbol };
addError(ErrorFunctions, record, errorCount, language);
return record.ToString();
}
private delegate System.Text.StringBuilder ErrorDelegate(System.Text.StringBuilder record, string language);
private static System.Text.StringBuilder insertSymbol(System.Text.StringBuilder record, string language)
{
Random rnd = new Random();
CharArray[] charRange = new CharArray[3] { numberChar, upperAlphabetChar, lowerAlphabetChar };
char symbol = charRange[rnd.Next(0, 2)](localCharRange(language));
return record.Insert(rnd.Next(0, record.Length - 1), symbol);
}
private delegate char CharArray(int[] charArray);
private static char numberChar(int[] charArray)
{
Random rnd = new Random();
return (char)rnd.Next(charArray[0], charArray[1]);
}
private static char upperAlphabetChar(int[] charArray)
{
Random rnd = new Random();
return (char)rnd.Next(charArray[2], charArray[3]);
}
private static char lowerAlphabetChar(int[] charArray)
{
Random rnd = new Random();
return (char)rnd.Next(charArray[4], charArray[5]);
}
private static int[] localCharRange(string language)
{
Dictionary<string, int[]> localCharRange = new Dictionary<string, int[]>
{
["en_US"] = new int[] { 0x0030, 0x0039, 0x0041, 0x005A, 0x0061, 0x007A },
["ru"] = new int[] { 0x0030, 0x0039, 0x0410, 0x042F, 0x0430, 0x044F },
["uk"] = new int[] { 0x0030, 0x0039, 0x0410, 0x042F, 0x0430, 0x044F },
};
return localCharRange[language];
}
private static System.Text.StringBuilder removeSymbol(System.Text.StringBuilder record, string language = "")
{
Random rnd = new Random();
return record.Remove(rnd.Next(0, record.Length - 1), 1);
}
private static System.Text.StringBuilder replaceSymbol(System.Text.StringBuilder record, string language = "")
{
int rnd = new Random().Next(0, record.Length - 1);
char replacedSymbol = record[rnd];
record.Remove(rnd, 1);
record.Insert(rnd + 1, replacedSymbol);
return record;
}
private static System.Text.StringBuilder addError(ErrorDelegate[] ErrorFunctions, StringBuilder errorRecord, double errorCount, string language)
{
return errorCount > 1 ? repeatAddError(ErrorFunctions, errorRecord, errorCount, language) : errorRecord;
}
private static System.Text.StringBuilder repeatAddError(ErrorDelegate[] ErrorFunctions, StringBuilder errorRecord, double errorCount, string language)
{
Random rnd = new Random();
errorRecord = ErrorFunctions[rnd.Next(0, 2)](errorRecord, language);
return addError(ErrorFunctions, errorRecord, errorCount - 1, language);
}
private static void printPersons(List<Person> persons, double errorCount, string language)
{
foreach (var person in persons)
{
Console.WriteLine(createRecord($"{person.Name}; {person.Address}; {person.Phone}", errorCount, language));
}
}
}
}