Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Globalization;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         List<Student> students = ReadStudentsDates();
  11.         AddStudentsComments(students);
  12.         students = students.OrderBy(s => s.Name).ToList();
  13.         SortStudentsByDate(students);
  14.         SortStudentsData(students);
  15.         PrintReport(students);
  16.     }
  17.  
  18.     static List<Student> ReadStudentsDates()
  19.     {
  20.         List<Student> students = new List<Student>();
  21.         while (true)
  22.         {
  23.             string input = Console.ReadLine();
  24.             if (input == "end of dates")
  25.             {
  26.                 break;
  27.             }
  28.             string[] splitInput = input.Split();
  29.             string name = splitInput[0];
  30.             if (splitInput.Length == 1)
  31.             {
  32.                 if (!students.Any(a => a.Name == name))
  33.                 {
  34.                     Student newStudent = new Student
  35.                     {
  36.                         Name = name
  37.                     };
  38.                     students.Add(newStudent);
  39.                 }
  40.                 continue;
  41.             }
  42.             List<DateTime> dates = splitInput[1].
  43.                 Split(',').
  44.                 Select(d => DateTime.ParseExact(d, "dd/MM/yyyy", CultureInfo.InvariantCulture)).
  45.                 ToList();
  46.             if (students.Any(a => a.Name == name))
  47.             {
  48.                 Student exitingStudent = students.First(a => a.Name == name);
  49.                 exitingStudent.Dates = exitingStudent.Dates.Concat(dates).ToList();
  50.             }
  51.             else
  52.             {
  53.                 Student newStudent = new Student
  54.                 {
  55.                     Name = name,
  56.                     Dates = dates,
  57.                 };
  58.                 students.Add(newStudent);
  59.             }
  60.         }
  61.         return students;
  62.     }
  63.  
  64.     static void AddStudentsComments(List<Student> students)
  65.     {
  66.         while (true)
  67.         {
  68.             string input = Console.ReadLine();
  69.             if (input == "end of comments")
  70.             {
  71.                 break;
  72.             }
  73.             string[] splitInput = input.Split('-');
  74.             string name = splitInput[0];
  75.             if (!students.Any(a => a.Name == name))
  76.             {
  77.                 continue;
  78.             }
  79.             string[] comments = splitInput.Skip(1).ToArray();
  80.             Student exitingStudent = students.First(a => a.Name == name);
  81.             if (exitingStudent.Comments == null)
  82.             {
  83.                 exitingStudent.Comments = new List<string>();
  84.             }
  85.             foreach (string comment in comments)
  86.             {
  87.                 exitingStudent.Comments.Add(comment);
  88.             }
  89.         }
  90.     }
  91.  
  92.  
  93.     static void SortStudentsByDate(List<Student> students)
  94.     {
  95.         foreach (Student student in students)
  96.         {
  97.             if (student.Dates != null)
  98.             {
  99.                 student.Dates.Sort();
  100.             }
  101.         }
  102.     }
  103.  
  104.     static void SortStudentsData(List<Student> students)
  105.     {
  106.         foreach (Student student in students)
  107.         {
  108.             if (student.Dates != null)
  109.             {
  110.                 student.Dates.OrderBy(a => a);
  111.             }
  112.             if (student.Comments != null)
  113.             {
  114.                 student.Comments.OrderBy(a => a);
  115.             }
  116.         }
  117.     }
  118.  
  119.     static void PrintReport(List<Student> students)
  120.     {
  121.         foreach (Student student in students.OrderBy(s => s.Name))
  122.         {
  123.             Console.WriteLine(student.Name);
  124.             Console.WriteLine("Comments:");
  125.             if (student.Comments != null)
  126.             {
  127.                 foreach (string comment in student.Comments)
  128.                 {
  129.                     Console.WriteLine($"- {comment}");
  130.                 }
  131.             }
  132.             Console.WriteLine("Dates attended:");
  133.             if (student.Dates != null)
  134.             {
  135.                 foreach (DateTime date in student.Dates)
  136.                 {
  137.                     Console.WriteLine($"-- {date.Date:dd/MM/yyyy}");
  138.                 }
  139.             }
  140.         }
  141.     }
  142. }
  143.  
  144. class Student
  145. {
  146.     public string Name { get; set; }
  147.     public List<DateTime> Dates { get; set; }
  148.     public List<string> Comments { get; set; }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement