Esempio n. 1
0
        public static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                throw new ArgumentNullException();
            }

            FileStream log = new FileStream(@"log.txt", FileMode.OpenOrCreate);

            var pathFrom = args[0];
            var pathTo   = args[1];
            var type     = args[2];

            var lines = File.ReadLines(pathFrom);

            var students = new HashSet <Student>(new StudentComparer());
            var studies  = new HashSet <Study>();

            foreach (var line in lines)
            {
                try
                {
                    var array = line.Split(",");

                    if (array.Length < 9)
                    {
                        throw new NotRightData(line);
                    }
                    ;

                    foreach (var value in array)
                    {
                        if (value.Length == 0)
                        {
                            throw new NotFullData(line);
                        }
                    }

                    var study = new Study {
                        Name = array[2]
                    };

                    var student = new Student
                    {
                        Name    = array[0],
                        Surname = array[1],
                        Lecture = new Lecture {
                            Name = array[2], Type = array[3]
                        },
                        Index       = int.Parse(array[4]),
                        Birthday    = DateTime.Parse(array[5]).ToString("dd.MM.yyyy"),
                        Email       = array[6],
                        MothersName = array[7],
                        FathersName = array[8]
                    };

                    if (students.Contains(student))
                    {
                        throw new DuplicateData(line);
                    }

                    students.Add(student);

                    bool exist = false;

                    foreach (var s in studies)
                    {
                        if (s.Equals(study))
                        {
                            exist = true;
                            s.CountOfStudents++;
                            break;
                        }
                    }

                    if (!exist)
                    {
                        studies.Add(new Study {
                            Name = array[2], CountOfStudents = 1
                        });
                    }
                }
                catch (Exception e)
                {
                    Byte[] info = new UTF8Encoding(true).GetBytes(e.Message + "\n");
                    log.Write(info, 0, info.Length);
                }
            }

            var listStudents = new List <Student>();

            foreach (var student in students)
            {
                listStudents.Add(student);
            }

            var listStudies = new List <Study>();

            foreach (var study in studies)
            {
                listStudies.Add(study);
            }

            var university = new University {
                Author        = "Taras Kulyavtes",
                CreatedAt     = DateTime.Now.ToString("dd.MM.yyyy"),
                Students      = listStudents,
                ActiveStudies = listStudies
            };

            if (type.Equals("xml"))
            {
                FileStream    writer     = new FileStream(pathTo, FileMode.Create);
                XmlSerializer serializer = new XmlSerializer(typeof(University));
                serializer.Serialize(writer, university);
            }
            else if (type.Equals("json"))
            {
                var jsonString = JsonSerializer.Serialize(university);
                File.WriteAllText(pathTo, jsonString);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            //Create blanck log
            createLog();

            //Standart values
            string path   = @"data.csv";
            string save   = @"rezult";
            string format = "xml";

            try
            {
                path = args[0];
            }
            catch (IndexOutOfRangeException)
            {
                Log("Use standart Path: " + path);
            }

            try
            {
                save = args[1];
            }
            catch (IndexOutOfRangeException)
            {
                Log("Use standart Save: " + save);
            }

            try
            {
                format = args[2];
            }
            catch (IndexOutOfRangeException)
            {
                Log("Use standart Format: " + format);
            }

            //read students
            var hash = new HashSet <Student>(new OwnComparer());

            try
            {
                using (var stream = new StreamReader(path))
                {
                    string line = null;
                    while ((line = stream.ReadLine()) != null)
                    {
                        string[] read = line.Split(',');

                        bool ok = true;
                        foreach (var tmp in read)
                        {
                            if (tmp.Equals(""))
                            {
                                Log("NULL value, student dismiss");
                                ok = false;
                            }
                        }
                        if (ok)
                        {
                            if (read.Length == 9)
                            {
                                var readStudies = new Studies {
                                    name = read[2], mode = read[3]
                                };
                                var st = new Student {
                                    fname = read[0], lname = read[1], indexNumber = read[4], birthdate = read[5], email = read[6], mothersName = read[7], fathersName = read[8], studies = readStudies
                                };
                                hash.Add(st);
                            }
                            else
                            {
                                Log("Not correct informations. Find: " + read.Length + " Need: 9");
                            }
                        }
                    }
                }
            }
            catch (FileNotFoundException)
            {
                Log("File not found");
            }
            //Counting
            Dictionary <string, int> activeStudnets = new Dictionary <string, int>();

            foreach (var s in hash)
            {
                if (!activeStudnets.ContainsKey(s.studies.name))
                {
                    activeStudnets.Add(s.studies.name, 1);
                }
                else
                {
                    activeStudnets[s.studies.name]++;
                }
            }

            //XML
            if (format.Equals("xml"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(HashSet <Student>), new XmlRootAttribute(@"uczelnia Created At " + DateTime.Now + new XmlRootAttribute("Author: Jan Kowalski")));
                FileStream    writer     = new FileStream(save + ".xml", FileMode.Create);
                serializer.Serialize(writer, hash);
            }
            //JSON
            if (format.Equals("json"))
            {
                University university = new University(hash, activeStudnets);

                var js = JsonConvert.SerializeObject(university, Formatting.Indented);
                File.WriteAllText(save + ".json", js);
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            // default arguments using Linq ElementAtOrDefault
            var maybeSourceFile = args.ElementAtOrDefault(0);
            var sourceFile      = String.IsNullOrEmpty(maybeSourceFile) ? "dane.csv" : maybeSourceFile.ToString();

            var maybeOutputFile = args.ElementAtOrDefault(1);
            var outputFile      = String.IsNullOrEmpty(maybeOutputFile) ? "result.xml" : maybeOutputFile.ToString();

            var maybeFormat = args.ElementAtOrDefault(2);
            var format      = String.IsNullOrEmpty(maybeFormat) ? "xml" : maybeFormat.ToString();

            Console.WriteLine($"Source File: {sourceFile}, output file: {outputFile}, format: {format}");

            if (!File.Exists(sourceFile))
            {
                throw new System.IO.FileNotFoundException($"File {sourceFile} was not found");
            }

            var errorLog = new StreamWriter("/Users/konole/Downloads/log.csv");

            Dictionary <int, Student> students = new Dictionary <int, Student>();

            using (var stream = new StreamReader(File.OpenRead(sourceFile)))
            {
                string line = null;
                while ((line = stream.ReadLine()) != null && line != "")
                {
                    string[] studentData = line.Split(',');
                    var      st          = new Student
                    {
                        FirstName       = studentData[0],
                        LastName        = studentData[1],
                        Course          = studentData[2],
                        TypeOfCourse    = studentData[3],
                        Number          = int.Parse(studentData[4]),
                        BirthdateString = studentData[5],
                        Email           = studentData[6],
                        MotherName      = studentData[7],
                        FatherName      = studentData[8]
                    };

                    if (!students.ContainsKey(st.Number))
                    {
                        if (st.IsValid())
                        {
                            students.Add(st.Number, st);
                        }
                        else
                        {
                            WriteToErrorFile(errorLog, line);
                        }
                    }
                    else
                    {
                        WriteToErrorFile(errorLog, line);
                    }
                }
            }

            errorLog.Close();

            University university = new University(students.Values.ToList())
            {
                Author    = "Konrad Oleksiuk",
                CreatedAt = DateTime.Now.ToString("dd.MM.yyyy")
            };

            FileStream writer = new FileStream(outputFile, FileMode.Create);

            switch (format)
            {
            case "xml":
                XmlSerializer serializer = new XmlSerializer(typeof(University));
                serializer.Serialize(writer, university);
                break;

            default:
                throw new Exception($"Unknown format: {format}");
            }

            writer.Close();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            var        path         = args[0];
            var        resultpath   = args[1];
            var        resultformat = args[2];
            var        lines        = File.ReadLines(path);
            var        hash         = new HashSet <Student>(new MyComp());
            var        today        = DateTime.Today;
            University uni          = new University();

            uni.createdAt = today.ToString("dd.MM.yyyy");
            uni.author    = "Zuzanna Bubrowska";


            using (var logput = new StreamWriter("log.txt"))
            {
                try
                {
                    foreach (var line in lines)
                    {
                        var  data = line.Split(",");
                        bool log  = false;

                        foreach (var value in data)
                        {
                            if (string.IsNullOrEmpty(value))
                            {
                                log = true;
                            }
                        }

                        if (log)
                        {
                            logput.WriteLine(line);
                        }
                        else
                        {
                            Studies studies = new Studies {
                                Name = data[2], Mode = data[3]
                            };
                            Student student = new Student
                            {
                                FirstName  = data[0],
                                LastName   = data[1],
                                Index      = data[4],
                                BirthDate  = DateTime.Parse(data[5]),
                                Email      = data[6],
                                MotherName = data[7],
                                FatherName = data[8],
                                Studies    = studies
                            };

                            if (!hash.Add(student))
                            {
                                logput.WriteLine(line);
                            }
                            else
                            {
                                hash.Add(student);
                            }
                        }
                    }

                    uni.students = hash;

                    if (resultformat == "xml")
                    {
                        XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
                        xns.Add("", "");
                        using (FileStream writer = new FileStream(resultpath, FileMode.Create))
                        {
                            XmlRootAttribute root       = new XmlRootAttribute("university");
                            XmlSerializer    serializer = new XmlSerializer(typeof(University), root);
                            serializer.Serialize(writer, uni, xns);
                        }
                    }
                    else if (resultformat == "json")
                    {
                        var result = JsonConvert.SerializeObject(uni, Newtonsoft.Json.Formatting.Indented);
                        File.WriteAllText(resultpath, result);
                    }
                    else
                    {
                        logput.WriteLine("Nieobslugiwane rozszerzenie");
                    }
                }

                //var parsedDate = DateTime.Parse("2020-03-09");
                //Console.WriteLine(parsedDate);



                catch (FileNotFoundException)
                {
                    logput.WriteLine("Plik nie istnieje");
                    throw;
                }
                catch (ArgumentException)
                {
                    logput.WriteLine("Podana sciezka jest niepoprawna");
                    throw;
                }
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            var    path    = "";
            var    pathDes = "";
            string format  = "";

            if (args.Length == 3)
            {
                path    = args[0];
                pathDes = args[1];
                format  = args[2];
            }
            else
            {
                path    = @"C:\Users\kasia\OneDrive\Pulpit\studia\IIrok\II_SEM\APBD\cw2\dane.csv";
                pathDes = @"C:\Users\kasia\OneDrive\Pulpit\studia\IIrok\II_SEM\APBD\cw2\data.xml";
                format  = "xml";
            }

            var pathOut = @"C:\Users\kasia\OneDrive\Pulpit\studia\IIrok\II_SEM\APBD\cw2\log.txt";


            if (File.Exists(pathOut))
            {
                File.Delete(pathOut);
            }

            using (FileStream fs = new FileStream(pathOut, FileMode.CreateNew))
            {
                using (BinaryWriter w = new BinaryWriter(fs))
                {
                    try
                    {
                        FileStream fileStream = new FileStream(pathDes, FileMode.Create);


                        var lines = File.ReadLines(path);

                        var hash = new HashSet <Student>(new OwnComparer());

                        var hashStudies = new HashSet <Studies>(new StudiesComparer());

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

                            Regex regex = new Regex("(,,)+");

                            if (data.Length != 9 || regex.IsMatch(line) || line.Length <= 8)
                            {
                                w.Write(line + '\n');
                            }
                            else
                            {
                                var studies = new Studies {
                                    Name = data[2],
                                    Mode = data[3]
                                };
                                hashStudies.Add(studies);

                                var student = new Student {
                                    Name        = data[0],
                                    LastName    = data[1],
                                    Studies     = studies,
                                    Index       = data[4],
                                    BirthDate   = DateTime.Parse(data[5]),
                                    Email       = data[6],
                                    MothersName = data[7],
                                    FathersName = data[8]
                                };


                                if (!hash.Add(student))
                                {
                                    w.Write(line + '\n');
                                }
                            }
                        }

                        var university = new University
                        {
                            Students = hash
                        };

                        if (format == "xml")
                        {
                            SerializeData(university, new XmlSerializer <University>(), fileStream);
                        }
                        else if (format == "json")
                        {
                            SerializeData(new { University = university }, new JsonSerializer <Object>(), fileStream);
                        }
                    }
                    catch (IOException e)
                    {
                        w.Write(e.Message);
                    }
                }
            }
        }