diff --git a/src/main/java/io/zipcoder/Classroom.java b/src/main/java/io/zipcoder/Classroom.java index 64566f0..482d85c 100644 --- a/src/main/java/io/zipcoder/Classroom.java +++ b/src/main/java/io/zipcoder/Classroom.java @@ -1,4 +1,139 @@ package io.zipcoder; +import java.util.*; + public class Classroom { + private Student[] students; + private int maxNumberOfStudents; + + + public Classroom(){ + this.students = new Student[30]; + this.maxNumberOfStudents = 30; + this.initializeStudentArray(); + } + + public Classroom(int maxNumberOfStudents){ + this.maxNumberOfStudents = maxNumberOfStudents; + this.students = new Student[maxNumberOfStudents]; + this.initializeStudentArray(); + } + + public Classroom(Student[] students){ + this.students = students; + } + + private void initializeStudentArray() { + for(int i = 0; i < students.length; i++){ + + } + } + + public Student[] getStudents() { + List studentList = new ArrayList(Arrays.asList(students)); + while(studentList.remove(null)){} + return studentList.toArray(new Student[0]); + } + + public double getAverageExamScore(){ + double sum = 0.00; + Student[] students = this.getStudents(); + for(Student element: students){ + sum += element.getAverageExamScore(); + } + return sum / actualStudentCount(); + } + + public int actualStudentCount(){ + Integer studentCount = 0; + Student[] students = this.getStudents(); + Integer length = students.length; + for(int i = 0; i < length; i++){ + if(students[i] != null) + studentCount++; + } + return studentCount; + } + + public void addStudent(Student student){ + List studentArrayList; + if(this.getStudents().length > 0){ + studentArrayList = new ArrayList(Arrays.asList(this.getStudents())); + } else { + studentArrayList = new ArrayList(); + } + + studentArrayList.add(student); + this.students = studentArrayList.toArray(new Student[0]); + } + + public void removeStudent(String firstName, String lastName){ + boolean adjust = false; + Student[] studentArray = this.getStudents(); + for(int i = 0; i < studentArray.length; i++){ + if(studentArray[i].getFirstName() == firstName){ + if(studentArray[i].getLastName() == lastName){ + studentArray[i] = null; + adjust = true; + } + } + } + if(adjust) + this.adjustForNull(); + } + + public void adjustForNull(){ + Student[] studentArray = this.getStudents(); + Student[] adjustedStudentArray = new Student[this.maxNumberOfStudents]; + int lastIndex = 0; + for(int i = 0; i < studentArray.length; i++){ + if(studentArray[i] != null){ + adjustedStudentArray[lastIndex] = studentArray[i]; + lastIndex++; + } + } + this.students = adjustedStudentArray; + } + + public Student[] getStudentsByScore(){ + List students = new ArrayList<>(Arrays.asList(this.getStudents())); + Comparator byScore = Comparator.comparing(Student::getAverageExamScore); + Comparator byFirstName = Comparator.comparing(Student::getLastName); + Comparator byLastName = Comparator.comparing(Student::getFirstName); + + Collections.sort(students, byScore.thenComparing(byLastName).thenComparing(byFirstName)); + Collections.reverse(students); + return students.toArray(new Student[0]); + } + + public char getDeviationScore(Student student){ + Double averageClassExamScore = this.getAverageExamScore(); + Double averageStudentExamScore = student.getAverageExamScore(); + 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 + standardDeviation) && + averageStudentExamScore > averageClassExamScore){ + return 'C'; + } else if(averageStudentExamScore <= (averageClassExamScore + standardDeviation)){ + return 'D'; + } else { + return 'F'; + } + } + + public Map getGradeBook(){ + Student[] studentlist = this.getStudents(); + Map gradeMap = new HashMap<>(); + int length = actualStudentCount(); + for(int i = 0; i < length; i++){ + gradeMap.put(studentlist[i], getDeviationScore(studentlist[i])); + } + return gradeMap; + } + } diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..89de898 100644 --- a/src/main/java/io/zipcoder/Student.java +++ b/src/main/java/io/zipcoder/Student.java @@ -1,4 +1,82 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.Arrays; + public class Student { + private String firstName; + private String lastName; + private ArrayList testScores; + + public Student(String firstName, String lastName, Double[] testScores){ + this.firstName = firstName; + this.lastName = lastName; + this.testScores = 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 ArrayList getTestScores(){ + return this.testScores; + } + + public Integer getNumberOfExamsTaken(){ + return this.getTestScores().size(); + } + + public void addExamScore(double examScore){ + this.getTestScores().add(examScore); + } + + public String getExamScores(){ + String printOut = ""; + ArrayList examScores = this.getTestScores(); + + for(int i = 0; i < examScores.size(); i++){ + printOut += " Exam " + (i + 1) + " -> "; + printOut += examScores.get(i); + printOut += "\n"; + } + return printOut; + } + + + public void setExamScore(int examNumber, double newScore){ + ArrayList testScores = this.getTestScores(); + testScores.set(examNumber - 1, newScore); + } + + public double getAverageExamScore(){ + double sum = this.getTestScores().stream().mapToDouble(Double::doubleValue).sum(); + return sum / this.getTestScores().size(); + } + + @Override + public String toString(){ + String readout = ""; + readout += "Student Name: " + this.firstName + " " + this.lastName; + readout += "\n"; + readout += "> Average Score: " + this.getAverageExamScore(); + readout += "\n"; + readout += "> Exam Scores:"; + readout += "\n"; + readout += this.getExamScores(); + + return readout; + } + } diff --git a/src/test/java/io/zipcoder/ClassroomTest.java b/src/test/java/io/zipcoder/ClassroomTest.java index 7bac1ff..d3132a1 100644 --- a/src/test/java/io/zipcoder/ClassroomTest.java +++ b/src/test/java/io/zipcoder/ClassroomTest.java @@ -1,4 +1,73 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Map; + public class ClassroomTest { + + @Test + public void testAverageExamScores(){ + // : 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 testAddStudent(){ + // : 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(); + classroom.addStudent(student); + Student[] postEnrollment = classroom.getStudents(); + + // Then + String preEnrollmentAsString = Arrays.toString(preEnrollment); + String postEnrollmentAsString = Arrays.toString(postEnrollment); + + System.out.println("==========================="); + System.out.println(preEnrollmentAsString); + System.out.println("==========================="); + System.out.println(postEnrollmentAsString); + } + + @Test + public void testGetStudentsByExamScore(){ + // : Given + Double[] s1Scores = { 100.0, 150.0, 123.0 }; + Double[] s2Scores = { 100.0, 150.0, 123.0 }; + Double[] s3Scores = { 145.3, 55.2, 400.1, 300.5}; + + Student s1 = new Student("student", "Yep", s1Scores); + Student s2 = new Student("student", "Two", s2Scores); + Student s3 = new Student("student", "three", s3Scores); + + Student[] students = {s1,s2,s3}; + Student[] expected = {s3, s1, s2}; + Classroom classroom = new Classroom(students); + + Student[] actual = classroom.getStudentsByScore(); + Map gradeBook = classroom.getGradeBook(); + Assert.assertArrayEquals(actual, expected); + + } } diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..801137b 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,84 @@ package io.zipcoder; +import org.junit.Test; + public class StudentTest { + @Test + public void testStudentConstructor(){ + // : 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 testAddExamScore() { + // : 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 testGetAverageScore(){ + // : 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); + } } \ 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..f1a7fb9 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..e862b8d 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..d023650 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..10b9514 Binary files /dev/null and b/target/test-classes/io/zipcoder/StudentTest.class differ