public static SortedDictionary<string, List<Student>> Parse(string filename)
        {
            string line;
            var dictionary = new SortedDictionary<string, List<Student>>();

            // Read the file and display it line by line.
            StreamReader file = new StreamReader(filename);
            while ((line = file.ReadLine()) != null)
            {
                var values = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                var course = values[2].Trim();
                var student = new Student()
                {
                    FirstName = values[0].Trim(),
                    LastName = values[1].Trim()
                };

                if (!dictionary.ContainsKey(course))
                {
                    var students = new List<Student>();
                    students.Add(student);
                    dictionary.Add(course, students);
                }
                else
                {
                    dictionary[course].Add(student);
                }
            }

            file.Close();

            return dictionary;
        }
        static void Main()
        {
            var studentsByCourse = new SortedDictionary<string, SortedSet<Student>>();

            using (StreamReader streamReader = new StreamReader("../../InputFile/Students.txt"))
            {
                string line;

                while ((line = streamReader.ReadLine()) != null)
                {
                    string[] tokens = line.Split('|');
                    string firstName = tokens[0].Trim();
                    string lastName = tokens[1].Trim();
                    string course = tokens[2].Trim();

                    Student currentStudent = new Student(firstName, lastName, course);

                    if (!studentsByCourse.ContainsKey(course))
                    {
                        studentsByCourse.Add(course, new SortedSet<Student>());
                    }

                    studentsByCourse[course].Add(currentStudent);
                }
            }

            foreach (var course in studentsByCourse)
            {
                Console.WriteLine(course.Key + ": " + String.Join(", ", course.Value));
            }
        }
 public void TestCourseAddSameStudentShouldThrow()
 {
     var course = new Course("C#");
     var student = new Student("Pesho", 10111);
     course.JoinCourse(student);
     course.JoinCourse(student);
 }
 public void TestCourseAddCorrectStudentShouldNotThrow()
 {
     var course = new Course("C#");
     var student = new Student("Pesho", 10111);
     course.JoinCourse(student);
     Assert.AreEqual(student, course.Students[0]);
 }
 public void RemoveStudent(Student studentToRemove)
 {
     if (studentToRemove == null)
     {
         throw new ArgumentNullException("You are trying to add null as a student to student list");
     }
     this.students.Remove(studentToRemove);
 }
        public void TestCourseRemoveExistantStudentShouldNotThrow()
        {
            var course = new Course("C#");
            var student = new Student("Pesho", 10111);
            course.JoinCourse(student);
            course.LeaveCourse(student);

            Assert.IsFalse(course.Students.Contains(student));
        }
        private static void PersistData(Student student, string course)
        {
            if (!data.ContainsKey(course))
            {
                data[course] = new SortedSet<Student>();
            }

            data[course].Add(student);
        }
        public void AddStudent(Student studentToAdd)
        {
            if (studentToAdd == null)
            {
                throw new ArgumentNullException("You are trying to add null as a student to student list");
            }

            this.students.Add(studentToAdd);
        }
        public void JoinSchool(Student student)
        {
            Validator.ObjectNotNullValidator(student, "Student");

            if (this.Students.Contains(student))
            {
                throw new InvalidOperationException(string.Join("Student {0} has already been enrolled", student.StudentId, this.Name));
            }

            this.Students.Add(student);
        }
Beispiel #10
0
        public void LeaveCourse(Student student)
        {
            Validator.ObjectNotNullValidator(student, "Student");

            if (!this.Students.Contains(student))
            {
                throw new InvalidOperationException(string.Join("Student {0} has not joined {1} course", student.StudentId, this.Name));
            }

            this.Students.Remove(student);
        }
Beispiel #11
0
        public void JoinCourse(Student student)
        {
            Validator.ObjectNotNullValidator(student, "Student");

            if (this.Students.Count > MaxStudents)
            {
                throw new InvalidOperationException("Course is full");
            }

            if (this.Students.Contains(student))
            {
                throw new InvalidOperationException(string.Join("Student {0} has already joined {1} course", student.StudentId, this.Name));
            }

            this.Students.Add(student);
        }
        private static void ParseInput()
        {
            using (StreamReader reader = new StreamReader("../../students.txt"))
            {
                while (!reader.EndOfStream)
                {
                    string currentLine = reader.ReadLine();

                    string[] lineParams = currentLine.Split('|');

                    string firstName = lineParams[0].Trim();
                    string lastName = lineParams[1].Trim();
                    string course = lineParams[2].Trim();

                    Student student = new Student()
                    {
                        FirstName = firstName,
                        LastName = lastName
                    };

                    PersistData(student, course);
                }
            }
        }
 public void TestStudentHasExpectedName()
 {
     var student = new Student("Pesho", 10111);
     Assert.AreEqual("Pesho", student.Name);
 }
 public void TestStudentHasExpectedId()
 {
     var student = new Student("Pesho", 10111);
     Assert.AreEqual(10111, student.StudentId);
 }
 public void TestNullStudentNameShouldThrow()
 {
     var student = new Student(null, 10111);
 }
 public void TestInvalidLowStudentIdShouldThrow()
 {
     var student = new Student("Pesho", 1);
 }
 public void TestInvalidHighStudentIdShouldThrow()
 {
     var student = new Student("Pesho", 100000);
 }
 public void TestCourseRemoveNotExistantStudentShouldThrow()
 {
     var course = new Course("C#");
     var student = new Student("Pesho", 10111);
     course.LeaveCourse(student);
 }
 public void TestCreateValidStudentShouldNotThrow()
 {
     var student = new Student("Pesho", 10111);
 }
 public void TestEmptyStudentNameShouldThrow()
 {
     var student = new Student(string.Empty, 10111);
 }