| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using friaLabbar.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace friaLabbar
- {
- public class RandomPersonGenerator
- {
- Random rnd = new Random();
- string[] streetAddresses = new string[] { "Stensvedsgatan 7", "Rydsvägen 332B", "Kläppen 12", "Barkaby 98", "Myrbacka 18" };
- string[] cities = new string[] { "Borlänge", "Linköping", "Leksand", "Dala-Järna", "Sälen", "Stockholm" };
- string[] states = new string[] { "Dalarna", "Östergötland", "Värmland", "Skåne", "Uppland" };
- string[] zipCodes = new string[] { "42314", "58330", "79330", "44231" };
- string[] firstNames = new string[] { "Sten", "Sture", "Bertil", "Greta", "Lisa", "Fia", "Sven" };
- string[] lastNames = new string[] { "Karlsson", "Svensson", "Rödluva", "Ekdal", "Melander" };
- bool[] aliveStatuses = new bool[] { true, false };
- DateTime lowEndDate = new DateTime(1943, 1, 1);
- int daysFromLowDate;
- public RandomPersonGenerator()
- {
- daysFromLowDate = (DateTime.Today - lowEndDate).Days;
- }
- public List<PersonModel> GetPeople(int total = 10)
- {
- List<PersonModel> result = new List<PersonModel>();
- for (int i = 0; i < total; i++)
- {
- result.Add(GetPerson(i + 1));
- }
- return result;
- }
- private PersonModel GetPerson(int id)
- {
- PersonModel result = new PersonModel();
- result.PersonId = id;
- result.FirstName = GetRandomItem(firstNames);
- result.LastName = GetRandomItem(lastNames);
- result.IsAlive = GetRandomItem(aliveStatuses);
- result.DateOfBirth = GetRandomDate();
- result.Age = GetAgeInYears(result.DateOfBirth);
- result.AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100);
- int addressCount = rnd.Next(1, 5);
- for (int i = 0; i < addressCount; i++)
- {
- result.Addresses.Add(GetAddress(((id - 1) * 5) + i + 1));
- }
- return result;
- }
- private AddressModel GetAddress(int id)
- {
- AddressModel result = new AddressModel();
- result.AddressId = id;
- result.StreetAddress = GetRandomItem(streetAddresses);
- result.City = GetRandomItem(cities);
- result.State = GetRandomItem(states);
- result.ZipCode = GetRandomItem(zipCodes);
- return result;
- }
- private T GetRandomItem<T>(T[] obj)
- {
- return obj[rnd.Next(0, obj.Length)];
- }
- private DateTime GetRandomDate()
- {
- return lowEndDate.AddDays(rnd.Next(daysFromLowDate));
- }
- private int GetAgeInYears(DateTime birthday)
- {
- DateTime now = DateTime.Today;
- int age = now.Year - birthday.Year;
- if (now < birthday.AddYears(age))
- {
- age--;
- }
- return age;
- }
- }
- }
|