/
RandomName.cs
140 lines (124 loc) · 4.89 KB
/
RandomName.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace RandomNameGen
{
/// <summary>
/// RandomName class, used to generate a random name.
/// </summary>
public class RandomName
{
/// <summary>
/// Class for holding the lists of names from names.json
/// </summary>
class NameList
{
public string[] boys { get; set; }
public string[] girls { get; set; }
public string[] last { get; set; }
public NameList()
{
boys = new string[] { };
girls = new string[] { };
last = new string[] { };
}
}
Random rand;
List<string> Male;
List<string> Female;
List<string> Last;
/// <summary>
/// Initialises a new instance of the RandomName class.
/// </summary>
/// <param name="rand">A Random that is used to pick names</param>
public RandomName(Random rand)
{
this.rand = rand;
NameList l = new NameList();
JsonSerializer serializer = new JsonSerializer();
using (StreamReader reader = new StreamReader("names.json"))
using (JsonReader jreader = new JsonTextReader(reader))
{
l = serializer.Deserialize<NameList>(jreader);
}
Male = new List<string>(l.boys);
Female = new List<string>(l.girls);
Last = new List<string>(l.last);
}
/// <summary>
/// Returns a new random name
/// </summary>
/// <param name="sex">The sex of the person to be named. true for male, false for female</param>
/// <param name="middle">How many middle names do generate</param>
/// <param name="isInital">Should the middle names be initials or not?</param>
/// <returns>The random name as a string</returns>
public string Generate(Sex sex, int middle = 0, bool isInital = false)
{
string first = sex == Sex.Male ? Male[rand.Next(Male.Count)] : Female[rand.Next(Female.Count)]; // determines if we should select a name from male or female, and randomly picks
string last = Last[rand.Next(Last.Count)]; // gets the last name
List<string> middles = new List<string>();
for (int i = 0; i < middle; i++)
{
if (isInital)
{
middles.Add("ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand.Next(0, 25)].ToString() + "."); // randomly selects an uppercase letter to use as the inital and appends a dot
}
else
{
middles.Add(sex == Sex.Male ? Male[rand.Next(Male.Count)] : Female[rand.Next(Female.Count)]); // randomly selects a name that fits with the sex of the person
}
}
StringBuilder b = new StringBuilder();
b.Append(first + " "); // put a space after our names;
foreach (string m in middles)
{
b.Append(m + " ");
}
b.Append(last);
return b.ToString();
}
/// <summary>
/// Generates a list of random names
/// </summary>
/// <param name="number">The number of names to be generated</param>
/// <param name="maxMiddleNames">The maximum number of middle names</param>
/// <param name="sex">The sex of the names, if null sex is randomised</param>
/// <param name="initials">Should the middle names have initials, if null this will be randomised</param>
/// <returns>List of strings of names</returns>
public List<string> RandomNames(int number, int maxMiddleNames, Sex? sex = null, bool? initials = null)
{
List<string> names = new List<string>();
for (int i = 0; i < number; i++)
{
if (sex != null && initials != null)
{
names.Add(Generate((Sex)sex, rand.Next(0, maxMiddleNames + 1), (bool)initials));
}
else if (sex != null)
{
bool init = rand.Next(0, 2) != 0;
names.Add(Generate((Sex)sex, rand.Next(0, maxMiddleNames + 1), init));
}
else if (initials != null)
{
Sex s = (Sex)rand.Next(0, 2);
names.Add(Generate(s, rand.Next(0, maxMiddleNames + 1), (bool)initials));
}
else
{
Sex s = (Sex)rand.Next(0, 2);
bool init = rand.Next(0, 2) != 0;
names.Add(Generate(s, rand.Next(0, maxMiddleNames + 1), init));
}
}
return names;
}
}
public enum Sex
{
Male,
Female
}
}