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

public class Classroom {
import org.junit.Test;

import java.util.*;

public class Classroom extends Student {

private Student[] students;
ArrayList<Student> studentList;
private ArrayList<Double> studentScores = new ArrayList<>();
private Integer maxNumberOfStudents;
public Classroom(int maxNumberOfStudents){
this.maxNumberOfStudents = maxNumberOfStudents;
students=new Student[maxNumberOfStudents];
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 getAverageExamScore(){
Double average=0.0;
Double total=0.0;
for (int i = 0; i < students.length; i++) {
total+=students[i].getAverageExamScore();
average=total/ students.length;
}
return average;
}

public void addStudent(Student student) {
//students = new Student[maxNumberOfStudents];
for(int i = 0; i < students.length; i++){
if(students[i] == null){
students[i] = student;
}
}
}
//0,1,2,3,4

public void removeStudent(String firstName, String lastName) {
List<Student> removeStudentArrayList = new ArrayList<>();
if(this.students!=null){
removeStudentArrayList=new ArrayList<>(Arrays.asList(this.students));
}
for (int i = 0; i < removeStudentArrayList.size(); i++) {
if(removeStudentArrayList.get(i).getFirstName().equals(firstName))
{
removeStudentArrayList.remove(i);
}


}
this.students = removeStudentArrayList.toArray(new Student[0]);
}

// Boolean check=false;
// for(int i=0;i<students.length;i++){
// if((students[i].getFirstName() + students[i].getLastName().equals(firstName+lastName)){
// students[i]=students[i+1];
// check=true;
// }
// if(check==true && i< students.length-1) {
// students[i]=students[i+1];
// }
// if(i== students.length-1)
// {
// students[i]=null;
// }
//
// }
// }
public Student getStudentsByScore(){
Arrays.sort(students);
return null;
}

//map<key,value>,grade string
public Map<Student,String> getGradeBook(){
Map<Student,String> gradeBook = new HashMap<>();
String grade="";
for(Student student:students){
if(student.getAverageExamScore() >=90)
grade="A";
else if(student.getAverageExamScore()>=80)
grade="B";
else if(student.getAverageExamScore()>=70)
grade="C";
else if(student.getAverageExamScore()>=60)
grade="D";
else
grade="F";
gradeBook.put(student,grade);
}
return gradeBook;
}
}
84 changes: 83 additions & 1 deletion src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,86 @@
package io.zipcoder;

public class Student {
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

public class Student extends Object{

private String firstName;
private String lastName;
private ArrayList<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 Integer getNumberOfExamsTaken(){
return examsTaken;
}

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 addExamScore(double examScore){
examScores.add(examScore);

return getExamScores().toString();
}

public String setExamScore(int examNumber, double newScore){
examScores.set(examNumber,newScore);
return getExamScores().toString();
}
public Double getAverageExamScore() {

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(getAverageExamScore())+"\n" +"> "+ "Exam Scores:" +"\n" +
getExamScores;

return result;
}


}
99 changes: 98 additions & 1 deletion src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,101 @@
package io.zipcoder;

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

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

public class ClassroomTest {
}

@Test
public void testGetAverageExamScore() {
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.getAverageExamScore();

// Then
System.out.println(output);
}
@Test
public void testAddStudent(){
// : Given
int maxNumberOfStudents = 2;
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.getStudents();
classroom.addStudent(student);
Student[] postEnrollment = classroom.getStudents();

// 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 testRemoveStudent() {
Double[] examScores = { 100.0, 150.0, 250.0};
Double[] examScores1 = { 100.0, 110.0, 250.0};
Double[] examScores2 = { 100.0, 150.0, 290.0};
Student student = new Student("Leon", "Hunter", examScores);
Student student1 = new Student("Dolio", "Durant", examScores1);
Student student2 = new Student("Kris", "Hunger", examScores2);
String firstName="Dolio";
String lastName="Durant";
Student[] students = new Student[]{student,student1,student2};
Classroom classroom = new Classroom(students);

classroom.removeStudent(firstName, lastName);
Student[] expected = classroom.getStudents();
Assert.assertEquals(student, expected[0]);
Assert.assertEquals(student2, expected[1]);

}

@Test
public void getStudentsByScoreTest() {
Double[] scores1 = new Double[]{45.0, 50.0, 55.0};
Double[] scores2 = new Double[]{60.0, 65.0};
Double[] scores3 = new Double[]{70.0, 70.0};
Student student1 = new Student("Leon", "Hunter", scores1);
Student student2 = new Student("Dolio", "Durant", scores2);
Student student3 = new Student("Kris", "Hunger", scores3);
Student[] newStudents = {student1, student2, student3};
Classroom classroom = new Classroom(newStudents);

}
@Test
public void getGradebookTest() {

Double[] examScores1 = new Double[] {99.0,97.0,95.0};
Double[] examScores2 = new Double[] {70.0,75.0,79.0};
Student student = new Student("Leon", "Hunter", examScores1 );
Student student1 = new Student("Kris", "Hunger", examScores2 );
Student[] newStudents = {student, student1};
Classroom classroom = new Classroom(newStudents);

Map<Student,String> gradebook = classroom.getGradeBook();

Assert.assertEquals(gradebook.get(student),"A");
Assert.assertEquals(gradebook.get(student1),"C");


}
}
Loading