Skip to content

Instructions complete #54

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 2 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
77 changes: 77 additions & 0 deletions src/main/java/io/zipcoder/Classroom.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,81 @@
package io.zipcoder;

import java.util.*;

public class Classroom {

Student[] students;

public Classroom(int maxNumberOfStudents) {
students = new Student[maxNumberOfStudents];
}

public Classroom(Student[] student) {
students = student;
}

public Classroom() {
students = new Student[30];
}

public Student[] getStudents() {
return students;
}

public Double getAverageExamScore() {
Double sumOfExamScores = 0.0;
for (Student student : students) {
sumOfExamScores += student.getAverageExamScore();

}
return sumOfExamScores / students.length;
}

public void addStudent(Student student) {
List<Student> arrayList = new ArrayList<>(Arrays.asList(students));
arrayList.add(student);
students = arrayList.toArray(new Student[arrayList.size()]);
}

public Student[] removeStudent(String firstName, String lastName) {
List<Student> arrayList = new ArrayList<>(Arrays.asList(students));
for (Student student : students) {
if (student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)) {
arrayList.remove(student);
}
}
return arrayList.toArray(new Student[arrayList.size()]);
}

public Student[] getStudentByScore() {
Arrays.sort(students);
return students;
}

public Map getGradeBook() {
Map<Student, Character> map = new LinkedHashMap<>();
Integer studentsInClass = students.length;
getStudentByScore();
for (int i = 0; i < studentsInClass; i++) {
if ((((i + 1) / studentsInClass) * 100) <= 10) {
map.put(students[i], 'A');
}
else if ((((i + 1) / studentsInClass) * 100) >= 11 &&
(((i + 1) / studentsInClass) * 100) <= 29) {
map.put(students[i], 'B');
}
else if ((((i + 1) / studentsInClass) * 100) >= 30 &&
(((i + 1) / studentsInClass) * 100) <= 50) {
map.put(students[i], 'C');
}
else if ((((i + 1) / studentsInClass) * 100) >= 51 &&
(((i + 1) / studentsInClass) * 100) <= 89) {
map.put(students[i], 'D');
}
else {
map.put(students[i], 'F');
}
}
return map;
}
}
85 changes: 84 additions & 1 deletion src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,87 @@
package io.zipcoder;

public class Student {
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Student implements Comparable<Student>{

String firstName;
String lastName;
List<Double> examScores;

public Student(String firstName, String lastName, Double[] testScores) {
this.firstName = firstName;
this.lastName = lastName;
examScores = new ArrayList<Double>(Arrays.asList(testScores));
}

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 getExamScores() {
Integer count = 1;
StringBuilder builder = new StringBuilder();
builder.append("> Exam Scores:\n");
Iterator<Double> iterator = examScores.iterator();
while (iterator.hasNext()) {
builder.append("\tExam ").append(count).append(" -> ").append(iterator.next()).append("\n");
count++;
}
return builder.toString();
}

public Integer getNumberOfExamsTaken() {
return examScores.size();
}

public void addExamScore(double examScore) {
examScores.add(examScore);
}

public void setExamScore(int examNumber, double newScore) {
examScores.set(examNumber, newScore);
}

public Double getAverageExamScore() {
Double sum = 0.0;
Iterator iterator = examScores.iterator();
while (iterator.hasNext()) {
sum += (Double)iterator.next();
}
return sum / examScores.size();
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Student Name: ").append(firstName).append(" ").append(lastName).append("\n")
.append("> Average Score: ").append(getAverageExamScore()).append("\n")
.append(getExamScores());
return builder.toString();
}

@Override
public int compareTo(Student student) {
if (Double.compare(getAverageExamScore(), student.getAverageExamScore()) != 0) {
return Double.compare(student.getAverageExamScore(), getAverageExamScore());
}
else {
return this.lastName.compareTo(student.getLastName());
}
}
}
73 changes: 73 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,77 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Test;

public class ClassroomTest {

@Test
public void getAverageExamScoreTest() {
// Given
Student student1 = new Student("Allen", "Shup", new Double[]{100.0, 88.0, 95.0});
Student student2 = new Student("Ben", "Alpit", new Double[]{100.0, 90.0, 84.0});
Student student3 = new Student("Charles", "Compton", new Double[]{100.0, 60.0, 70.0});
Double expectedAverageScore = 88.0;

// When
Student[] students = new Student[]{student1, student2, student3};
Classroom classroom = new Classroom(students);
Double actualAverageScore = Math.ceil(classroom.getAverageExamScore());

// Then
Assert.assertEquals(expectedAverageScore, actualAverageScore);
}

@Test
public void addStudentTest() {
// Given
Student student1 = new Student("Allen", "Shup", new Double[]{100.0, 88.0, 95.0});
Student student2 = new Student("Ben", "Alpit", new Double[]{100.0, 90.0, 84.0});
Student student3 = new Student("Charles", "Compton", new Double[]{100.0, 60.0, 70.0});
Student expectedStudent = student3;

// When
Student[] students = new Student[]{student1, student2};
Classroom classroom = new Classroom(students);
classroom.addStudent(student3);
Student actualStudent = classroom.students[2];

// Then
Assert.assertEquals(expectedStudent, actualStudent);
}

@Test
public void removeStudentTest() {
// Given
Student student1 = new Student("Allen", "Shup", new Double[]{100.0, 88.0, 95.0});
Student student2 = new Student("Ben", "Alpit", new Double[]{100.0, 90.0, 84.0});
Student student3 = new Student("Charles", "Compton", new Double[]{100.0, 60.0, 70.0});
Integer expectedLength = 2;

// When
Student[] students = new Student[]{student1, student2, student3};
Classroom classroom = new Classroom(students);
Student[] newArray = classroom.removeStudent(student3.getFirstName(), student3.getLastName());
Integer actualLength = newArray.length;

// Then
Assert.assertEquals(expectedLength, actualLength);
}

@Test
public void getStudentByScoreTest() {
// Given
Student student1 = new Student("Allen", "Shup", new Double[]{60.0, 88.0, 95.0});
Student student2 = new Student("Ben", "Alpit", new Double[]{100.0, 90.0, 84.0});
Student student3 = new Student("Charles", "Compton", new Double[]{100.0, 100.0, 90.0});
Student[] expectedArray = new Student[]{student3, student2, student1};

// When
Student[] students = new Student[]{student1, student2, student3};
Classroom classroom = new Classroom(students);
Student[] actualArray = classroom.getStudentByScore();

// Then
Assert.assertArrayEquals(expectedArray, actualArray);
}
}
55 changes: 55 additions & 0 deletions src/test/java/io/zipcoder/StudentTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Test;

public class StudentTest {

@Test
public void constructorTest() {
// Given
String expectedFirstName = "Zach";
String expectedLastName = "Kitto";
Double[] expectedExamScores = new Double[]{95.0, 84.0, 78.0, 82.0};

// When
Student student = new Student(expectedFirstName, expectedLastName, expectedExamScores);
String actualFirstName = student.getFirstName();
String actualLastName = student.getLastName();
Double[] actualExamScores = student.examScores.toArray(new Double[0]);

// Then
Assert.assertEquals(expectedFirstName, actualFirstName);
Assert.assertEquals(expectedLastName, actualLastName);
Assert.assertEquals(expectedExamScores, actualExamScores);
}

@Test
public void addExamScoreTest() {
// : Given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = { };
Student student = new Student(firstName, lastName, examScores);

// When
Double expectedScore = 100.0;
student.addExamScore(expectedScore);
Double actualScore = student.examScores.get(0);

// Then
Assert.assertEquals(expectedScore, actualScore);
}

@Test
public void setExamScoreTest() {
// : Given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = { 100.0 };
Student student = new Student(firstName, lastName, examScores);

// When
Double newExpectedScore = 150.0;
student.setExamScore(0, newExpectedScore);
Double newActualScore = student.examScores.get(0);

// Then
Assert.assertEquals(newExpectedScore, newActualScore);
}
}
Binary file added target/classes/io/zipcoder/Classroom.class
Binary file not shown.
Binary file added target/classes/io/zipcoder/Student.class
Binary file not shown.
Binary file not shown.
Binary file added target/test-classes/io/zipcoder/StudentTest.class
Binary file not shown.