Esempio n. 1
0
        public static void SplitStudentsWithLinkedList(string directory, string fileName, int count)
        {
            var path = Path.Combine(directory, fileName.Replace("{#count}", count.ToString()));

            using var sr = new StreamReader(path);
            var header    = string.Empty;
            var firstLine = true;

            var line     = string.Empty;
            var students = new LinkedList <Student>();
            var columns  = new List <string>();

            while ((line = sr.ReadLine()) != null)
            {
                if (firstLine)
                {
                    columns   = TextFormatter.FormatLineFromFileWithoutTabulation(line);
                    header    = line;
                    firstLine = false;
                    continue;
                }
                students.AddFirst(Student.PrepareFromText(columns, TextFormatter.FormatLineFromFileWithoutTabulation(line)));
            }
            sr.Close();

            var badStudentsPath = Path.Combine(directory, BadStudentsFileName
                                               .Replace("{#count}", count.ToString())
                                               .Replace("{#container}", "LinkedList"));
            var goodStudentsPath = Path.Combine(directory, GoodStudentsFileName
                                                .Replace("{#count}", count.ToString())
                                                .Replace("{#container}", "LinkedList"));

            if (File.Exists(goodStudentsPath))
            {
                File.Delete(goodStudentsPath);
            }
            if (File.Exists(badStudentsPath))
            {
                File.Delete(badStudentsPath);
            }

            using var gsw = new StreamWriter(goodStudentsPath, true, Encoding.UTF8);
            using var bsw = new StreamWriter(badStudentsPath, true, Encoding.UTF8);


            var minimumTabulation = students.Last().FirstName.Length > students.Last().LastName.Length
                ? Math.Ceiling(new decimal(students.Last().FirstName.Length) / 4)
                                    .ToString(CultureInfo.InvariantCulture)
                : Math.Ceiling(new decimal(students.Last().LastName.Length) / 4)
                                    .ToString(CultureInfo.InvariantCulture);

            gsw.WriteLine(header);
            bsw.WriteLine(header);

            foreach (var student in students.OrderBy(x => x.FirstName).ThenBy(x => x.LastName))
            {
                var rowText = TextFormatter.PrepareFileColumn($"{student.FirstName}", int.Parse(minimumTabulation) + 1);
                rowText += TextFormatter.PrepareFileColumn($"{student.FirstName}", int.Parse(minimumTabulation) + 1);
                rowText += TextFormatter.PrepareFileColumn($"{student.HomeworkGrades[0]}", 2);
                rowText += TextFormatter.PrepareFileColumn($"{student.HomeworkGrades[1]}", 2);
                rowText += TextFormatter.PrepareFileColumn($"{student.HomeworkGrades[2]}", 2);
                rowText += TextFormatter.PrepareFileColumn($"{student.HomeworkGrades[3]}", 2);
                rowText += TextFormatter.PrepareFileColumn($"{student.HomeworkGrades[4]}", 2);
                rowText += TextFormatter.PrepareFileColumn($"{student.ExamGrade}", 2);

                if (student.Average >= 5)
                {
                    gsw.WriteLine(rowText);
                }
                else
                {
                    bsw.WriteLine(rowText);
                }
            }
            gsw.Close();
            bsw.Close();
        }