Aliendreamer

mentor group

Jul 11th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. public class Program
  8. {
  9.     const string DateFormat = "dd/MM/yyyy";
  10.     const string DatesEnd = "end of dates";
  11.     const string CommentsEnd = "end of comments";
  12.  
  13.     static void Main()
  14.     {
  15.         SortedDictionary<string, Student> studentGroup = new SortedDictionary<string, Student>();
  16.  
  17.         string input;
  18.         // четем входа докато е валиден тук
  19.         while ((input = Console.ReadLine()) != DatesEnd)
  20.         {
  21.             string[] tokens = input?.Split().ToArray();
  22.             string studentName = tokens[0];
  23.             bool studentAttendance = tokens.Length > 1;
  24.  
  25.             Student student = new Student(studentName);
  26.            
  27.             // проверяваме дали съществува студента ако не го добавяме
  28.             if (!studentGroup.ContainsKey(studentName))
  29.             {
  30.                 studentGroup.Add(studentName, student);
  31.             }
  32.            
  33.             //ако е само име на студент не правим нищо друго и прескачаме долните редове
  34.             if (!studentAttendance) continue;
  35.             string[] dates = tokens[1].Split(',').ToArray();
  36.             foreach (string date in dates)
  37.             {
  38.                 DateTime currentDate = DateTime.ParseExact(date, DateFormat, CultureInfo.InvariantCulture);
  39.                 studentGroup[studentName].Attendance.Add(currentDate);
  40.             }
  41.         }
  42.         // тук четем 2 поредица входове докато получаваме валидни коментари и провеярваме дали студента същестува ако не не правим нищо
  43.         while ((input = Console.ReadLine()) != CommentsEnd)
  44.         {
  45.             string[] inputTokens = input?.Split('-').ToArray();
  46.             string studentName = inputTokens[0];
  47.             if (!studentGroup.ContainsKey(studentName)) continue;
  48.             string comment = inputTokens[1];
  49.             studentGroup[studentName].Comments.Add(comment);
  50.         }
  51.         // накрая печатаме резултата
  52.         foreach (var s in studentGroup)
  53.         {
  54.             Console.WriteLine(s.Value.ToString());
  55.         }
  56.  
  57.  
  58.     }
  59.     // един клас студент които ни държи всичко и му ovveride-ваме string-a за да си спестим писане на изхода
  60.     public class Student
  61.     {
  62.         public Student(string name)
  63.         {
  64.             Name = name;
  65.             this.Comments = new List<string>();
  66.             this.Attendance = new List<DateTime>();
  67.         }
  68.  
  69.         public string Name { get; set; }
  70.         public List<string> Comments { get; set; }
  71.         public List<DateTime> Attendance { get; set; }
  72.  
  73.         public override string ToString()
  74.         {
  75.             StringBuilder sb = new StringBuilder();
  76.             List<DateTime> orderedDates = this.Attendance.OrderBy(s => s).ToList();
  77.  
  78.             sb.AppendLine(this.Name)
  79.                 .AppendLine("Comments:");
  80.             foreach (string c in this.Comments)
  81.             {
  82.                 sb.AppendLine($"- {c}");
  83.             }
  84.  
  85.             sb.AppendLine("Dates attended:");
  86.             foreach (DateTime d in orderedDates)
  87.             {
  88.                 sb.AppendLine($"-- {d.ToString(DateFormat)}");
  89.             }
  90.             return sb.ToString().TrimEnd();
  91.  
  92.         }
  93.     }
  94. }
Add Comment
Please, Sign In to add comment