diff --git a/src/main/java/io/zipcoder/Classroom.java b/src/main/java/io/zipcoder/Classroom.java index 64566f0..520f73a 100644 --- a/src/main/java/io/zipcoder/Classroom.java +++ b/src/main/java/io/zipcoder/Classroom.java @@ -1,4 +1,107 @@ package io.zipcoder; -public class Classroom { +import java.util.*; +import java.util.function.ToLongFunction; + +public class Classroom implements Comparator { + private Student[] students; + private Integer maxNumberOfStudents; + + //constructor 1: int representative of the maxNumberOfStudents that this Classroom can hold. + public Classroom(Integer maxNumberOfStudents) { + this.students = new Student[maxNumberOfStudents]; + } + //constructor 2 Student[] representative of the collection of Student objects this Classroom will store. + public Classroom(Student[] students) { + this.students = students; + } + //constructor 3: nullary. initializes the composite students object o + //be an empty array of 30 Student objects + public Classroom() { + this.students = new Student[30]; + } + //getter return the composit students object + + public String getStudents() { + StringBuilder builder = new StringBuilder(); + for (Student s : students) { + builder.append("===================\n"); + try { + builder.append(s.toString()); + } catch (Exception e) { + builder.append("No Students Currently Enrolled\n"); + } + builder.append("===================\n\n"); + } + return builder.toString(); + } + //getter: returns sum of all exams divided by the number of students + public Double getAverageExamScore() { + double sum = 0.0; + for (Student s : students) { + if (s != null) { + sum += s.getAverageExamScore(); + } + } + return sum / students.length; + } + //addStudent student uses Student parameter to add a Student object to students list + public void addStudent(Student student) { + List studentsList = new ArrayList<>(Arrays.asList(students)); + studentsList.add(student); + studentsList.remove(null); + students = studentsList.toArray(new Student[0]); + } + + //removes student from students object. array is re-ordered after removed. null at end + public void removeStudent(String firstName, String lastName) { + List studentList = new ArrayList<>(Arrays.asList(students)); + for (Student s : studentList) { + if (s.getFirstName().equals(firstName) && s.getLastName().equals(lastName)) { + studentList.remove(s); + break; + } + } + studentList.sort(this); + students = studentList.toArray(new Student[0]); + } + + //can't mdify arrays.asList so wrapper ArrayList + public ArrayList getStudentByScore() { + ArrayList studentList = new ArrayList<>(Arrays.asList(students)); + studentList.sort(this); + return studentList; + } + + //getStudentByScore returns and array representation of Student object sorted in descending score order. + //if two students same score, order them lexigraphically + public String getGradeBook() { + Map> gradeBook = new TreeMap<>(); + ArrayList sortedStudentList = getStudentByScore(); + Integer binSize = sortedStudentList.size() / 5; + gradeBook.put("A", new ArrayList<>(sortedStudentList.subList(0, binSize))); + gradeBook.put("B", new ArrayList<>(sortedStudentList.subList(binSize, binSize * 2))); + gradeBook.put("C", new ArrayList<>(sortedStudentList.subList(binSize * 2, binSize * 3))); + gradeBook.put("D", new ArrayList<>(sortedStudentList.subList(binSize * 3, binSize * 4))); + gradeBook.put("F", new ArrayList<>(sortedStudentList.subList(binSize * 4, binSize * 5))); + + StringBuilder builder = new StringBuilder(); + for (Map.Entry entry : gradeBook.entrySet()) { + builder.append("" + entry.getKey() + " Students: \n\n\n" + entry.getValue().toString() + "\n"); + } + return builder.toString(); + } + + @Override + public int compare(Student o1, Student o2) { + return 0; + } + + @Override + public Comparator thenComparingLong(ToLongFunction keyExtractor) { + return null; + } } + + + diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..07b86e5 100644 --- a/src/main/java/io/zipcoder/Student.java +++ b/src/main/java/io/zipcoder/Student.java @@ -1,4 +1,85 @@ package io.zipcoder; +import java.util.Arrays; +import java.util.ArrayList; -public class Student { +public class Student { + private String firstName; + private String lastName; + private ArrayList examScores; + + public Student(String firstName, String lastName, Double[] examScores) { + this.firstName = firstName; + this.lastName = lastName; + this.examScores = new ArrayList<>(Arrays.asList(examScores)); + } + + public Student() { + this.firstName = "First"; + this.lastName = "Last"; + this.examScores = null; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public ArrayList getExamScores() { + return examScores; + } + + //public Double getExamScoresAsString() { + // Student student = new Student();; + // return getScoresAsString(input); + // } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Integer getNumberOfExamsTaken() { + return this.getExamScores().size(); + } + + public void addExamScore(double examScore) { + this.examScores.add(examScore); + } + + public void setExamScores(int examNumber, double newScore) { + examScores.remove(examNumber - 1); + examScores.add(examNumber - 1, newScore); + } + + public double getAverageExamScore() { + Double sum = 0.0; + for (int i = 0; i < examScores.size(); i++) { + sum += examScores.get(i); + } + return sum / getNumberOfExamsTaken(); + + } + + public String toString() { + String output = "Student Name: " + firstName + " " + lastName + "\n> Average Score: " + getAverageExamScore() + "\n> Exam Scores:\n"; + return getScoresAsString(output); + } + + private String getScoresAsString(String output) { + for (int i = 0; i < examScores.size(); i++) { + output += "\tExam " + (i + 1) + " -> " + Math.round(examScores.get(i)) + "\n"; + } + + return output; + } } + + + + diff --git a/src/test/java/io/zipcoder/ClassroomTest.java b/src/test/java/io/zipcoder/ClassroomTest.java index 7bac1ff..3f338ad 100644 --- a/src/test/java/io/zipcoder/ClassroomTest.java +++ b/src/test/java/io/zipcoder/ClassroomTest.java @@ -1,4 +1,20 @@ package io.zipcoder; +import org.junit.Test; + public class ClassroomTest { + + @Test + public void gtStudentsTest() { + //given + Double[] examScores = {}; + Student student1 = new Student("Cameron", "Howard", examScores); + Student student2 = new Student("April", "Howard", examScores); + Student[] students = {student1, student2}; +Classroom classroom = new Classroom(students); + //when + String output = classroom.getStudents(); + //then + System.out.println(output); + } } diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..8c8f69a 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,136 @@ package io.zipcoder; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; + public class StudentTest { + String expectedFirstName = ""; + String expectedLastName = ""; + Double[] examScores = {}; + Student student = new Student(); + + + @Before + public void setUp() throws Exception { + String expectedFirstName = ""; + String expectedLastName = ""; + Double[] examScores = {}; + Student student = new Student(); + } + + // @After + //public void tearDown() throws Exception { + // return null; + // } + + @Test + public void setFirstNameTest() { + //Given + Student student = new Student(); + String expected = "April"; + //When + student.setFirstName(expected); + String actual = student.getFirstName(); + //Then + Assert.assertEquals(expected, actual); + System.out.println(expected); + System.out.println(actual); + } + + @Test + public void getFirstName() { + //Given + String expected = "Jill"; + //When + student.getFirstName(); + String actual = student.getFirstName(); + //Then + Assert.assertEquals(expected, actual); + System.out.println(expected); + System.out.println(actual); + } + + + @Test + public void getLastName() { + Student student = new Student(); + String expected = "Jordan"; + + // When + student.getLastName(); + String actual = student.getLastName(); + + // Then + assertEquals(expected, actual); + } + + + @Test + public void setLastName() { + //Given + Student student = new Student(); + String expected = "April"; + //When + student.setLastName(expected); + String actual = student.getLastName(); + //Then + Assert.assertEquals(expected, actual); + System.out.println(expected); + System.out.println(actual); + } + + @Test + public void getExamScores() { + //Given + Student student = new Student(); + Double expected = 55.00; + //When + student.getExamScores(); + ArrayList actual = student.getExamScores(); + //Then + Assert.assertEquals(expected, actual); + System.out.println(expected); + System.out.println(actual); + } + + @Test + public void addExamScore() { + //Give + String firstName = "Geg"; + String lastName = "Geggy"; + Double[] examScores = {100.}; + Student student = new Student(); + + addExamScore(); + + ArrayList actual = student.getExamScores(); + Assert.assertEquals("Exam: ", actual); + } + + @Test + public void setExamScores() { + Student student = new Student(); + student.setExamScores(1, 100.); + ArrayList actual = student.getExamScores(); + Assert.assertEquals("Exam 1: 100.00", actual); + } + + @Test + public void getAverageExamScore() { + Student student = new Student(); + Double[] actual = {100., 75., 95., 100.}; + double expected = 92.5; + 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..e3204d5 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..f9f9515 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..c19cae1 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..c7adbd6 Binary files /dev/null and b/target/test-classes/io/zipcoder/StudentTest.class differ