Advertisement
ghostd0g

Untitled

Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 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. using System.Threading.Tasks;
  7.  
  8. namespace Exercise8
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<Student> students = new List<Student>();
  15.  
  16.             while (true)
  17.             {
  18.                 string line = Console.ReadLine();
  19.  
  20.                 if (line == "end of dates")
  21.                 {
  22.                     break;
  23.                 }
  24.  
  25.                 string[] currentStudent = line.Split();
  26.  
  27.  
  28.                 Student student = new Student();
  29.                 string name = currentStudent[0];
  30.                 student.Name = name;
  31.                 student.Dates = new List<DateTime>();
  32.                 student.Comments = new List<string>();
  33.  
  34.                 if (currentStudent.Length > 1)
  35.                 {
  36.                     string[] eachDate = currentStudent[1].Split(',');
  37.  
  38.                     for (int i = 0; i < eachDate.Length; i++)
  39.                     {
  40.  
  41.                         student.Dates.Add(DateTime.ParseExact(eachDate[i], "dd/MM/yyyy", CultureInfo.InvariantCulture));
  42.                     }
  43.  
  44.                     if (students.Any(x => x.Name == student.Name))
  45.                     {
  46.                         Student existingStudent = students
  47.                                 .First(x => x.Name == student.Name);
  48.                         existingStudent.Dates.AddRange(student.Dates);
  49.  
  50.                     }
  51.                     else
  52.                     {
  53.                         students.Add(student);
  54.                     }
  55.                 }
  56.                 else
  57.                 {
  58.                     students.Add(student);
  59.                 }
  60.  
  61.             }
  62.             while (true)
  63.             {
  64.                 string line = Console.ReadLine();
  65.  
  66.                 if (line == "end of comments")
  67.                 {
  68.                     break;
  69.                 }
  70.  
  71.                 string[] currentStudent = line.Split('-');
  72.  
  73.                 Student student = new Student();
  74.                 string name = currentStudent[0];
  75.                 student.Comments = new List<string>();
  76.                 student.Name = name;
  77.  
  78.  
  79.                 string comment = currentStudent[1];
  80.                 student.Comments.Add(comment);
  81.  
  82.                 if (students.Any(x => x.Name == student.Name))
  83.                 {
  84.                     Student existingStudent = students
  85.                             .First(x => x.Name == student.Name);
  86.  
  87.                     existingStudent.Comments.AddRange(student.Comments);
  88.                 }                
  89.  
  90.  
  91.             }
  92.  
  93.  
  94.  
  95.             foreach (var student in students.OrderBy(x => x.Name))
  96.             {
  97.                 Console.WriteLine(student.Name);
  98.                 Console.WriteLine("Comments:");
  99.                 foreach (var comment in student.Comments)
  100.                 {
  101.                     Console.WriteLine($"- {comment}");
  102.                 }
  103.                 Console.WriteLine("Dates attended:");
  104.                 foreach (var date in student.Dates.OrderBy(x => x))
  105.                 {
  106.                     Console.WriteLine($"-- {date:dd/MM/yyyy}");
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     class Student
  113.     {
  114.         public string Name { get; set; }
  115.         public List<string> Comments { get; set; }
  116.         public List<DateTime> Dates { get; set; }
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement