Example #1
0
        public static void Main(string[] args)
        {
            var john = new Student("John", "Capioca", "johnc");
            var mario = new Student("Mario", "Esposito", "marioe");
            var giovanna = new Student("Giovanna", "Marinozzi", "giovannam");
            var students = new List<Student>() { john, mario, giovanna };

            var results = new Dictionary<string, HomeWork>() {
                { "johnc", new HomeWork() { MathResult = 5, AnswerOnBiology = "yes", AnswerOnHistory = "no", AnswerOnMusic = "no"} },
                { "marioe", new HomeWork() { MathResult = 10, AnswerOnBiology = "yes", AnswerOnHistory = "no", AnswerOnMusic = "no"} },
                { "giovannam", new HomeWork() { MathResult = 10, AnswerOnBiology = "no", AnswerOnHistory = "no", AnswerOnMusic = "no"} },
            };

            var solution = new Solution() { MathResult = 10, AnswerOnBiology = "no", AnswerOnHistory = "no", AnswerOnMusic = "no"};

            Console.WriteLine("The solution should be" + solution.PrintReport());

            var gradeEvaluator = new MarksEvaluator();
            var grades = gradeEvaluator.Eval(students, results, solution);
            foreach(var grade in grades)
            {
                Console.WriteLine("{0} got {1}", grade.Key.FirstName, grade.Value);
                Console.WriteLine("  Homework report: {0}", results[grade.Key.Id]);

                if (grade.Value == 3)
                    Console.WriteLine(string.Format("  -> {0} will be rejected from school", grade.Key.FirstName));
            }
        }
        public Dictionary<Student, int> Eval(List<Student> students, Dictionary<string, HomeWork> results, Solution solution)
        {
            try
            {
                var marks = new Dictionary<Student, int>();
                foreach(var pair in results)
                {
                    var studentId = pair.Key;

                    var student = GetStudentFromId(studentId, students);
                    if(student == null)
                    {
                        Log.Error(string.Format("Cannot find student with id {0}", studentId));
                        break;
                    }

                    var homework = pair.Value;
                    if(pair.Value == null)
                        break;

                    bool perfectSolution = false;
                    try
                    {
                        if (solution == homework && homework.CountErrors(solution) == 0)
                        {
                            perfectSolution = true;
                            TweetResult(student.FirstName);
                        }
                    }
                    catch(Exception e)
                    {
                        Log.Error(e, "Cannot tweet result");
                    }

                    if(perfectSolution)
                    {
                        var highestMark = solution.HighestMark;
                        if(highestMark == 0)
                        {
                            Log.Error(string.Format("Skipping student {0}, no HighestMark found.", student.FirstName));
                            break;
                        }
                        marks.Add(student, highestMark);
                    }

                    try
                    {
                        if (homework.CountErrors(solution) == 1)
                        {
                            var mediumMark = solution.MediumMark;
                            if(mediumMark == 0)
                            {
                                Log.Error(string.Format("Skipping student {0}, no MediumMark found", student.FirstName));
                                break;
                            }
                            marks.Add(student, mediumMark);

                        }
                        else
                        {
                            var lowestMark = solution.LowestMark;
                            if(lowestMark == 0)
                            {
                                Log.Error(string.Format("Skipping student {0}, no LowestMark found", student.FirstName));
                                break;
                            }
                            marks.Add(student, lowestMark);
                            TweetResult(student.FirstName);
                        }
                    }
                    catch(Exception e)
                    {
                        Log.Error(e, "Can't add marks to student " + student.FirstName);
                    }
                }
                return marks;
            }
            catch(Exception e)
            {
                Log.Error(e);
                return null;
            }
        }