source: 2014/30/MiskaK/The Reclaim/The Reclaim/The Reclaim/The Reclaim/Names.cs @ 5689

Revision 5554, 1.8 KB checked in by anlakane, 6 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using System.Reflection;
7using Jypeli;
8
9public static class Names
10{
11    public static List<String> FirstNames { get; set; }
12    public static List<String> LastNames { get; set; }
13
14    public static string PelinHakemistoPolku = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Names)).CodeBase).Remove(0, 6);
15    public static string EtunimiPolku = PelinHakemistoPolku + "\\" + "Content\\Nimet\\Etunimet.txt";
16    public static string SukunimiPolku = PelinHakemistoPolku + "\\" + "Content\\Nimet\\Sukunimet.txt";
17
18    public static String GetRandomName()
19    {
20        if (FirstNames == null) FirstNames = new List<string>();
21        if (LastNames == null) LastNames = new List<string>();
22        if (FirstNames.Count == 0 || LastNames.Count == 0)
23            LoadNames();
24        if (FirstNames.Count == 0 || LastNames.Count == 0) return "MISSING_NO";
25
26        String etuNimi = RandomGen.SelectOne<String>(FirstNames);
27        String sukuNimi = RandomGen.SelectOne<String>(LastNames);
28        return etuNimi + " " + sukuNimi;
29    }
30
31    public static void LoadNames()
32    {
33        FirstNames.Clear();
34        LastNames.Clear();
35        using (StreamReader reader = new StreamReader(EtunimiPolku))
36        {
37            String rivi;
38            while ((rivi = reader.ReadLine()) != null)
39            {
40                FirstNames.Add(rivi);
41            }
42            reader.Close();
43        }
44
45        using (StreamReader reader = new StreamReader(SukunimiPolku))
46        {
47            String rivi;
48            while ((rivi = reader.ReadLine()) != null)
49            {
50                LastNames.Add(rivi);
51            }
52            reader.Close();
53        }
54    }
55}
Note: See TracBrowser for help on using the repository browser.