Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Collections_Homework
- {
- class Program
- {
- static void Main(string[] args)
- {
- Dictionary<string, Dictionary<string,List<int>>> students = new Dictionary<string,Dictionary<string,List<int>>>();
- string[] input;
- Console.WriteLine(" Menu ");
- Console.WriteLine("add -- Add score to a student's diary");
- Console.WriteLine("print [name] -- Print scores for all students or for a specific student");
- Console.WriteLine("exit -- exits the program");
- do
- {
- input = Console.ReadLine().Split(" ");
- switch (input[0])
- {
- case "add":
- Add(students,input);
- break;
- case "print":
- Print(students,input[1]);
- break;
- }
- } while (!input.Equals("exit"));
- }
- private static void Print(Dictionary<string, Dictionary<string, List<int>>> students, string studentName="somebody")
- {
- if (studentName == null)
- {
- //Print all scores of all students
- foreach (KeyValuePair<string, Dictionary<string, List<int>>> currentStudent in students)
- {
- Console.WriteLine($"Student name: {currentStudent.Key}");
- foreach (KeyValuePair<string, List<int>> subject in currentStudent.Value)
- {
- Console.WriteLine($"Subject: {subject.Key}");
- foreach (var score in subject.Value)
- {
- Console.WriteLine(score + " ");
- }
- }
- }
- }
- else
- {
- //print scores just for the mentioned student
- Console.WriteLine($"Student name: {studentName}");
- foreach(KeyValuePair<string,List<int>> subject in students[studentName]){
- Console.WriteLine($"Subject: {subject.Key}");
- foreach(var score in subject.Value)
- {
- Console.WriteLine(score+" ");
- }
- }
- }
- }
- public static void Add(Dictionary<string, Dictionary<string, List<int>>> students,string[] input)
- {
- string studentName = input[1];
- string subject = input[2];
- int subjectScore = int.Parse(input[3]);
- if (students.ContainsKey(studentName))
- {
- if (students[studentName].ContainsKey(subject))
- {
- students[studentName][subject].Add(subjectScore);
- }
- else
- {
- students[studentName].Add(subject, new List<int> { subjectScore });
- }
- }
- else
- {
- students[studentName] = new Dictionary<string, List<int>>();
- students[studentName].Add(subject, new List<int> { subjectScore });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement