Advertisement
bullit3189

Santa's new List - TF-Exam-Dictionary-10Jan19

Jan 27th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _04Santa_sNewList
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Dictionary<string, int> kidsCount = new Dictionary<string, int>();
  12. Dictionary<string, int> presentsCount = new Dictionary<string, int>();
  13.  
  14. while (true)
  15. {
  16. string command = Console.ReadLine();
  17.  
  18. if (command == "END")
  19. {
  20. break;
  21. }
  22.  
  23. string[] tokens = command.Split("->");
  24.  
  25. string name = tokens[0];
  26.  
  27. if (name!="Remove")
  28. {
  29. string typeOfToy = tokens[1];
  30. int amount =int.Parse(tokens[2]);
  31.  
  32. if (!kidsCount.ContainsKey(name))
  33. {
  34. kidsCount[name] = amount;
  35. }
  36. else
  37. {
  38. kidsCount[name] += amount;
  39. }
  40.  
  41. if (!presentsCount.ContainsKey(typeOfToy))
  42. {
  43. presentsCount[typeOfToy] = amount;
  44. }
  45. else
  46. {
  47. presentsCount[typeOfToy] += amount;
  48. }
  49. }
  50. else
  51. {
  52. string childName = tokens[1];
  53.  
  54. kidsCount.Remove(childName);
  55. }
  56. }
  57.  
  58. Console.WriteLine("Children:");
  59.  
  60. foreach (var kvp in kidsCount.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  61. {
  62. string name = kvp.Key;
  63. int count = kvp.Value;
  64.  
  65. Console.WriteLine($"{name} -> {count}");
  66. }
  67.  
  68. Console.WriteLine("Presents:");
  69.  
  70. foreach (var kvp in presentsCount)
  71. {
  72. string present = kvp.Key;
  73. int amount = kvp.Value;
  74. Console.WriteLine($"{present} -> {amount}");
  75. }
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement