diff --git a/src/main/java/io/zipcoder/Classroom.java b/src/main/java/io/zipcoder/Classroom.java index 64566f0..77901c5 100644 --- a/src/main/java/io/zipcoder/Classroom.java +++ b/src/main/java/io/zipcoder/Classroom.java @@ -1,4 +1,116 @@ package io.zipcoder; +import java.text.DecimalFormat; +import java.util.*; + public class Classroom { + + Student[] students; + int maxNumberOfStudents; + + + public Classroom(int maxNumberOfStudents) { + this.maxNumberOfStudents = maxNumberOfStudents; + } + + public Classroom(Student... students) { + this.students = students; + } + + public Classroom() { + this.students = new Student[30]; + } + + public Student[] getStudents() { return students; } + + public double getAverageExamScores(Student[] students) { + double total = 0.0; + for (Student s : students) { + total += s.getAverageTestScore(); + + } + return total / students.length; + } + + public void addStudent(Student student) { + for (int i = 0; i < students.length; i++) { + if(students[i] == null) { + students[i] = student; + break; + } + } + } + + public int actualStudentCount() { //counts how many actual students there are in a classroom size of 30 + int totalStudents = 0; + for(int i = 0; i < students.length; i++) { + if(students[i] != null) { + totalStudents++; + } + } + return totalStudents; + } + + public void removeStudent(String firstName, String lastName) { + ArrayList newStudentList = new ArrayList (Arrays.asList(students)); + + for (int i = 0; i < newStudentList.size(); i++) { + Student student = newStudentList.get(i); + if(student == null) { + continue; + } else if(student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)){ + newStudentList.remove(student); + newStudentList.add(null); + } + } + this.students = newStudentList.toArray(new Student[0]); + } + + public Student[] getStudentsByScore() { + List studentList = new ArrayList (Arrays.asList(students)); + + Comparator byExamScores = Comparator.comparing(Student::getAverageTestScore); + Comparator byFullName = Comparator.comparing(Student::getFullName); + + Collections.sort(studentList, byExamScores.reversed().thenComparing(byFullName)); + + Student[] studentsSortedByScore = studentList.toArray(new Student[0]); + + return studentsSortedByScore; + } + + public Map getGradeBook() { + Student[] studentList = this.getStudents(); + Map gradeBookResult = new HashMap<>(); + int length = actualStudentCount(); + for(int i = 0; i < length; i++) { + gradeBookResult.put(studentList[i], getDeviation(studentList[i])); + } + return gradeBookResult; + } + + public char getDeviation(Student student) { + //need average score of each person in class + Double averageClassExamScore = this.getAverageExamScores(students); + Double averageStudentExamScore = student.getAverageTestScore(); + Double preDeviation = 0.0; + for(Student testScoreAverage : students) { + preDeviation += Math.pow(averageStudentExamScore - averageClassExamScore, 2); + } + Double standardDeviation = Math.sqrt((preDeviation / actualStudentCount() - 1)); +// Double preDeviation = Math.pow(averageStudentExamScore - averageClassExamScore, 2); +// Double standardDeviation = Math.sqrt(preDeviation/(actualStudentCount() -1)); + + if(averageStudentExamScore >= (averageClassExamScore + (standardDeviation * 2))){ + return 'A'; + } else if(averageStudentExamScore >= (averageClassExamScore + standardDeviation)){ + return 'B'; + } else if(averageStudentExamScore >= averageClassExamScore){ + return 'C'; + } else if(averageStudentExamScore >= (averageClassExamScore - standardDeviation)){ + return 'D'; + } else { + return 'F'; + } + } } diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..16dfa7d 100644 --- a/src/main/java/io/zipcoder/Student.java +++ b/src/main/java/io/zipcoder/Student.java @@ -1,4 +1,79 @@ package io.zipcoder; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.jar.JarEntry; + public class Student { + + private String firstName; + private String lastName; + ArrayList examScores; + Double[] testScores; + + public Student(String firstName, String lastName, Double[] testScores) { + this.firstName = firstName; + this.lastName = lastName; + this.testScores = testScores; + this.examScores = new ArrayList(Arrays.asList(testScores)); + } + + 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; + } + + public int getNumberOfExamsTake() { + return testScores.length; + } + + public String getExamScores() { + StringBuilder testScoreString = new StringBuilder(); + for (int i = 0; i < testScores.length; i++) { + testScoreString.append(testScores[i] + " | "); + } + return testScoreString.toString(); + } + + public void addExamScore(double testScores) { + this.examScores.add(testScores); + } + + public void setExamScore(int examNumber, double newScore) { + this.examScores.set(examNumber, newScore); + } + + public double getAverageTestScore() { + double sum = 0.0; + for (int i = 0; i < testScores.length; i++) { + sum += testScores[i]; + } + DecimalFormat format = new DecimalFormat("#.#"); + return sum / testScores.length; + } + + @Override + public String toString() { + return String.format("Student Name: %s %s\n" + "Average Score: %.1f\n" + "Exam Scores: %s", firstName, lastName, getAverageTestScore(), getExamScores()); + //Student Name: Jen + //Average Score: 80.0 + //Exam Score 1: 90.0 | 80.0 | 70.0 | + } + + public String getFullName() { + String fullName = getFirstName() + " " + getLastName(); + return fullName; + } } diff --git a/src/test/java/io/zipcoder/ClassroomTest.java b/src/test/java/io/zipcoder/ClassroomTest.java index 7bac1ff..cee0aee 100644 --- a/src/test/java/io/zipcoder/ClassroomTest.java +++ b/src/test/java/io/zipcoder/ClassroomTest.java @@ -1,4 +1,118 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class ClassroomTest { + + @Test + public void testGetStudents() { + //given + Double[] testScores = {90.0, 80.0, 70.0}; + Student student = new Student("Tohru", "Honda", testScores); + //when + Classroom classroom = new Classroom(); + classroom.addStudent(student); + Student actual = classroom.getStudents()[0]; + //then + Assert.assertEquals(student, actual); + } + + @Test + public void testGetAverageExamScore() { + //given + Double[] examScores1 = {90.0, 95.0, 100.0}; //95 + Double[] examScores2 = {85.0, 90.0, 87.0}; //87.3 + Double[] examScores3 = {70.0, 65.0, 55.0}; //63.3 + Student student1 = new Student("Blossom", "Ultonium",examScores1); + Student student2 = new Student("Buttercup", "Ultonium", examScores2); + Student student3 = new Student("Bubbles", "Ultonium", examScores3); + Student[] testStudentArr = {student1, student2, student3}; + double expected = 81.888; + //when + Classroom classroom = new Classroom(); + double actual = classroom.getAverageExamScores(testStudentArr); + //then + Assert.assertEquals(expected, actual, 0.001); + } + + @Test + public void testAddStudents() { + //given + Double[] examScores1 = {90.0, 95.0, 100.0}; //95 + Double[] examScores2 = {85.0, 90.0, 87.0}; //87.3 + Double[] examScores3 = {70.0, 65.0, 55.0}; //63.3 + Student student1 = new Student("Blossom", "Ultonium",examScores1); + Student student2 = new Student("Buttercup", "Ultonium", examScores2); + Student student3 = new Student("Bubbles", "Ultonium", examScores3); + Student[] expected = {student1, student2, student3, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}; + //when + Classroom testClassroom = new Classroom(); + testClassroom.addStudent(student1); + testClassroom.addStudent(student2); + testClassroom.addStudent(student3); + Student[] actual = testClassroom.getStudents(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testRemoveStudents() { + //given + Double[] examScores1 = {90.0, 95.0, 100.0}; + Double[] examScores2 = {85.0, 90.0, 87.0}; + Double[] examScores3 = {70.0, 65.0, 55.0}; + Student student1 = new Student("Erwin", "Smith",examScores1); + Student student2 = new Student("Hanji", "Zoe", examScores2); + Student student3 = new Student("Eren", "Yeager", examScores3); + Student[] expected = {student2, student3, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}; + //when + Classroom testClassroom = new Classroom(); + testClassroom.addStudent(student1); + testClassroom.addStudent(student2); + testClassroom.addStudent(student3); + testClassroom.removeStudent("Erwin", "Smith"); + Student[] actual = testClassroom.getStudents(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetStudentsByScore() { + //given + Double[] examScores1 = {85.0, 90.0, 87.0}; + Double[] examScores2 = {90.0, 95.0, 100.0}; + Double[] examScores3 = {85.0, 90.0, 87.0}; + Student student1 = new Student("Craig", "Boone",examScores1); + Student student2 = new Student("Veronica", "Santangelo", examScores2); + Student student3 = new Student("Cass", "Sharon", examScores3); + Student[] expected = {student2, student3, student1}; + //when + Classroom testClassroom = new Classroom(student1, student2, student3); + Student[] actual = testClassroom.getStudentsByScore(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetGradeBook() { + //given + Double[] examScores1 = {85.0, 90.0, 87.0}; + Double[] examScores2 = {90.0, 95.0, 100.0}; + Double[] examScores3 = {85.0, 90.0, 87.0}; + Double[] examScores4 = {65.0, 50.0, 57.0}; + Double[] examScores5 = {75.0, 60.0, 67.0}; + Double[] examScores6 = {85.0, 80.0, 77.0}; + Student student1 = new Student("Craig", "Boone", examScores1); + Student student2 = new Student("Veronica", "Santangelo", examScores2); + Student student3 = new Student("Cass", "Sharon", examScores3); + Student student4 = new Student("Kyo", "Sohma", examScores4); + Student student5 = new Student("Tohru", "Honda", examScores5); + Student student6 = new Student("Hatsuharu", "Sohma", examScores6); + Student[] expected = {student1, student2, student3, student4, student5, student6}; + //when + Classroom testClassroom = new Classroom(student1, student2, student3, student4, student5, student6); + //then + System.out.println(testClassroom.getGradeBook()); + } } diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..fe032cb 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,91 @@ package io.zipcoder; +import java.util.Arrays; +import org.junit.Assert; +import org.junit.Test; public class StudentTest { + @Test + public void testSetFirstName() { + //given + String firstName = "Stewart"; + String lastName = "Little"; + Double[] examScores = {90.0, 85.0, 99.0}; + Student newStudent = new Student(firstName, lastName, examScores); + //when + String actual = newStudent.getFirstName(); + //then + Assert.assertEquals(firstName, actual); + } + + @Test + public void testSetLastName() { + //given + String firstName = "Stewart"; + String lastName = "Little"; + Double[] examScores = {90.0, 85.0, 99.0}; + Student newStudent = new Student(firstName, lastName, examScores); + //when + String actual = newStudent.getLastName(); + //then + Assert.assertEquals(lastName, actual); + } + + + @Test + public void testGetExamScores() { + //given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] testScores = {100.0, 95.0, 123.0, 96.0}; + Student student = new Student(firstName, lastName, testScores); + //when + String expected = "100.0 | 95.0 | 123.0 | 96.0 | "; + String actual = student.getExamScores(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetAverageExamScore() { + //given + String firstName = "Sasha"; + String lastName = "Braus"; + Double[] testScores = {90.0, 70.0, 80.0, 80.0}; + Student student = new Student(firstName, lastName, testScores); + //when + double expected = 80.0; + double actual = student.getAverageTestScore(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testToStringOverride() { + //given + String firstName = "Levi"; + String lastName = "Ackerman"; + Double[] testScores = {90.0, 80.0, 70.0}; + Student newStudent = new Student(firstName, lastName, testScores); + //when + String expected = "Student Name: Levi Ackerman\nAverage Score: 80.0\nExam Scores: 90.0 | 80.0 | 70.0 | "; + String actual = newStudent.toString(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetFullName() { + //given + String firstName = "Reina"; + String lastName = "Collier"; + Double[] testScores = {90.0, 80.0, 70.0}; + Student newStudent = new Student(firstName, lastName, testScores); + String expected = "Reina Collier"; + //when + String actual = newStudent.getFullName(); + //then + Assert.assertEquals(expected, actual); + } + } \ No newline at end of file diff --git a/target/classes/io/zipcoder/Classroom.class b/target/classes/io/zipcoder/Classroom.class new file mode 100644 index 0000000..94ce7ec 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..e5b321c Binary files /dev/null and b/target/classes/io/zipcoder/Student.class 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..c71df10 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..e99d0cc Binary files /dev/null and b/target/test-classes/io/zipcoder/StudentTest.class differ