public Student() { indexNumber = null; fname = null; lname = null; birthDate = DateTime.Today.ToString("d"); email = null; mothersName = null; fathersName = null; studies = null; }
public Student(string indexNumber, string fname, string lname, DateTime birthDate, string email, string mothersName, string fathersName, Studies studies) { this.indexNumber = "s" + indexNumber; this.fname = fname; this.lname = lname; this.birthDate = birthDate.ToString("d"); this.email = email; this.mothersName = mothersName; this.fathersName = fathersName; this.studies = studies; }
static void Main(string[] args) { var adress = args.Length > 1 ? args[0] : @"dane.csv"; var resultAdress = args.Length > 2 ? args[1] : @"result.xml"; var resultType = args.Length > 3 ? args[2] : "xml"; var logAdress = "log.txt"; var today = DateTime.Now.ToString("dd.MM.yyyy"); try { var lines = File.ReadLines(adress); var students = new HashSet <Student>(new OwnComparer()); Dictionary <string, int> activeStdCount = new Dictionary <string, int>(); using (var log = new StreamWriter(logAdress)) { foreach (var line in lines) { var data = line.Split(","); bool correct = true; foreach (var item in data) { if (string.IsNullOrEmpty(item) || string.IsNullOrWhiteSpace(item)) { correct = false; } } if (!correct) { log.WriteLine("Puste dane " + line); } else if (correct && data.Length < 9) { log.WriteLine("Dane za krótkie " + line); } else { if (!activeStdCount.ContainsKey(data[2])) { activeStdCount.Add(data[2], 1); } else { activeStdCount[data[2]]++; } var studies = new Studies { StudiesName = data[2], StudiesMode = data[3] }; var student = new Student { FirstName = data[0], SecondtName = data[1], Studies = studies, IndexNumber = "s" + data[4], Birthdate = DateTime.Parse(data[5]), Email = data[6], MotherName = data[7], FathersName = data[8], }; if (!students.Add(student)) { log.WriteLine("Student się powtarza " + line); } } } } var activeStd = new List <ActiveStudies>(); foreach (var item in activeStdCount) { activeStd.Add(new ActiveStudies { id = item.Key, value = item.Value }); } var uczelnia = new Uczelnia { createdAt = today, studenci = students, activeStudies = activeStdCount }; if (resultType == "xml") { XDocument doc = new XDocument(new XElement("university", new XAttribute("createdAt", today), new XAttribute("author", "Piotr Dębowski"), new XElement("studenci", from student in students select new XElement("student", new XAttribute("indexNumber", student.IndexNumber), new XElement("name", student.FirstName), new XElement("secondName", student.SecondtName), new XElement("birthdate", student.Birthdate), new XElement("email", student.Email), new XElement("mothersName", student.MotherName), new XElement("fathersName", student.FathersName), new XElement("studies", new XElement("name", student.Studies.StudiesName), new XElement("mode", student.Studies.StudiesMode) ))), new XElement("activeStudies", from asc in activeStdCount select new XElement("studies", new XAttribute("name", asc.Key), new XAttribute("numberOfStudents", asc.Value) ) ))); doc.Save(resultAdress); //var ns = new XmlSerializerNamespaces(); //ns.Add("", ""); //FileStream writer = new FileStream(resultAdress, FileMode.Create); //XmlSerializer serializer = new XmlSerializer(typeof(Uczelnia), new XmlRootAttribute("uczelnia")); //serializer.Serialize(writer, uczelnia, ns); } else if (resultType == "json") { var json = JsonConvert.SerializeObject(uczelnia, (Newtonsoft.Json.Formatting)Formatting.Indented); File.WriteAllText(resultAdress + ".json", json); } } catch (FileNotFoundException e) { Console.WriteLine("Exception caught: {0}", e); } catch (ArgumentException e) { Console.WriteLine("Exception caught: {0}", e); } catch (IOException e) { Console.WriteLine("Exception caught: {0}", e); } }
static void Main(string[] args) { //Create blanck log createLog(); //Standart values string path = @"data.csv"; string save = @"rezult"; string format = "xml"; try { path = args[0]; } catch (IndexOutOfRangeException) { Log("Use standart Path: " + path); } try { save = args[1]; } catch (IndexOutOfRangeException) { Log("Use standart Save: " + save); } try { format = args[2]; } catch (IndexOutOfRangeException) { Log("Use standart Format: " + format); } //read students var hash = new HashSet <Student>(new OwnComparer()); try { using (var stream = new StreamReader(path)) { string line = null; while ((line = stream.ReadLine()) != null) { string[] read = line.Split(','); bool ok = true; foreach (var tmp in read) { if (tmp.Equals("")) { Log("NULL value, student dismiss"); ok = false; } } if (ok) { if (read.Length == 9) { var readStudies = new Studies { name = read[2], mode = read[3] }; var st = new Student { fname = read[0], lname = read[1], indexNumber = read[4], birthdate = read[5], email = read[6], mothersName = read[7], fathersName = read[8], studies = readStudies }; hash.Add(st); } else { Log("Not correct informations. Find: " + read.Length + " Need: 9"); } } } } } catch (FileNotFoundException) { Log("File not found"); } //Counting Dictionary <string, int> activeStudnets = new Dictionary <string, int>(); foreach (var s in hash) { if (!activeStudnets.ContainsKey(s.studies.name)) { activeStudnets.Add(s.studies.name, 1); } else { activeStudnets[s.studies.name]++; } } //XML if (format.Equals("xml")) { XmlSerializer serializer = new XmlSerializer(typeof(HashSet <Student>), new XmlRootAttribute(@"uczelnia Created At " + DateTime.Now + new XmlRootAttribute("Author: Jan Kowalski"))); FileStream writer = new FileStream(save + ".xml", FileMode.Create); serializer.Serialize(writer, hash); } //JSON if (format.Equals("json")) { University university = new University(hash, activeStudnets); var js = JsonConvert.SerializeObject(university, Formatting.Indented); File.WriteAllText(save + ".json", js); } }
static void Main(string[] args) { var path = args[0]; var resultpath = args[1]; var resultformat = args[2]; var lines = File.ReadLines(path); var hash = new HashSet <Student>(new MyComp()); var today = DateTime.Today; University uni = new University(); uni.createdAt = today.ToString("dd.MM.yyyy"); uni.author = "Zuzanna Bubrowska"; using (var logput = new StreamWriter("log.txt")) { try { foreach (var line in lines) { var data = line.Split(","); bool log = false; foreach (var value in data) { if (string.IsNullOrEmpty(value)) { log = true; } } if (log) { logput.WriteLine(line); } else { Studies studies = new Studies { Name = data[2], Mode = data[3] }; Student student = new Student { FirstName = data[0], LastName = data[1], Index = data[4], BirthDate = DateTime.Parse(data[5]), Email = data[6], MotherName = data[7], FatherName = data[8], Studies = studies }; if (!hash.Add(student)) { logput.WriteLine(line); } else { hash.Add(student); } } } uni.students = hash; if (resultformat == "xml") { XmlSerializerNamespaces xns = new XmlSerializerNamespaces(); xns.Add("", ""); using (FileStream writer = new FileStream(resultpath, FileMode.Create)) { XmlRootAttribute root = new XmlRootAttribute("university"); XmlSerializer serializer = new XmlSerializer(typeof(University), root); serializer.Serialize(writer, uni, xns); } } else if (resultformat == "json") { var result = JsonConvert.SerializeObject(uni, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(resultpath, result); } else { logput.WriteLine("Nieobslugiwane rozszerzenie"); } } //var parsedDate = DateTime.Parse("2020-03-09"); //Console.WriteLine(parsedDate); catch (FileNotFoundException) { logput.WriteLine("Plik nie istnieje"); throw; } catch (ArgumentException) { logput.WriteLine("Podana sciezka jest niepoprawna"); throw; } } }
static void Main(string[] args) { var path = ""; var pathDes = ""; string format = ""; if (args.Length == 3) { path = args[0]; pathDes = args[1]; format = args[2]; } else { path = @"C:\Users\kasia\OneDrive\Pulpit\studia\IIrok\II_SEM\APBD\cw2\dane.csv"; pathDes = @"C:\Users\kasia\OneDrive\Pulpit\studia\IIrok\II_SEM\APBD\cw2\data.xml"; format = "xml"; } var pathOut = @"C:\Users\kasia\OneDrive\Pulpit\studia\IIrok\II_SEM\APBD\cw2\log.txt"; if (File.Exists(pathOut)) { File.Delete(pathOut); } using (FileStream fs = new FileStream(pathOut, FileMode.CreateNew)) { using (BinaryWriter w = new BinaryWriter(fs)) { try { FileStream fileStream = new FileStream(pathDes, FileMode.Create); var lines = File.ReadLines(path); var hash = new HashSet <Student>(new OwnComparer()); var hashStudies = new HashSet <Studies>(new StudiesComparer()); foreach (var line in lines) { var data = line.Split(","); Regex regex = new Regex("(,,)+"); if (data.Length != 9 || regex.IsMatch(line) || line.Length <= 8) { w.Write(line + '\n'); } else { var studies = new Studies { Name = data[2], Mode = data[3] }; hashStudies.Add(studies); var student = new Student { Name = data[0], LastName = data[1], Studies = studies, Index = data[4], BirthDate = DateTime.Parse(data[5]), Email = data[6], MothersName = data[7], FathersName = data[8] }; if (!hash.Add(student)) { w.Write(line + '\n'); } } } var university = new University { Students = hash }; if (format == "xml") { SerializeData(university, new XmlSerializer <University>(), fileStream); } else if (format == "json") { SerializeData(new { University = university }, new JsonSerializer <Object>(), fileStream); } } catch (IOException e) { w.Write(e.Message); } } } }