Esempio n. 1
0
        static void Main(string[] args)
        {
            //var path = @"D:\APBD\task2\data.csv";


            XmlSerializer xMLSerializer = new XmlSerializer(typeof(University), new XmlRootAttribute("university"));


            var studentList = new List <Student>();

            try
            {
                string csvpath       = Console.ReadLine();
                string xmlOrJsonPath = Console.ReadLine();
                string format        = Console.ReadLine();


                if (File.Exists(csvpath) && Directory.Exists(xmlOrJsonPath))
                {
                    using (var stream = new StreamReader(File.OpenRead(csvpath)))
                    {
                        string line = null;

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

                            var st = new Student
                            {
                                FirstName   = student[0],
                                LastName    = student[1],
                                indexNumber = student[2],
                                birthdate   = student[3],
                                email       = student[4],
                                mothersName = student[5],
                                fathersName = student[6],
                                studies     = new Studies(student[7], student[8])
                            };

                            studentList.Add(st);
                        }
                        var university = new University
                        {
                            createdAt = DateTime.Now.ToString(),
                            Author    = "Maryia Ruzava",
                            students  = studentList
                        };

                        if (format.Equals("xml"))
                        {
                            FileStream writer = new FileStream(xmlOrJsonPath + "result.xml", FileMode.Create);
                            xMLSerializer.Serialize(writer, university);
                        }
                        else if (format.Equals("json"))
                        {
                            var jsonString = JsonSerializer.Serialize(university);
                            File.WriteAllText(xmlOrJsonPath + "result.json", jsonString);
                        }
                    }
                }
                else
                {
                    if (!File.Exists(csvpath))
                    {
                        throw new FileNotFoundException("file does not exist");
                    }
                    if (!Directory.Exists(xmlOrJsonPath))
                    {
                        throw new ArgumentException("the path is incorrect");
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogging(ex);
            }

            //var jsonString = JsonSerializer.Serialize(list);
            //File.WriteAllText("data.json", jsonString);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            string path       = @"C:\Users\Administrator\Desktop\APBD\tutorial2\ConsoleApp1\file.csv";
            var    FileWriter = new FileStream(@"data.xml", FileMode.Create);
            var    LogWriter  = new FileStream("log.txt", FileMode.Create);

            using (var writer = new StreamWriter(LogWriter))
                try
                {
                    var students = new List <Student>();
                    using (var stream = new StreamReader(File.OpenRead(path)))
                    {
                        string line = null;
                        while ((line = stream.ReadLine()) != null)
                        {
                            string[] student = line.Split(',');
                            var      stud    = new Student
                            {
                                FirstName     = student[0],
                                LastName      = student[1],
                                Studies       = new Studies(student[2], student[3]),
                                StudentNumber = student[4],
                                DateOfBirth   = student[5],
                                Email         = student[6],
                                MotherName    = student[7],
                                FatherName    = student[8]
                            };
                            students.Add(stud);
                        }
                        var uni = new University
                        {
                            CreatedAt = DateTime.Now.ToString("dd.MM.yyyy", DateTimeFormatInfo.InvariantInfo),
                            Author    = "Vlad Kotowski",
                            Students  = students
                        };

                        if (args.Length != 3 || !args[2].ToLower().Equals("json"))
                        {
                            var serializer = new XmlSerializer(typeof(University));
                            serializer.Serialize(FileWriter, uni);
                        }
                        else
                        {
                            var UniWrapper = new UniversityHeader {
                                University = uni
                            };
                            var jsonString = JsonSerializer.Serialize(UniWrapper);
                            File.WriteAllText("data.json", jsonString);
                        }
                    }
                }
                catch (FileNotFoundException ex)
                {
                    writer.WriteLine("File does not exists");
                    Log("File does not exists");
                }
            catch (ArgumentException ex)
            {
                writer.WriteLine("The path is incorrect");
                Log("The path is incorrect");
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var listOfStudents = new HashSet <Student>(new CustomComparer());

            //error log
            using var streamWriter = new StreamWriter(@"log.txt");
            string path, outpath, format;

            //default xml, dane/dane.csv , result.xml
            format = args.Length > 2 ? args[2] : "xml";
            // path = args.Length > 0 ? args[0] : @"Dane/dane.csv"; TODO: UNCOMMENT
            path    = @"C:\Users\Weronika Wawrzyniak\Desktop\APBD-tut2\ConsoleApp1\ConsoleApp1\Dane\dane.csv";
            outpath = args.Length > 1 ? args[1] : $"output/result.{format}";



            if (!File.Exists(path))
            {
                streamWriter.WriteLine("Data source doesnt exist");
            }
            if (!File.Exists(outpath))
            {
                streamWriter.WriteLine("Output file doesnt exist - creating a new one ");
            }
            else
            {
                streamWriter.WriteLine("Output file exists - override");
            }


            var fi = new FileInfo(path);

            using (var stream = new StreamReader(fi.OpenRead()))
            {
                string line = null;
                //creating a new student from the stream
                while ((line = stream.ReadLine()) != null)
                {
                    string[] columns = line.Split(',');
                    //only if number of columns is 9
                    if (columns.GetLength(0) == 9)
                    {
                        Boolean allGood = true;
                        //and if none of the columns is empty
                        foreach (string c in columns)
                        {
                            if (string.IsNullOrWhiteSpace(c))
                            {
                                allGood = false;
                            }
                        }
                        if (allGood == true)
                        {
                            var buffStudent = new Student
                            {
                                FirstName = columns[0],
                                LastName  = columns[1],
                                BirthDate = DateTime.Parse(columns[5]),
                                Studies   = new Studies
                                {
                                    mode = columns[3],
                                    name = columns[2]
                                },
                                Email        = columns[6],
                                MothersName  = columns[7],
                                FathersName  = columns[8],
                                StudentIndex = columns[4]
                            };
                            //will be added to the list only if there are no duplicates
                            if (listOfStudents.Contains(buffStudent))
                            {
                                //duplicate error into log.txt

                                streamWriter.WriteLine($"Student with the firstName: {buffStudent.FirstName} was not added due to duplicate");
                            }
                            else
                            {
                                listOfStudents.Add(buffStudent);
                            }
                        }
                        else
                        {  //empty columns error into log.txt
                            streamWriter.WriteLine("One or more columns empty");
                        }
                    }
                    else
                    {  //not 9 columns error into log.txt
                        streamWriter.WriteLine("Not described by 9 data columns.");
                    }
                }
            }
            activeStudies ac = new activeStudies()
            {
                studentsList = listOfStudents,
            };
            University uni = new University()
            {
                students = listOfStudents, pary = ac.countSubjects()
            };

            uni.random();
            string   author  = "Weronika_Wawrzyniak_s19515";
            DateTime time    = DateTime.Now;
            string   message = ($"university_created_at_{time}_author_{author}");

            switch (format)
            {
            case "xml":
                //  var writer = new FileStream(outpath, FileMode.Create);
                var writer      = new FileStream("result.xml", FileMode.Create);
                var serialaizer = new XmlSerializer(typeof(University), new XmlRootAttribute(message));
                serialaizer.Serialize(writer, uni);
                break;

            case "json":
                var jsonString = JsonSerializer.Serialize(listOfStudents);
                File.WriteAllText("data.json", jsonString);
                break;

            default:
                streamWriter.WriteLine("Invalid path");
                break;
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            var context = new DbContext(connectionString);

            /*
             * BaseRepository<Student> studentrep = new BaseRepository<Student>(context);
             * var studentlist = studentrep.AsEnumerable().ToList();
             *
             * BaseRepository<Teacher> teacherrep = new BaseRepository<Teacher>(context);
             * var teacherlist = teacherrep.AsEnumerable().ToList();
             *
             * BaseRepository<University> univerrep = new BaseRepository<University>(context);
             * var univerlist = univerrep.AsEnumerable().ToList();
             *
             * BaseRepository<Teacher_University> teach_univerrep = new BaseRepository<Teacher_University>(context);
             * var teach_univerlist = teach_univerrep.AsEnumerable().ToList();
             *
             * BaseRepository<Address> addressrep = new BaseRepository<Address>(context);
             * var addresslist = addressrep.AsEnumerable().ToList();
             *
             *
             * var query1 = from s in studentlist
             *          from u in univerlist
             *          where u.Id == s.UniversityId
             *          from a in addresslist
             *           where a.Id == s.AddressId
             *          select (s.FirstName, s.LastName,
             *                  a.City, a.StreetOrDistrict, a.House, a.Appartment,
             *                  u.Name);
             *
             * var stquery = query1.ToList();
             *
             * var query2 = from tu in teach_univerlist
             *          from t in teacherlist
             *          where t.Id == tu.TeacherId
             *          from u in univerlist
             *          where u.Id == tu.UniversityId
             *          from a in addresslist
             *          where a.Id == u.AddressId
             *          select (t.FirstName, t.LastName,
             *                 u.Name, a.City);
             *
             * var teachersquery = query2.ToList();
             *
             *
             * Console.WriteLine("QUERY - Students\n");
             * foreach (var item in stquery)
             * {
             * Console.WriteLine(item);
             * }
             * Console.WriteLine();
             *
             * Console.WriteLine("QUERY - Teachers\n");
             * foreach (var item in teachersquery)
             * {
             * Console.WriteLine(item);
             * }
             *
             */
            var rep = new BaseRepository <University>(context);

            University univer1 = new University
            {
                Name        = "Taterakan",
                AddressId   = 2,
                Email       = "*****@*****.**",
                PhoneNumber = "+374333333"
            };

            Console.WriteLine("ADD " + rep.Add(univer1));

            Console.WriteLine("UPDATE " + rep.Update(2011, new University {
                DestroyDate = DateTime.Now
            }));

            Console.WriteLine("REMOVE " + rep.RemoveAt(8002));
        }