示例#1
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;
            }
        }
示例#2
0
 private static void CopyFileUsingFileInfo(FileInfo source, FileInfo des)
 {
     source.CopyTo(des.FullName, true);
 }
示例#3
0
文件: Program.cs 项目: GuddiniJr/czw2
        static void Main(string[] args)
        {
            //arrlist =  list
            //set  = hashset
            //map = dictionary


            string fileStart     = CheckArgument1(args);
            string fileResult    = CheckArgument2(args);
            string fileExtension = CheckArgument3(args);
            string fileLogs      = "data/łog.txt";

            StreamWriter streamWriter = new StreamWriter(fileLogs);
            FileInfo     file         = new FileInfo(fileStart);

            List <Student>           studentList = new List <Student>();
            Dictionary <string, int> stundetMap  = new Dictionary <string, int>();

            try
            {
                using (StreamReader stream = new StreamReader(file.OpenRead()))
                {
                    string line = "";
                    while ((line = stream.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        string[] student = line.Split(",");
                        Console.WriteLine(line);

                        if (checkData(student))
                        {
                            var kierunek = new Kierunek
                            {
                                nazwa = student[2],
                                tryb  = student[3],
                            };
                            Student student1 = new Student
                            {
                                Imie          = student[0],
                                Nazwisko      = student[1],
                                Index         = int.Parse(student[4]),
                                Kierunek      = kierunek,
                                DataUrodzenia = student[5],
                                Email         = student[6],
                                Ojciec        = student[8],
                                Matka         = student[7]
                            };
                            studentList.Add(student1);

                            if (stundetMap.ContainsKey(kierunek.nazwa))
                            {
                                stundetMap[kierunek.nazwa]++;
                            }
                            else
                            {
                                stundetMap.Add(kierunek.nazwa, 1);
                            }
                        }
                        else
                        {
                            streamWriter.WriteLine("Zle podane dane : " + line);
                        }
                    }
                }
            }catch (FileNotFoundException)
            {
                Console.WriteLine("Plik nazwa nie istnieje");
                streamWriter.WriteLine("Plik nazwa nie istnieje");
                return;
            }catch (ArgumentException)
            {
                Console.WriteLine("Podana ścieżka jest niepoprawna");
                streamWriter.WriteLine("Podana ścieżka jest niepoprawna");
                return;
            }catch (Exception e3)
            {
                Console.WriteLine(e3.Message);
                streamWriter.WriteLine(e3.Message);
                return;
            }

            streamWriter.Flush();
            streamWriter.Close();


            ActiveStudies activeSt = new ActiveStudies()
            {
                activeStudies = new List <ActiveStudie>()
            };

            foreach (KeyValuePair <string, int> entry in stundetMap)
            {
                activeSt.activeStudies.Add(new ActiveStudie {
                    name = entry.Key, number = entry.Value
                });
            }

            Students students = new Students
            {
                Student = studentList.Distinct(new EqualityComp()).ToList <Student>()
            };

            Uczelnia uczelnia = new Uczelnia
            {
                data          = "14.03.2020",
                autor         = "Alvan Maksym",
                student       = students,
                activeStudies = activeSt,
            };


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

            XmlSerializer           serializer = new XmlSerializer(typeof(Uczelnia));
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add("", "");

            serializer.Serialize(writer, uczelnia, namespaces);
        }