Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp123
  8. {
  9. class human
  10. {
  11. string name;
  12. int personnr;
  13.  
  14. public human(string name, int personnr)
  15. {
  16. this.name = name;
  17. this.personnr = personnr;
  18. }
  19.  
  20. public string Name
  21. {
  22. get { return name; }
  23. set { name = value; }
  24. }
  25.  
  26. public int Personnr
  27. {
  28. get { return personnr; }
  29. set { personnr = value; }
  30. }
  31. }
  32. class Program
  33. {
  34. static int search(List<human> list, int key)
  35. {
  36. for (int i = 0; i < list.Count; i++)
  37. {
  38. if (list[i].Personnr == key)
  39. return i;
  40.  
  41. }
  42. return -1;
  43.  
  44. }
  45. static int search(List<human> list, int key)
  46. {
  47. int found = -1;
  48.  
  49. for (int i = 0; i < list.Count; i++)
  50. {
  51.  
  52. if (list[i].Personnr == key)
  53. found = i;
  54.  
  55. }
  56.  
  57. return found;
  58.  
  59. }
  60.  
  61. static void Main(string[] args)
  62. {
  63. while (true)
  64. {
  65. List<human> person = new List<human>();
  66.  
  67.  
  68. person.Add(new human("Thomas", 900331));
  69. person.Add(new human("Lena", 470727));
  70. person.Add(new human("Lars", 430721));
  71.  
  72.  
  73. Console.WriteLine("[L]ägg till person och personnummer");
  74. Console.WriteLine("[S]ök efter personnummer");
  75. string menu = Console.ReadLine();
  76.  
  77.  
  78. if (menu == "L" || menu == "l")
  79. {
  80. Console.WriteLine("Skriv in namn");
  81. string input = Console.ReadLine();
  82. Console.WriteLine("Skriv in personnummer 6 siffror");
  83. int year = Convert.ToInt32(Console.ReadLine());
  84.  
  85. person.Add(new human(input, year));
  86.  
  87.  
  88. }
  89.  
  90. else if (menu == "S" || menu == "s")
  91. {
  92.  
  93. Console.WriteLine("Skriv in personnr");
  94. string personnr = Console.ReadLine();
  95. int key = Convert.ToInt32(personnr);
  96. int index = search(person, key);
  97.  
  98. if (index == -1)
  99.  
  100. Console.WriteLine("personen finns inte");
  101.  
  102.  
  103. else
  104.  
  105. Console.WriteLine("Personen med personnr " + key + " finns på index " + (index + 1) + " och heter " +
  106. person[index].Name);
  107. }
  108.  
  109. }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement