Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Collections_Homework
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10.  
  11. Dictionary<string, Dictionary<string,List<int>>> students = new Dictionary<string,Dictionary<string,List<int>>>();
  12. string[] input;
  13. Console.WriteLine(" Menu ");
  14. Console.WriteLine("add -- Add score to a student's diary");
  15. Console.WriteLine("print [name] -- Print scores for all students or for a specific student");
  16. Console.WriteLine("exit -- exits the program");
  17. do
  18. {
  19.  
  20. input = Console.ReadLine().Split(" ");
  21.  
  22. switch (input[0])
  23. {
  24. case "add":
  25. Add(students,input);
  26. break;
  27. case "print":
  28. if (input.Length == 1)
  29. {
  30. Print(students);
  31. }
  32. else
  33. {
  34. Print(students, input[1]);
  35. }
  36.  
  37. break;
  38.  
  39.  
  40. }
  41. } while (!input.Equals("exit"));
  42.  
  43.  
  44.  
  45. }
  46.  
  47. private static void Print(Dictionary<string, Dictionary<string, List<int>>> students)
  48. {
  49.  
  50.  
  51. //Print all scores of all students
  52. foreach (KeyValuePair<string, Dictionary<string, List<int>>> currentStudent in students)
  53. {
  54. Console.WriteLine($"Student name: {currentStudent.Key}");
  55.  
  56. foreach (KeyValuePair<string, List<int>> subject in currentStudent.Value)
  57. {
  58. Console.WriteLine($"Subject: {subject.Key}");
  59. foreach (var score in subject.Value)
  60. {
  61. Console.WriteLine(score + " ");
  62. }
  63. }
  64. }
  65.  
  66.  
  67. }
  68.  
  69. private static void Print(Dictionary<string, Dictionary<string, List<int>>> students, string studentName)
  70. {
  71.  
  72. //print scores just for the mentioned student
  73. Console.WriteLine($"Student name: {studentName}");
  74. foreach (KeyValuePair<string, List<int>> subject in students[studentName])
  75. {
  76. Console.WriteLine($"Subject: {subject.Key}");
  77.  
  78. foreach (var score in subject.Value)
  79. {
  80. Console.WriteLine(score + " ");
  81. }
  82. }
  83.  
  84. }
  85.  
  86. public static void Add(Dictionary<string, Dictionary<string, List<int>>> students,string[] input)
  87. {
  88.  
  89. string studentName = input[1];
  90. string subject = input[2];
  91. int subjectScore = int.Parse(input[3]);
  92.  
  93. if (students.ContainsKey(studentName))
  94. {
  95. if (students[studentName].ContainsKey(subject))
  96. {
  97. students[studentName][subject].Add(subjectScore);
  98. }
  99. else
  100. {
  101. students[studentName].Add(subject, new List<int> { subjectScore });
  102. }
  103. }
  104. else
  105. {
  106. students[studentName] = new Dictionary<string, List<int>>();
  107. students[studentName].Add(subject, new List<int> { subjectScore });
  108. }
  109.  
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement