diff --git a/org/junit/annotations.xml b/org/junit/annotations.xml new file mode 100644 index 0000000..66fa72f --- /dev/null +++ b/org/junit/annotations.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/Classroom.java b/src/main/java/io/zipcoder/Classroom.java index 64566f0..65c5096 100644 --- a/src/main/java/io/zipcoder/Classroom.java +++ b/src/main/java/io/zipcoder/Classroom.java @@ -1,4 +1,79 @@ package io.zipcoder; +import java.util.*; + public class Classroom { -} + + private Student[] students; + + //Constructors + public Classroom(int maxNumberOfStudents) { + students = new Student[maxNumberOfStudents]; + } + + public Classroom(Student[] students) { + this.students = students; + } + + public Classroom() { + this(30); + } + + + public Student[] getStudents() { + return students; + } + + public Double getAverageExamScore() { + double classTotalScore = 0; + double numberOfStudents = students.length; + + for (int i = 0; i < students.length; i++) + classTotalScore += students[i].getAverageExamScore(); + + return (classTotalScore / numberOfStudents); + } + + public String addStudent(Student student) { + + for (int index = 0; index < students.length; index++) { + if (students[index] == null) { + students[index] = student; + + return student.toString(); + } + } + + return "Classroom is full"; + } + + public boolean removeStudent(String firstName, String lasName) { + + for (int index = 0; index < students.length; index++) { + Student student = students[index]; + + if (student.getFirstName().contains(firstName) && + student.getLastName().contains(lasName)) { + + students[index] = null; + return true; + } + } + + return false; + } + + public List getStudentByScore(double score) { + List studentScore = new ArrayList<>(); + + for (Student student : getStudents()) { + if (student.getExamScores().contains(String.valueOf(score))) + studentScore.add(student); + } + + Collections.sort(studentScore, new NameComparator()); + + return studentScore; + } + +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/NameComparator.java b/src/main/java/io/zipcoder/NameComparator.java new file mode 100644 index 0000000..82cc13c --- /dev/null +++ b/src/main/java/io/zipcoder/NameComparator.java @@ -0,0 +1,11 @@ +package io.zipcoder; + +import java.util.Comparator; + +public class NameComparator implements Comparator { + + @Override + public int compare(Student o1, Student o2) { + return o1.getFirstName().compareTo(o2.getFirstName()); + } +} diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..44ec3f2 100644 --- a/src/main/java/io/zipcoder/Student.java +++ b/src/main/java/io/zipcoder/Student.java @@ -1,4 +1,88 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.Arrays; + public class Student { + + private String firstName; + private String lastName; + private ArrayList examScores; + + + //Constructor + + public Student(String firstName, String lastName, Double[] examScores) { + this.firstName = firstName; + this.lastName = lastName; + this.examScores = new ArrayList(Arrays.asList(examScores)); + ; + } + + + //setters and getters + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + + //methods + public int getNumberOfExamsTaken(){ + return examScores.size(); + } + + public String getExamScores(){ + String outputOfScores = ""; + + for(int index = 0; index < examScores.size(); index ++){ + + int exam = (index + 1); + double score = examScores.get(index); + + outputOfScores += ("Exame " + exam + " -> " + score + "\n"); + } + + return outputOfScores; + } + + public void addExamScore(double newScore){ + examScores.add(newScore); + } + + public void setExamScore(int examNumber, double newScore){ + examScores.set(examNumber - 1, newScore); + } + + public Double getAverageExamScore(){ + double totalScore = 0.00; + + for(int index = 0; index < examScores.size(); index++) + totalScore += examScores.get(index); + + return (totalScore/examScores.size()); + } + + @Override + public String toString(){ + + String ouput = ("\n> Student Name: " + firstName + " " + lastName + "\n" + + "> Average Score: " + getAverageExamScore() + "\n" + + "> Exam Scores: \n" + getExamScores()); + + return ouput; + } + } diff --git a/src/test/java/io/zipcoder/ClassroomTest.java b/src/test/java/io/zipcoder/ClassroomTest.java index 7bac1ff..93ff39c 100644 --- a/src/test/java/io/zipcoder/ClassroomTest.java +++ b/src/test/java/io/zipcoder/ClassroomTest.java @@ -1,4 +1,106 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.TreeMap; + +import static org.junit.Assert.*; + public class ClassroomTest { -} + + @Test + public void getAverageExamScoreTest(){ + // : Given + Double[] s1Scores = { 100.0, 150.0 }; + Double[] s2Scores = { 225.0, 25.0 }; + + Student s1 = new Student("student", "one", s1Scores); + Student s2 = new Student("student", "two", s2Scores); + + Student[] students = {s1,s2}; + Classroom classroom = new Classroom(students); + + // When + double output = classroom.getAverageExamScore(); + + // Then + System.out.println(output); + } + + @Test + public void addStudentTest(){ + // : Given + int maxNumberOfStudents = 1; + Classroom classroom = new Classroom(maxNumberOfStudents); + Double[] examScores = { 100.0, 150.0, 250.0, 0.0 }; + Student student = new Student("Leon", "Hunter", examScores); + + // When + Student[] preEnrollment = classroom.getStudents(); + String preEnrollmentAsString = Arrays.toString(preEnrollment); + System.out.println("==========================="); + System.out.println(preEnrollmentAsString); + + classroom.addStudent(student); + Student[] postEnrollment = classroom.getStudents(); + String postEnrollmentAsString = Arrays.toString(postEnrollment); + System.out.println("==========================="); + System.out.println(postEnrollmentAsString); + } + + @Test + public void removeStudentTest() { + // : Given + int maxNumberOfStudents = 2; + Classroom classroom = new Classroom(maxNumberOfStudents); + + Double[] examScores = {100.0, 150.0, 250.0, 0.0}; + Student student = new Student("Leon", "Hunter", examScores); + + Double[] examScores2 = {100.0, 150.0, 250.0, 0.0}; + Student student2 = new Student("Lilian", "Smith", examScores2); + + classroom.addStudent(student); + classroom.addStudent(student2); + + // When + Student[] preRemovingStudent = classroom.getStudents(); + String preRemovingAsString = Arrays.toString(preRemovingStudent); + System.out.println("==========================="); + System.out.println(preRemovingAsString); + + classroom.removeStudent("Leon", "Hunter"); + + Student[] afterRemoving = classroom.getStudents(); + String afterRemovigAsString = Arrays.toString(afterRemoving); + System.out.println("==========================="); + System.out.println(afterRemovigAsString); + } + + @Test + public void getStudentByScoreTest(){ + int maxNumberOfStudents = 4; + Classroom classroom = new Classroom(maxNumberOfStudents); + Double[] examScoresS1 = {92.0, 150.0, 250.0, 50.0}; + Double[] examScoresS2 = {250.0, 100.0, 100.0, 100.0}; + Double[] examScoresS3 = {92.0, 150.0, 250.0, 50.00}; + Double[] examScoresS4 = {90.0, 120.0, 99.0, 100.0}; + // When + Student testS1 = new Student("Leon", "Hunter", examScoresS1); + Student testS2 = new Student("Joe", "Smith", examScoresS2); + Student testS3 = new Student("Mike", "Jones", examScoresS3); + Student testS4 = new Student("Bob", "Johnson", examScoresS4); + + classroom.addStudent(testS1); + classroom.addStudent(testS2); + classroom.addStudent(testS3); + classroom.addStudent(testS4); + + System.out.println(classroom.getStudentByScore(100.0).toString()); + } + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..1c91414 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,120 @@ package io.zipcoder; + +import org.junit.Test; + +import static org.junit.Assert.*; + public class StudentTest { -} \ No newline at end of file + @Test + public void getStudentName(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { 100.0, 98.0 }; + Student student = new Student(firstName, lastName, examScores); + + // When + String output = student.getFirstName(); + String output2 = student.getLastName(); + + // Then + System.out.println(String.format("Student's name: %s %s", output, output2)); + } + + @Test + public void getNumberOfExamsTakenTest(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { 100.0, 98.0 }; + Student student = new Student(firstName, lastName, examScores); + + // When + int output = student.getNumberOfExamsTaken(); + + // Then + System.out.println("Student took: " + output); + } + + @Test + public void getExamsScoresTest(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { 100.0, 95.0, 123.0, 96.0 }; + Student student = new Student(firstName, lastName, examScores); + + // When + String output = student.getExamScores(); + + // Then + System.out.println(output); + } + + @Test() + public void addExamScoreTest(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { }; + Student student = new Student(firstName, lastName, examScores); + + // When + student.addExamScore(100.0); + String output = student.getExamScores(); + + // Then + System.out.println(output); + } + + @Test + public void testSetExamScore(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { 100.0 }; + Student student = new Student(firstName, lastName, examScores); + + // When + student.setExamScore(1, 150.0); + String output = student.getExamScores(); + + // Then + System.out.println(output); + } + + @Test + public void testAverageExamScore(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { 100.0, 150.0, 250.0, 0.0 }; + Student student = new Student(firstName, lastName, examScores); + + // When + double output = student.getAverageExamScore(); + + // Then + System.out.println(output); + } + + @Test + + public void testToString(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { 100.0, 150.0, 250.0, 0.0 }; + Student student = new Student(firstName, lastName, examScores); + + // When + String output = student.toString(); + + // Then + System.out.println(output); + } + +} + diff --git a/target/classes/io/zipcoder/Classroom.class b/target/classes/io/zipcoder/Classroom.class new file mode 100644 index 0000000..9b463c4 Binary files /dev/null and b/target/classes/io/zipcoder/Classroom.class differ diff --git a/target/classes/io/zipcoder/Student.class b/target/classes/io/zipcoder/Student.class new file mode 100644 index 0000000..ef2ba5c Binary files /dev/null and b/target/classes/io/zipcoder/Student.class differ diff --git a/target/test-classes/META-INF/classroom.kotlin_module b/target/test-classes/META-INF/classroom.kotlin_module new file mode 100644 index 0000000..8fb6019 Binary files /dev/null and b/target/test-classes/META-INF/classroom.kotlin_module differ diff --git a/target/test-classes/io/zipcoder/ClassroomTest.class b/target/test-classes/io/zipcoder/ClassroomTest.class new file mode 100644 index 0000000..5cab40c Binary files /dev/null and b/target/test-classes/io/zipcoder/ClassroomTest.class differ diff --git a/target/test-classes/io/zipcoder/StudentTest.class b/target/test-classes/io/zipcoder/StudentTest.class new file mode 100644 index 0000000..457c18d Binary files /dev/null and b/target/test-classes/io/zipcoder/StudentTest.class differ