<file_sep>﻿using System;
using System.Collections.Generic;
using System.Text;

namespace lab1
{
    public interface IConverter
    {
        void Convert(List<Student> students, string outFile, List<string> header);
    }
}
<file_sep>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using OfficeOpenXml;

namespace lab1
{
    public class ExcelConverter : IConverter
    {

        /// <summary>
        /// Create .xlsx file and write data there
        /// </summary>
        public void Convert(List<Student> students, string outFile, List<string> header)
        {
            FileInfo fi = new FileInfo(outFile + ".xlsx");

            //Create a new ExcelPackage
            using (ExcelPackage excelPackage = new ExcelPackage())
            {
                //Create the WorkSheet
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");

                for (int i = 0; i < header.Count; i++)
                {
                    worksheet.Cells[1, i + 1].Value = header[i].ToString();
                }
                for (int i = 0, j = 0; i < students.Count; i++)
                {
                    worksheet.Cells[i + 2, j + 1].Value = students[i].Name;
                    worksheet.Cells[i + 2, j + 2].Value = students[i].Surname;
                    worksheet.Cells[i + 2, j + 3].Value = students[i].Fathername;
                    foreach (Subject subject in students[i].Subjects)
                    {
                        worksheet.Cells[i + 2, j + 4].Value = subject.Mark;
                        j++;
                    }
                    worksheet.Cells[i + 2, header.Count + 1].Value = students[i].AverageMark;
                    j = 0;

                }
                int number = 0;
                for (int z = 3, j = 4; z < students[0].Subjects.Count + 3; z++)
                {
                    worksheet.Cells[students.Count + 2, j].Value = AverageForSubject(students, header[z]);
                    j++;
                    number = j;
                }
                worksheet.Cells[students.Count + 2, number].Value = AverageForAllGroup(students);

                //Save your file
                excelPackage.SaveAs(fi);
            }

        }

        /// <summary>
        /// Average for subject
        /// </summary>
        /// <param name="nameSubject"></param>
        /// <returns></returns>
        public double AverageForSubject(List<Student> students, string nameSubject)
        {
            int sum = 0;
            foreach (Student student in students)
            {
                foreach (Subject subject in student.Subjects)
                {
                    if (subject.Name.Equals(nameSubject))
                    {
                        sum += subject.Mark;
                    }
                }
            }
            return Math.Round((double)sum / students.Count, 3);
        }

        /// <summary>
        /// Average for all group and all subject
        /// </summary>
        /// <returns>one number(average mark)</returns>
        public double AverageForAllGroup(List<Student> students)
        {
            double sum = 0;

            foreach (Student student in students)
            {
                sum += student.AverageMark;
            }
            return Math.Round(sum / students.Count, 2);
        }
    }
}
<file_sep>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;

namespace lab1
{
    public class JsonConverter : IConverter
    {
        
        public void Convert(List<Student> students, string outFile, List<string> header)
        {
            //open file stream
            using (StreamWriter file = File.CreateText(outFile + ".json"))
            {
                JsonSerializer serializer = new JsonSerializer();
                //serialize object directly into file stream
                serializer.Serialize(file, students);
            }
        }
    }
}
<file_sep>﻿using System;

namespace lab1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileWorker excelAgregator = new FileWorker(args[0], args[1], args[2]);
            foreach (Student student in excelAgregator.Students)
            {
                Console.WriteLine(student);
            }

            if (excelAgregator.Format == "Excel")
            {
                excelAgregator.Convert(new ExcelConverter());
            }
            else
            {
                excelAgregator.Convert(new JsonConverter());
            }
            Console.ReadKey();
        }
    }
}
<file_sep>﻿using Newtonsoft.Json;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace lab1
{
    public class FileWorker
    {
        public List<Student> Students = new List<Student>();
        public List<string> Header = new List<string>();
        public string Format;
        public readonly string OutFile;
        public readonly string NameFile;
        /// <summary>
        /// Constructor for ClassCollection
        /// </summary>
        /// <param name="nameFile"></param>
        /// <param name="outFile"></param>
        public FileWorker(string nameFile = "", string outFile = "", string format = "Excel")
        {
            OutFile = outFile;
            NameFile = nameFile;
            Format = format;
            ReadFromFile();
        }

        public void ReadFromFile()
        {
            bool isHeader = true;
            try
            {
                StreamReader streamReader = new StreamReader(NameFile + ".csv");
                string str;
                while ((str = streamReader.ReadLine()) != null)
                {
                    if (isHeader)
                    {
                        CreateHeader(str);
                        isHeader = false;
                    }
                    else
                    {
                        try
                        {
                            Students.Add(CreateStudent(str));
                        }
                        catch (CsvLineException e)
                        {
                            Console.WriteLine("Ошибка " + e.Message);
                            Console.WriteLine($"Некорректная строка : {e.CsvStr}");
                        }
                    }

                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Файл " + NameFile + " не найден.");
            }
        }

        public void Convert(IConverter converter)
        {
            converter.Convert(Students, OutFile, Header);
        }

        /// <summary>
        /// Create header for sheet
        /// </summary>
        /// <param name="str"></param>
        public void CreateHeader(string str)
        {
            string[] mass = str.Split(';');
            foreach (var header in mass)
            {
                Header.Add(header);
            }
        }
        

        /// <summary>
        /// Factory for create student
        /// </summary>
        /// <returns>Student</returns>
        public Student CreateStudent(string inputData)
        {
            string[] splitData = inputData.Split(';');
            List<Subject> subjects = new List<Subject>();

            for (int i = 3; i < splitData.Length; i++)
            {
                subjects.Add(new Subject() { Name = Header[i], Mark = Int32.Parse(splitData[i]) });
            }
            var student = new Student() { Name = splitData[0], Surname = splitData[1], Fathername = splitData[2], Subjects = subjects };
            student.AverageMark = student.GetAverageMark();
            return student;

        }
    }
}
