diff --git a/src/main/java/io/zipcoder/Classroom.java b/src/main/java/io/zipcoder/Classroom.java index 64566f0..acff8f3 100644 --- a/src/main/java/io/zipcoder/Classroom.java +++ b/src/main/java/io/zipcoder/Classroom.java @@ -1,4 +1,143 @@ package io.zipcoder; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + public class Classroom { -} + private Student[] studentArray; + private int maxNumberOfStudents; + + + public Classroom() { + this.studentArray = new Student[30]; + } + + public Classroom(int maxNumberOfStudents) { + this.studentArray = new Student[maxNumberOfStudents]; + } + + public Classroom(Student... students){ + this.studentArray = students; + } + + public Student[] getStudentArray() { + return studentArray; + } + + public double getClassAverageExamScore() { + double totalSum = 0; + for (Student element: studentArray) { + totalSum += element.getStudentAverageExamScore(); + } + return totalSum / studentArray.length; + } + + public void addStudent(Student student) { + for (int i = 0; i < studentArray.length; i++) { + if (studentArray[i] == null) { + studentArray[i] = student; + break; + } + } +// ArrayList newEnrollment = new ArrayList<>(Arrays.asList(students)); +// if (students.length < maxNumberOfStudents) { +// newEnrollment.add(student); +// } else System.out.println("Enrollment full"); +// this.students = newEnrollment.toArray(new Student[0]); + } + + public void removeStudent(String firstName, String lastName) { +// for (int i = 0; i < studentArray.length - 1; i++) { +// if (studentArray[i].getFirstName().equals(firstName) && +// studentArray[i].getLastName().equals(lastName)){ +// studentArray[i] = null; +// } studentArray[i] = studentArray[i + 1]; +// } +// studentArray[studentArray.length - 1] = null; + +//newStudentList.set(newStudentList.indexOf(student), null); + ArrayList newStudentList = new ArrayList(Arrays.asList(studentArray)); + + 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); // adding null to the end of list to replace student position. + } + } + this.studentArray = newStudentList.toArray(new Student[0]); + } + + + + public Student[] getStudentsByScore () { + List studentList = new ArrayList(Arrays.asList(studentArray)); + + Comparator byAvgExamScores = Comparator.comparing(Student::getStudentAverageExamScore); + Comparator byFullName = Comparator.comparing(Student::getFullName); + + Collections.sort(studentList, byAvgExamScores.reversed().thenComparing(byFullName)); + + //Collections.reverse(studentList); // Highest to lowest + + Student[] studentsSortedByScore = studentList.toArray(new Student[0]); + + return studentsSortedByScore; + + // by score , last name, first name + } + + + + // ~*~ Thank you Leon ! ~*~ + public Map getGradeBook() { + Map gradeBookResult = new HashMap<>(); + for (Student student : studentArray) { + Double percentile = getPercentile(student); + boolean isPercentileBetween0And10 = percentile > 0 && percentile <= 10; + boolean isPercentileBetween11And29 = percentile > 11 && percentile <= 29; + boolean isPercentileBetween30And50 = percentile > 30 && percentile <= 50; + boolean isPercentileBetween51And89 = percentile > 51 && percentile <= 89; + + if (isPercentileBetween0And10) { + gradeBookResult.put(student, 'A'); + } else if (isPercentileBetween11And29) { + gradeBookResult.put(student, 'B'); + } else if (isPercentileBetween30And50) { + gradeBookResult.put(student, 'C'); + } else if (isPercentileBetween51And89) { + gradeBookResult.put(student, 'D'); + } else { + gradeBookResult.put(student, 'F'); + } + } + return gradeBookResult; + } + public double getPercentile (Student student){ + List allStudentGrades = Stream + .of(studentArray) + .map(Student::getStudentAverageExamScore) + .collect(Collectors.toList()); + + double percentage = student.getStudentAverageExamScore(); + boolean isPercentileGreaterThan0 = percentage >= 0; + boolean isPercentileLessThan100 = percentage <= 100; + boolean isPercentileValid = isPercentileGreaterThan0 && isPercentileLessThan100; + boolean isItemsEmpty = allStudentGrades.isEmpty(); + boolean areArgumentsValid = isPercentileValid && !isItemsEmpty; + if (!areArgumentsValid) { + throw new IllegalArgumentException(); + } + Collections.sort(allStudentGrades); + return allStudentGrades.get((int) + Math.round(percentage / 100.0 * (allStudentGrades.size() - 1))); + } + + } + + + diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..c4e7663 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; + ArrayList examScores; + Double[] testScores; + + public Student(String firstName, String lastName, Double[] testScores) { + this.firstName = firstName; + this.lastName = lastName; + this.examScores = new ArrayList(Arrays.asList(testScores)); + } + + + + // ================== GETTERS ================== // + public String getFullName(){ + return this.lastName + ", " + this.firstName; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public int getNumberOfExamsTaken() { + return examScores.size(); + } + + public String getExamScores() { + StringBuilder stringOfTestScores = new StringBuilder(); + + for (int i = 0; i < examScores.size() ; i++) { + stringOfTestScores.append(examScores.get(i)).append(" | "); + stringOfTestScores.append(i); + } + return String.valueOf(examScores); + //Arrays.toString(testScores); + } + + public void addExamScore(double testScore) { + this.examScores.add(testScore); + } + + public Double getStudentAverageExamScore() { + double totalSum = 0.0; + for (int i = 0; i < examScores.size(); i++) { + totalSum = totalSum + examScores.get(i); + } + return totalSum / examScores.size(); + } + + // ===================== SETTERS =================== // + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public void setExamScore(int examNumber, double newScore){ + this.examScores.set(examNumber - 1, newScore); + } + + @Override + public String toString() { + + return String.format("Student Name: %s %s\n" + " Average Score: %.2f\n" + " Exam Scores: %s \n", + firstName, lastName, getStudentAverageExamScore(), getExamScores()); +// return "Student Name: " + firstName + " " + lastName + '\n' + +// "Average Score: " + getAverageExamScore() + '\n' + +// "Exam Scores: " + getExamScores(); + } } diff --git a/src/test/java/io/zipcoder/ClassroomTest.java b/src/test/java/io/zipcoder/ClassroomTest.java index 7bac1ff..d50c168 100644 --- a/src/test/java/io/zipcoder/ClassroomTest.java +++ b/src/test/java/io/zipcoder/ClassroomTest.java @@ -1,4 +1,127 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + 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.getClassAverageExamScore(); + + // Then + Assert.assertEquals(output,classroom.getClassAverageExamScore(), 0.00); + } + + @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.getStudentArray(); + classroom.addStudent(student); + Student[] postEnrollment = classroom.getStudentArray(); + + // 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 getStudentsByScoreTest() { + // Given // What do i need to make + String givenFirstName1 = "Leon"; + String givenLastName1 = "Hunter"; + Double[] examScores1 = {100.0, 95.0, 123.0, 96.0}; + Student student1 = new Student(givenFirstName1, givenLastName1, examScores1); + + String givenFirstName2 = "Leon"; + String givenLastName2 = "Gamer"; + Double[] examScores2 = {100.0, 95.0, 123.0, 96.0}; + Student student2 = new Student(givenFirstName2, givenLastName2, examScores2); + + String givenFirstName3 = "Sammy"; + String givenLastName3 = "Sheen"; + Double[] examScores3 = {98.0, 95.0, 103.0, 90.0}; + Student student3 = new Student(givenFirstName3, givenLastName3, examScores3); + + Classroom classroom = new Classroom(student1, student2, student3); + Student[] expected = {student2, student1, student3}; + + + // When // Getting the actual + Student[] actual = classroom.getStudentsByScore(); + + + // Then // Test expected against actual + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void removeStudentTest() { + // Given + Double[] examScores1 = {85.0, 90.0, 95.0}; + Double[] examScores2 = {88.0, 90.0, 100.0}; + Double[] examScores3 = {80.0, 97.0, 85.0}; + Student student1 = new Student("Frank", "Kelp", examScores1); + Student student2 = new Student("Sarah", "Blanco", examScores2); + Student student3 = new Student("James", "Bond", examScores3); + Student[] students = {student1, student2, student3}; + + // When + Classroom testRoom = new Classroom(); + testRoom.addStudent(student1); + testRoom.addStudent(student2); + testRoom.addStudent(student3); + testRoom.removeStudent("James", "Bond"); + + // Then + + System.out.println((Arrays.toString(testRoom.getStudentArray()))); + Student actual = testRoom.getStudentArray()[2]; + Assert.assertNull(actual); + } + + @Test + public void getGradeBookTest() { + // Given + Double[] examScores1 = {85.0, 90.0, 95.0}; + Double[] examScores2 = {88.0, 90.0, 90.0}; + Double[] examScores3 = {80.0, 97.0, 85.0}; + Student student1 = new Student("Frank", "Kelp", examScores1); + Student student2 = new Student("Sarah", "Blanco", examScores2); + Student student3 = new Student("James", "Bond", examScores3); + Student[] students = {student1, student2, student3}; + + // When + Classroom testRoom = new Classroom(student1,student2, student3); + + + // Then + System.out.println(testRoom.getGradeBook()); + } + } diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..e3fe22f 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,80 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + public class StudentTest { + @Test + public void studentConstructorTest() { + // Given + String givenFirstName = "Leon"; + String givenLastName = "Hunter"; + Double[] examScores = {100.0, 95.0, 123.0, 96.0}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + // When + String expectedFirstName = student.getFirstName(); + String expectedLastName = student.getLastName(); + //ArrayList output = student.getExamScores(); + + // Then + Assert.assertEquals(expectedFirstName, givenFirstName); + Assert.assertEquals(expectedLastName, givenLastName); + } + + @Test + public void getStudentExamScoresTest() { + // Given + String givenFirstName = "Leon"; + String givenLastName = "Hunter"; + Double[] examScores = {100.0, 95.0, 123.0, 96.0}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + // When + String output = student.getExamScores(); + + // Then + Assert.assertEquals(output, Arrays.toString(examScores)); + } + + @Test + public void addExamScoreTest() { + // Given + String givenFirstName = "Leon"; + String givenLastName = "Hunter"; + Double[] examScores = { }; + Student student = new Student(givenFirstName, givenLastName, examScores); + + // When + student.addExamScore(100.0); + + String output = student.getExamScores(); + + // Then + Assert.assertEquals(output, "[100.0]"); + } + + @Test + public void setExamScoreTest() { + // Given + String givenFirstName = "Leon"; + String givenLastName = "Hunter"; + Double[] examScores = {100.0}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + // When + student.setExamScore(1,150.0); + //student.addExamScore(150); + + String expectedOutput = student.getExamScores(); + + + // Then + Assert.assertEquals(expectedOutput, "[150.0]"); + } + } \ 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..5eaf2f6 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..7af0f90 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..ffa114f 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..1c46998 Binary files /dev/null and b/target/test-classes/io/zipcoder/StudentTest.class differ