Advertisement
Guest User

Untitled

a guest
Jul 10th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. namespace TempDemo
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<Student> studentGroup = new List<Student>();
  13.             while (true)
  14.             {
  15.                 string command = Console.ReadLine();
  16.  
  17.                 if (command == "end of dates")
  18.                 {
  19.                     break;
  20.                 }
  21.                 string[] tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  22.                 string name = tokens[0];
  23.                 List<string> tempDates = new List<string>();
  24.                 if (tokens.Length > 1)
  25.                 {
  26.                     tempDates = tokens[1].Split(',').ToList();
  27.                 }
  28.  
  29.                 Student student = studentGroup.FirstOrDefault(x => x.Name == name);
  30.                 if (student == null)
  31.                 {
  32.                     var newStudent = new Student(name);
  33.                     for (int i = 0; i < tempDates.Count; i++)
  34.                     {
  35.                         DateTime parsedDate = DateTime.ParseExact(tempDates[i], "dd/MM/yyyy", CultureInfo.InvariantCulture);
  36.                         newStudent.Dates.Add(parsedDate);
  37.                     }
  38.                     studentGroup.Add(newStudent);
  39.                 }
  40.                 else
  41.                 {
  42.                     for (int i = 0; i < tempDates.Count; i++)
  43.                     {
  44.                         DateTime parsedDate = DateTime.ParseExact(tempDates[i], "dd/MM/yyyy", CultureInfo.InvariantCulture);
  45.                         student.Dates.Add(parsedDate);
  46.                     }
  47.                 }
  48.             }
  49.            
  50.             while (true)
  51.             {
  52.                 string command = Console.ReadLine();
  53.                 if (command == "end of comments")
  54.                 {
  55.                     break;
  56.                 }
  57.                 string[] tokens = command.Split('-', StringSplitOptions.RemoveEmptyEntries);
  58.                 string name = tokens[0];
  59.  
  60.                 var student = studentGroup.FirstOrDefault(n => n.Name == name);
  61.                 if (student != null)
  62.                 {
  63.                     student.Comments.Add(tokens[1]);
  64.                 }
  65.             }
  66.  
  67.             foreach (var student in studentGroup.OrderBy(n => n.Name))
  68.             {
  69.                 Console.WriteLine(student.Name);
  70.                 Console.WriteLine("Comments:");
  71.  
  72.                 foreach (var comment in student.Comments)
  73.                 {
  74.                     Console.WriteLine("- {0}", comment);
  75.                 }
  76.  
  77.                 Console.WriteLine("Dates attended:");
  78.                 foreach (var date in student.Dates.OrderBy(d => d.Date))
  79.                 {
  80.                     Console.WriteLine("-- {0:dd/MM/yyyy}", date);
  81.                 }
  82.             }
  83.  
  84.         }
  85.     }
  86.  
  87.     class Student
  88.     {
  89.         public Student(string name)
  90.         {
  91.             this.Name = name;
  92.             this.Dates = new List<DateTime>();
  93.             this.Comments = new List<string>();
  94.         }
  95.  
  96.         public string Name { get; set; }
  97.         public List<DateTime> Dates = new List<DateTime>();
  98.         public List<string> Comments = new List<string>();
  99.  
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement