diff --git a/tp1/Cohort.java b/tp1/Cohort.java index e199e790e8c0dad4a95033147e150e6a0a1a8fd6..a36bdc8f8191887de110c61d5a70c4cb3b89a56a 100644 --- a/tp1/Cohort.java +++ b/tp1/Cohort.java @@ -11,6 +11,7 @@ public class Cohort { /** * Constructs a cohort with a name equals to the specified {@code name} and no students. + * * @param name the name of the constructed Cohort */ @@ -21,33 +22,45 @@ public class Cohort { /** * Add the specified {@code student} to the students of the cohort. + * * @param student the student to be added to the cohort */ - public void addStudent(Student student){ + public void addStudent(Student student) { + students.add(student); } /** * Returns the list of students of the cohort. + * * @return the list of students of the cohort. */ - public List<Student> getStudents(){ + public List<Student> getStudents() { + return students; } /** * Print via the standard output the name of the cohort and all results associated to the students with their average * grade. */ - public void printStudentsResults(){ + public void printStudentsResults() { + printName(); + for (Student student : students){ + student.printResults(); + } + } - private void printName(){ + private void printName() { + System.out.println(name); } /** * Returns the name of the cohort. + * * @return the name of the cohort */ @Override public String toString() { - } + return name; + } } diff --git a/tp1/Grade.java b/tp1/Grade.java index 97516bc14d27ba69a0cf592062861f977303abcb..930be1a3b07a544951019f26440616f617fa72c6 100644 --- a/tp1/Grade.java +++ b/tp1/Grade.java @@ -28,7 +28,7 @@ public class Grade { */ public double getValue() { - return value; + return this.value; } /** @@ -48,6 +48,12 @@ public class Grade { * @return a grade corresponding to the mean of grade in {@code grades} */ public static Grade averageGrade(List<Grade> grades){ + double averageValue = 0; + for (Grade grade : grades) { + averageValue += grade.getValue(); + } + averageValue = averageValue / grades.size(); + return new Grade(averageValue); } /** diff --git a/tp1/Student.java b/tp1/Student.java index 9898a666f45ccd49189150bba98d8aa44f45912e..2424e329aa800718cd6ee27905d111c4d2fd9d17 100644 --- a/tp1/Student.java +++ b/tp1/Student.java @@ -31,6 +31,8 @@ public class Student { * @param grade the grade of the added result */ public void addResult(String teachingUnitName, Grade grade){ + TeachingUnitResult unitResult = new TeachingUnitResult(teachingUnitName, grade); + results.add(unitResult); } /** @@ -39,6 +41,7 @@ public class Student { */ @Override public String toString() { + return firstName + lastName; } @@ -48,6 +51,11 @@ public class Student { * @return the grades of the student */ public List<Grade> getGrades(){ + List<Grade> listOfGrades = new ArrayList<>(); + for (TeachingUnitResult result : results) { + listOfGrades.add(result.getGrade()); + } + return listOfGrades; } /** @@ -56,6 +64,12 @@ public class Student { * @return the average grade of the student */ public Grade getAverageGrade() { + double averageGrade = 0; + for (Grade grade : this.getGrades()) { + averageGrade += grade.getValue(); + } + averageGrade = averageGrade / this.getGrades().size(); + return new Grade(averageGrade); } @Override @@ -81,12 +95,21 @@ public class Student { * the average grade of the student. */ public void printResults(){ + printName(); + for (TeachingUnitResult teachingUnitResult : results) { + System.out.println(teachingUnitResult.toString()); + } + printAverageGrade(); + System.out.println(); } private void printName() { + System.out.println(this.toString()); } private void printAverageGrade() { + System.out.println(this.getAverageGrade().toString()); } + } diff --git a/tp1/TeachingUnitResult.java b/tp1/TeachingUnitResult.java index c052586a62e9513c55979121bb6ff8b67972fc71..a173c7eb33b302f6eb3e0767c33d4e4869810726 100644 --- a/tp1/TeachingUnitResult.java +++ b/tp1/TeachingUnitResult.java @@ -26,6 +26,7 @@ public class TeachingUnitResult { * @return the grade associated to the result */ public Grade getGrade() { + return grade; } /** @@ -34,5 +35,6 @@ public class TeachingUnitResult { */ @Override public String toString() { + return this.teachingUnitName + " : " + grade.toString(); } } diff --git a/tp1/TestStudent.java b/tp1/TestStudent.java new file mode 100644 index 0000000000000000000000000000000000000000..afd4fbe351a8bcf5bc1584ddece8bfb52756f791 --- /dev/null +++ b/tp1/TestStudent.java @@ -0,0 +1,78 @@ +import java.util.ArrayList; +import java.util.List; + +public class TestStudent { + public static void main (String[] args){ + TestStudent test = new TestStudent(); + + if (test.testAddResult() == true) + System.out.println("addResult = correct"); + else + System.out.println("addResult = incorrect"); + + if (test.testGetGrades() == true) + System.out.println("getGrades = correct"); + else + System.out.println("getGrades = incorrect"); + + if (test.testToString() == true) + System.out.println("toString = correct"); + else + System.out.println("toString = incorrect"); + + if (test.testAverageGrade() == true) + System.out.println("averageGrade = correct"); + else + System.out.println("averageGrade = incorrect"); + + if (test.testPrintResults() == true) + System.out.println("printResults = correct"); + else + System.out.println("printResults = incorrect"); + } + public boolean testAddResult(){ + Grade grade = new Grade(11.7); + TeachingUnitResult unitResult = new TeachingUnitResult("Programmation 2", grade); + Student student = new Student("tt", "t morts"); + student.addResult("Programmation 2", grade); + if (student.equals(unitResult)) + return true; + else + return false; + + } + public boolean testGetGrades(){ + Grade grade = new Grade(11.7); + Student student = new Student("t","t"); + student.addResult("Prog", grade); + List<Grade> grades = student.getGrades(); + if (grades.contains(grade)) + return true; + else + return false; + } + public boolean testToString(){ + Student student = new Student("Leo", "Rangheard"); + String name = student.toString(); + if (name.equals("Leo Rangheard")) + return true; + else + return false; + } + public boolean testAverageGrade(){ + Student student = new Student("t", "t"); + student.addResult("Programmation 2", new Grade(8)); + student.addResult("Programmation 2", new Grade(9)); + student.addResult("Programmation 2", new Grade(10)); + student.addResult("Programmation 2", new Grade(11)); + student.addResult("Programmation 2", new Grade(12)); + Grade averageGrade = student.getAverageGrade(); + if (averageGrade.getValue() == 10) + return true; + else + return false; + } + public boolean testPrintResults(){ + + } +} diff --git a/tp1/TestTeachingUnitResult.java b/tp1/TestTeachingUnitResult.java new file mode 100644 index 0000000000000000000000000000000000000000..d8972dd1216fcf6f2f4e22f20ef32f470e47d14d --- /dev/null +++ b/tp1/TestTeachingUnitResult.java @@ -0,0 +1,30 @@ +public class TestTeachingUnitResult { + public static void main(String [] args){ + TestTeachingUnitResult test = new TestTeachingUnitResult(); + if (test.testgetGrade() == true) + System.out.println("testgetGrade = correct"); + else + System.out.println("testgetGrade = incorrect"); + if (test.testtoString() == true) + System.out.println("testtoString = correct"); + else + System.out.println("testtoString = incorrect"); + } + public boolean testgetGrade(){ + Grade grade = new Grade(11.5); + double valeur = grade.getValue(); + if (valeur == 11.5) + return true; + else + return false; + } + public boolean testtoString(){ + Grade grade = new Grade(11.7); + TeachingUnitResult test = new TeachingUnitResult("Programmation 2", grade); + String TURString = test.toString(); + if (TURString.equals("Programmation 2 : 11.7/20")) + return true; + else + return false; + } +}