public void AddStudent(Student toAddStudent) { foreach (Student student in StudentList) { if (student.UniqueId.Equals(toAddStudent.UniqueId)) { throw new Exception("Cannot add two Students with the same ID"); } } StudentList.Add(toAddStudent); }
public Student this[string studentId] { get { bool found = false; Student toReturnStudent = new Student(); foreach (Student student in StudentList) { if (student.UniqueId.Equals(studentId)) { found = true; toReturnStudent = student; } } if (found) { return toReturnStudent; } else throw new Exception("Student ID: " + studentId + " not found"); } }
private static void Main() { Student student = new Student("1234", "John", 'm'); Student student2 = new Student("5678", "Alice", 'f'); Student student3 = new Student("9101112", "Fred", 'm'); Student student4 = new Student("13141516", "Suzy", 'f'); ClassRoom classR = new ClassRoom("1234", "John"); try { classR[0] = student; classR[1] = student2; classR[2] = student3; classR[3] = student4; Console.WriteLine(classR); Console.ReadKey(); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadKey(); } }