Advertisement
vvccs

EXP6_STRING

Apr 13th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. public class StudentRecordSystem
  2. {
  3. private string[] studentNames;
  4. private int[] studentAges;
  5. private string[] deptNames;
  6. private int totalStudents;
  7. private const int MAX_STUDENTS = 100; // Maximum number of students
  8.  
  9. public StudentRecordSystem()
  10. {
  11. studentNames = new string[MAX_STUDENTS];
  12. studentAges = new int[MAX_STUDENTS];
  13. deptNames = new string[MAX_STUDENTS];
  14. totalStudents = 0;
  15. }
  16. public void AddStudent(string name, int age, string gradeLevel)
  17. {
  18. if (totalStudents < MAX_STUDENTS)
  19. {
  20. studentNames[totalStudents] = name;
  21. studentAges[totalStudents] = age;
  22. deptNames[totalStudents] = gradeLevel;
  23. totalStudents++;
  24. Console.WriteLine("Student added successfully.");
  25. }
  26. else
  27. {
  28. Console.WriteLine("Maximum number of students reached.");
  29. }
  30. }
  31. public void UpdateStudent(string name, int age, string gradeLevel)
  32. {
  33. int index = FindStudentIndex(name);
  34. if (index != -1)
  35. {
  36. studentAges[index] = age;
  37. deptNames[index] = gradeLevel;
  38. Console.WriteLine("Student record updated successfully.");
  39. }
  40. else
  41. {
  42. Console.WriteLine("Student not found.");
  43. }
  44. }
  45. public void DeleteStudent(string name)
  46. {
  47. int index = FindStudentIndex(name);
  48. if (index != -1)
  49. {
  50. for (int i = index; i < totalStudents - 1; i++)
  51. {
  52. studentNames[i] = studentNames[i + 1];
  53. studentAges[i] = studentAges[i + 1];
  54. deptNames[i] = deptNames[i + 1];
  55. }
  56. totalStudents--;
  57. Console.WriteLine("Student record deleted successfully.");
  58. }
  59. else
  60. {
  61. Console.WriteLine("Student not found.");
  62. }
  63. }
  64.  
  65. public void SearchStudent(string name)
  66. {
  67. int index = FindStudentIndex(name);
  68. if (index != -1)
  69. {
  70. Console.WriteLine("Name: {studentNames[index]}, Age: {studentAges[index]}, Grade Level: {studentGradeLevels[index]}");
  71. }
  72. else
  73. {
  74. Console.WriteLine("Student not found.");
  75. }
  76. }
  77.  
  78. public string GetAllStudentsInfo()
  79. {
  80. if (totalStudents == 0)
  81. {
  82. return "No students found.";
  83. }
  84. string allStudentsInfo = "";
  85. for (int i = 0; i < totalStudents; i++)
  86. {
  87. allStudentsInfo += "Name:" + studentNames[i] + ", Age: " + studentAges[i] + ", department name: " + deptNames[i] + "\n";
  88. }
  89. return allStudentsInfo;
  90. }
  91.  
  92. private int FindStudentIndex(string name)
  93. {
  94. for (int i = 0; i < totalStudents; i++)
  95. {
  96. if (studentNames[i] == name)
  97. {
  98. return i;
  99. }
  100. }
  101. return -1; // Student not found
  102. }
  103. }
  104.  
  105. class Program
  106. {
  107. static void Main(string[] args)
  108. {
  109. StudentRecordSystem recordSystem = new StudentRecordSystem();
  110. char choice;
  111.  
  112. do
  113. {
  114. Console.WriteLine("\nMenu:");
  115. Console.WriteLine("1. Add Student");
  116. Console.WriteLine("2. Update Student");
  117. Console.WriteLine("3. Delete Student");
  118. Console.WriteLine("4. Search Student");
  119. Console.WriteLine("5. Display All Students");
  120. Console.WriteLine("6. Exit");
  121. Console.Write("Enter your choice: ");
  122. choice = Console.ReadKey().KeyChar;
  123. Console.WriteLine();
  124.  
  125. switch (choice)
  126. {
  127. case '1':
  128. Console.Write("Enter name: ");
  129. string name = Console.ReadLine();
  130. Console.Write("Enter age: ");
  131. int age = int.Parse(Console.ReadLine());
  132. Console.Write("Enter Department name: ");
  133. string gradeLevel = Console.ReadLine();
  134. recordSystem.AddStudent(name, age, gradeLevel);
  135. break;
  136. case '2':
  137. Console.Write("Enter name of student to update: ");
  138. string nameUpdate = Console.ReadLine();
  139. Console.Write("Enter age: ");
  140. int ageUpdate = int.Parse(Console.ReadLine());
  141. Console.Write("Enter Department name: ");
  142. string gradeLevelUpdate = Console.ReadLine();
  143. recordSystem.UpdateStudent(nameUpdate, ageUpdate, gradeLevelUpdate);
  144. break;
  145. case '3':
  146. Console.Write("Enter name of student to delete: ");
  147. string nameDelete = Console.ReadLine();
  148. recordSystem.DeleteStudent(nameDelete);
  149. break;
  150. case '4':
  151. Console.Write("Enter name of student to search: ");
  152. string searchName = Console.ReadLine();
  153. recordSystem.SearchStudent(searchName);
  154. break;
  155. case '5':
  156. Console.WriteLine("All Students:");
  157. Console.WriteLine(recordSystem.GetAllStudentsInfo());
  158. break;
  159. case '6':
  160. Console.WriteLine("Exiting...");
  161. break;
  162. default:
  163. Console.WriteLine("Invalid choice. Please enter a valid option.");
  164. break;
  165. }
  166. } while (choice != '6');
  167. }
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement