Advertisement
KeepCoding

C#, ObjectsAndClassesExercise, 10-StudentGroups

Feb 22nd, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7.  
  8. namespace P10StudentGroups
  9. {
  10.     class StudentInfo
  11.     {
  12.         public string Name { get; set; }
  13.         public string Email { get; set; }
  14.         public DateTime RegistrationDate { get; set; }
  15.  
  16.         public StudentInfo(string name, string email, DateTime registrationDate)
  17.         {
  18.             Name = name;
  19.             Email = email;
  20.             RegistrationDate = registrationDate;
  21.         }
  22.     }
  23.     class Group
  24.     {
  25.         public List<StudentInfo> Students { get; set; } = new List<StudentInfo>();
  26.         static public int AllGroupsCount { get; set; }
  27.     }
  28.  
  29.     class City
  30.     {
  31.         public string CityName { get; set; }
  32.         public int GroupSize { get; set; }
  33.         public List<Group> Groups { get; set; } = new List<Group>();
  34.         // ne e static, zashtoto iskam vsichki studenti za edin grad da si gi imam, za da moga posle da gi razpredelq po grupi SLED kato gi sortiram kakto trqbva
  35.         public List<StudentInfo> AllStudents { get; set; } = new List<StudentInfo>();
  36.  
  37.         public City(string cityName, int groupSize)
  38.         {
  39.             CityName = cityName;
  40.             GroupSize = groupSize;
  41.         }
  42.  
  43.         static public int CitiesCount { get; set; } = 0; // ne samo shte go polzvam nakraq za da iznesa broikata na gradovete, no i za indexirane na
  44.     } // listut ot gradove. No prosto moje i s List<City> cities.Count...
  45.  
  46.     class Program
  47.     {
  48.         static void Main()
  49.         {
  50.             Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
  51.             string dateFormat = "d-MMM-yyyy";
  52.            
  53.             Regex regexCityName = new Regex(@"[A-Z][a-z]+(\s[A-Z][a-z]+)?"); // sumnqva me da ima grad s tri imena...
  54.             Match matchCityName;
  55.             Regex regexGroupSize = new Regex(@"\d+"); // tursi poredica ot chisla
  56.             Match matchGroupSize;
  57.  
  58.             List<City> cities = new List<City>();
  59.  
  60.             while (true) // vuvejdane na danni
  61.             {
  62.                 string input = Console.ReadLine();
  63.                 if (input == "End")
  64.                 {
  65.                     break;
  66.                 }
  67.  
  68.                 if (input.Contains("=>")) // oznachava che e vuveden grad i mesta
  69.                 {//ne pokrivam sluchaqt, v koito moje edin grad da bude vuveden vtori put. Ako bude vuveden ima 2 varianta: infoto da se ignorira ili da se obnovi
  70.                     matchCityName = regexCityName.Match(input);
  71.                     matchGroupSize = regexGroupSize.Match(input);
  72.                     if (CityNameIsUnique(matchCityName.ToString(), cities))
  73.                     {
  74.                         City.CitiesCount++;
  75.                     }
  76.                     else
  77.                     {
  78.                         continue;
  79.                     }
  80.                     City currentCity = new City(matchCityName.ToString(), int.Parse(matchGroupSize.ToString()));
  81.                     cities.Add(currentCity);
  82.                 }
  83.                 else
  84.                 { // ako sme v segashniq grad
  85.                     string[] studentInfo = input.Split('|');
  86.  
  87.                     string name = studentInfo[0].Trim();
  88.                     string email = studentInfo[1].Trim();
  89.                     string date = studentInfo[2].Trim();
  90.                     DateTime registrationDate = DateTime.ParseExact(date, dateFormat, CultureInfo.InvariantCulture);
  91.                     StudentInfo currentStudent = new StudentInfo(name, email, registrationDate);
  92.                     cities[cities.Count - 1].AllStudents.Add(currentStudent); // cities[cities.Count - 1] vinagi shte vrushta posledniq vuveden grad kakto si e redut
  93.                 }
  94.             } // works
  95.  
  96.             //razpredelqne na studenti po grupi kato bivat podrejdani po data na zapisvane (ascending), ime (ascending), email(ascending)
  97.             foreach (City city in cities.OrderBy(n => n.CityName))
  98.             {
  99.                 int groupCounter = -1; // nomer na grupata v segashniq grad
  100.                 int studentCounter = 0;
  101.                 foreach (StudentInfo student in city.AllStudents.OrderBy(r => r.RegistrationDate).ThenBy(n => n.Name).ThenBy(e => e.Email)) //podredeni sa i trqbva da se razpredelqt v List<Group> groups
  102.                 {
  103.                     if (studentCounter % city.GroupSize == 0) //
  104.                     {
  105.                         Group currentGroup = new Group();
  106.                         currentGroup.Students.Add(student);
  107.                         groupCounter++;
  108.                         city.Groups.Add(currentGroup);
  109.                         Group.AllGroupsCount++;
  110.                     }
  111.                     else
  112.                     {
  113.                         city.Groups[groupCounter].Students.Add(student);
  114.                     }
  115.                     studentCounter++;
  116.                 }
  117.             }
  118.  
  119.             Console.WriteLine($"Created {Group.AllGroupsCount} groups in {cities.Count} towns:");
  120.             foreach (City city in cities.OrderBy(n => n.CityName))
  121.             {
  122.                 foreach (var group in city.Groups)
  123.                 {
  124.                     Console.Write($"{city.CityName} => ");
  125.                     for (int i = 0; i < group.Students.Count; i++)
  126.                     {
  127.                         if (i != group.Students.Count - 1)
  128.                         {
  129.                             Console.Write(group.Students[i].Email + ", ");
  130.                         }
  131.                         else
  132.                         {
  133.                             Console.WriteLine(group.Students[i].Email);
  134.                         }
  135.                     }
  136.                 }
  137.             }
  138.  
  139.             //main ends here
  140.         }
  141.  
  142.         static bool CityNameIsUnique(string cityName, List<City> cities)
  143.         {
  144.             for (int i = 0; i < cities.Count; i++)
  145.             {
  146.                 if (cityName == cities[i].CityName)
  147.                 {
  148.                     return false;
  149.                 }
  150.             }
  151.  
  152.             return true;
  153.         }
  154.  
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement