Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Globalization;
- namespace ExamPreparation
- {
- public class Town
- {
- public Town(string name, int seats)
- {
- this.Name = name;
- this.Seats = seats;
- this.Students = new List<Student>();
- }
- public string Name { get; private set; }
- public int Seats { get; private set; }
- public int GroupsCount
- {
- get
- {
- int groups = 1;
- if (this.Students.Count > this.Seats)
- {
- if (this.Students.Count % this.Seats == 0)
- {
- groups = this.Students.Count / this.Seats;
- }
- else
- {
- groups = this.Students.Count / this.Seats + this.Students.Count % this.Seats;
- }
- }
- return groups;
- }
- }
- public List<Student> Students { get; set; }
- public override string ToString()
- {
- StringBuilder output = new StringBuilder();
- List<Student> ordered = this.Students.OrderBy(s => s.RegisterDate)
- .ThenBy(s => s.Name)
- .ThenBy(s => s.Email)
- .ToList();
- while (ordered.Any())
- {
- IEnumerable<Student> currentGroup = ordered.Take(this.Seats);
- output.AppendLine($"{this.Name} => {string.Join(", ", currentGroup)}");
- if (this.Seats > ordered.Count)
- {
- ordered.Clear();
- }
- else
- {
- ordered.RemoveRange(0, this.Seats);
- }
- }
- return output.ToString().Trim();
- }
- }
- public class Student
- {
- public Student(string name, string email, DateTime registerDate)
- {
- this.Name = name;
- this.Email = email;
- this.RegisterDate = registerDate;
- }
- public string Name { get; private set; }
- public string Email { get; private set; }
- public DateTime RegisterDate { get; private set; }
- public override string ToString()
- {
- return this.Email;
- }
- }
- public sealed class Startup
- {
- public static void Main()
- {
- string input = Console.ReadLine();
- List<Town> towns = new List<Town>();
- while (input != "End")
- {
- if (input.Contains("=>"))
- {
- string[] tokens = input.Split(new[] { '=', '>' }, StringSplitOptions.RemoveEmptyEntries);
- string townName = tokens[0].Trim();
- string[] seatTokens = tokens[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- int seatsCount = int.Parse(seatTokens[0]);
- Town currentTown = new Town(townName, seatsCount);
- towns.Add(currentTown);
- }
- else
- {
- string[] studentTokens = input.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
- string studentName = studentTokens[0];
- string email = studentTokens[1].Trim();
- DateTime registerDate = DateTime.ParseExact(studentTokens[2].Trim(), "d-MMM-yyyy", CultureInfo.InvariantCulture);
- Student student = new Student(studentName, email, registerDate);
- towns[towns.Count - 1].Students.Add(student);
- }
- input = Console.ReadLine();
- }
- Console.WriteLine($"Created {towns.Sum(t => t.GroupsCount)} groups in {towns.Count} towns:");
- foreach (var town in towns.OrderBy(t => t.Name))
- {
- Console.WriteLine(town);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement