Created
December 16, 2025 04:58
-
-
Save isaacssemugenyi/ec58a3c3f80154174ae2c23ea43fb15f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /************************ | |
| * Clean Grading System | |
| * Refactored for clarity, | |
| * structure, and safety | |
| ************************/ | |
| /* ===== Constants (No Magic Numbers) ===== */ | |
| const SUBJECT_COUNT = 4; | |
| const ATTENDANCE_BONUS_THRESHOLD = 90; | |
| const ATTENDANCE_BONUS_POINTS = 5; | |
| const GRADE_THRESHOLDS = [ | |
| { min: 80, grade: "A" }, | |
| { min: 70, grade: "B" }, | |
| { min: 60, grade: "C" }, | |
| { min: 50, grade: "D" }, | |
| { min: 0, grade: "F" } | |
| ]; | |
| /* ===== State Encapsulation ===== */ | |
| class GradingSystem { | |
| constructor() { | |
| this.students = []; | |
| } | |
| processStudent(student) { | |
| if (!this.isValidStudent(student)) { | |
| return; | |
| } | |
| const average = this.calculateAverage(student); | |
| const grade = this.determineGrade(average); | |
| this.saveResult(student.name, average, grade); | |
| this.printResult(student.name, average, grade); | |
| } | |
| isValidStudent({ name, scores, attendance }) { | |
| return ( | |
| name && | |
| scores.every(score => score >= 0) && | |
| attendance >= 0 | |
| ); | |
| } | |
| calculateAverage({ scores, attendance, bonusPoints, extraCredit }) { | |
| let totalScore = this.sumScores(scores); | |
| totalScore += this.calculateAttendanceBonus(attendance); | |
| totalScore += this.calculateExtraPoints(bonusPoints); | |
| totalScore += this.calculateExtraPoints(extraCredit); | |
| return totalScore / SUBJECT_COUNT; | |
| } | |
| sumScores(scores) { | |
| return scores.reduce((sum, score) => sum + score, 0); | |
| } | |
| calculateAttendanceBonus(attendance) { | |
| return attendance > ATTENDANCE_BONUS_THRESHOLD | |
| ? ATTENDANCE_BONUS_POINTS | |
| : 0; | |
| } | |
| calculateExtraPoints(points) { | |
| return points > 0 ? points : 0; | |
| } | |
| determineGrade(average) { | |
| return GRADE_THRESHOLDS.find( | |
| threshold => average >= threshold.min | |
| ).grade; | |
| } | |
| saveResult(name, average, grade) { | |
| this.students.push({ name, average, grade }); | |
| } | |
| printResult(name, average, grade) { | |
| console.log("Student:", name); | |
| console.log("Average:", average); | |
| console.log("Grade:", grade); | |
| } | |
| printSummary() { | |
| console.log("Total students processed:", this.students.length); | |
| console.log("All students:", this.students); | |
| } | |
| } | |
| /* ===== Program Execution ===== */ | |
| const gradingSystem = new GradingSystem(); | |
| gradingSystem.processStudent({ | |
| name: "Alice", | |
| scores: [78, 85, 90, 88], | |
| attendance: 95, | |
| bonusPoints: 3, | |
| extraCredit: 2 | |
| }); | |
| gradingSystem.processStudent({ | |
| name: "Bob", | |
| scores: [55, 60, 58, 62], | |
| attendance: 80, | |
| bonusPoints: 0, | |
| extraCredit: 1 | |
| }); | |
| gradingSystem.processStudent({ | |
| name: "Charlie", | |
| scores: [40, 45, 50, 48], | |
| attendance: 70, | |
| bonusPoints: 0, | |
| extraCredit: 0 | |
| }); | |
| gradingSystem.printSummary(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Clean Grading System | |
| Refactored for readability, structure, and maintainability | |
| """ | |
| # ===== Constants (No Magic Numbers) ===== | |
| SUBJECT_COUNT = 4 | |
| ATTENDANCE_BONUS_THRESHOLD = 90 | |
| ATTENDANCE_BONUS_POINTS = 5 | |
| GRADE_THRESHOLDS = [ | |
| (80, "A"), | |
| (70, "B"), | |
| (60, "C"), | |
| (50, "D"), | |
| (0, "F") | |
| ] | |
| # ===== Grading System ===== | |
| class GradingSystem: | |
| def __init__(self): | |
| self.students = [] | |
| def process_student(self, student): | |
| if not self.is_valid_student(student): | |
| return | |
| average = self.calculate_average(student) | |
| grade = self.determine_grade(average) | |
| self.save_result(student["name"], average, grade) | |
| self.print_result(student["name"], average, grade) | |
| def is_valid_student(self, student): | |
| return ( | |
| student["name"] | |
| and all(score >= 0 for score in student["scores"]) | |
| and student["attendance"] >= 0 | |
| ) | |
| def calculate_average(self, student): | |
| total_score = sum(student["scores"]) | |
| total_score += self.calculate_attendance_bonus(student["attendance"]) | |
| total_score += self.calculate_extra_points(student["bonus_points"]) | |
| total_score += self.calculate_extra_points(student["extra_credit"]) | |
| return total_score / SUBJECT_COUNT | |
| def calculate_attendance_bonus(self, attendance): | |
| if attendance > ATTENDANCE_BONUS_THRESHOLD: | |
| return ATTENDANCE_BONUS_POINTS | |
| return 0 | |
| def calculate_extra_points(self, points): | |
| return points if points > 0 else 0 | |
| def determine_grade(self, average): | |
| for minimum, grade in GRADE_THRESHOLDS: | |
| if average >= minimum: | |
| return grade | |
| def save_result(self, name, average, grade): | |
| self.students.append({ | |
| "name": name, | |
| "average": average, | |
| "grade": grade | |
| }) | |
| def print_result(self, name, average, grade): | |
| print("Student:", name) | |
| print("Average:", average) | |
| print("Grade:", grade) | |
| def print_summary(self): | |
| print("Total students processed:", len(self.students)) | |
| print("All students:", self.students) | |
| # ===== Program Execution ===== | |
| grading_system = GradingSystem() | |
| grading_system.process_student({ | |
| "name": "Alice", | |
| "scores": [78, 85, 90, 88], | |
| "attendance": 95, | |
| "bonus_points": 3, | |
| "extra_credit": 2 | |
| }) | |
| grading_system.process_student({ | |
| "name": "Bob", | |
| "scores": [55, 60, 58, 62], | |
| "attendance": 80, | |
| "bonus_points": 0, | |
| "extra_credit": 1 | |
| }) | |
| grading_system.process_student({ | |
| "name": "Charlie", | |
| "scores": [40, 45, 50, 48], | |
| "attendance": 70, | |
| "bonus_points": 0, | |
| "extra_credit": 0 | |
| }) | |
| grading_system.print_summary() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment