Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Globalization;
- namespace ExamPreparation
- {
- public sealed class Group
- {
- public Group(Town town)
- {
- this.Town = town;
- this.Students = new List<Student>();
- }
- public Town Town { get; private set; }
- public List<Student> Students { get; set; }
- public override string ToString()
- {
- return $"{this.Town.Name} => {string.Join(", ", this.Students)}";
- }
- }
- public sealed class Town
- {
- public Town(string name, int seatsCount)
- {
- this.Name = name;
- this.SeatsCount = seatsCount;
- this.Students = new List<Student>();
- }
- public string Name { get; private set; }
- public int SeatsCount { get; private set; }
- public List<Student> Students { get; set; }
- }
- public sealed class Student
- {
- public Student(string name, string email, DateTime registrationDate)
- {
- this.Name = name;
- this.Email = email;
- this.RegistrationDate = registrationDate;
- }
- public string Name { get; private set; }
- public string Email { get; private set; }
- public DateTime RegistrationDate { get; private set; }
- public override string ToString()
- {
- return this.Email;
- }
- }
- public sealed class Preparation
- {
- public static void Main()
- {
- string input = Console.ReadLine();
- List<Town> towns = new List<Town>();
- while (input != "End")
- {
- if (input.Contains("=>"))
- {
- string[] townTokens = input.Split(new[] { '=', '>'}, StringSplitOptions.RemoveEmptyEntries);
- string townName = townTokens[0].Trim();
- string[] seatTokens = townTokens[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- int seatsCount = int.Parse(seatTokens[0]);
- Town town = new Town(townName, seatsCount);
- towns.Add(town);
- }
- else
- {
- string[] tokens = input.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
- string studentName = tokens[0].Trim();
- string email = tokens[1].Trim();
- DateTime registrationDate = DateTime.ParseExact(tokens[2].Trim(), "d-MMM-yyyy", CultureInfo.InvariantCulture);
- Student student = new Student(studentName, email, registrationDate);
- towns[towns.Count - 1].Students.Add(student);
- }
- input = Console.ReadLine();
- }
- List<Group> groups = DistributeStudentsIntoGroups(towns);
- Console.WriteLine($"Created {groups.Count} groups in {towns.Count} towns:");
- groups.ForEach(g => Console.WriteLine(g));
- }
- private static List<Group> DistributeStudentsIntoGroups(List<Town> towns)
- {
- List<Group> groups = new List<Group>();
- foreach (var town in towns.OrderBy(t => t.Name))
- {
- IEnumerable<Student> students = town.Students
- .OrderBy(s => s.RegistrationDate)
- .ThenBy(s => s.Name)
- .ThenBy(s => s.Email);
- while (students.Any())
- {
- Group group = new Group(town);
- group.Students = students.Take(group.Town.SeatsCount).ToList();
- students = students.Skip(town.SeatsCount);
- groups.Add(group);
- }
- }
- return groups;
- }
- }
- }
Add Comment
Please, Sign In to add comment