Advertisement
AlexandraCatana

Untitled

Nov 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Student
  7. {
  8. class Student
  9. {
  10. public string nume;
  11. public double medie;
  12. public int nrRestante;
  13.  
  14. public Student (string nume, double medie, int nrRestante = 0 )
  15. {
  16. this.nume = nume;
  17. this.medie = medie;
  18. this.nrRestante = nrRestante;
  19. }
  20.  
  21. public double getMedie()
  22. {
  23. return this.medie;
  24. }
  25.  
  26. public int getNrRestante()
  27. {
  28. return this.nrRestante;
  29. }
  30.  
  31. public void displayStudent()
  32. {
  33. Console.WriteLine(this.nume + " " + this.medie + " " + this.nrRestante);
  34. }
  35. }
  36.  
  37. class GrupStudenti
  38. {
  39. public int nr;
  40. public System.Collections.Generic.List<Student> grupList = new System.Collections.Generic.List<Student>();
  41.  
  42. public GrupStudenti(int nr)
  43. {
  44. this.nr = nr;
  45. }
  46.  
  47. public void readGrup()
  48. {
  49. for (int i = 1; i <= this.nr; ++i)
  50. {
  51. string nume;
  52. double medie;
  53. int restante;
  54.  
  55. Console.Write("Numele studentului " + i + ": ");
  56. nume = Console.ReadLine();
  57.  
  58. Console.Write("Media studentului " + i + ": ");
  59. medie = Convert.ToDouble(Console.ReadLine());
  60.  
  61. Console.Write("Studentul " + i + " are restante? Daca da, cate?: ");
  62. restante = Convert.ToInt32(Console.ReadLine());
  63.  
  64. //Student student = new Student(nume, medie, restante);
  65.  
  66. grupList.Add(new Student(nume, medie, restante));
  67. }
  68. }
  69.  
  70. public void displayList()
  71. {
  72. foreach (Student i in grupList)
  73. {
  74. i.displayStudent();
  75. }
  76. }
  77.  
  78. private static int compByMedie (Student s1, Student s2)
  79. {
  80. if (s1.medie == s2.medie) return 0;
  81. else if (s1.medie > s2.medie) return -1;
  82. else return 1;
  83. }
  84.  
  85. public void sortByMedie()
  86. {
  87. grupList.Sort(compByMedie);
  88. }
  89.  
  90. private static int compByNume (Student s1, Student s2)
  91. {
  92. if (s1.nume == s2.nume) return 0;
  93. else if (String.Compare(s1.nume, s2.nume) > 0) return 1;
  94. else return -1;
  95. }
  96.  
  97. public void sortByNume()
  98. {
  99. grupList.Sort(compByNume);
  100. }
  101.  
  102. public Student getMedieMax()
  103. {
  104. double Max = -1;
  105. Student ans = new Student("Student", -1, 0);
  106.  
  107. foreach (Student i in grupList)
  108. {
  109. if (i.medie > Max)
  110. {
  111. Max = i.medie;
  112. ans = i;
  113. }
  114. }
  115.  
  116. return ans;
  117. }
  118. }
  119.  
  120. class Program
  121. {
  122. static void Main(string[] args)
  123. {
  124. GrupStudenti var1 = new GrupStudenti(3);
  125. var1.readGrup();
  126.  
  127. Console.WriteLine("Lista studentilor: ");
  128. var1.displayList();
  129.  
  130. Console.WriteLine("\nLista studentilor ordonata dupa medie: ");
  131. var1.sortByMedie();
  132. var1.displayList();
  133.  
  134. Console.WriteLine("\nLista stdentilor ordonata alfabetic: ");
  135. var1.sortByNume();
  136. var1.displayList();
  137.  
  138. Student var2 = var1.getMedieMax();
  139. Console.Write("\nCea mai mare medie o are: ");
  140. var2.displayStudent();
  141.  
  142. Console.ReadKey();
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement