NelIfandieva

MakeEntriesInDictionaryAndSortThemByName_UnsuccessfulAttempt

Feb 25th, 2018
134
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Learning
  6. {
  7.    
  8.     /*  Enter Teams and Season Points in a Sorted Dictionary, then print all entries sorted by names */
  9.  
  10.     class Dictionaries_Lab_
  11.     {
  12.         static void Main()
  13.         {
  14.             char[] sepSpace = { ' ' };
  15.             char[] sepComma = {','};
  16.             Console.Write("Team names: ");
  17.             var teamNamesAndPoints = Console.ReadLine().Trim() // this is a string
  18.                                             .Split(sepComma, StringSplitOptions.RemoveEmptyEntries).ToList(); // this is now a string array
  19.             var premierTeamsAndPoints = new SortedDictionary<string, int>();
  20.             var currentTeamName = "";
  21.             var currentTeamPoints = 0;
  22.             for (int i = 0; i < teamNamesAndPoints.Count(); i++)
  23.             {
  24.                 if(i == 0 || i % 2 == 0)
  25.                 {
  26.                     currentTeamName = teamNamesAndPoints[i];
  27.                     if(premierTeamsAndPoints.ContainsKey(currentTeamName) == false)
  28.                     {
  29.                         premierTeamsAndPoints[currentTeamName] = 0;
  30.                     }
  31.                 }
  32.                 else
  33.                 {
  34.                     currentTeamPoints = int.Parse(teamNamesAndPoints[i]);
  35.                     if (premierTeamsAndPoints[currentTeamName] == 0)
  36.                     {
  37.                         premierTeamsAndPoints[currentTeamName] = currentTeamPoints;
  38.                     }
  39.                 }
  40.             }
  41.             foreach(var team in premierTeamsAndPoints)
  42.             {
  43.                 var teamName = team.Key;
  44.                 var teamPoints = team.Value;
  45.                 Console.WriteLine($"{teamName}: {teamPoints}");
  46.             }
  47.         }
  48.     }
  49. }
RAW Paste Data