Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- public class Program
- {
- const string DateFormat = "dd/MM/yyyy";
- const string DatesEnd = "end of dates";
- const string CommentsEnd = "end of comments";
- static void Main()
- {
- SortedDictionary<string, Student> studentGroup = new SortedDictionary<string, Student>();
- string input;
- // четем входа докато е валиден тук
- while ((input = Console.ReadLine()) != DatesEnd)
- {
- string[] tokens = input?.Split().ToArray();
- string studentName = tokens[0];
- bool studentAttendance = tokens.Length > 1;
- Student student = new Student(studentName);
- // проверяваме дали съществува студента ако не го добавяме
- if (!studentGroup.ContainsKey(studentName))
- {
- studentGroup.Add(studentName, student);
- }
- //ако е само име на студент не правим нищо друго и прескачаме долните редове
- if (!studentAttendance) continue;
- string[] dates = tokens[1].Split(',').ToArray();
- foreach (string date in dates)
- {
- DateTime currentDate = DateTime.ParseExact(date, DateFormat, CultureInfo.InvariantCulture);
- studentGroup[studentName].Attendance.Add(currentDate);
- }
- }
- // тук четем 2 поредица входове докато получаваме валидни коментари и провеярваме дали студента същестува ако не не правим нищо
- while ((input = Console.ReadLine()) != CommentsEnd)
- {
- string[] inputTokens = input?.Split('-').ToArray();
- string studentName = inputTokens[0];
- if (!studentGroup.ContainsKey(studentName)) continue;
- string comment = inputTokens[1];
- studentGroup[studentName].Comments.Add(comment);
- }
- // накрая печатаме резултата
- foreach (var s in studentGroup)
- {
- Console.WriteLine(s.Value.ToString());
- }
- }
- // един клас студент които ни държи всичко и му ovveride-ваме string-a за да си спестим писане на изхода
- public class Student
- {
- public Student(string name)
- {
- Name = name;
- this.Comments = new List<string>();
- this.Attendance = new List<DateTime>();
- }
- public string Name { get; set; }
- public List<string> Comments { get; set; }
- public List<DateTime> Attendance { get; set; }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- List<DateTime> orderedDates = this.Attendance.OrderBy(s => s).ToList();
- sb.AppendLine(this.Name)
- .AppendLine("Comments:");
- foreach (string c in this.Comments)
- {
- sb.AppendLine($"- {c}");
- }
- sb.AppendLine("Dates attended:");
- foreach (DateTime d in orderedDates)
- {
- sb.AppendLine($"-- {d.ToString(DateFormat)}");
- }
- return sb.ToString().TrimEnd();
- }
- }
- }
Add Comment
Please, Sign In to add comment