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;
        }
예제 #2
0
        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));
            }
        }
예제 #3
0
 public void TestCourseAddSameStudentShouldThrow()
 {
     var course = new Course("C#");
     var student = new Student("Pesho", 10111);
     course.JoinCourse(student);
     course.JoinCourse(student);
 }
예제 #4
0
 public void TestCourseAddCorrectStudentShouldNotThrow()
 {
     var course = new Course("C#");
     var student = new Student("Pesho", 10111);
     course.JoinCourse(student);
     Assert.AreEqual(student, course.Students[0]);
 }
예제 #5
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);
 }
예제 #6
0
        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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #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);
        }
예제 #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);
                }
            }
        }
예제 #13
0
 public void TestStudentHasExpectedName()
 {
     var student = new Student("Pesho", 10111);
     Assert.AreEqual("Pesho", student.Name);
 }
예제 #14
0
 public void TestStudentHasExpectedId()
 {
     var student = new Student("Pesho", 10111);
     Assert.AreEqual(10111, student.StudentId);
 }
예제 #15
0
 public void TestNullStudentNameShouldThrow()
 {
     var student = new Student(null, 10111);
 }
예제 #16
0
 public void TestInvalidLowStudentIdShouldThrow()
 {
     var student = new Student("Pesho", 1);
 }
예제 #17
0
 public void TestInvalidHighStudentIdShouldThrow()
 {
     var student = new Student("Pesho", 100000);
 }
예제 #18
0
 public void TestCourseRemoveNotExistantStudentShouldThrow()
 {
     var course = new Course("C#");
     var student = new Student("Pesho", 10111);
     course.LeaveCourse(student);
 }
예제 #19
0
 public void TestCreateValidStudentShouldNotThrow()
 {
     var student = new Student("Pesho", 10111);
 }
예제 #20
0
 public void TestEmptyStudentNameShouldThrow()
 {
     var student = new Student(string.Empty, 10111);
 }