static void Main(string[] args) { string csvFile; string neededFilePath; string logFile = "log.txt"; string typeNeeded; switch (args.Length) { case 1: csvFile = args[0]; neededFilePath = "result.txt"; typeNeeded = "xml"; break; case 2: csvFile = args[0]; neededFilePath = args[1]; typeNeeded = "xml"; break; case 3: csvFile = args[0]; neededFilePath = args[1]; typeNeeded = args[2]; break; default: csvFile = "dane.csv"; neededFilePath = "result.txt"; typeNeeded = "xml"; break; } try { if (!File.Exists(csvFile)) { throw new FileNotFoundException(); } else if (!Directory.Exists(neededFilePath)) { throw new ArgumentException(); } } catch (FileNotFoundException f) { Console.WriteLine("Plik data nie istnieje"); using (StreamWriter sw = new StreamWriter(logFile)) { sw.WriteLine("Błąd odczytu pliku data! " + csvFile + " " + DateTime.Now.ToString()); } return; } catch (ArgumentException argument) { Console.WriteLine("Podana sciezka jest niepoprawna"); using (StreamWriter sw = new StreamWriter(logFile)) { sw.WriteLine("Podana ścieżka jest nieprawidłowa! " + neededFilePath + " " + DateTime.Now.ToString() + "\n" + argument.Message.ToString()); } return; } var lines = File.ReadLines(csvFile); List <Student> personList = new List <Student>(); List <Student> errorList = new List <Student>(); var counter = 0; var tmp2 = new Student(); Hashtable std = new Hashtable(); int numOfStudents = 0; foreach (var line in lines) { counter = 0; var readed = line.Split(","); //sprawdzenie czy jest 9 kolumn for (int i = 0; i < readed.Length; i++) { if (!(readed[i].Length == 0)) { counter++; } } Student tmp = new Student { firstName = readed[0], lastName = Regex.Replace(readed[1], @"[\d-]", ""), studies = readed[2], mode = readed[3], index = readed[4], birth = DateTime.Parse(readed[5]), email = readed[6], mother = readed[7], father = readed[8] }; if (tmp2.index == readed[4] && counter != 0) { errorList.Add(tmp); } else { if (counter == 9) { personList.Add(tmp); } else { errorList.Add(tmp); } } tmp2 = tmp; } using (StreamWriter sw = new StreamWriter(logFile)) { foreach (Student person in errorList) { sw.WriteLine(person.ToString()); } } foreach (Student person in personList) { if (!std.ContainsKey(person.studies)) { foreach (Student tmpP in personList) { if (tmpP.studies == person.studies) { numOfStudents++; } } std.Add(person.studies, numOfStudents); } } if (typeNeeded.Contains("xml")) { CreateXml tmpX = new CreateXml(); tmpX.CreateFile(personList, std, neededFilePath); } else if (typeNeeded.Contains("json")) { CreateJson tmpJ = new CreateJson(); tmpJ.CreateFile(personList, std, neededFilePath); } }
static void Main(string[] args) { string csv; string results; string logs = "log.txt"; string type; switch (args.Length) { case 1: csv = args[0]; results = "result.txt"; type = "xml"; break; case 2: csv = args[0]; results = args[1]; type = "xml"; break; case 3: csv = args[0]; results = args[1]; type = args[2]; break; default: csv = "dane.csv"; results = "result.txt"; type = "xml"; break; } try { if (File.Exists(csv)) { if (!Directory.Exists(results)) { throw new ArgumentException(); } } else { throw new FileNotFoundException(); } } catch (FileNotFoundException) { Console.WriteLine("Plik data.csv nie dostepny"); using (StreamWriter sw = new StreamWriter(logs)) { sw.WriteLine("Brak/blad odczytu pliku data.csv " + csv + " " + DateTime.Now.ToString()); } return; } catch (ArgumentException argument) { Console.WriteLine("Bledny argument"); using (StreamWriter sw = new StreamWriter(logs)) { sw.WriteLine("Bledny argument " + results + " " + DateTime.Now.ToString() + "\n" + argument.Message.ToString()); } return; } var data = File.ReadLines(csv); List <Student> students = new List <Student>(); List <Student> errors = new List <Student>(); var itmp = 0; var jtmp = new Student(); Hashtable hash = new Hashtable(); int students_count = 0; foreach (string line in data) { itmp = 0; var readed = line.Split(","); for (int i = 0; i < readed.Length; i++) { if (readed[i].Length == 0) { continue; } itmp++; } Student stmp = new Student { fname = readed[0], lname = Regex.Replace(readed[1], @"[\d-]", ""), studies = readed[2], mode = readed[3], indexNumber = readed[4], birthdate = DateTime.Parse(readed[5]), email = readed[6], mothersName = readed[7], fathersName = readed[8] }; if (jtmp.indexNumber != readed[4] || itmp == 0) { if (itmp != 9) { errors.Add(stmp); } else { students.Add(stmp); } } else { errors.Add(stmp); } jtmp = stmp; } using (StreamWriter sw = new StreamWriter(logs)) { for (int i = 0; i < errors.Count; i++) { Student person = errors[i]; sw.WriteLine(person.ToString()); } } for (int i = 0; i < students.Count; i++) { if (hash.ContainsKey(students[i].studies)) { continue; } for (int j = 0; j < students.Count; j++) { Student tmpP = students[j]; if (tmpP.studies == students[i].studies) { students_count++; } } hash.Add(students[i].studies, students_count); } if (type.Contains("xml")) { CreateXml xml = new CreateXml(); xml.CreateFile(students, hash, results); } else { CreateJson json = new CreateJson(); json.CreateFile(students, hash, results); } }