static void Main(string[] args) { SortedDictionary <string, Student> studentsDict = new SortedDictionary <string, Student>(); string input = Console.ReadLine(); while (input != "end of dates") { string[] userAndDates = input.Split(' ', ',').ToArray(); string name = userAndDates[0]; Student newStudent = new Student(); newStudent.Name = name; newStudent.DatesAttended = new List <DateTime>(); newStudent.Comments = new List <string>(); if (userAndDates.Length > 1) { for (int i = 1; i < userAndDates.Length; i++) { DateTime currentDate = DateTime.ParseExact(userAndDates[i], "dd/MM/yyyy", CultureInfo.InvariantCulture); newStudent.DatesAttended.Add(currentDate); } } if (!studentsDict.ContainsKey(newStudent.Name)) { studentsDict.Add(name, newStudent); } else { foreach (var date in newStudent.DatesAttended) { studentsDict[name].DatesAttended.Add(date); } } input = Console.ReadLine(); } input = Console.ReadLine(); while (input != "end of comments") { string[] userComments = input.Split('-').ToArray(); string name = userComments[0]; string comment = userComments[1]; if (!studentsDict.ContainsKey(name)) { input = Console.ReadLine(); continue; } else { studentsDict[name].Comments.Add(comment); } input = Console.ReadLine(); } foreach (var student in studentsDict) { Console.WriteLine(student.Key); Console.WriteLine("Comments:"); foreach (var comment in student.Value.Comments) { Console.WriteLine($"- {comment}"); } Console.WriteLine("Dates attended:"); foreach (var date in student.Value.DatesAttended.OrderBy(x => x)) { Console.WriteLine($"-- {date.ToString("dd/MM/yyyy")}"); } } }
static void Main(string[] args) { List<Student> students = new List<Student>(); string[] info = Console.ReadLine().Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); while (info[0] != "end") { string name = info[0]; if (!students.Any(x => x.Name == name)) { Student temp = new Student(); temp.Name = name; List<DateTime> tempList = new List<DateTime>(); for (int i = 1; i < info.Length; i++) { tempList.Add(DateTime.ParseExact(info[i], "dd/MM/yyyy", CultureInfo.InvariantCulture)); } temp.Dates = tempList; students.Add(temp); } else { Student temp = students.Where(x => x.Name == name).First(); for (int i = 1; i < info.Length; i++) { temp.Dates.Add(DateTime.ParseExact(info[i], "dd/MM/yyyy", CultureInfo.InvariantCulture)); } } info = Console.ReadLine().Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); } string[] comments = Console.ReadLine().Split('-').ToArray(); while (comments[0] != "end of comments") { string name = comments[0]; string comment = comments[1]; if (students.Any(x => x.Name == name)) { Student student = students.Where(x => x.Name == name).First(); if (student.Comments == null) { List<string> temp = new List<string>(); temp.Add(comment); student.Comments = temp; } else { student.Comments.Add(comment); } } comments = Console.ReadLine().Split('-').ToArray(); } students = students.OrderBy(x => x.Name).ToList(); for (int i = 0; i < students.Count; i++) { students[i].Dates = students[i].Dates.OrderBy(x => x).ToList(); } foreach (var student in students) { Console.WriteLine(student.Name); Console.WriteLine("Comments:"); if (student.Comments != null) { foreach (var comment in student.Comments) { Console.WriteLine("-- {0}", comment); } } Console.WriteLine("Dates attended:"); foreach (var date in student.Dates) { Console.WriteLine("-- {0}", date.ToString(@"dd\/MM\/yyyy")); } } }