Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Globalization;
  7. //10. Student Groups
  8. namespace StudentGroups
  9. {
  10.     class StudentGroups
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<Town> towns = GetTownData();
  15.             List<Group> groups = GetGroups(towns);
  16.             PrintResults(groups);
  17.         }
  18.  
  19.         private static void PrintResults(List<Group> groups)
  20.         {
  21.  
  22.             int townsCount = groups.Select(x => x.Town).Distinct().Count();
  23.  
  24.             Console.WriteLine($"Created {groups.Count} groups in {townsCount} towns:");
  25.  
  26.             foreach (var group in groups.OrderBy(x => x.Town.Name))
  27.             {
  28.                 Console.Write($"{group.Town.Name} => ");
  29.  
  30.                 Console.WriteLine($"{string.Join(", ", group.Students.Select(x => x.Email))}");
  31.             }
  32.  
  33.         }
  34.  
  35.         private static List<Group> GetGroups(List<Town> towns)
  36.         {
  37.             List<Group> groups = new List<Group>();
  38.  
  39.             foreach (var town in towns.OrderBy(x => x.Name))
  40.             {
  41.  
  42.                 IEnumerable<Student> studs = town.Students.OrderBy(x => x.RegistrationDate).ThenBy(z => z.Name).ThenBy(y => y.Email);
  43.  
  44.                 while (studs.Any())
  45.                 {
  46.                     Group group = new Group();
  47.                     group.Town = town;
  48.                     group.Students = studs.Take(group.Town.SeatsCount).ToList();
  49.                     studs = studs.Skip(group.Town.SeatsCount);
  50.                     groups.Add(group);
  51.                 }
  52.             }
  53.             return groups;
  54.         }
  55.  
  56.         private static List<Town> GetTownData()
  57.         {
  58.             List<Town> towns = new List<Town>();
  59.             string inputLine = Console.ReadLine();
  60.  
  61.             while (inputLine != "End")
  62.             {
  63.                 if (inputLine.Contains("=>"))
  64.                 {
  65.                     Town town = new Town();
  66.                     List<string> input = Regex.Split(inputLine, @"\s=>\s").ToList();
  67.                     string townName = input[0];
  68.                     int seats = int.Parse(Regex.Match(input[1], @"\d+").Value);
  69.  
  70.                     town.Name = townName;
  71.                     town.SeatsCount = seats;
  72.                     town.Students = new List<Student>();
  73.  
  74.                     towns.Add(town);
  75.                 }
  76.                 else
  77.                 {
  78.                     List<string> input = inputLine.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
  79.                     Student student = new Student();
  80.                     string studName = input[0];
  81.                     string eMail = input[1];
  82.                     DateTime regDate = DateTime.ParseExact(input[2], "d-MMM-yyyy", CultureInfo.InvariantCulture);
  83.  
  84.                     student.Name = studName;
  85.                     student.Email = eMail;
  86.                     student.RegistrationDate = regDate;
  87.  
  88.                     towns.LastOrDefault().Students.Add(student);
  89.                 }
  90.                 inputLine = Console.ReadLine();
  91.             }
  92.             return towns;
  93.         }
  94.     }
  95.  
  96.     class Student
  97.     {
  98.         public string Name { get; set; }
  99.         public string Email { get; set; }
  100.         public DateTime RegistrationDate { get; set; }
  101.     }
  102.  
  103.     class Town
  104.     {
  105.         public string Name { get; set; }
  106.         public int SeatsCount { get; set; }
  107.         public List<Student> Students { get; set; }
  108.     }
  109.  
  110.     class Group
  111.     {
  112.         public Town Town { get; set; }
  113.         public List<Student> Students { get; set; }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement