Advertisement
Aliendreamer

mentorgroup

May 15th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 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.             if (!studentGroup.ContainsKey(studentName))
  28.             {
  29.                 studentGroup.Add(studentName, student);
  30.             }
  31.  
  32.             if (!studentAttendance) continue;
  33.             string[] dates = tokens[1].Split(',').ToArray();
  34.             foreach (string date in dates)
  35.             {
  36.                 DateTime currentDate = DateTime.ParseExact(date, DateFormat, CultureInfo.InvariantCulture);
  37.                 studentGroup[studentName].Attendance.Add(currentDate);
  38.             }
  39.         }
  40.  
  41.         while ((input = Console.ReadLine()) != CommentsEnd)
  42.         {
  43.             string[] inputTokens = input?.Split('-').ToArray();
  44.             string studentName = inputTokens[0];
  45.             if (!studentGroup.ContainsKey(studentName)) continue;
  46.             string comment = inputTokens[1];
  47.             studentGroup[studentName].Comments.Add(comment);
  48.         }
  49.  
  50.         foreach (var s in studentGroup)
  51.         {
  52.             Console.WriteLine(s.Value.ToString());
  53.         }
  54.  
  55.  
  56.     }
  57.     public class Student
  58.     {
  59.         public Student(string name)
  60.         {
  61.             Name = name;
  62.             this.Comments = new List<string>();
  63.             this.Attendance = new List<DateTime>();
  64.         }
  65.  
  66.         public string Name { get; set; }
  67.         public List<string> Comments { get; set; }
  68.         public List<DateTime> Attendance { get; set; }
  69.  
  70.         public override string ToString()
  71.         {
  72.             StringBuilder sb = new StringBuilder();
  73.             List<DateTime> orderedDates = this.Attendance.OrderBy(s => s).ToList();
  74.  
  75.             sb.AppendLine(this.Name)
  76.                 .AppendLine("Comments:");
  77.             foreach (string c in this.Comments)
  78.             {
  79.                 sb.AppendLine($"- {c}");
  80.             }
  81.  
  82.             sb.AppendLine("Dates attended:");
  83.             foreach (DateTime d in orderedDates)
  84.             {
  85.                 sb.AppendLine($"-- {d.ToString(DateFormat)}");
  86.             }
  87.             return sb.ToString().TrimEnd();
  88.  
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement