Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 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.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApp1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             List<Town> towns = new List<Town>();
  16.             string input = Console.ReadLine();
  17.             towns = GetDataRecursively(input, towns);
  18.             towns = SortedTowns(towns);
  19.             towns = GroupingStudents(towns);
  20.             Print(towns);
  21.         }
  22.         public static Town CreateTown(string input)
  23.         {
  24.             Town town = new Town();
  25.             string[] townInfo = input.Split(new[] { " => " }, StringSplitOptions.RemoveEmptyEntries);
  26.             town.Name = townInfo[0];
  27.             town.SeatCapacity = townInfo[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).First();
  28.             town.Students = new List<Student>();
  29.             town.Groups = new List<Group>();
  30.             return town;
  31.         }
  32.         public static Student CreateStudent(string input)
  33.         {
  34.             string[] studentInfo = Regex.Split(input, @"\s*\|\s*");
  35.             Student student = new Student();
  36.             student.Name = studentInfo[0];
  37.             student.email = studentInfo[1];
  38.             student.RegistrationDate = DateTime.ParseExact(studentInfo[2], "d-MMM-yyyy", CultureInfo.InvariantCulture);
  39.             return student;
  40.         }
  41.         public static void Print(List<Town> towns)
  42.         {
  43.             Console.WriteLine($"Created {towns.Sum(x => x.Groups.Count)} groups in {towns.Count} towns:");
  44.             foreach (var town in towns.OrderBy(x => x.Name))
  45.             {
  46.                 foreach (var group in town.Groups)
  47.                 {
  48.                     List<string> emails = new List<string>();
  49.                     foreach (var student in group.StudentsInGroup)
  50.                     {
  51.                         emails.Add(student.email);
  52.                     }
  53.                     Console.WriteLine($"{town.Name} => { string.Join(", ", emails)}");
  54.                 }                
  55.             }
  56.             return;
  57.         }
  58.         public static List<Town> GetDataRecursively(string input, List<Town> towns)
  59.         {
  60.             if (input == "End")
  61.             {
  62.                 return towns;
  63.             }
  64.             Town town = CreateTown(input);
  65.             while (true)
  66.             {
  67.                 input = Console.ReadLine();
  68.                 if (!input.Contains(" => ") && input != "End")
  69.                 {
  70.                     Student student = CreateStudent(input);
  71.                     town.Students.Add(student);
  72.                 }
  73.                 else
  74.                 {
  75.                     towns.Add(town);
  76.                     if (input == "End")
  77.                     {
  78.                         break;
  79.                     }
  80.                     else
  81.                     {
  82.                         towns = GetDataRecursively(input, towns);
  83.                         break;
  84.                     }
  85.                 }
  86.             }
  87.             return towns;
  88.         }
  89.         public static List<Town> SortedTowns(List<Town> towns)
  90.         {
  91.             for (int i = 0; i < towns.Count; i++)
  92.             {
  93.                 towns[i].Students = towns[i].Students.OrderBy(z => z.RegistrationDate).ThenBy(y => y.Name).ThenBy(x => x.email).ToList();
  94.             }
  95.             return towns;
  96.         }
  97.         public static List<Town> GroupingStudents(List<Town> towns)
  98.         {
  99.             foreach (var town in towns)
  100.             {
  101.                 Group group = new Group();
  102.                 group.StudentsInGroup = new List<Student>();
  103.                 int studentCounter = 0;
  104.                 foreach (var student in town.Students)
  105.                 {
  106.                     studentCounter++;
  107.                     if (group.StudentsInGroup.Count < town.SeatCapacity)
  108.                     {
  109.                         group.StudentsInGroup.Add(student);
  110.                         if (studentCounter == town.Students.Count)
  111.                         {
  112.                             town.Groups.Add(group);
  113.                         }
  114.                     }
  115.                     else
  116.                     {
  117.                         town.Groups.Add(group);
  118.                         group = new Group();
  119.                         group.StudentsInGroup = new List<Student>();
  120.                         group.StudentsInGroup.Add(student);
  121.                         if (studentCounter == town.Students.Count)
  122.                         {
  123.                             town.Groups.Add(group);
  124.                         }
  125.                     }
  126.                 }
  127.             }
  128.             return towns;
  129.         }
  130.     }
  131.     class Town
  132.     {
  133.         public List<Student> Students { get; set; }
  134.         public string Name { get; set; }
  135.         public int SeatCapacity { get; set; }
  136.         public List<Group> Groups { get; set; }
  137.     }
  138.     class Student
  139.     {
  140.         public string Name { get; set; }
  141.         public string email { get; set; }
  142.         public DateTime RegistrationDate { get; set; }
  143.     }
  144.     class Group
  145.     {
  146.         public List<Student> StudentsInGroup { get; set; }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement