Advertisement
Guest User

Untitled

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