diff --git a/src/main/java/io/zipcoder/Classroom.java b/src/main/java/io/zipcoder/Classroom.java index 64566f0..eee0597 100644 --- a/src/main/java/io/zipcoder/Classroom.java +++ b/src/main/java/io/zipcoder/Classroom.java @@ -1,4 +1,151 @@ package io.zipcoder; +import java.util.*; + public class Classroom { + + private Student[] students; + private int maxNumOfStudents; + + + public Classroom() { + this.students = new Student[30]; + } + + public Classroom(int maxNumOfStudents) { + this.students = new Student[maxNumOfStudents]; + + } + + public Classroom(Student... students) { + this.students = students; + } + + public Student[] getStudents() { + return students; + } //do I need to clone? + + public double getAverageExamScore() { + double total = 0; + for (Student s : students) { + total += s.getAverageExamScore(); + } + + double average = total / correctForNull(); + + return average; + } + + public void addStudent(Student student) { + for (int i = 0; i < students.length; i++) { + if (students[i] == null) { + students[i] = student; + break; + } + + } + + } + +// ArrayList newEnrollment = new ArrayList<>(Arrays.asList(students)); +// newEnrollment.add(student); +// this.students = newEnrollment.toArray(new Student[0]); + + public void removeStudentbyName(String firstName, String lastName) { + ArrayList studentArray = new ArrayList<>(Arrays.asList(students)); + for (int i = 0; i < studentArray.size(); i++) { + Student student = studentArray.get(i); + if (student == null) { + continue; + } else if (student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)) { + studentArray.remove(student); + studentArray.add(null); + } + } + this.students = studentArray.toArray(new Student[0]); + + } +// ArrayList studentArray = new ArrayList<>(Arrays.asList(students)); +// for (int i = 0; i < studentArray.size() ; i++) { +// Student student = studentArray.get(i); +// if (student.getLastName().equalsIgnoreCase(lastName) && student.getFirstName().equalsIgnoreCase(firstName)) { +// studentArray.set(student); +// } +// } studentArray.add(null); + + public Student[] getStudentByScore() { + List studentbyScore = new ArrayList<>(Arrays.asList(students)); + + Comparator byExamScores = Comparator.comparing(Student::getAverageExamScore); + Comparator byFullName = Comparator.comparing(Student::getFullName); + + studentbyScore.sort(byExamScores.reversed().thenComparing(byFullName)); + + return studentbyScore.toArray(new Student[0]); + } + + public Map getGradeBookMap() { + Student[] studentArray = students; + Map gradeBookAssigned = new HashMap<>(); + for (int i = 0; i < correctForNull(); i++) { + gradeBookAssigned.put(studentArray[i], assignGrade(studentArray[i])); + } + + return gradeBookAssigned; + } + + public void getGradeBook(){ + for (int i = 0; i < correctForNull(); i++) { + char grade = assignGrade(students[i]); + String name = students[i].getFullName(); + System.out.println(name + " -> " + grade); + + } + } + + public char assignGrade(Student student){ + double studentAvg = student.getAverageExamScore(); + double classAvg = getAverageExamScore(); + double deviation = getDeviation(); + + if (studentAvg >= classAvg + (2 * deviation)){ + return 'A'; + } else if (studentAvg >= classAvg + deviation) { + return 'B'; + } else if (studentAvg >= classAvg){ + return 'C'; + }else if (studentAvg >= classAvg - deviation){ + return 'D'; + } + return 'F'; + } + + public double getDeviation() { + double deviation = 0; + double classAverage = getAverageExamScore(); + for (Student s : students) { + if (s == null) { + deviation += 0; + } else { + double diff = classAverage - s.getAverageExamScore(); + deviation += Math.pow(diff, 2); + } + } + double squaredDev = deviation / correctForNull(); + return Math.sqrt(squaredDev); + + + } + + public int correctForNull() { + int studentCount = 0; + for (Student student : students) { + if (student != null) { + studentCount++; + } + } + return studentCount; + + } + } diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..d87349c 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.util.ArrayList; +import java.util.Arrays; + public class Student { + + private String firstName; + private String lastName; + ArrayList examScores; + + public Student(String givenFirstName, String givenLastName, Double...examScores) { + this.firstName = givenFirstName; + this.lastName = givenLastName; + this.examScores = new ArrayList<>(Arrays.asList(examScores)); + } + + 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 String getFullName (){ + return this.firstName + " " + this.lastName; + } + + public Integer getNumberOfExamsTaken(){ + return examScores.size(); + } + + public String getExamScores(){ + String output = "Exam Scores: \n"; + + for (int i = 0; i < examScores.size(); i++) { + output += "\t Exam " + (i + 1) + " -> "; + output += examScores.get(i); + output += "\n"; + + } + return output; + } + + public void addExamScore(double score){ + examScores.add(score); + } + + public void setExamScore(int testNumber, double newScore){ + examScores.remove(testNumber-1); + examScores.add(testNumber-1, newScore); + } + + public Double getAverageExamScore(){ + double total = 0; + for (double score: examScores) { + total += score; + } + Double avg = total/examScores.size(); + + return avg; + } + + @Override + public String toString(){ + return String.format("Student Name: %s %s\n" + "> Average Score: %.1f \n" + "> %s", firstName, lastName, getAverageExamScore(), getExamScores()); + } + + } diff --git a/src/test/java/io/zipcoder/ClassroomTest.java b/src/test/java/io/zipcoder/ClassroomTest.java index 7bac1ff..cedf1cc 100644 --- a/src/test/java/io/zipcoder/ClassroomTest.java +++ b/src/test/java/io/zipcoder/ClassroomTest.java @@ -1,4 +1,232 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.awt.geom.Arc2D; +import java.util.Arrays; + public class ClassroomTest { + + @Test + public void nullaryConstructorTest(){ + //given + int expectedEnrollment = 30; + + //when + Classroom classroom = new Classroom(); + int actual = classroom.getStudents().length; + + //then + Assert.assertEquals(expectedEnrollment, actual); + } + + @Test + public void maxNumberStudentTest() { + //given + int maxStudents = 25; + + //when + Classroom testClassroom = new Classroom(maxStudents); + int actual = testClassroom.getStudents().length; + + //then + Assert.assertEquals(maxStudents, actual); + } + + @Test + public void studentCollectionTest() { + //given + Double[] examScores = {85.0, 90.0, 95.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores); + Student student3 = new Student("Linda", "Jones", examScores); + Student[] students = {student1, student2, student3}; + + //when + Classroom testClassroom = new Classroom(students); + int actual = testClassroom.getStudents().length; + + //then + Assert.assertEquals(3, actual); + } + @Test + public void classAverageTest(){ + //given + Double[] examScores = {85.0, 90.0, 95.0}; + Double[] examScores2 ={80.0, 90.0, 100.0}; + Double[] examScores3 = {100.0, 100.0, 80.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores2); + Student student3 = new Student("Linda", "Jones", examScores3); + Student[] students = {student1, student2, student3}; + + //when + Classroom testClass = new Classroom(students); + double actual = testClass.getAverageExamScore(); + + //then + double expected = 91.11; + Assert.assertEquals(expected, actual, 0.01); + } + @Test + public void addStudentTest(){ + //given + int maxNumOfStudents = 1; + Classroom testClassroom = new Classroom(maxNumOfStudents); + Double[] examScores = {85.0, 90.0, 95.0}; + Student student1 = new Student("Jay", "Kay", examScores); + + //when + Student[] preEnrollment = testClassroom.getStudents(); + System.out.println("==========================="); + + String preEnrollmentAsString = Arrays.toString(preEnrollment); + System.out.println(preEnrollmentAsString); + + //then + testClassroom.addStudent(student1); + Student[] postEnrollment = testClassroom.getStudents(); + String postEnrollmentAsString = Arrays.toString(postEnrollment); + + + System.out.println("==========================="); + System.out.println(postEnrollmentAsString); + } + + @Test + public void removeStudentTest (){ + //given + Double[] examScores = {85.0, 90.0, 95.0}; + Double[] examScores2 ={80.0, 90.0, 100.0}; + Double[] examScores3 = {100.0, 100.0, 80.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores2); + Student student3 = new Student("Linda", "Jones", examScores3); + Student[] students = {student1, student2, student3}; + + //when + Classroom testRoom = new Classroom(); + testRoom.addStudent(student1); + testRoom.addStudent(student2); + testRoom.addStudent(student3); + testRoom.removeStudentbyName("Jay", "Kay"); + + //then + System.out.println(Arrays.toString(testRoom.getStudents())); + Student removedStudent = testRoom.getStudents()[2]; + Assert.assertNull(removedStudent); + + // Student actual = students[0]; +// Assert.assertNull(actual); + + } + + @Test + public void removeStudentTest2 () { + //given + Double[] examScores = {85.0, 90.0, 95.0}; + Double[] examScores2 = {80.0, 90.0, 100.0}; + Double[] examScores3 = {100.0, 100.0, 80.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores2); + Student student3 = new Student("Linda", "Jones", examScores3); + + + //when + Classroom testRoom = new Classroom(); + testRoom.addStudent(student1); + testRoom.addStudent(student2); + testRoom.addStudent(student3); + testRoom.removeStudentbyName("Shelly-Ann", "Frasier"); + + //then + System.out.println(testRoom.getStudents()[1]); //equals second student Linda Jones + Student removedStudent = testRoom.getStudents()[2]; + Assert.assertNull(removedStudent); + } + + @Test + public void sortStudentScoreTest () { + //given + Double[] examScores = {85.0, 90.0, 95.0}; + Double[] examScores2 = {80.0, 90.0, 100.0}; + Double[] examScores3 = {100.0, 100.0, 80.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores2); + Student student3 = new Student("Linda", "Jones", examScores3); + Student[] students = {student1, student2, student3}; + + //when + Classroom testRoom = new Classroom(students); + Student[] testSort = testRoom.getStudentByScore(); + + //then + System.out.println(Arrays.toString(testSort)); + } + + @Test + public void getGradesTest(){ + //given + Double[] examScores = {85.0, 90.0, 95.0}; + Double[] examScores2 = {80.0, 90.0, 100.0}; + Double[] examScores3 = {100.0, 100.0, 80.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores2); + Student student3 = new Student("Linda", "Jones", examScores3); + Student[] students = {student1, student2, student3}; + + //when + Classroom testRoom = new Classroom(students); + char grade = testRoom.assignGrade(student2); + + //then + System.out.println(grade); + + //System.out.println(testRoom.getGradeBook()); + + } + + @Test + public void getGradeBookTest() { + //given + Double[] examScores = {85.0, 70.0, 75.0}; + Double[] examScores2 = {90.0, 95.0, 100.0}; + Double[] examScores3 = {100.0, 100.0, 98.0}; + Double[] examScores4 = {60.0, 72.0, 65.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores2); + Student student3 = new Student("Linda", "Jones", examScores3); + Student student4 = new Student("Bud", "Dunce", examScores4); + Student[] students = {student1, student2, student3, student4}; + + //when + Classroom testRoom = new Classroom(students); + + + //then + testRoom.getGradeBook(); + + } + @Test + public void getGradeBookMapTest() { + //given + Double[] examScores = {85.0, 70.0, 75.0}; + Double[] examScores2 = {90.0, 95.0, 100.0}; + Double[] examScores3 = {100.0, 100.0, 98.0}; + Double[] examScores4 = {60.0, 72.0, 65.0}; + Student student1 = new Student("Jay", "Kay", examScores); + Student student2 = new Student("Shelly-Ann", "Frasier", examScores2); + Student student3 = new Student("Linda", "Jones", examScores3); + Student student4 = new Student("Bud", "Dunce", examScores4); + Student[] students = {student1, student2, student3, student4}; + + //when + Classroom testRoom = new Classroom(students); + char actualGrade = testRoom.getGradeBookMap().get(student4); + + //then + Assert.assertEquals('F', actualGrade); + + } } diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..f6e47ab 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,163 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class StudentTest { + @Test + public void getFirstNameTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + String actualFirstName = student.getFirstName(); + + //then + Assert.assertEquals(givenFirstName, actualFirstName); + } + + @Test + public void setFirstNameTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + String expected = "Jamie"; + + //when + student.setFirstName(expected); + String actualFirstName = student.getFirstName(); + + //then + Assert.assertEquals(expected, actualFirstName); + } + @Test + public void getLastNameTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + String actualLastName = student.getLastName(); + + //then + Assert.assertEquals(givenLastName, actualLastName); + } + + @Test + public void setLastNameTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + String expected = "Lewis"; + + //when + student.setLastName(expected); + String actualLastName = student.getLastName(); + + //then + Assert.assertEquals(expected, actualLastName); + } + + @Test + public void getNumOfExamsTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + int numberOfExams = student.getNumberOfExamsTaken(); + + //then + Assert.assertEquals(3, numberOfExams ); + } + + @Test + public void printScoresTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + String output = student.getExamScores(); + + //then + System.out.println(output); + } + + @Test + public void addScoresTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + student.addExamScore(100.0); + String output = student.getExamScores(); + + //then + System.out.println(output); + } + + @Test + public void setScoresTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + student.addExamScore(100.0); + student.setExamScore(1, 150.0); + String output = student.getExamScores(); + + //then + System.out.println(output); + } + + @Test + public void printAverageTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + double output = student.getAverageExamScore(); + + //then + System.out.printf("%.1f", output); + } + + @Test + public void overrideToStringTest(){ + //given + String givenFirstName = "Sean"; + String givenLastName = "John"; + Double[] examScores = {82.0, 81.0, 68.5}; + Student student = new Student(givenFirstName, givenLastName, examScores); + + //when + String expected = student.toString(); + + //then + System.out.println(expected); + } } \ 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..ef39cc2 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..9dab84a 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..6fdf6ed 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..038d4d3 Binary files /dev/null and b/target/test-classes/io/zipcoder/StudentTest.class differ