0

im currently working on random name credentials generator. This is my beta code:

using FileHelpers;
using System;
using System.Collections.Generic;
using System.IO;

class Person
{

    public static List<String> snList = new List<string>();
    public static List<String> fnList = new List<string>();

    static bool isLoaded = false;

    private string _surname;
    private string _forename;
    private long _pesel;
    private int _phonenum;

    public string Surname { get => _surname; set => _surname = value; }
    public string Forename { get => _forename; set => _forename = value; }
    public long Pesel { get => _pesel; set => _pesel = value; }
    public int PhoneNum { get => _phonenum; set => _phonenum = value; }

    public Person()
    {
        if (isLoaded == false)
        {
            FileHelperEngine<Name> snEngine = new FileHelperEngine<Name>();
            string path = Path.Combine(Environment.CurrentDirectory, 
                                                      "firstnamescsv.csv");
            var read = snEngine.ReadFile(path);
            foreach (Name item in read)
            {
                snList.Add(item.Names);
            }

            FileHelperEngine<Name> fnEngine = new FileHelperEngine<Name>();
            path = Path.Combine(Environment.CurrentDirectory, "lastnamescsv.csv");
            var read1 = fnEngine.ReadFile(path);
            foreach (Name item in read)
            {
                fnList.Add(item.Names);
            }
            isLoaded = true;
        }

        PickSurname();
        PickForename();
        PickPesel();
        PickPhonenum();
    }

    public void PickSurname()
    {
        var rand = new Random();

        var pickedRand = rand.Next(1,5493);
        Surname = snList[pickedRand];
    }

    public void PickForename()
    {
        var rand1 = new Random();
        var random = rand1.Next(1,88777);
        Forename = fnList[random];
    }

    public void PickPhonenum()
    {
        var rand = new Random();
        PhoneNum = rand.Next(500000000, 799999999);
    }

    public void PickPesel()
    {
        var rand = new Random();
        Pesel = rand.Next(100000000, 999999999);
    }

    public override string ToString()
    {
        return $"Name: {Surname} {Forename}\n" +
            $"Phone number: {PhoneNum}\n" +
            $"Pesel: {Pesel}";
    }
}

Name class

[FileHelpers.DelimitedRecord("\n")] 
class Name {
    private string _names;
    public string Names
    {
        get => _names;
        set => _names = value;
    }
 }

The problem is the output seen below:

Name: Felicidad Garrels Phone number: 595204613 Pesel: 385613840
Name: Felicidad Garrels Phone number: 595204613 Pesel: 385613840
Name: Felicidad Garrels Phone number: 595204613 Pesel: 385613840
Name: Felicidad Garrels Phone number: 595204613 Pesel: 385613840
Name: Felicidad Garrels Phone number: 595204613 Pesel: 385613840

And it's caused by this Program.cs class: (main)

 var list = new List<Person>();
        for (i = 0; i < 5 ; i++)
        {
            list.Add(new Person());
        }
        foreach(var item in list)
        {
            Console.WriteLine(item);
        }
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Perki
  • 1
  • 1
  • 2
    Use only one `new Random()` in your code. – Mephy Oct 08 '17 at 18:36
  • If you store the name data as one name per line, you could read them into the collection with one line of code. Also, a phone number and national ID number arent really numbers....you cant add and subtract them. If you construct the phone in pieces you can end up with a more realistic looking set with repeating exchanges/sections – Ňɏssa Pøngjǣrdenlarp Oct 08 '17 at 18:40

0 Answers0