Example #1
0
        public static void Main(string[] args)
        {
            string pathToCsv;
            string pathToResult;
            string conversionType;

            if (args.Length == 3)
            {
                pathToCsv      = args[0];
                pathToResult   = args[1];
                conversionType = args[2];
            }
            else if (args.Length == 1)
            {
                pathToCsv      = args[0];
                pathToResult   = @"result.xml";
                conversionType = "xml";
            }
            else if (args.Length == 2)
            {
                pathToCsv      = args[0];
                pathToResult   = args[1];
                conversionType = "xml";
            }
            else if (args.Length == 0)
            {
                pathToCsv      = @"dane.csv";
                pathToResult   = @"result.xml";
                conversionType = "xml";
            }
            else
            {
                Console.Error.Write("Niepoprawna liczba argumentow!");
                return;
            }

            HashSet <Student>            studenci    = new HashSet <Student>(new OwnComparer());
            Dictionary <string, Studies> studiesDict = new Dictionary <string, Studies>();
            StringBuilder logsb = new StringBuilder();

            logsb.Append("Studenci nie dodani z powodu blednych danych:");
            logsb.AppendLine();
            try
            {
                var  lines = File.ReadLines(pathToCsv);
                bool ok;
                foreach (var line in lines)
                {
                    ok = true;
                    string[] student = line.Split(',');
                    if (student.Length != 9)
                    {
                        logsb.Append(line + "\t | zla ilosc danych!");
                        logsb.AppendLine();
                        continue;
                    }

                    foreach (var pole in student)
                    {
                        if (string.IsNullOrEmpty(pole) || string.IsNullOrWhiteSpace(pole))
                        {
                            ok = false;
                            logsb.Append(line + "\t | puste pole!");
                            logsb.AppendLine();
                            break;
                        }
                    }
                    if (!ok)
                    {
                        continue;
                    }
                    Studies studiestmp = new Studies
                    {
                        name = student[2],
                        mode = student[3]
                    };

                    Student st = new Student
                    {
                        fname       = student[0],
                        lname       = student[1],
                        studies     = studiestmp,
                        indexNumber = student[4],
                        birthdate   = student[5],
                        email       = student[6],
                        mothersName = student[7],
                        fathersName = student[8]
                    };
                    if (!studenci.Add(st))
                    {
                        logsb.Append(line + "\t | powtorka studenta!");
                        logsb.AppendLine();
                        ok = false;
                    }

                    if (ok)
                    {
                        if (studiesDict.ContainsKey(studiestmp.name))
                        {
                            studiesDict[studiestmp.name].numberOfStudents++;
                        }
                        else
                        {
                            studiesDict[studiestmp.name] = studiestmp;
                        }
                    }
                }
            }
            catch (ArgumentException e)
            {
                logsb.AppendLine();
                logsb.Append("==================");
                logsb.AppendLine();
                logsb.Append(e);
                Console.WriteLine("Podana sciezka jest niepoprawna!");
            }
            catch (FileNotFoundException e)
            {
                logsb.AppendLine();
                logsb.Append("==================");
                logsb.AppendLine();
                logsb.Append(e);
                Console.WriteLine("Plik " + pathToCsv + " nie istnieje!");
            }

            File.WriteAllText(@"log.txt", logsb.ToString());

            var today = DateTime.Today;
            List <ActiveStudies> activeStudies = new List <ActiveStudies>();

            foreach (var keyPar in studiesDict)
            {
                ActiveStudies tmp = new ActiveStudies()
                {
                    name             = keyPar.Value.name,
                    numberOfStudents = keyPar.Value.numberOfStudents
                };
                activeStudies.Add(tmp);
            }

            Uczelnia uczelnia = new Uczelnia()
            {
                author        = "Jakub Oleksiak",
                createdAt     = today.ToShortDateString(),
                studenci      = studenci,
                activeStudies = activeStudies
            };

            if (conversionType.Equals("xml"))
            {
                FileStream toResWriter     = new FileStream(pathToResult, FileMode.Create);
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Uczelnia));
                xmlSerializer.Serialize(toResWriter, uczelnia, ns);
                toResWriter.Close();
                toResWriter.Dispose();
            }
            else if (conversionType.Equals("json"))
            {
                JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
                jsonSerializerOptions.WriteIndented = true;
                Package package = new Package();
                package.uczelnia = new Uczelnia(uczelnia);
                var json = JsonSerializer.Serialize(package, jsonSerializerOptions);
                File.WriteAllText(pathToResult, json);
            }
        }
Example #2
0
        public List <Student> getDataFromFile()
        {
            List <Student> studentList = new List <Student>();
            var            file        = new FileInfo(dataPath);

            if (!file.Exists)
            {
                string exc = "Plik nie istnieje";
                addLog(exc);
                throw new FileNotFoundException(exc);
            }
            else
            {
                var fi = new FileInfo(dataPath);
                using (var stream = new StreamReader(fi.OpenRead()))
                {
                    string line = null;

                    while ((line = stream.ReadLine()) != null)
                    {
                        string[] studentData = line.Split(',');

                        if (studentData.Length == 9)
                        {
                            bool correctData = true;
                            for (int i = 0; i < studentData.Length; i++)
                            {
                                if (studentData[i].Equals(""))
                                {
                                    addLog(line);
                                    correctData = false;
                                    break;
                                }
                                if (studentData[i] is null)
                                {
                                    correctData = false;
                                    addLog(line);
                                    break;
                                }
                            }
                            if (correctData)
                            {
                                var student = new Student
                                {
                                    Name        = studentData[0],
                                    Lastname    = studentData[1],
                                    Study       = studentData[2],
                                    StudyType   = studentData[3],
                                    Index       = "s" + studentData[4],
                                    Birthday    = studentData[5],
                                    MothersName = studentData[7],
                                    FathersName = studentData[8],
                                };

                                bool exist = false;
                                foreach (Student s in studentList)
                                {
                                    if (s.Equals(student))
                                    {
                                        exist = true;
                                    }
                                }
                                if (!exist)
                                {
                                    bool studyExist = false;
                                    foreach (var s in activeStudies)
                                    {
                                        if ((s.name).Equals(studentData[2]))
                                        {
                                            studyExist = true;
                                            s.numberOfStudents++;
                                        }
                                    }
                                    if (!studyExist)
                                    {
                                        var newStudy = new ActiveStudies
                                        {
                                            name             = studentData[2],
                                            numberOfStudents = 1
                                        };

                                        activeStudies.Add(newStudy);
                                    }

                                    studentList.Add(student);
                                }
                            }
                        }
                        else
                        {
                            addLog(line);
                        }
                    }
                }
            }
            return(studentList);
        }
Example #3
0
        public static StudentArray ReadData(string path)
        {
            try
            {
                var lines   = File.ReadLines(path);
                var hash    = new HashSet <Student>(new StudentComparer());
                var studies = new HashSet <ActiveStudies>(new NameComparer());

                foreach (var line in lines)
                {
                    String[] data = line.Split(",");

                    bool nullElement = false;
                    foreach (var item in data)
                    {
                        if (String.IsNullOrWhiteSpace(item))
                        {
                            nullElement = true;
                        }
                    }

                    if (!nullElement)
                    {
                        var student = new Student()
                        {
                            firstName = data[0],
                            lastName  = data[1],

                            studies = new Studies
                            {
                                studyName = data[2],
                                studyMode = data[3]
                            },
                            index     = data[4],
                            birthdate = DateTime.Parse(data[5]).ToShortDateString()
                        };

                        var study = new ActiveStudies
                        {
                            name             = student.studies.studyName,
                            numberOfStudents = 1
                        };

                        if (!studies.Add(study))
                        {
                            int countStudents = studies.Where(x => x.name == study.name).First().numberOfStudents;

                            study.numberOfStudents = ++countStudents;
                            studies.RemoveWhere(x => x.name == study.name);
                            studies.Add(study);
                        }


                        if (!hash.Add(student) || nullElement)
                        {
                            WriteToLog($"Błąd przy dodowaniu studenta. Dane: {line}");
                        }
                    }
                }

                var studentsModel = new StudentArray
                {
                    students      = hash,
                    author        = "Miłosz Pliżga",
                    createdAt     = DateTime.Today.ToShortDateString(),
                    activeStudies = studies
                };

                return(studentsModel);
            }
            catch (ArgumentException e)
            {
                string error = "Podana ścieżka jest niepoprawna";
                throw new ArgumentException(WriteToLog(error).ToString(), e);
            }
            catch (FileNotFoundException e)
            {
                string error = "Plik nazwa nie istnieje";
                throw new FileNotFoundException(WriteToLog(error).ToString(), e);
            }
        }