コード例 #1
0
        public void ProcessGradeOptions(string fileName, Grades grades)
        {
            char option = Console.ReadKey().KeyChar;

            Console.Clear();

            switch (option)
            {
            case '0':
                System.Environment.Exit(1);
                break;

            case '1':
                foreach (KeyValuePair <string, List <int> > entry in grades.GetGradeDictionary())
                {
                    string line = entry.Key + ": " + String.Join(", ", entry.Value);
                    Console.WriteLine(line);
                }
                break;

            case '2':
                Console.WriteLine("Enter name of the student");
                try
                {
                    string name = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine(name + ": " + String.Join(", ", grades.GetStudentGrades(name)));
                }
                catch
                {
                    Console.Clear();
                    Console.WriteLine("! Invalid student name !");
                }

                break;

            case '3':
                foreach (KeyValuePair <string, List <string> > entry in grades.GetBestWorstStudent())
                {
                    string line = entry.Key + ": " + String.Join(", ", entry.Value);
                    Console.WriteLine(line);
                }
                break;

            case '4':
                Console.WriteLine("Enter please the grade");

                if (float.TryParse(Console.ReadKey().KeyChar.ToString(), out float grade))
                {
                    List <string> students = grades.GetBetterStudents(grade);

                    Console.Clear();

                    if (students == null || students.Count == 0)
                    {
                        Console.WriteLine("no student with better result found...");
                    }

                    Console.WriteLine(String.Join(", ", students));
                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("Invalid grade has been provided!");
                }
                break;

            case '5':
                Console.WriteLine(grades.GetOverallAverageGrade());
                break;

            case '6':
                Console.WriteLine("Choose the test");
                int numberOfTests = grades.GetTestsQuantity();

                if (numberOfTests < 1)
                {
                    Console.WriteLine("! There are no test results available !");
                    break;
                }

                for (int i = 0; i < numberOfTests; i++)
                {
                    int testDisplayNumber = i + 1;
                    Console.WriteLine("[" + i + "] Test " + testDisplayNumber);
                }

                try
                {
                    int testNumber = int.Parse(Console.ReadKey().KeyChar.ToString());
                    Console.Clear();
                    Console.WriteLine(grades.GetTestAverageGrade(testNumber));
                }
                catch
                {
                    Console.Clear();
                    Console.WriteLine("! Invalid Test number !");
                }

                break;

            case '7':
                int newTestNumber = grades.GetTestsQuantity() + 1;

                IDictionary <string, List <int> > gradesCopy = new Dictionary <string, List <int> >(grades.GetGradeDictionary());

                foreach (KeyValuePair <string, List <int> > entry in gradesCopy)
                {
                    string line = "Enter Test " + newTestNumber;
                    line += " (new) result for student: " + entry.Key;
                    Console.Clear();
                    Console.WriteLine(line);

                    bool isCorrectInput = false;

                    while (!isCorrectInput)
                    {
                        if (int.TryParse(Console.ReadKey().KeyChar.ToString(), out int newGrade))
                        {
                            grades.AddStudentGrade(entry.Key, newGrade);
                            isCorrectInput = true;
                        }
                        else
                        {
                            Console.WriteLine("\n! Invalid grade has been provided!");
                            Console.WriteLine("\nPress [0] to go back to menu");
                            Console.WriteLine("Press any other key to try again");

                            option = Console.ReadKey().KeyChar;

                            switch (option)
                            {
                            case '0':
                                isCorrectInput = true;
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }

                FileReader.OverwriteStudentGrades(fileName, grades.GetGradeDictionary());

                break;

            default:
                break;
            }

            Console.ReadKey();
        }