plamen27

Student Groups with errors

Sep 21st, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. namespace StudentGroups
  7. {
  8. public class Group
  9. {
  10. public Town Town { get; set; }
  11. public List<Student> Students { get; set; }
  12. }
  13. public class Student
  14. {
  15. public string Name { get; set; }
  16. public string Email { get; set; }
  17. public DateTime RegistrationDate { get; set; }
  18. }
  19. public class Town
  20. {
  21. public string Name { get; set; }
  22. public int SeatsCount { get; set; }
  23. public List<Student> Students { get; set; }
  24. }
  25.  
  26.  
  27. public class StudentGroups
  28. {
  29. public static void Main()
  30. {
  31. List<Town> listOfTowns = ReadTownsAndStudents();
  32. List<Group> finalGroups = DistributeStudentsInGroups(listOfTowns);
  33.  
  34. Console.WriteLine("Created {0} groups in {1} towns:", finalGroups.Count, listOfTowns.Count);
  35. foreach (var group in finalGroups)
  36. {
  37. Console.Write("{0} => ", group.Town.Name);
  38. foreach (var student in group.Town.Students)
  39. {
  40. Console.Write(" {0}", student.Email);
  41. }
  42. Console.WriteLine();
  43.  
  44. }
  45. }
  46.  
  47. public static List<Town> ReadTownsAndStudents()
  48. {
  49. List<Town> towns = new List<Town>();
  50.  
  51. string inputLine = Console.ReadLine();
  52. while (inputLine != "End")
  53. {
  54. if (inputLine.Contains("=>"))
  55. {
  56. char[] separator = new char[] { '=', '>' };
  57. string[] parsedTownInput = inputLine.Split(separator, StringSplitOptions.RemoveEmptyEntries);
  58. string[] seats = parsedTownInput[1].Trim().Split();
  59.  
  60. Town town = new Town();
  61. string townName = parsedTownInput[0].Trim();
  62. int SeatsCount = int.Parse(seats[0].Trim());
  63.  
  64. town.Name = townName;
  65. town.SeatsCount = SeatsCount;
  66. town.Students = new List<Student>();
  67. towns.Add(town);
  68. }
  69. else
  70. {
  71. Student student = new Student();
  72. string[] parsedStudentInput = inputLine.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  73.  
  74. string studentName = parsedStudentInput[0].Trim();
  75. string studentEmail = parsedStudentInput[1].Trim();
  76. DateTime studentRegistrationDate = DateTime.ParseExact(parsedStudentInput[2].Trim(), "d-MMM-yyyy",
  77. CultureInfo.InvariantCulture);
  78.  
  79. student.Name = studentName;
  80. student.Email = studentEmail;
  81. student.RegistrationDate = studentRegistrationDate;
  82. towns[towns.Count-1].Students.Add(student);
  83. }
  84. inputLine = Console.ReadLine();
  85. }
  86. return towns;
  87. }
  88.  
  89. static List<Group> DistributeStudentsInGroups(List<Town> towns)
  90. {
  91.  
  92. var groups = new List<Group>();
  93. foreach (var town in towns)
  94. {
  95.  
  96. var students = town.Students.OrderBy(x => x.RegistrationDate).ThenBy(x => x.Name).ThenBy(x => x.Email).ToList();
  97.  
  98. while (students.Any())
  99. {
  100. Group group = new Group();
  101. group.Town = town;
  102. //Console.WriteLine(group.Town.SeatsCount);
  103. group.Students = students.Take(group.Town.SeatsCount).ToList();
  104. students = students.Skip(group.Town.SeatsCount).ToList();
  105. groups.Add(group);
  106. }
  107. }
  108. return groups;
  109. }
  110. }
  111. }
Add Comment
Please, Sign In to add comment