/// <summary>
        /// Creates a new list of students from text file
        /// </summary>
        /// <param name="file">Name and path of the file with students data</param>
        /// <returns>StudentsList</returns>
        static public StudentsList StudentsListFactory(StreamReader file)
        {
            string       line;
            StudentsList studentsList = new StudentsList();

            line = file.ReadLine();

            if (!line.Contains("Name") || !line.Contains("Surname"))
            {
                throw new ArgumentOutOfRangeException("No Valid headers found in input file. Valid headers (\"Name,Surname,Age,Tuition,Height,Date,Phone\")");
            }

            string[] headers = line.Split(',');

            // Read From File And Create Student Object Then Add The Object To List
            while ((line = file.ReadLine()) != null)
            {
                string[] fields = line.Split(',');

                Student student = CreateStudent(headers, fields);

                studentsList.Add(student);
            }

            return(studentsList);
        }
Beispiel #2
0
 static void PrintStudents(StudentsList students, string compareField)
 {
     Console.WriteLine("Sort by " + compareField);
     foreach (Student student in students)
     {
         Console.WriteLine(student);
     }
 }
        /// <summary>
        /// Creates a new list of students from xml string
        /// </summary>
        /// <param name="xmlStudents">xml with students data</param>
        /// <returns>StudentList</returns>
        static public StudentsList StudentsListFactory(string xmlStudents)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(StudentsList), new XmlRootAttribute("StudentsList"));

            using (TextReader reader = new StringReader(xmlStudents))
            {
                StudentsList students = (StudentsList)serializer.Deserialize(reader);
                return(students);
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            try
            {
                StudentsList students = StudentsList.StudentsListFactory(new StreamReader(@"Y:\Coding\Bootcamp\StudentsFromFile\test.txt"));
                //                StudentsList students = StudentsList.StudentsListFactory(@"<StudentsList>
                //                                                    <Student>
                //                                                        <Name>George</Name>
                //                                                        <Surname>Xiros</Surname>
                //                                                        <Age>42</Age>
                //                                                        <Height>184</Height>
                //                                                        <Tuition>2200</Tuition>
                //                                                        <Phone>6976900103</Phone>
                //                                                    </Student>
                //                                                    <Student>
                //                                                        <Name>Nikos</Name>
                //                                                        <Surname>Diakos</Surname>
                //                                                        <Age>32</Age>
                //                                                        <Height>170</Height>
                //                                                        <Tuition>2500</Tuition>
                //                                                        <Phone>6879595888</Phone>
                //                                                    </Student>
                //                                                </StudentsList>");

                PrintStudentsSortedBy(students, "Surname", StudentsList.ShortByName);
                PrintStudentsSortedBy(students, "Age", StudentsList.ShortByAge);
                PrintStudentsSortedBy(students, "Phone", StudentsList.ShortByPhone);
            }
            catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); }
            catch (DirectoryNotFoundException e) { Console.WriteLine(e.Message); }
            catch (FileNotFoundException e) { Console.WriteLine(e.Message); }
            catch (FieldAccessException e) { Console.WriteLine(e.Message); }
            catch (Exception e) { Console.WriteLine(e.Message); }

            Console.ReadKey();
        }
Beispiel #5
0
 static void PrintStudentsSortedBy(StudentsList students, string compareField, Comparison <Student> comparison)
 {
     students.Sort(comparison); PrintStudents(students, compareField);
 }