diff --git a/1ano/2semestre/poo/src/aula11/ex2/Gradebook.java b/1ano/2semestre/poo/src/aula11/ex2/Gradebook.java new file mode 100644 index 0000000..00867ed --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex2/Gradebook.java @@ -0,0 +1,57 @@ +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 { + private final LinkedList students = new LinkedList<>(); + + public void load(String filename) { + LinkedList lines = new LinkedList<>(); + Path path = Paths.get(filename); + try { + lines = new LinkedList<>(Files.readAllLines(path)); + } catch (IOException e) { + System.out.printf("Certifique-se que o ficheiro \"%s\" está na raiz da pasta do projeto", path); + System.exit(1); + } + + for (String line : lines) { + String[] data = line.split("\\|"); + String name = data[0]; + LinkedList grades = new LinkedList<>(Arrays.stream(data).toList().subList(1, data.length).stream().map(Double::parseDouble).toList()); + students.add(new Student(name, grades)); + } + } + + public void addStudent(Student newStudent) { + students.add(newStudent); + } + + public void removeStudent(String name) { + students.removeIf(student -> student.getName().equals(name)); + } + + public Student getStudent(String name) { + return students.stream().filter(student -> student.getName().equals(name)).findFirst().orElse(null); + } + + public double calculateAverageGrade(String name) { + return calculate(getStudent(name).getGrades()); + } + + @Override + public double calculate(LinkedList grades) { + return MathTools.media(grades); + } + + public void printAllStudents() { + for (Student student : students) + System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), calculate(student.getGrades())); + } +} diff --git a/1ano/2semestre/poo/src/aula11/ex2/GradebookTester.java b/1ano/2semestre/poo/src/aula11/ex2/GradebookTester.java new file mode 100644 index 0000000..a1b8bb5 --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex2/GradebookTester.java @@ -0,0 +1,32 @@ +package aula11.ex2; + +import java.util.LinkedList; +import java.util.List; + +public class GradebookTester { + public static void main(String[] args) { + // Create a new Gradebook instance + Gradebook gradebook = new Gradebook(); + + // Load the student data from a text file + gradebook.load("datafiles/aula11/ex2/alunos.txt"); + + // Add a new student to the collection + Student newStudent = new Student("Johny May", new LinkedList<>(List.of(10.0, 15.0, 19.0))); + gradebook.addStudent(newStudent); + + // Remove a student from the collection + gradebook.removeStudent("Jane Smith"); + + // Retrieve a student from the collection + Student student = gradebook.getStudent("John Doe"); + + // Calculate the average grade for a specific student + double averageGrade = gradebook.calculateAverageGrade("John Doe"); + System.out.println("Average grade for John Doe: " + averageGrade); + + // Print a summary of all students in the collection + gradebook.printAllStudents(); + } +} + diff --git a/1ano/2semestre/poo/src/aula11/ex2/IGradeCalculator.java b/1ano/2semestre/poo/src/aula11/ex2/IGradeCalculator.java new file mode 100644 index 0000000..3c41645 --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex2/IGradeCalculator.java @@ -0,0 +1,7 @@ +package aula11.ex2; + +import java.util.LinkedList; + +public interface IGradeCalculator { + double calculate(LinkedList grades); +} diff --git a/1ano/2semestre/poo/src/aula11/ex2/Student.java b/1ano/2semestre/poo/src/aula11/ex2/Student.java new file mode 100644 index 0000000..2229373 --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex2/Student.java @@ -0,0 +1,30 @@ +package aula11.ex2; + +import java.util.LinkedList; + +public class Student { + + private String name; + private LinkedList grades; + + public Student(String name, LinkedList grades) { + this.name = name; + this.grades = grades; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LinkedList getGrades() { + return grades; + } + + public void setGrades(LinkedList grades) { + this.grades = grades; + } +}