RandomPersonGenerator.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using friaLabbar.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace friaLabbar
  8. {
  9. public class RandomPersonGenerator
  10. {
  11. Random rnd = new Random();
  12. string[] streetAddresses = new string[] { "Stensvedsgatan 7", "Rydsvägen 332B", "Kläppen 12", "Barkaby 98", "Myrbacka 18" };
  13. string[] cities = new string[] { "Borlänge", "Linköping", "Leksand", "Dala-Järna", "Sälen", "Stockholm" };
  14. string[] states = new string[] { "Dalarna", "Östergötland", "Värmland", "Skåne", "Uppland" };
  15. string[] zipCodes = new string[] { "42314", "58330", "79330", "44231" };
  16. string[] firstNames = new string[] { "Sten", "Sture", "Bertil", "Greta", "Lisa", "Fia", "Sven" };
  17. string[] lastNames = new string[] { "Karlsson", "Svensson", "Rödluva", "Ekdal", "Melander" };
  18. bool[] aliveStatuses = new bool[] { true, false };
  19. DateTime lowEndDate = new DateTime(1943, 1, 1);
  20. int daysFromLowDate;
  21. public RandomPersonGenerator()
  22. {
  23. daysFromLowDate = (DateTime.Today - lowEndDate).Days;
  24. }
  25. public List<PersonModel> GetPeople(int total = 10)
  26. {
  27. List<PersonModel> result = new List<PersonModel>();
  28. for (int i = 0; i < total; i++)
  29. {
  30. result.Add(GetPerson(i + 1));
  31. }
  32. return result;
  33. }
  34. private PersonModel GetPerson(int id)
  35. {
  36. PersonModel result = new PersonModel();
  37. result.PersonId = id;
  38. result.FirstName = GetRandomItem(firstNames);
  39. result.LastName = GetRandomItem(lastNames);
  40. result.IsAlive = GetRandomItem(aliveStatuses);
  41. result.DateOfBirth = GetRandomDate();
  42. result.Age = GetAgeInYears(result.DateOfBirth);
  43. result.AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100);
  44. int addressCount = rnd.Next(1, 5);
  45. for (int i = 0; i < addressCount; i++)
  46. {
  47. result.Addresses.Add(GetAddress(((id - 1) * 5) + i + 1));
  48. }
  49. return result;
  50. }
  51. private AddressModel GetAddress(int id)
  52. {
  53. AddressModel result = new AddressModel();
  54. result.AddressId = id;
  55. result.StreetAddress = GetRandomItem(streetAddresses);
  56. result.City = GetRandomItem(cities);
  57. result.State = GetRandomItem(states);
  58. result.ZipCode = GetRandomItem(zipCodes);
  59. return result;
  60. }
  61. private T GetRandomItem<T>(T[] obj)
  62. {
  63. return obj[rnd.Next(0, obj.Length)];
  64. }
  65. private DateTime GetRandomDate()
  66. {
  67. return lowEndDate.AddDays(rnd.Next(daysFromLowDate));
  68. }
  69. private int GetAgeInYears(DateTime birthday)
  70. {
  71. DateTime now = DateTime.Today;
  72. int age = now.Year - birthday.Year;
  73. if (now < birthday.AddYears(age))
  74. {
  75. age--;
  76. }
  77. return age;
  78. }
  79. }
  80. }