Guest User

Untitled

a guest
Jan 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Work2
  7. {
  8. class Student
  9. {
  10. public string name;
  11. public DateTime birthDate;
  12. public string course;
  13. public int grade;
  14.  
  15. public Student(string name, DateTime birthDate, string course, int grade)
  16. {
  17. this.name = name;
  18. this.birthDate = birthDate;
  19. this.course = course;
  20. this.grade = grade;
  21. }
  22.  
  23.  
  24.  
  25.  
  26. }
  27. }
  28.  
  29.  
  30.  
  31.  
  32.  
  33. using System;
  34. using System.Collections;
  35.  
  36. namespace Work2
  37. {
  38. class Program
  39. {
  40. static ArrayList list = new ArrayList();
  41.  
  42. static void Main(string[] args)
  43. {
  44. DateTime bd;
  45. string name, course;
  46. int grade, choice;
  47.  
  48. do
  49. {
  50. Console.Write("Name: ");
  51. name = Console.ReadLine();
  52. Console.Write("Date of birth: ");
  53. bd = Convert.ToDateTime(Console.ReadLine());
  54. Console.Write("Choose course:\t C# Programming...1\t Software Development...2 ");
  55.  
  56. if (Console.ReadLine().Equals("1")) course = "C# Programming";
  57. else course = "Software Development";
  58.  
  59. Console.Write("Grade: ");
  60. grade = Convert.ToInt32(Console.ReadLine());
  61. Student student = new Student(name, bd, course, grade);
  62. list.Add(student);
  63.  
  64. Console.WriteLine("Add a new student?\tYes...1 \t No...0");
  65.  
  66. } while (Convert.ToInt32(Console.ReadLine()) != 0);
  67.  
  68. do
  69. {
  70. Console.WriteLine("\nSelect an action:\nFind a student...1\nDisplay C# Programming course students...2\nDisplay the list...3\nRemove a student...4\nExit...0");
  71. choice = Convert.ToInt32(Console.ReadLine());
  72.  
  73. switch (choice)
  74. {
  75. case 1:
  76. Console.Write("Enter name: ");
  77. if (isOnList(Console.ReadLine())) Console.WriteLine("The student is on the list.");
  78. else Console.WriteLine("The student is not on the list!");
  79. break;
  80. case 2:
  81. Console.WriteLine("C# Programming course students: ");
  82. DisplayCSStudents();
  83. break;
  84. case 3:
  85. DisplayAll();
  86. break;
  87. case 4:
  88. Console.Write("Enter name: ");
  89. name = Console.ReadLine();
  90. Remove(name);
  91. break;
  92. }
  93.  
  94. } while(choice != 0);
  95.  
  96. }
  97.  
  98. static bool isOnList(string name)
  99. {
  100. bool found = false;
  101. foreach (Student student in list)
  102. {
  103. if (student.name.Equals(name))
  104. {
  105. found = true;
  106. break;
  107. }
  108. }
  109. return found;
  110. }
  111.  
  112. static void DisplayCSStudents()
  113. {
  114. foreach (Student student in list)
  115. {
  116. if (student.course.Equals("C# Programming"))
  117. {
  118. Console.WriteLine("Name: " + student.name);
  119. Console.WriteLine("Grade: " + student.grade + "\n");
  120. }
  121. }
  122. }
  123.  
  124. static void Sort()
  125. {
  126. string[] names = new string[list.Count];
  127. int i = 0; //index in string array
  128. int lIndex; //index in list
  129. Student s;
  130.  
  131. foreach (Student student in list) //copying of names from the arraylist
  132. {
  133. names[i] = student.name;
  134. i++;
  135. }
  136.  
  137. Array.Sort(names); //sorting of names
  138.  
  139. i = 0;
  140.  
  141. for (lIndex = 0; lIndex < list.Count; lIndex++ ) //sorting of objects in the arraylist
  142. {
  143. s = (Student)list[lIndex];
  144. while (!s.name.Equals(names[i])) i++;
  145.  
  146. s = (Student)list[i];
  147. list[i] = list[lIndex];
  148. list[lIndex] = s;
  149. i = 0;
  150.  
  151. }
  152.  
  153. }
  154. static void DisplayAll()
  155. {
  156. Sort();
  157. Console.WriteLine("Students in the list: ");
  158. foreach (Student student in list)
  159. {
  160. Console.WriteLine("Name: " + student.name);
  161. Console.WriteLine("Date of birth: " + student.birthDate);
  162. Console.WriteLine("Course: " + student.course);
  163. Console.WriteLine("Grade: " + student.grade + "\n");
  164. }
  165.  
  166. }
  167.  
  168. static void Remove(string name)
  169. {
  170. if (!isOnList(name)) Console.WriteLine("The student is not on the list!");
  171. else
  172. {
  173. foreach (Student student in list)
  174. {
  175. if (student.name.Equals(name))
  176. {
  177. list.Remove(student);
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
Add Comment
Please, Sign In to add comment