Skip to content
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
109 changes: 108 additions & 1 deletion src/main/java/io/zipcoder/Classroom.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,111 @@
package io.zipcoder;

public class Classroom {
import java.util.*;

public class Classroom extends Student {

private Student[] students;
private List<Student> studentList;
private ArrayList<Double> studentsScore = new ArrayList<>();
private Integer maxNoOfStudents;

public Classroom(int maxNoOfStudents){
this.maxNoOfStudents = maxNoOfStudents;
students = new Student[maxNoOfStudents];
studentList = new ArrayList<>();
}

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

public Classroom(){
studentList = new ArrayList<>();
this.students = new Student[30];
}


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

public void setStudents(Student[] students) {
this.students = students;
}

public Double getAverageScore(){
Double average = 0.00;
Double total = 0.00;
for (int i = 0; i < students.length; i++) {
total += students[i].getAverageScore();

}
return total / students.length;
}

public void addStudent(Student student){
for (int i = 0; i < students.length; i++) {
if(students[i]== null){
students[i] = student;
}
}

}

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


public Student[] getStudentByScore() {
Student[] students = this.students.clone();
for (int i = 0; i < students.length; i++) {
for (int j = 0; j < students.length - i - 1; j++) {
if (students[j].getAverageScore() < students[j + 1].getAverageScore()) {
Student student = students[i];
students[j] = students[j + 1];
students[j + 1] = student;
} else if (students[j].getAverageScore() == students[j + 1].getAverageScore()) {
if (students[j].getFirstName().compareTo(students[j + 1].getFirstName()) > 0) {
Student student = students[i];
students[j] = students[j + 1];
students[j + 1] = student;
}
}
}
}
return students;
}

public Map<Student,String> getGradeBook(){
Map<Student, String> gradeBook=new HashMap<>();
String grade="";
for(Student student:students){
double averageExamScore=student.getAverageScore();
if(averageExamScore>=90)
grade="A";
else if(averageExamScore>=80)
grade="B";
else if(averageExamScore>=70)
grade="C";
else if(averageExamScore>=60)
grade="D";
else
grade="E";
gradeBook.put(student,grade);
}
return gradeBook;
}


}
85 changes: 85 additions & 0 deletions src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,89 @@
package io.zipcoder;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class Student {

private String firstName;
private String lastName;
private List<Double> examScores = new ArrayList<>();
private Integer examsTaken;

public Student(String firstName,String lastName,Double[] testScores){
this.firstName = firstName;
this.lastName = lastName;
for (int i = 0; i < testScores.length ; i++) {
examScores.add(testScores[i]);

}

}

public Student(){

}

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() {
StringBuilder score = new StringBuilder();
for (int i = 0; i < examScores.size(); i++) {
score.append("Exam"+ (i+1) +" -> " + examScores.get(i) );
score.append("\n");


}

return score.toString();
}

public String setExamScores(int examNumber, Double newScore) {
examScores.set(examNumber,newScore);
return getExamScores().toString();

}

public String addExamScore(double examScore){
examScores.add(examScore);
return getExamScores().toString();
}

public Integer getExamsTaken() {
return examsTaken;
}

public Double getAverageScore(){
Double total = 0.00;
for (int i = 0; i < examScores.size(); i++) {
total += examScores.get(i);
}
return total / examScores.size();
}

@Override
public String toString(){
NumberFormat nf = new DecimalFormat();
String getExamScores = getExamScores().replace(".0","");
String result = "Student Name: "+firstName+" "+lastName+"\n"+"> "+"Average Score: "+
Math.round(getAverageScore())+"\n" + getExamScores;
return result;
}

}
100 changes: 100 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,104 @@
package io.zipcoder;

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

import java.util.Arrays;
import java.util.Map;

public class ClassroomTest {

@Test
public void testGetAverageExamScore(){
//given
Double[] s1Scores = {100.0 , 150.0};
Double[] s2Scores = {250.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.getAverageScore();

//Then
System.out.println(output);

}

@Test

public void testAddStudent() {
//given
int maxNumberOfStudents = 1;
Classroom classroom = new Classroom(1);
Student[] preEnrollment = classroom.getStudents();
String preEnrollmentAsString = Arrays.toString(preEnrollment);
Double[] examScores = {100.0, 150.0, 250.0, 0.0};
Student student = new Student("Leon", "Hunter", examScores);

//When
classroom.addStudent(student);
Student[] postEnrollment = classroom.getStudents();

//Then
String postEnrollmentAsString = Arrays.toString(preEnrollment);
System.out.println("----------------");
System.out.println("preEnrollmentAsString");
System.out.println("----------------");
System.out.println("postEnrollmentAsString");
}


@Test

public void testGetStudentByScore(){
Double[] examScore1 = new Double[]{45.0, 50.0, 55.0};
Double[] examScore2 = new Double[]{99.0, 65.0,99.0};
Double[] examScore3 = new Double[]{10.0, 10.0,20.2};
Student student1 = new Student("Leon","Hunter",examScore1);
Student student2 = new Student("Dolio","Durant",examScore2);
Student student3 = new Student("Kris","Younger",examScore3);

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


//Then
Student[] expected= classroom.getStudentByScore();
Assert.assertEquals(expected[0],student2);
Assert.assertEquals(expected[1],student1);
Assert.assertEquals(expected[2],student3);


}

@Test

public void testGetGradeBook(){
Double[] examScore1 = {45.0,55.0,65.0};
Double[] examScore2 = {90.0,95.0,99.0};

Student student1 = new Student("Dolio","Durrant",examScore1);
Student student2 = new Student("Leon","Hunter",examScore2);


//When
Student[] students = new Student[]{student1, student2};
Classroom classroom = new Classroom(students);
Map<Student,String> gradeBook = classroom.getGradeBook();

//Then

Assert.assertEquals(gradeBook.get(student1),"E");
Assert.assertEquals(gradeBook.get(student2),"A");


}



}
74 changes: 74 additions & 0 deletions src/test/java/io/zipcoder/StudentTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,79 @@
package io.zipcoder;

import org.junit.Test;

public class StudentTest {

@Test
public void testGetExamScores(){
//given
String firstName = "Leon";
String lastName ="Hunter";
Double[] examScore = {100.0, 95.0, 123.0, 96.0};
Student student = new Student(firstName,lastName,examScore);
//when
String output = student.getExamScores();
//Then
System.out.println(output);
}

@Test
public void testAddExamScores() {

//given
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScore = { };
Student student = new Student(firstName, lastName, examScore);
//when
student.addExamScore(100.0);
String output = student.getExamScores();
//Then
System.out.println(output);
}

@Test
public void testSetExamScores(){
//given
String firstName = "Leon";
String lastName ="Hunter";
Double[] examScore = {100.0};
Student student = new Student(firstName,lastName,examScore);
//when
student.setExamScores(0,150.0);
String output = student.getExamScores();
//Then
System.out.println(output);
}

@Test
public void testGetAverageExamScores(){
//given
String firstName = "Leon";
String lastName ="Hunter";
Double[] examScore = {100.0,150.0,250.0,0.0};
Student student = new Student(firstName,lastName,examScore);
//when
Double output = student.getAverageScore();
//Then
System.out.println(output);

}

@Test
public void testToString(){
//given
String firstName = "Leon";
String lastName ="Hunter";
Double[] examScore = {100.0,150.0,250.0,0.0};
Student student = new Student(firstName,lastName,examScore);
//when
String output = student.toString();
//Then
System.out.println(output);

}



}