Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.Write("Enter number of courses: ");
            int coursesCount = int.TryParse(Console.ReadLine(), out coursesCount) ? coursesCount : 0;

            CreateCoursesList(coursesCount);

            Console.Write("\nEnter number of students: ");
            int studentsCount = int.TryParse(Console.ReadLine(), out studentsCount) ? studentsCount : 0;

            CreateStudentsList(studentsCount);

            Console.WriteLine(
                "\nPlease, add tasks to students. Format: <studentId> <courseId> <taskName>//<score> \nTo finish write \"quit\"");

            Regex pattern = new Regex(@"^\d+\s\d+\s(\w|\-)+\/\/(\d{1,2}|\d{1,2}[.,]\d{1,2}|100)$");

            while (true)
            {
                string input = Console.ReadLine();

                if (input.ToLower().Equals("quit"))
                {
                    break;
                }

                if (pattern.IsMatch(input))
                {
                    string   splitPattern = @"(\s)|(\//)";
                    string[] line         = Regex.Split(input, splitPattern);

                    int studentId = int.TryParse(line[0], out studentId) ? studentId : -1;
                    int courseId  = int.TryParse(line[2], out courseId) ? courseId : -1;

                    string taskName = line[4];

                    double taskGrade;
                    if (Double.TryParse(line[6], out taskGrade))
                    {
                        try
                        {
                            Academy.AddTaskToStudent(courseId, studentId, taskName, (float)taskGrade);
                        }
                        catch (StudentNotFoundException se)
                        {
                            Console.WriteLine("Student not found: " + se.Message);
                        }
                        catch (CourseNotFoundException ce)
                        {
                            Console.WriteLine("Course not found: " + ce.Message);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Wrong score input! Try again.");
                    }
                }
                else
                {
                    Console.WriteLine("Invalid format! Please, try again.");
                }
            }

            PrintAcademyInfo();
        }