diff --git a/pom.xml b/pom.xml index 08123ed..6402c2c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,8 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.zipcoder - classroom + com.zipcodewilmington + loop_labs 1.0-SNAPSHOT diff --git a/src/main/java/io/zipcoder/Classroom.java b/src/main/java/io/zipcoder/Classroom.java index 64566f0..f82a55c 100644 --- a/src/main/java/io/zipcoder/Classroom.java +++ b/src/main/java/io/zipcoder/Classroom.java @@ -1,4 +1,99 @@ package io.zipcoder; +import com.sun.org.apache.xml.internal.security.encryption.AgreementMethod; + +import java.util.*; + public class Classroom { -} + // Declaring variables + public int maxNumOfStudents; + Student[] students = new Student[30]; + ArrayList studentsList = new ArrayList(); + + public Classroom(int maxNumOfStudents){ + this.maxNumOfStudents = maxNumOfStudents; + } + + public Classroom(Student[] students) { + this.students = students; + } + + public Classroom(){ + } + + public ArrayList getStudents(){ + return studentsList; + } + + public Double getAverageExamScore() { + Double totalExams = 0.0; + for (int i = 0; i < students.length; i++) { + totalExams += students[i].getAverageExamScore(); + } + return totalExams / students.length; + } + + public void addStudent(Student student1) { + studentsList.add(student1); + } + + public void removeStudent(Student firstName, Student lastName) { + for (int i = 0; i < studentsList.size(); i++) { + if(studentsList.get(i).firstName.equals(firstName) && studentsList.get(i).lastName.equals(lastName)){ + studentsList.remove(i); + } + students[students.length-1] = null; + break; + } + } + + public Student[] getStudentsByScore() { + Arrays.sort(students, Collections.reverseOrder()); + return students; + } + + public Map getGradeBook() { + Map gradeBook = new TreeMap<>(); + for (int i = 0; i < students.length; i++) { + if(students[i].getAverageExamScore() >= 90){ + gradeBook.put(students[i].firstName, "A"); + } + else if(students[i].getAverageExamScore() >= 80) { + gradeBook.put(students[i].firstName, "B"); + } + else if(students[i].getAverageExamScore() >= 70) { + gradeBook.put(students[i].firstName, "C"); + } + else if(students[i].getAverageExamScore() >= 60) { + gradeBook.put(students[i].firstName, "D"); + } + else {gradeBook.put(students[i].firstName, "F"); + } + + } + for (Map.Entry j: gradeBook.entrySet()){ + System.out.println(j); + } + return gradeBook; + } + + + + + + + + + + + + + + + + + + + + + } diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..f75d4fd 100644 --- a/src/main/java/io/zipcoder/Student.java +++ b/src/main/java/io/zipcoder/Student.java @@ -1,4 +1,88 @@ package io.zipcoder; -public class Student { +import java.util.ArrayList; +import java.util.Arrays; + +// this is a blueprint in case we want multiple students +public class Student implements Comparable{ + // declaring variables + String firstName; + String lastName; + ArrayList examScores; + + public Student(String firstName, String lastName, ArrayList examScores){ + // assigning parameter variables to the object "Student" in line 13 + this.firstName = firstName; + this.lastName = lastName; + this.examScores = examScores; + } + + public Student(String firstName, String lastName, Double[] testScores){ + this.firstName = firstName; + this.lastName = lastName; + // this converts an array to an arrayList + this.examScores = new ArrayList<>(Arrays.asList(testScores)); + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int 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) + " -> " + Math.round(examScores.get(i)); + // if i is not equal to the index of last element than make a new line, + // otherwise if its the last index dont go to the next line. + if (i != examScores.size()-1) { + output += "\n"; + } + } + return output; + } + + + public void addExamScore(double newScore) { + examScores.add(newScore); + } + + public void setExamScore(int examNum, double newScoreOnExam){ + examScores.set(examNum, newScoreOnExam); + } + + public Double getAverageExamScore() { + Double totalExams = 0.0; + for (int i = 0; i < getNumberOfExamsTaken(); i++) { + totalExams += examScores.get(i); + } + return totalExams / getNumberOfExamsTaken(); + } + + @Override + public String toString(){ + return "Student Name: " + getFirstName() + " " + getLastName() + "\n" + + "> Average Score: " + getAverageExamScore() + "\n>" + getExamScores(); + } + + + @Override + public int compareTo(Student o) { + return Double.compare(this.getAverageExamScore(), o.getAverageExamScore()); + } } diff --git a/src/test/java/io/zipcoder/ClassroomTest.java b/src/test/java/io/zipcoder/ClassroomTest.java index 7bac1ff..7570df7 100644 --- a/src/test/java/io/zipcoder/ClassroomTest.java +++ b/src/test/java/io/zipcoder/ClassroomTest.java @@ -1,4 +1,179 @@ package io.zipcoder; +import com.sun.xml.internal.fastinfoset.tools.XML_SAX_StAX_FI; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + public class ClassroomTest { + + @Test + public void getStudentsTest() { + int maxNumOfStudents = 4; + Classroom class1 = new Classroom(maxNumOfStudents); + ArrayList s1Scores = new ArrayList<>(Arrays.asList(100.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(125.0)); + ArrayList s3Scores = new ArrayList<>(Arrays.asList(150.0)); + ArrayList s4Scores = new ArrayList<>(Arrays.asList(175.0)); + + Student s11 = new Student("student", "one", s1Scores); + Student s12 = new Student("student", "two", s2Scores); + Student s13 = new Student("student", "three", s3Scores); + Student s14 = new Student("student", "four", s4Scores); + + ArrayList expectedStudents = new ArrayList(Arrays.asList(s11, s12, s13, s14)); + class1.studentsList = expectedStudents; + ArrayList actual = class1.getStudents(); + ArrayList expected = expectedStudents; + + Assert.assertEquals(expected, actual); + + } + + @Test + public void getAverageExamScore() { + int maxNumOfStudents = 2; + // : 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); + + Double expected = 125.0; + + // When + Double output = classroom.getAverageExamScore(); + + // Then + Assert.assertEquals(expected, output); + } + + @Test + public void addStudentTest() { + // : Given + int maxNumberOfStudents = 1; + Classroom classroom = new Classroom(maxNumberOfStudents); + Double[] examScores = { 100.0, 150.0, 250.0, 0.0 }; + Student s1 = new Student("Leon", "Hunter", examScores); + + String expected = " ===========================\n" + + " []\n" + + " ===========================\n" + + " [Student Name: Leon Hunter\n" + + " > Average Score: 125\n" + + " > Exam Scores:\n" + + " Exam 1 -> 100\n" + + " Exam 2 -> 150\n" + + " Exam 3 -> 250\n" + + " Exam 4 -> 0"; + + // When + ArrayList preEnrollment = classroom.getStudents(); + classroom.addStudent(s1); + ArrayList postEnrollment = classroom.getStudents(); + + // Then + String preEnrollmentAsString = preEnrollment.toString(); + String postEnrollmentAsString = postEnrollment.toString(); + + System.out.println("==========================="); + System.out.println(preEnrollmentAsString); + System.out.println("==========================="); + System.out.println(postEnrollmentAsString); + + } + + @Test + public void removeStudentTest() { + // : Given + int maxNumberOfStudents = 1; + Classroom classroom = new Classroom(maxNumberOfStudents); + Double[] examScores = { 100.0, 150.0, 250.0, 0.0 }; + Student s1 = new Student("Leon", "Hunter", examScores); + classroom.addStudent(s1); + + int actual = classroom.getStudents().size(); + + // When + ArrayList preEnrollment = classroom.getStudents(); + classroom.removeStudent(s1, s1); + ArrayList postEnrollment = classroom.getStudents(); + + + Assert.assertEquals(maxNumberOfStudents, actual); + System.out.println(maxNumberOfStudents); + + } + + @Test + public void getStudentsByScoreTest() { + + // : Given + ArrayList s1Scores = new ArrayList<>(Arrays.asList(100.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(125.0)); + ArrayList s3Scores = new ArrayList<>(Arrays.asList(150.0)); + ArrayList s4Scores = new ArrayList<>(Arrays.asList(175.0)); + + Student s1 = new Student("student", "one", s1Scores); + Student s2 = new Student("student", "two", s2Scores); + Student s3 = new Student("student", "three", s3Scores); + Student s4 = new Student("student", "four", s4Scores); + + Student[] expectedStudents = {s2, s3, s4, s1}; + Classroom expectedClassroom = new Classroom(expectedStudents); + + // When + Student[] actualStudents = {s3, s4, s1, s2}; + Classroom actualClassroom = new Classroom(actualStudents); + actualClassroom.getStudentsByScore(); + + // When + Assert.assertEquals(actualClassroom.getStudents(), expectedClassroom.getStudents()); + + } + @Test + public void getGradeBookTest() { + + // : Given + ArrayList s1Scores = new ArrayList<>(Arrays.asList(90.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(85.0)); + ArrayList s3Scores = new ArrayList<>(Arrays.asList(77.0)); + ArrayList s4Scores = new ArrayList<>(Arrays.asList(65.0)); + + Student s1 = new Student("student", "one", s1Scores); + Student s2 = new Student("student", "two", s2Scores); + Student s3 = new Student("student", "three", s3Scores); + Student s4 = new Student("student", "four", s4Scores); + + Student[] expectedStudents = {s2, s3, s4, s1}; + Classroom expectedClassroom = new Classroom(expectedStudents); + + // When + Student[] actualStudents = {s3, s4, s1, s2}; + Classroom actualClassroom = new Classroom(actualStudents); + actualClassroom.getStudentsByScore(); + + // When + Assert.assertEquals(actualClassroom.getStudents(), expectedClassroom.getStudents()); + + } + + + + + + + + + + + + + } diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..ccfda66 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,148 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; + public class StudentTest { + @Test + public void StudentTest() { + String expectedFirstName = "Johnny"; + String expectedLastName = "Tokhi"; + ArrayList scores = new ArrayList<>(Arrays.asList(100.0, 90.0)); + + Student actualStudent = new Student(expectedFirstName, expectedLastName, scores); + + Assert.assertEquals(expectedFirstName, actualStudent.getFirstName()); + Assert.assertEquals(expectedLastName, actualStudent.getLastName()); + } + + @Test + public void StudentTest2() { + String expectedFirstName = "Johnny"; + String expectedLastName = "Tokhi"; + Double[] scores = {10.0, 20.0}; + + Student actualStudent = new Student(expectedFirstName, expectedLastName, scores); + + Assert.assertEquals(expectedFirstName, actualStudent.getFirstName()); + Assert.assertEquals(expectedLastName, actualStudent.getLastName()); + } + + @Test + public void numberOfExamsTakenTest() { + String expectedFirstName = "Johnny"; + String expectedLastName = "Tokhi"; + ArrayList scores = new ArrayList<>(Arrays.asList(100.0, 90.0)); + + Student actualStudent = new Student(expectedFirstName, expectedLastName, scores); + + Assert.assertEquals(2, actualStudent.getNumberOfExamsTaken()); + } + + @Test //this test copied in from README + public void getExamScoresTest() { + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = {100.0, 95.0, 123.0, 96.0}; + Student student = new Student(firstName, lastName, examScores); + // added this for the test + String expected = " Exam Scores:\n" + + " \tExam 1 -> 100\n" + + " \tExam 2 -> 95\n" + + " \tExam 3 -> 123\n" + + " \tExam 4 -> 96"; + + // When + String output = student.getExamScores(); + + // Then //Only made assert line + Assert.assertEquals(expected, output); + } + + @Test //this test copied in from README + public void addExamScoreTest(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { }; + Student student = new Student(firstName, lastName, examScores); + // added this for the test + String expected = " Exam Scores:\n" + + " \tExam 1 -> 100"; + + // When + student.addExamScore(100.0); + String output = student.getExamScores(); + + // Then + Assert.assertEquals(expected, output); + + } + + @Test //this test copied in from README + public void setExamScoreTest(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + Double[] examScores = { 100.0 }; + Student student = new Student(firstName, lastName, examScores); + // added expected for the test + String expected = " Exam Scores:\n" + + " \tExam 1 -> 150"; + + // When + student.setExamScore(0, 150.0); + String output = student.getExamScores(); + + // Then // Only made assert line + Assert.assertEquals(expected, output); + } + + @Test //this test copied in from README + public void getAverageExamScoreTest(){ + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + ArrayList examScores = new ArrayList<>(Arrays.asList( 100.0, 150.0, 250.0, 0.0 )); + Student student = new Student(firstName, lastName, examScores); + Double expected = 125.0; + + // When + Double output = student.getAverageExamScore(); + + // Then // Only made assert line + Assert.assertEquals(expected, output); + + } + + @Test + public void toStringTest() { + // : Given + String firstName = "Leon"; + String lastName = "Hunter"; + ArrayList examScores = new ArrayList<>(Arrays.asList( 100.0, 150.0, 250.0, 0.0 )); + Student student = new Student(firstName, lastName, examScores); + // added this for the test + String expected = "Student Name: Leon Hunter\n" + + "> Average Score: 125.0\n" + + "> Exam Scores:\n" + + " \tExam 1 -> 100\n" + + " \tExam 2 -> 150\n" + + " \tExam 3 -> 250\n" + + " \tExam 4 -> 0"; + + // When + String actual = student.toString(); + + // 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..56d3d7b 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..cabf33f 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..cbdb279 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..5c5eea5 Binary files /dev/null and b/target/test-classes/io/zipcoder/StudentTest.class differ