Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 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. Print(students,input[1]);
  29. break;
  30.  
  31.  
  32. }
  33. } while (!input.Equals("exit"));
  34.  
  35.  
  36.  
  37. }
  38.  
  39. private static void Print(Dictionary<string, Dictionary<string, List<int>>> students, string studentName="somebody")
  40. {
  41. if (studentName == null)
  42. {
  43. //Print all scores of all students
  44. foreach (KeyValuePair<string, Dictionary<string, List<int>>> currentStudent in students)
  45. {
  46. Console.WriteLine($"Student name: {currentStudent.Key}");
  47.  
  48. foreach (KeyValuePair<string, List<int>> subject in currentStudent.Value)
  49. {
  50. Console.WriteLine($"Subject: {subject.Key}");
  51. foreach (var score in subject.Value)
  52. {
  53. Console.WriteLine(score + " ");
  54. }
  55. }
  56. }
  57. }
  58. else
  59. {
  60. //print scores just for the mentioned student
  61. Console.WriteLine($"Student name: {studentName}");
  62. foreach(KeyValuePair<string,List<int>> subject in students[studentName]){
  63. Console.WriteLine($"Subject: {subject.Key}");
  64.  
  65. foreach(var score in subject.Value)
  66. {
  67. Console.WriteLine(score+" ");
  68. }
  69. }
  70. }
  71. }
  72.  
  73. public static void Add(Dictionary<string, Dictionary<string, List<int>>> students,string[] input)
  74. {
  75.  
  76. string studentName = input[1];
  77. string subject = input[2];
  78. int subjectScore = int.Parse(input[3]);
  79.  
  80. if (students.ContainsKey(studentName))
  81. {
  82. if (students[studentName].ContainsKey(subject))
  83. {
  84. students[studentName][subject].Add(subjectScore);
  85. }
  86. else
  87. {
  88. students[studentName].Add(subject, new List<int> { subjectScore });
  89. }
  90. }
  91. else
  92. {
  93. students[studentName] = new Dictionary<string, List<int>>();
  94. students[studentName].Add(subject, new List<int> { subjectScore });
  95. }
  96.  
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement