Пример #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Select task of Exercise:");
            Console.WriteLine("1. Train:");
            Console.WriteLine("2. ChangeList:");
            Console.WriteLine("3. HouseParty:");
            Console.WriteLine("4. ListOperation:");
            Console.WriteLine("5. BombNumbers:");
            Console.WriteLine("6. CardGame:");
            Console.WriteLine("7. AppendArrays:");
            Console.WriteLine("8. AnonymousThreat:");
            Console.WriteLine("9. PokemonDontGo:");
            Console.WriteLine("10. SoftUniCourse:");

            Train           train           = new Train();
            ListOperation   listOperation   = new ListOperation();
            HouseParty      houseParty      = new HouseParty();
            ChangeList      changeList      = new ChangeList();
            CardGame        cardGame        = new CardGame();
            BombNumbers     bombNumbers     = new BombNumbers();
            AppendArrays    appendArrays    = new AppendArrays();
            AnonymousThreat anonymousThreat = new AnonymousThreat();
            PokemonDontGo   pokemonDontGo   = new PokemonDontGo();
            SoftUniCourse   softUniCourse   = new SoftUniCourse();

            int switch_on = int.Parse(Console.ReadLine());

            switch (switch_on)
            {
            case 1:
            {
                train.TaskTrain();
                break;
            }

            case 2:
            {
                listOperation.TaskListOperation();
                break;
            }

            case 3:
            {
                houseParty.TaskHouseParty();
                break;
            }

            case 4:
            {
                cardGame.TaskCardGame();
                break;
            }

            case 5:
            {
                changeList.TaskChangeList();
                break;
            }

            case 6:
            {
                appendArrays.TaskAppendArrays();
                break;
            }

            case 7:
            {
                bombNumbers.TaskBombNumbers();
                break;
            }

            case 8:
            {
                anonymousThreat.TaskAnonymousThreat();
                break;
            }

            case 9:
            {
                pokemonDontGo.TaskPokemonDontGo();
                break;
            }

            case 10:
            {
                softUniCourse.TaskSoftUniCourse();
                break;
            }

            default:
                Console.WriteLine("Exit:");
                break;
            }
        }
Пример #2
0
        /// <summary>
        /// Reads the given data of the courses and students and scores and initializes it in a dictionary
        /// </summary>
        private void ReadData(string fileName)
        {
            //Keep track on the line number of the file to show which line is broken in the stacktrace
            var lineNumber = 0;

            try
            {
                var path = fileName;

                //If the path doesn't contain even one path separator it means it is relative path and full path needs to be pointed out
                if (!path.Contains(SessionData.PathSeparator))
                {
                    path = SessionData.CurrentPath + SessionData.PathSeparator + fileName;
                }

                if (File.Exists(path))
                {
                    var lines = new Queue <string>(File.ReadAllLines(path));

                    while (lines.Count > 0)
                    {
                        lineNumber++;
                        var matches = matcher.Match(lines.Dequeue());
                        if (matches.Value.Length != 0)
                        {
                            var courseName  = matches.Groups["courseName"].Value;
                            var studentName = matches.Groups["userName"].Value;
                            var scores      = matches.Groups["scores"].Value.Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

                            if (scores.Any(s => s > SoftUniStudent.MaxScoreOnExam || s < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScoreException);
                            }

                            if (scores.Length > SoftUniCourse.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScoresException);
                                continue;
                            }

                            if (!HasStudent(studentName))
                            {
                                students[studentName] = new SoftUniStudent(studentName);
                            }

                            if (!HasCourse(courseName))
                            {
                                courses[courseName] = new SoftUniCourse(courseName);
                            }

                            var student = students[studentName];
                            var course  = courses[courseName];

                            //The method will also enroll the student in the course class :)
                            student.EnrollInCourse(course);

                            student.SetMarkOnCourse(courseName, scores);
                        }
                    }

                    isDataInitialized = true;
                    OutputWriter.WriteMessageOnNewLine("Data read!");
                }
                else
                {
                    throw new InvalidPathException();
                }
            }
            catch (FormatException fex)
            {
                OutputWriter.DisplayException(fex, lineNumber);
            }
        }
        private void ReadData(string fileName)
        {
            string path = SessionData.currentPath + "\\" + fileName;

            if (File.Exists(path))
            {
                OutputWriter.WriteMessageOnNewLine("Reading data...");

                string   pattern       = @"([A-Z][a-zA-Z#\++]*_[A-Z][a-z]{2}_\d{4})\s+([A-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)";
                Regex    rgx           = new Regex(pattern);
                string[] allInputLines = File.ReadAllLines(path);

                for (int line = 0; line < allInputLines.Length; line++)
                {
                    if (!string.IsNullOrEmpty(allInputLines[line]) && rgx.IsMatch(allInputLines[line]))
                    {
                        Match  currentMatch = rgx.Match(allInputLines[line]);
                        string courseName   = currentMatch.Groups[1].Value;
                        string username     = currentMatch.Groups[2].Value;
                        string scoresStr    = currentMatch.Groups[3].Value;
                        try
                        {
                            int[] scores = scoresStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                           .Select(int.Parse)
                                           .ToArray();

                            if (scores.Any(x => x > 100 || x < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                                continue;
                            }

                            if (scores.Length > SoftUniCourse.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                                continue;
                            }

                            if (!this.Students.ContainsKey(username))
                            {
                                this.students.Add(username, new SoftUniStudent(username));
                            }

                            if (!this.Courses.ContainsKey(courseName))
                            {
                                this.courses.Add(courseName, new SoftUniCourse(courseName));
                            }

                            SoftUniCourse  course  = this.Courses[courseName];
                            SoftUniStudent student = this.Students[username];

                            student.EnrollInCourse(course);
                            student.SetMarkOnCourse(courseName, scores);

                            course.EnrollStudent(student);
                        }
                        catch (Exception ex)
                        {
                            OutputWriter.DisplayException(ex.Message + $"at line : {line}");
                        }
                    }
                }

                this.isDataInitialized = true;
                OutputWriter.WriteMessageOnNewLine("Data read!");
            }
            else
            {
                throw new InvalidPathException();
            }
        }