/// <summary> /// Calculate average from Student Courses and Diploma Requirements /// </summary> /// <param name="diploma"></param> /// <param name="student"></param> /// <param name="average"></param> private static void CalculateAverage(Diploma diploma, Student student, ref int average) { foreach (int req in diploma.Requirements) { foreach (Course course in student.Courses) { Requirement requirement = Repository.GetRequirement(req); foreach (int c in requirement.Courses) { if (c == course.Id) { average += course.Mark; } } } } average = average / student.Courses.Length; }
public Tuple <bool, STANDING> HasGraduated(Diploma diploma, Student student) { try { bool allCreditsPassed = false; var average = 0; var requirement = new Requirement(); var requirements = _repository.GetRequirements(); requirements = _checks.GetValidRequirements(requirements, diploma.Requirements); // Another logic which I supposed we need it but we can ignore it based // business logic. I supposed we should have all requirements otherwise // calculation returns wrong answer. if (requirements?.Count == 0 || requirements.Count < diploma.Requirements.Count) { return(new Tuple <bool, STANDING>(false, STANDING.NotEnoughRequirements)); } else { allCreditsPassed = _checks.CheckAllCreditsPassed(requirements, student.Courses); if (allCreditsPassed == false) { return(new Tuple <bool, STANDING>(false, STANDING.None)); } else { average = _calculation.GetAverageScore(student.Courses); return(_checks.GetResult(average)); } } } catch (Exception ex) { /* * Logging Exception via 3rd parties like Logentries or * traditional ways like file or database */ throw; } }
public Tuple <bool, STANDING> HasGraduated(Diploma diploma, Student student) { // variables int average = 0; bool isFailedAnyCourse = false; // validations if (diploma == null || diploma.Requirements == null) { throw new Exception("Invalid diploma"); } if (student == null || student.Courses == null) { throw new Exception("Invalid student"); } // check if one is qualified for graduation foreach (int req in diploma.Requirements) { Requirement requirement = repository.GetRequirementById(req); // validation if (requirement == null) { throw new Exception("Incorrect requirement id " + req); } // get averages and check if the one failed any courses bool isCourseFound = false; foreach (int reqCourse in requirement.Courses) { foreach (Course course in student.Courses) { if (reqCourse == course.Id) { isCourseFound = true; average += course.Mark; if (course.Mark < requirement.MinimumMark) { isFailedAnyCourse = true; } break; } } if (isCourseFound) { break; } } if (!isCourseFound) { throw new Exception(String.Format("Course not found for a student id {0}", student.Id)); } } // calculate course average average /= student.Courses.Length; // get standing STANDING standing = GetStanding(average); // if failed any course, return false if (isFailedAnyCourse) { return(new Tuple <bool, STANDING>(false, standing)); } // return result based on the standing value switch (standing) { case STANDING.Remedial: return(new Tuple <bool, STANDING>(false, standing)); case STANDING.Average: return(new Tuple <bool, STANDING>(true, standing)); case STANDING.SumaCumLaude: return(new Tuple <bool, STANDING>(true, standing)); case STANDING.MagnaCumLaude: return(new Tuple <bool, STANDING>(true, standing)); default: return(new Tuple <bool, STANDING>(false, standing)); } }
public Tuple <bool, STANDING> HasGraduated(Diploma diploma, Student student) { var credits = 0; var average = 0; var totalMarks = 0; bool hasGraduated = false; //for(int i = 0; i < diploma.Requirements.Length; i++) //{ // for(int j = 0; j < student.Courses.Length; j++) // { // var requirement = Repository.GetRequirement(diploma.Requirements[i]); // for (int k = 0; k < requirement.Courses.Length; k++) // { // if (requirement.Courses[k] == student.Courses[j].Id) // { // average += student.Courses[j].Mark; // if (student.Courses[j].Mark > requirement.MinimumMark) // { // credits += requirement.Credits; // } // } // } // } //} foreach (var requirementId in diploma.Requirements) { Requirement requirement = Repository.GetRequirement(requirementId); foreach (var course in student.Courses) { if (requirement.Courses.Any(c => c.Equals(course.Id))) { totalMarks += course.Mark; if (course.Mark >= requirement.MinimumMark) { credits += requirement.Credits; } } } } //Assumption: if a student does not get required credits for a diploma, his standing will be none var standing = STANDING.None; if (credits >= diploma.Credits) { hasGraduated = true; average = totalMarks / student.Courses.Length; if (average < 50) { standing = STANDING.Remedial; } else if (average < 80) { standing = STANDING.Average; } else if (average < 95) { standing = STANDING.MagnaCumLaude; } else { standing = STANDING.SumaCumLaude; //highest honors } } //student.Standing = standing; switch (standing) { case STANDING.Remedial: return(new Tuple <bool, STANDING>(hasGraduated, standing)); case STANDING.Average: return(new Tuple <bool, STANDING>(hasGraduated, standing)); case STANDING.SumaCumLaude: return(new Tuple <bool, STANDING>(hasGraduated, standing)); case STANDING.MagnaCumLaude: return(new Tuple <bool, STANDING>(hasGraduated, standing)); default: return(new Tuple <bool, STANDING>(hasGraduated, standing)); } }