tanya_zheleva

10

Feb 12th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Globalization;
  5.  
  6. namespace ExamPreparation
  7. {
  8.     public sealed class Group
  9.     {
  10.         public Group(Town town)
  11.         {
  12.             this.Town = town;
  13.             this.Students = new List<Student>();
  14.         }
  15.  
  16.         public Town Town { get; private set; }
  17.  
  18.         public List<Student> Students { get; set; }
  19.  
  20.         public override string ToString()
  21.         {
  22.             return $"{this.Town.Name} => {string.Join(", ", this.Students)}";
  23.         }
  24.     }
  25.  
  26.     public sealed class Town
  27.     {
  28.         public Town(string name, int seatsCount)
  29.         {
  30.             this.Name = name;
  31.             this.SeatsCount = seatsCount;
  32.             this.Students = new List<Student>();
  33.         }
  34.  
  35.         public string Name { get; private set; }
  36.  
  37.         public int SeatsCount { get; private set; }
  38.  
  39.         public List<Student> Students { get; set; }
  40.     }
  41.  
  42.     public sealed class Student
  43.     {
  44.         public Student(string name, string email, DateTime registrationDate)
  45.         {
  46.             this.Name = name;
  47.             this.Email = email;
  48.             this.RegistrationDate = registrationDate;
  49.         }
  50.  
  51.         public string Name { get; private set; }
  52.  
  53.         public string Email { get; private set; }
  54.  
  55.         public DateTime RegistrationDate { get; private set; }
  56.  
  57.         public override string ToString()
  58.         {
  59.             return this.Email;
  60.         }
  61.     }
  62.  
  63.     public sealed class Preparation
  64.     {
  65.         public static void Main()
  66.         {
  67.             string input = Console.ReadLine();
  68.             List<Town> towns = new List<Town>();
  69.  
  70.             while (input != "End")
  71.             {
  72.                 if (input.Contains("=>"))
  73.                 {
  74.                     string[] townTokens = input.Split(new[] { '=', '>'}, StringSplitOptions.RemoveEmptyEntries);
  75.                     string townName = townTokens[0].Trim();
  76.  
  77.                     string[] seatTokens = townTokens[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  78.                     int seatsCount = int.Parse(seatTokens[0]);
  79.                     Town town = new Town(townName, seatsCount);
  80.                     towns.Add(town);
  81.                 }
  82.                 else
  83.                 {
  84.                     string[] tokens = input.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  85.                     string studentName = tokens[0].Trim();
  86.                     string email = tokens[1].Trim();
  87.                     DateTime registrationDate = DateTime.ParseExact(tokens[2].Trim(), "d-MMM-yyyy", CultureInfo.InvariantCulture);
  88.                     Student student = new Student(studentName, email, registrationDate);
  89.  
  90.                     towns[towns.Count - 1].Students.Add(student);
  91.                 }
  92.  
  93.                 input = Console.ReadLine();
  94.             }
  95.  
  96.             List<Group> groups = DistributeStudentsIntoGroups(towns);
  97.             Console.WriteLine($"Created {groups.Count} groups in {towns.Count} towns:");
  98.  
  99.             groups.ForEach(g => Console.WriteLine(g));
  100.         }
  101.  
  102.         private static List<Group> DistributeStudentsIntoGroups(List<Town> towns)
  103.         {
  104.             List<Group> groups = new List<Group>();
  105.  
  106.             foreach (var town in towns.OrderBy(t => t.Name))
  107.             {
  108.                 IEnumerable<Student> students = town.Students
  109.                         .OrderBy(s => s.RegistrationDate)
  110.                         .ThenBy(s => s.Name)
  111.                         .ThenBy(s => s.Email);
  112.  
  113.                 while (students.Any())
  114.                 {
  115.                     Group group = new Group(town);
  116.                     group.Students = students.Take(group.Town.SeatsCount).ToList();
  117.                     students = students.Skip(town.SeatsCount);
  118.                     groups.Add(group);
  119.                 }
  120.             }
  121.  
  122.             return groups;
  123.         }
  124.     }
  125. }
Add Comment
Please, Sign In to add comment