Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1.  
  2. using System;
  3. using System.IO;
  4. using System.Collections;
  5.  
  6. class Worker
  7. {
  8. int id;
  9. string name;
  10. float wage;
  11.  
  12. public Worker(int id, string name, float wage)
  13. {
  14. this.id = id;
  15. this.name = name;
  16. this.wage = wage;
  17. }
  18.  
  19. public void WriteToFile(string file)
  20. {
  21. string tempString = "id: " + this.id + " name: " + this.name + " wage: " + this.wage + "\n";
  22. File.AppendAllText(file, tempString);
  23. }
  24.  
  25. }
  26.  
  27.  
  28. class Ohjelma
  29. {
  30. static void Main(string[] args)
  31. {
  32. ArrayList workers = new ArrayList();
  33. string path = "C:\\Users\\Aksu\\source\\repos\\testiprojekti\\testiprojekti\\rekisteri.txt";
  34.  
  35.  
  36. if (File.Exists(path))
  37. {
  38. File.Delete(path);
  39. }
  40. else
  41. {
  42. File.Create(path);
  43. }
  44.  
  45. Console.WriteLine("Anna kolmen työntekijän tiedot (id, nimi, palkka): ");
  46.  
  47.  
  48. //creating three workers
  49. CreateWorker(workers);
  50. CreateWorker(workers);
  51. CreateWorker(workers);
  52.  
  53. for (int i = 0; i < workers.Count; i++)
  54. {
  55. ((Worker)workers[i]).WriteToFile(path);
  56. }
  57.  
  58.  
  59.  
  60. //asks for id and prints worker with that id
  61. Console.WriteLine("Anna työntekijän id:");
  62. int IdSearch = Int32.Parse(Console.ReadLine());
  63.  
  64. string result = string.Empty;
  65. var lines = File.ReadAllLines(path);
  66. bool containsString = false;
  67. foreach (var line in lines)
  68. {
  69. if (line.Contains("id: " + IdSearch + " "))
  70. {
  71. var text = line.Replace("id :", "");
  72. result = text.Trim();
  73. containsString = true;
  74. }
  75. }
  76. if (containsString == true)
  77. {
  78. Console.WriteLine(result);
  79. }
  80. else if (containsString == false)
  81. {
  82. Console.WriteLine("Työntekijää ei löydy id-numerolla " + IdSearch);
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. }
  91.  
  92. //asks for worker info and creates worker
  93. static void CreateWorker(ArrayList workers)
  94. {
  95. int id;
  96. string name;
  97. float wage;
  98.  
  99. Console.WriteLine("Anna id:");
  100. id = Convert.ToInt32(Console.ReadLine());
  101.  
  102. Console.WriteLine("Anna nimi:");
  103. name = Convert.ToString(Console.ReadLine());
  104.  
  105. Console.WriteLine("Anna palkka:");
  106. wage = (float)Convert.ToDouble(Console.ReadLine());
  107.  
  108. workers.Add(new Worker(id, name, wage));
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement