Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- namespace StudentGroups
- {
- public class Group
- {
- public Town Town { get; set; }
- public List<Student> Students { get; set; }
- }
- public class Student
- {
- public string Name { get; set; }
- public string Email { get; set; }
- public DateTime RegistrationDate { get; set; }
- }
- public class Town
- {
- public string Name { get; set; }
- public int SeatsCount { get; set; }
- public List<Student> Students { get; set; }
- }
- public class StudentGroups
- {
- public static void Main()
- {
- List<Town> listOfTowns = ReadTownsAndStudents();
- List<Group> finalGroups = DistributeStudentsInGroups(listOfTowns);
- Console.WriteLine("Created {0} groups in {1} towns:", finalGroups.Count, listOfTowns.Count);
- foreach (var group in finalGroups)
- {
- Console.Write("{0} => ", group.Town.Name);
- foreach (var student in group.Town.Students)
- {
- Console.Write(" {0}", student.Email);
- }
- Console.WriteLine();
- }
- }
- public static List<Town> ReadTownsAndStudents()
- {
- List<Town> towns = new List<Town>();
- string inputLine = Console.ReadLine();
- while (inputLine != "End")
- {
- if (inputLine.Contains("=>"))
- {
- char[] separator = new char[] { '=', '>' };
- string[] parsedTownInput = inputLine.Split(separator, StringSplitOptions.RemoveEmptyEntries);
- string[] seats = parsedTownInput[1].Trim().Split();
- Town town = new Town();
- string townName = parsedTownInput[0].Trim();
- int SeatsCount = int.Parse(seats[0].Trim());
- town.Name = townName;
- town.SeatsCount = SeatsCount;
- town.Students = new List<Student>();
- towns.Add(town);
- }
- else
- {
- Student student = new Student();
- string[] parsedStudentInput = inputLine.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
- string studentName = parsedStudentInput[0].Trim();
- string studentEmail = parsedStudentInput[1].Trim();
- DateTime studentRegistrationDate = DateTime.ParseExact(parsedStudentInput[2].Trim(), "d-MMM-yyyy",
- CultureInfo.InvariantCulture);
- student.Name = studentName;
- student.Email = studentEmail;
- student.RegistrationDate = studentRegistrationDate;
- towns[towns.Count-1].Students.Add(student);
- }
- inputLine = Console.ReadLine();
- }
- return towns;
- }
- static List<Group> DistributeStudentsInGroups(List<Town> towns)
- {
- var groups = new List<Group>();
- foreach (var town in towns)
- {
- var students = town.Students.OrderBy(x => x.RegistrationDate).ThenBy(x => x.Name).ThenBy(x => x.Email).ToList();
- while (students.Any())
- {
- Group group = new Group();
- group.Town = town;
- //Console.WriteLine(group.Town.SeatsCount);
- group.Students = students.Take(group.Town.SeatsCount).ToList();
- students = students.Skip(group.Town.SeatsCount).ToList();
- groups.Add(group);
- }
- }
- return groups;
- }
- }
- }
Add Comment
Please, Sign In to add comment