From f85ff5ee93cb475512e7b20919ab7e9e0625834e Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Fri, 12 May 2023 21:06:42 +0100 Subject: [PATCH] [POO] aula11: implemented SimpleGradeCalculator --- 1ano/2semestre/poo/src/aula11/ex2/Gradebook.java | 14 ++++---------- .../poo/src/aula11/ex2/SimpleGradeCalculator.java | 13 +++++++++++++ 2 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula11/ex2/SimpleGradeCalculator.java diff --git a/1ano/2semestre/poo/src/aula11/ex2/Gradebook.java b/1ano/2semestre/poo/src/aula11/ex2/Gradebook.java index 00867ed..5c2b05a 100644 --- a/1ano/2semestre/poo/src/aula11/ex2/Gradebook.java +++ b/1ano/2semestre/poo/src/aula11/ex2/Gradebook.java @@ -1,15 +1,14 @@ package aula11.ex2; -import utils.MathTools; - import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; -public class Gradebook implements IGradeCalculator { +public class Gradebook { private final LinkedList students = new LinkedList<>(); + private final IGradeCalculator gradeCalculator = new SimpleGradeCalculator(); public void load(String filename) { LinkedList lines = new LinkedList<>(); @@ -42,16 +41,11 @@ public class Gradebook implements IGradeCalculator { } public double calculateAverageGrade(String name) { - return calculate(getStudent(name).getGrades()); - } - - @Override - public double calculate(LinkedList grades) { - return MathTools.media(grades); + return gradeCalculator.calculate(getStudent(name).getGrades()); } public void printAllStudents() { for (Student student : students) - System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), calculate(student.getGrades())); + System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), gradeCalculator.calculate(student.getGrades())); } } diff --git a/1ano/2semestre/poo/src/aula11/ex2/SimpleGradeCalculator.java b/1ano/2semestre/poo/src/aula11/ex2/SimpleGradeCalculator.java new file mode 100644 index 0000000..7192ed8 --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex2/SimpleGradeCalculator.java @@ -0,0 +1,13 @@ +package aula11.ex2; + +import java.util.LinkedList; + +public class SimpleGradeCalculator implements IGradeCalculator { + @Override + public double calculate(LinkedList grades) { + double sum = 0; + for (double n : grades) + sum += n; + return sum / grades.size(); + } +}