diff --git a/src/main/java/Classroom.java b/src/main/java/Classroom.java new file mode 100644 index 0000000..8250de1 --- /dev/null +++ b/src/main/java/Classroom.java @@ -0,0 +1,141 @@ +import io.zipcoder.Student; + +//import com.google.common.collect; +import java.util.*; + +public class Classroom { + + //instance variables + private ArrayList students; + private Integer maxNumberOfStudents; + private static Map limits; + + + + //Nullary Constructor + public Classroom() { + students = new ArrayList(30); + this.students = students; + } + + //Constructor with int maxNumberOfStudents + public Classroom(Integer maxNumberOfStudents) { + this.maxNumberOfStudents = maxNumberOfStudents; + students = new ArrayList<>(maxNumberOfStudents); + } + + //Constructor with the students representative of the collection of student objects this classroom will store. + public Classroom(ArrayList students) { + this.students = students; + } + + + //Getters + public ArrayList getStudents() { + return students; + } + + public Integer getMaxNumOfStudents() { + return maxNumberOfStudents; + } + + public Double getAverageExamScore() { + Double totalExamScores = 0.0; + int numOfStudents = students.size(); + + for (int i = 0; i < students.size(); i++) { + totalExamScores += students.get(i).getAverageExamScore(); + } + return totalExamScores / numOfStudents; + } + + public void addStudent (Student studentToAdd){ + //StringBuilder sb = new StringBuilder(); + students.add(studentToAdd); +// sb.append("\n========================================\n"); +// sb.append(studentToAdd.printToString()); +// sb.append("========================================"); + //return sb.toString(); + } + + public void removeStudent (String firstName, String lastName){ + for (int i = 0; i < students.size(); i++) { + if(students.get(i).getFirstName().equals(firstName) && students.get(i).getLastName().equals(lastName)){ + students.remove(i); + } + } + } + public ArrayList getStudentsByScore () { + Collections.sort(students); + return students; + } +//Tried creating some maps and things to sort the student data. Ended up using a custom comparator to sor the student list + +// for (int i = 0; i < students.size(); i++) { +// students.get(i).getAverageExamScore(); +// students.sort(Comparator.naturalOrder()); +// HashMap studentDoubleHashMap = new HashMap<>(); +// for (int i = 0; i < students.size(); i++) { +// studentDoubleHashMap.put(students.get(i), students.get(i).getAverageExamScore()); +// } + + public Map getGradeBook (){ + //Integer numOfStudentsInClass = students.size(); + String letterGrade = "I"; + //orders students by score + getStudentsByScore(); + //Limits for the tree map +// static { +// limits = new TreeMap<>(); +// limits.put(0.0 , "F"); +// limits.put(students.size()*0.11 , "D"); +// limits.put(students.size()*0.31, "C"); +// limits.put(students.size()*0.51, "B"); +// limits.put(students.size()*0.71, "B"); +// limits.put(students.size()* 0.9, "A"); + + TreeMap gradeMap = new TreeMap<>(); + +// gradeMap.put(90.0, "A"); +// gradeMap.put(71.0, "B"); +// gradeMap.put(51.0, "C"); +// gradeMap.put(11.0, "D"); +// gradeMap.put( 0.0, "F"); + + for (int i = 0; i < students.size(); i++ ){ + + if ( students.get(i).getAverageExamScore() >= 90){ + letterGrade = " A";} + else if (students.get(i).getAverageExamScore() <= 90 && students.get(i).getAverageExamScore() >= 71){ + letterGrade = " B";} + else if (students.get(i).getAverageExamScore() <= 70 && students.get(i).getAverageExamScore() >= 50){ + letterGrade= " C";} + else if (students.get(i).getAverageExamScore() <= 90 && students.get(i).getAverageExamScore() >= 71){ + letterGrade = " D";} + else { + letterGrade = " F";} + gradeMap.put(students.get(i), letterGrade); + } + return gradeMap; + } + + } + + // +//+ " " + letterGrade + +// students.get(i).getFirstName() + " " + students.get(i).getLastName()); +// RangeMap gradeLetter = ImmutableRangeMap.builder() +// .put(Range.closed(90, 100), "A") +// .put(Range.closed(60, 89), "B") +// // ... +// .build(); +// +// public String getGrade(String studentName) { +// int averageScore = getAverageExamGrade(studentName); +// return gradeLetter.get(averageScore); +// } + + + + diff --git a/src/main/java/io/zipcoder/Student.java b/src/main/java/io/zipcoder/Student.java index b543e36..793c85b 100644 --- a/src/main/java/io/zipcoder/Student.java +++ b/src/main/java/io/zipcoder/Student.java @@ -1,4 +1,125 @@ package io.zipcoder; -public class Student { -} +//import com.sun.javafx.binding.StringFormatter; + + + +import java.lang.instrument.ClassDefinition; +import java.lang.reflect.Array; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.logging.Logger; + +public class Student implements Comparable { + + //***Instance Fields*** + private String firstName; + private String lastName; + private ArrayList examScores; + +//constructor method + + public Student(String firstName, String lastName, ArrayList examScores) { + this.firstName = firstName; + this.lastName = lastName; + this.examScores = examScores; + +//ArrayList examScores = new ArrayList<>(); +// Collections.addAll(examScores, testScores); + } + +//Getters and setters + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + + public String getFirstName() { + return this.firstName; + } + + public String getLastName() { + return this.lastName; + } + + public Integer getNumOfExamsTaken() { + return examScores.size(); + } + + public ArrayList getUnformatttedExamScores(){ + return examScores; + } + +//get exam scores method + + public String getExamScores() { + + StringBuilder sb = new StringBuilder(); + sb.append("Exam Scores:\n"); + for (int i = 0; i < examScores.size(); i++){ + sb.append(String.format("\tExam %d -> %.1f\n" , i+1, examScores.get(i))); + } + + return sb.toString(); + } + + public String addExamScore(Double examScore) + { + examScores.add(examScore); + //StringBuilder sb = new StringBuilder(); + return getExamScores(); + } + + public String setExamScore(Integer examNumber, Double newScore){ + examScores.set(examNumber-1, newScore); + return getExamScores(); + } + + public Double getAverageExamScore (){ + Double sum = 0.0; + Double result = 0.0; + Double numOfScores = getNumOfExamsTaken()*1.0; + +// double sum = 0; +// double numOfScores = examScores.size(); +// double result = 0; + + for (int i = 0; i < examScores.size(); i++) { + sum += examScores.get(i); + } + result = (sum / numOfScores); + DecimalFormat df = new DecimalFormat("#.#"); + + return Double.parseDouble(df.format(result)); + + } + public String printToString(){ + StringBuilder sb= new StringBuilder(); + sb.append(String.format("Student Name: %s %s\n", firstName, lastName)); + sb.append(String.format("Average Score: %.1f\n", getAverageExamScore())); + sb.append(getExamScores()); + return sb.toString(); + } + + + @Override + public int compareTo(Student other) { + //Sort by score in reverse order (high to low) + if(this.getAverageExamScore().compareTo(other.getAverageExamScore()) != 0) {return -1 * this.getAverageExamScore().compareTo(other.getAverageExamScore());} + //if scores are the same, use last name from a to z as the next check + else if (lastName.compareTo(other.lastName) != 0) {return lastName.compareTo(other.lastName);} + //if the last names are the same, use first name from a to z + else {return firstName.compareTo(other.firstName);} + } + + + +} \ No newline at end of file diff --git a/src/test/java/ClassRoomTest.java b/src/test/java/ClassRoomTest.java new file mode 100644 index 0000000..8efb820 --- /dev/null +++ b/src/test/java/ClassRoomTest.java @@ -0,0 +1,247 @@ +import io.zipcoder.Student; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.*; + +public class ClassRoomTest { + + @Test + public void nullaryConstructorTest() { + // Given + ArrayList expectedStudents = new ArrayList(30); + + // When + Classroom testClass = new Classroom(); + + // Then + ArrayList actualStudents = testClass.getStudents(); + + Assert.assertEquals(expectedStudents, actualStudents); + } + + + @Test + public void maxStudentsConstructorTest() { + // Given + Integer expected = 25; + + // When + Classroom testClass = new Classroom(expected); + + // Then + Integer actual = testClass.getMaxNumOfStudents(); + Assert.assertEquals(expected, actual); + + } + + @Test + public void getStudentsTest() { + ArrayList examScoresTest = new ArrayList(Arrays.asList(85D, 75D, 90D)); + ArrayList examScoresTest2 = new ArrayList(Arrays.asList(88D, 90D, 92D)); + ArrayList examScoresTest3 = new ArrayList(Arrays.asList(88D, 90D, 92D)); + + Student testStudent1 = new Student("Peter", "Moogie", examScoresTest); + Student testStudent2 = new Student("Pope", "John Paul", examScoresTest2); + Student testStudent3 = new Student("May", "West", examScoresTest3); + + ArrayList expectedStudents = new ArrayList(Arrays.asList(testStudent1, testStudent2, testStudent3)); + + Classroom testClass = new Classroom(expectedStudents); + + ArrayList actual = testClass.getStudents(); + + Assert.assertEquals(expectedStudents, actual); + + + } + + @Test + public void studentObjectsConstructorTest() { + // Given + ArrayList examScoresTest = new ArrayList(Arrays.asList(85D, 75D, 90D)); + ArrayList examScoresTest2 = new ArrayList(Arrays.asList(88D, 90D, 92D)); + ArrayList examScoresTest3 = new ArrayList(Arrays.asList(88D, 90D, 92D)); + + Student testStudent1 = new Student("Peter", "Moogie", examScoresTest); + Student testStudent2 = new Student("Pope", "John Paul", examScoresTest2); + Student testStudent3 = new Student("May", "West", examScoresTest3); + + ArrayList expectedStudents = new ArrayList(Arrays.asList(testStudent1, testStudent2, testStudent3)); + + // When + Classroom testClass = new Classroom(expectedStudents); + + // Then + ArrayList actualStudents = testClass.getStudents(); + + Assert.assertEquals(expectedStudents, actualStudents); + } + + @Test + public void getAverageExamScoreTest() { + // : Given + ArrayList s1Scores = new ArrayList(Arrays.asList(100.0, 150.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(225.0, 25.0)); + + Student testStudent1 = new Student("student", "one", s1Scores); + Student testStudent2 = new Student("student", "two", s2Scores); + + ArrayList testStudents = new ArrayList(Arrays.asList(testStudent1, testStudent2)); + Classroom classroom = new Classroom(testStudents); + + // When + Double output = classroom.getAverageExamScore(); + + // Then + System.out.println(output); + + } + + @Test + public void addStudentTest() { + + ArrayList s1Scores = new ArrayList(Arrays.asList(100.0, 150.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(225.0, 25.0)); + + Student testStudent1 = new Student("Petey", "Pablo", s1Scores); + Student testStudent2 = new Student("Lil", "Jon", s2Scores); + + ArrayList testStudents = new ArrayList(Arrays.asList(testStudent1)); + Classroom classroom = new Classroom(testStudents); + + classroom.addStudent(testStudent2); + Student actual = classroom.getStudents().get(1); + Student expected = testStudent2; + //System.out.println(actual.printToString()); + Assert.assertEquals(expected, actual); + } + + @Test + public void removeStudentTest() { + ArrayList s1Scores = new ArrayList(Arrays.asList(100.0, 150.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(225.0, 25.0)); + + Student testStudent1 = new Student("Petey", "Pablo", s1Scores); + Student testStudent2 = new Student("Lil", "Jon", s2Scores); + + ArrayList testStudents = new ArrayList(Arrays.asList(testStudent1, testStudent2)); + + Classroom classroom = new Classroom(testStudents); + classroom.removeStudent("Petey", "Pablo"); + ArrayList expected = new ArrayList<>(Arrays.asList(testStudent2)); + ArrayList actual = classroom.getStudents(); + //System.out.println(actual); + Assert.assertEquals(expected, actual); + } + + @Test + public void getStudentsByScoreTest() { + ArrayList s1Scores = new ArrayList(Arrays.asList(10.0, 150.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(225.0, 25.0)); + ArrayList s3scores = new ArrayList<>(Arrays.asList(75.0, 125.0)); + + Student testStudent1 = new Student("Petey", "Pablo", s1Scores); + Student testStudent2 = new Student("Lil", "Jon", s2Scores); + Student testStudent3 = new Student("Anne", "Buttigeg", s3scores); + + ArrayList testStudents = new ArrayList(Arrays.asList(testStudent1, testStudent2, testStudent3)); + + ArrayList expected = new ArrayList<>(Arrays.asList(testStudent2, testStudent3, testStudent1)); + + Classroom classroom = new Classroom(testStudents); + + ArrayList actual = classroom.getStudentsByScore(); + +// Iterator myStudents = testStudents.iterator(); +// while (myStudents.hasNext()){ +// System.out.println(myStudents.next().printToString()); +// } + + Assert.assertEquals(expected, actual); + + + } + + @Test + public void getStudentsByScoreTest2() { + ArrayList s1Scores = new ArrayList(Arrays.asList(50.0, 150.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(120.0, 80.0)); + ArrayList s3scores = new ArrayList<>(Arrays.asList(200.0, 0.0)); + + Student testStudent1 = new Student("Petey", "Pablo", s1Scores); + Student testStudent2 = new Student("Lil", "Jon", s2Scores); + Student testStudent3 = new Student("Paul", "Bacchas", s3scores); + + ArrayList expected = new ArrayList<>(Arrays.asList(testStudent2, testStudent3, testStudent1)); + + ArrayList testStudents = new ArrayList(Arrays.asList(testStudent2, testStudent1, testStudent3)); + + Classroom classroom = new Classroom(testStudents); + ArrayList actual = classroom.getStudentsByScore(); + Assert.assertEquals(expected, actual); + + } + + @Test + public void getGradeBooKTest (){ + ArrayList s1Scores = new ArrayList(Arrays.asList(10.0, 150.0)); + ArrayList s2Scores = new ArrayList<>(Arrays.asList(70.0, 50.0)); + ArrayList s3scores = new ArrayList<>(Arrays.asList(95.0, 95.0)); + + Student testStudent1 = new Student("Petey", "Pablo", s1Scores); + Student testStudent2 = new Student("Lil", "Jon", s2Scores); + Student testStudent3 = new Student("Anne", "Buttigeg", s3scores); + + ArrayList testStudents = new ArrayList(Arrays.asList(testStudent1, testStudent2, testStudent3)); + + //TreeMap expected = new TreeMap(testStudent2, testStudent3, testStudent1); + + Classroom classroom = new Classroom(testStudents); + + TreeMap expected = new TreeMap(); + expected.put(testStudent3, " A"); + expected.put(testStudent1, " B"); + expected.put(testStudent2, " C"); + + Map actual = classroom.getGradeBook(); + + Assert.assertEquals(expected,actual); + +// for (Map.Entry entry : actual.entrySet()) { +// System.out.println("Key: " + entry.getKey().printToString() + ". Value: " + entry.getValue()); + } +// for (Entry entry : tree.entrySet()) { +// String key = entry.getKey(); +// String value = entry.getValue(); +// +// System.out.printf("%s : %s\n", key, value); +// } + + +// for(Map.Entry entry : selects.entrySet()) { +// String key = entry.getKey(); +// HashMap value = entry.getValue(); + + //Collection myStuff = actual.entrySet(); + //Spliterator sit = myStuff.entrySet().spliterator(); + //System.out.println(actual); + //actual.forEach((k,v)) hm.forEach((k,v) -> System.out.println("key: "+k+" value:"+v));; + //Iterator itr = actual.iterator(); + + //Iterator<> myStudents = testStudents.iterator(); + //while (actual.hasNext()){ + //System.out.println(actual.next().printToString()); + +// + } + + + + + + + + + diff --git a/src/test/java/io/zipcoder/StudentTest.java b/src/test/java/io/zipcoder/StudentTest.java index a9fedec..2b7817e 100644 --- a/src/test/java/io/zipcoder/StudentTest.java +++ b/src/test/java/io/zipcoder/StudentTest.java @@ -1,5 +1,171 @@ 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 testConstructor() { + // Given + String expectedFirstName = "David"; + String expectedLastName = "Trombello"; + ArrayList expectedScores = new ArrayList(Arrays.asList(67D, 98D)); + + // When + Student testStudent = new Student (expectedFirstName, expectedLastName, expectedScores); + + // Then + String actualFirstName = testStudent.getFirstName(); + String actualLastName = testStudent.getLastName(); + ArrayList actualScores = testStudent.getUnformatttedExamScores(); + + Assert.assertEquals(expectedFirstName, actualFirstName); + Assert.assertEquals(expectedLastName, actualLastName); + Assert.assertEquals(expectedScores, actualScores); + } + + + @Test + public void setFirstNameTest() { + + // Given + ArrayList expectedScores = new ArrayList(Arrays.asList(67D, 98D)); + Student testStudent = new Student("Mike", "Doe", expectedScores); + String expected = "John"; + + // When + testStudent.setFirstName(expected); + String actual = testStudent.getFirstName(); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void getFirstNameTest() { + + // Given + ArrayList expectedScores = new ArrayList(Arrays.asList(67D, 98D)); + + Student testStudent = new Student("Mike", "Doe", expectedScores); + String expected = "John"; + + // When + testStudent.setFirstName(expected); + String actual = testStudent.getFirstName(); + + // Then + Assert.assertEquals(expected, actual); + } + + + @Test + public void setLastNameTest() { + + // Given + ArrayList expectedScores = new ArrayList(Arrays.asList(67D, 98D)); + Student testStudent = new Student("Mike", "Doe", expectedScores); + String expected = "Tyson"; + + // When + testStudent.setLastName(expected); + String actual = testStudent.getLastName(); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void getLastNameTest() { + + // Given + ArrayList expectedScores = new ArrayList(Arrays.asList(67D, 98D)); + Student testStudent = new Student("Mike", "Doe", expectedScores); + String expected = "Tyson"; + + // When + testStudent.setLastName(expected); + String actual = testStudent.getLastName(); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void getNumberOfExamsTakenTest(){ + ArrayList expectedScores = new ArrayList<>(Arrays.asList(90D, 87D, 99D)); + //Double[] expectedScores = {90D, 87D, 99D}; + Student testStudent = new Student("Mike", "Doe", expectedScores); + Integer expected = expectedScores.size(); + Integer actual= testStudent.getNumOfExamsTaken(); + Assert.assertEquals(expected,actual); + + } + + @Test + public void getExamScoresTest(){ + ArrayList actualScores = new ArrayList<>(Arrays.asList(90D, 87D, 99D)); + Student testStudent = new Student("Mike", "Doe", actualScores); + String actual = testStudent.getExamScores(); + String expected =("Exam Scores:\n" + "\tExam 1 -> 90.0\n" + "\tExam 2 -> 87.0\n" + "\tExam 3 -> 99.0\n"); + Assert.assertEquals(expected, actual); + //System.out.println(actual); + } + + @Test + public void addExamScoreTest(){ + ArrayList actualScores = new ArrayList<>(Arrays.asList(90D, 87D, 99D)); + Student testStudent = new Student("Mike", "Doe", actualScores); + String actual = testStudent.addExamScore(95D); + String expected =("Exam Scores:\n" + "\tExam 1 -> 90.0\n" + "\tExam 2 -> 87.0\n" + "\tExam 3 -> 99.0\n" + "\tExam 4 -> 95.0\n"); + Assert.assertEquals(expected,actual); + //System.out.println(actual); + } + + @Test + public void setExamScoreTest(){ + ArrayList actualScores = new ArrayList<>(Arrays.asList(90D, 87D, 99D)); + Student testStudent = new Student("Mike", "Doe", actualScores); + String actual = testStudent.setExamScore(2, 65D); + String expected =("Exam Scores:\n" + "\tExam 1 -> 90.0\n" + "\tExam 2 -> 65.0\n" + "\tExam 3 -> 99.0\n"); + Assert.assertEquals(expected,actual); + //System.out.println(actual); + } + + @Test + public void getAverageExamScoreTest(){ + ArrayList actualScores = new ArrayList<>(Arrays.asList(90D, 87D, 99D)); + Student testStudent = new Student("Mike", "Doe", actualScores); + Double actual = testStudent.getAverageExamScore(); + Double expected =92.0; + Assert.assertEquals(expected,actual); + //System.out.println(actual); + } + + @Test + public void getAverageExamScoreTest2(){ + ArrayList actualScores = new ArrayList<>(Arrays.asList(97D, 87D, 99D)); + Student testStudent = new Student("Mike", "Doe", actualScores); + Double actual = testStudent.getAverageExamScore(); + Double expected =94.3; + Assert.assertEquals(expected,actual); + //System.out.println(actual); + } + + @Test + public void printToStringTest(){ + ArrayList actualScores = new ArrayList<>(Arrays.asList(97D, 87D, 99D)); + Student testStudent = new Student("Mike", "Doe", actualScores); + String actual = testStudent.printToString(); + String expected = ("Student Name: Mike Doe\n" + "Average Score: 94.3\n" + "Exam Scores:\n" + "\tExam 1 -> 97.0\n" + "\tExam 2 -> 87.0\n" + "\tExam 3 -> 99.0\n"); + Assert.assertEquals(expected, actual); + //System.out.println(actual); + } + + } \ No newline at end of file