Пример #1
0
        static void Main(string[] args)
        {
            string file;

            if (args.Length == 0 || args[0].Equals(""))
            {
                file = "data/dane.csv";
            }
            else
            {
                file = args[0];
            }
            string gool;

            if (args.Length < 2 || args[1].Equals(""))
            {
                gool = "żesult.xml";
            }
            else
            {
                gool = args[1];
            }
            if (args.Length == 3 && !args[2].Equals("xml"))
            {
                Console.WriteLine("nieprwaidłowy format pliku");
                return;
            }


            string file2 = "data/łog.txt";

            StreamWriter streamWriter = null;

            streamWriter = new StreamWriter(file2);

            FileInfo                 f    = new FileInfo(file);
            List <Student>           los  = new List <Student>();
            Dictionary <string, int> stud = new Dictionary <string, int>();

            try
            {
                using (StreamReader stream = new StreamReader(f.OpenRead()))
                {
                    string line = "";
                    while ((line = stream.ReadLine()) != null)
                    {
                        string[] studentWiersz = line.Split(",");
                        //Console.WriteLine(line);
                        if (studentWiersz.Length == 9)
                        {
                            bool t = true;

                            foreach (string str in studentWiersz)
                            {
                                if (str.Equals(""))
                                {
                                    t = false;
                                }
                            }

                            if (t)
                            {
                                Studia s = new Studia
                                {
                                    nazwa = studentWiersz[2],
                                    tryb  = studentWiersz[3],
                                };

                                var stu = new Student
                                {
                                    Imie          = studentWiersz[0],
                                    Nazwisko      = studentWiersz[1],
                                    index         = int.Parse(studentWiersz[4]),
                                    stud          = s,
                                    DataUrodzenia = studentWiersz[5],
                                    Email         = studentWiersz[6],
                                    Ojciec        = studentWiersz[8],
                                    Matka         = studentWiersz[7]
                                };
                                los.Add(stu);

                                if (stud.ContainsKey(s.nazwa))
                                {
                                    stud[s.nazwa]++;
                                }
                                else
                                {
                                    stud.Add(s.nazwa, 1);
                                }
                            }
                            else
                            {
                                streamWriter.WriteLine("nieprawidłowa struktura danych:" + line);
                            }
                        }
                        else
                        {
                            streamWriter.WriteLine("nieprawidłowa struktura danych:" + line);
                        }
                    }
                }
            }catch (FileNotFoundException e)
            {
                Console.WriteLine("FileNotFoundException(\"Plik nazwa nie istnieje\")");
                streamWriter.WriteLine("FileNotFoundException(\"Plik nazwa nie istnieje\")");
                return;
            }catch (ArgumentException e1)
            {
                Console.WriteLine("ArgumentException(\"Podana ścieżka jest niepoprawna\")");
                streamWriter.WriteLine("ArgumentException(\"Podana ścieżka jest niepoprawna\")");
                return;
            }catch (Exception e2)
            {
                Console.WriteLine(e2.Message);
                streamWriter.WriteLine(e2.Message);
                return;
            }
            streamWriter.Flush();
            streamWriter.Close();
            ActiveStudies active = new ActiveStudies()
            {
                ACl = new List <AC>()
            };

            foreach (KeyValuePair <string, int> entry in stud)
            {
                AC a = new AC
                {
                    name   = entry.Key,
                    number = entry.Value
                };
                active.ACl.Add(a);
            }
            Students st = new Students
            {
                student = los.Distinct(new EqualityComp()).ToList <Student>()
            };

            Uczelnia uczelnia = new Uczelnia
            {
                data          = DateTime.Now,
                autor         = "Michał Zieliński",
                student       = st,
                activeStudies = active
            };


            FileStream       writer     = new FileStream(@gool, FileMode.Create);
            XmlRootAttribute rotatr     = new XmlRootAttribute("studenci");
            XmlSerializer    serializer = new XmlSerializer(typeof(Uczelnia));



            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("", "");
            serializer.Serialize(writer, uczelnia, ns);
        }
Пример #2
0
        private static List <Student> handleData(string path)
        {
            var  students      = new List <Student>();
            var  activeStudies = new List <ActiveStudies>();
            var  fi            = new FileInfo(path);
            bool corrupted     = false;

            using (var stream = new StreamReader(fi.OpenRead()))
            {
                string line = null;
                while ((line = stream.ReadLine()) != null)
                {
                    string[] columns = line.Split(',');
                    foreach (var column in columns)
                    {
                        if (column.Equals(""))
                        {
                            corrupted = true;
                            break;
                        }
                    }
                    if (columns.Length != 9 || corrupted)
                    {
                        string errorMessage = "Liczba kolumn jest zbyt mała lub dane zawierają błąd.";
                        errorLog(errorMessage);
                        corrupted = false;
                    }
                    else
                    {
                        var studentToAdd = new Student
                        {
                            name        = columns[0],
                            lastName    = columns[1],
                            studiesName = columns[2],
                            studiesMode = columns[3],
                            indexNumber = $"s{columns[4]}",
                            birthdate   = DateTime.Parse(columns[5]).Date,
                            email       = columns[6],
                            mothersName = columns[7],
                            fathersName = columns[8]
                        };

                        bool   duplicateStudent = false;
                        string duplicateInfo    = "Duplikat danych studenta: ";
                        foreach (var student in students)
                        {
                            if (student.Equals(studentToAdd))
                            {
                                duplicateStudent = true;
                                errorLog(duplicateInfo + student.indexNumber);
                                break;
                            }
                        }
                        if (!duplicateStudent)
                        {
                            students.Add(studentToAdd);
                        }

                        bool studiesInList = false;
                        if (!duplicateStudent)
                        {
                            foreach (var study in activeStudies)
                            {
                                if ((study.name.Equals(studentToAdd.studiesName)))
                                {
                                    studiesInList = true;
                                    study.numberOfStudents++;
                                }
                            }

                            if (!studiesInList)
                            {
                                var studies = new ActiveStudies
                                {
                                    name             = columns[2],
                                    numberOfStudents = 1
                                };
                                activeStudies.Add(studies);
                            }
                        }
                    }
                }
            }
            foreach (var s in students)
            {
                Console.WriteLine(s);
            }
            return(students);
        }