Exemplo n.º 1
0
        public static void createXmlFile(ArrayList studentsList, Dictionary <string, ArrayList> studiesInfo, string path)
        {
            Student[] students = new Student[studentsList.Count];
            int       i        = 0;

            foreach (Student s in studentsList)
            {
                students[i++] = s;
            }


            i = 0;
            Studies[] studies = new Studies[studiesInfo.Count];
            foreach (KeyValuePair <string, ArrayList> keyValues in studiesInfo)
            {
                studies[i++] = new Studies {
                    NameAttribute = keyValues.Key, NumberOfStudents = keyValues.Value.Count
                };
            }

            DateTime now = DateTime.Now;

            College col = new College
            {
                Students      = students,
                ActiveStudies = studies,
                Author        = "Jan Rejnowski",
                CreatedAt     = now.ToString("d")
            };

            makeXmlFile(path, col);
        }
Exemplo n.º 2
0
 public Student()
 {
     index   = "";
     fname   = "";
     lname   = "";
     bdate   = "";
     email   = "";
     mother  = "";
     father  = "";
     studies = new Studies("", "");
 }
Exemplo n.º 3
0
 public Student(String fname, String lName, String studies, String modeOfStudies, String index, String birthdate, String email, String motherName, String fathersName)
 {
     this.fName       = fname;
     this.lName       = lName;
     this.studies     = new Studies(studies, modeOfStudies);
     this.index       = "s" + index;
     this.email       = email;
     this.birthdate   = birthdate;
     this.mothersName = motherName;
     this.fathersName = fathersName;
 }
Exemplo n.º 4
0
 public Student(string[] values)
 {
     if (values.Length == 9)
     {
         fname   = values[0];
         lname   = values[1];
         studies = new Studies(values[2], values[3]);
         index   = "s" + values[4];
         bdate   = values[5];
         email   = values[6];
         mother  = values[7];
         father  = values[8];
     }
     else
     {
         string msg = "Wrong values: ";
         foreach (string s in values)
         {
             msg += s + " ";
         }
         Program.writeLog(msg);
     }
 }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            if (args.Length > 3)
            {
                Console.WriteLine("Nie może być więcej niź 3 parametry");
                return;
            }
            string defaultAdresPlikuCSV       = "data.csv",
                   defaultAdresDocelowyWyniku = "result.xml",
                   defaultTypDanych           = "xml";
            string sourceFilePath,
                   aimFilePath;

            sourceFilePath = ((args[0] == null) || (args[0] == "")) ? defaultAdresPlikuCSV : args[0];
            aimFilePath    = ((args[1] == null) || (args[1] == "")) ? defaultAdresDocelowyWyniku : args[1];
            string formatDanych = ((args[2] == null) || (args[2] == "")) ? defaultTypDanych : args[2];

            Console.WriteLine("args[0] " + args[0] +
                              "args[1] " + args[1] +
                              "args[2] " + args[2] +
                              "\n sourceFilePath " + sourceFilePath +
                              "aimFile " + aimFilePath +
                              "formatDanych " + formatDanych);

            var students = new List <Student>();

            Dictionary <string, int> hashCourses = new Dictionary <string, int>();

            using (StreamWriter logStreamWriter = new StreamWriter("log.txt", false, System.Text.Encoding.Default)) {
                try
                {
                    using (var stream = new StreamReader(new FileInfo(sourceFilePath).OpenRead()))
                    {
                        string line = null;
                        while ((line = stream.ReadLine()) != null)
                        {
                            string[] studentParametrs = line.Split(',');

                            bool   isMessageForLog = false;
                            string infoStudent     = "";
                            for (int i = 0; i < studentParametrs.Length; i++)
                            {
                                infoStudent += " " + studentParametrs[i];
                            }

                            if (studentParametrs.Length < 9)
                            {
                                infoStudent = "Student: " + infoStudent + " ma " + studentParametrs.Length + " parametrów, " +
                                              "a potrzebno mieć 9";
                                logStreamWriter.Write(infoStudent + "\n");
                                logStreamWriter.Flush();
                                continue;
                            }
                            for (int i = 0; i < studentParametrs.Length; i++)
                            {
                                if (studentParametrs[i] == null || studentParametrs[i] == "")
                                {
                                    infoStudent    += "; Ma pusty parametry";
                                    isMessageForLog = true;
                                    break;
                                }
                            }
                            var studies = new Studies
                            {
                                Name = studentParametrs[2],
                                Mode = studentParametrs[3]
                            };

                            var st = new Student
                            {
                                Ska         = studentParametrs[4],
                                Fname       = studentParametrs[0],
                                Lname       = studentParametrs[1],
                                Birthdate   = studentParametrs[5],
                                Email       = studentParametrs[6],
                                MothersName = studentParametrs[7],
                                FathersName = studentParametrs[8],
                                Studies     = studies
                            };


                            for (int i = 0; i < students.Count; i++)
                            {
                                if (students[i].Equals(st))
                                {
                                    infoStudent    += " ; Takij student już istnieje w Baze Dannych";
                                    isMessageForLog = true;
                                }
                            }

                            if (isMessageForLog)
                            {
                                logStreamWriter.Write(infoStudent + "\n");
                                logStreamWriter.Flush();
                                continue;
                            }

                            students.Add(st);
                            string courseName = studentParametrs[2];
                            if (hashCourses.ContainsKey(courseName))
                            {
                                hashCourses[courseName]++;
                            }
                            else
                            {
                                hashCourses.Add(courseName, 1);
                            }
                        }
                    }
                }
                catch (FileNotFoundException fnfe)
                {
                    logStreamWriter.WriteLine("Plik " + fnfe.FileName + " nie istnieje");
                    logStreamWriter.Flush();
                }
                catch (DirectoryNotFoundException dnfe) {
                    logStreamWriter.WriteLine("Podana ścieżka jest niepoprawna");
                    logStreamWriter.Flush();
                }
            }

            var myWriter = FactoryWriter.createWriter(formatDanych);

            myWriter.WriteDataIntoFile(students, hashCourses, aimFilePath);
            Console.WriteLine("Program finshed!");
        }