Skip to content

GradedStudents #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 140 additions & 1 deletion src/main/java/io/zipcoder/Classroom.java
Original file line number Diff line number Diff line change
@@ -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<Student> 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<Student> newStudentList = new ArrayList<Student>(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<Student> studentList = new ArrayList<Student>(Arrays.asList(studentArray));

Comparator<Student> byAvgExamScores = Comparator.comparing(Student::getStudentAverageExamScore);
Comparator<Student> 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<Student, Character> getGradeBook() {
Map<Student, Character> 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<Double> 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)));
}

}



78 changes: 78 additions & 0 deletions src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -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<Double> examScores;
Double[] testScores;

public Student(String firstName, String lastName, Double[] testScores) {
this.firstName = firstName;
this.lastName = lastName;
this.examScores = new ArrayList<Double>(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();
}
}
123 changes: 123 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Loading