Skip to content

Overdue #36

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

public class Classroom {
import java.lang.reflect.Array;
import java.util.*;

public class Classroom extends Student{

Student[] students;


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

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

public Double getAverageExamScore() {
double total = 0;
for (int i = 0; i < students.length; i++) {
total += students[i].getAverageExamScore();
}
return total / students.length;
}
public Student[] addStudent(Student student){
List<Student> studentList = Arrays.asList(students);
studentList.add(student);
return (Student[]) studentList.toArray();
}

public Student[] removeStudent(String firstName, String lastName){
List<Student> studentList = Arrays.asList(students);
for(int i =0; i < students.length; i++){
if(students[i].getFirstName().equals(firstName) && students[i].getLastName().equals(lastName)) {
studentList.remove(i);
}
}
return (Student[])studentList.toArray();
}
public Student[] getStudentByScore(){
StudentSorter itemSorter = new StudentSorter(students);
Comparator<Student> comparator = new ScoreComparator();
return itemSorter.sort(comparator);
}
//******Correct this to meet requirements ******
public Map getGradeBook(){
Map<Character, Student> gradeBook= new HashMap<>();
Student[] sortedStudents =getStudentByScore();
for(int i =0; i < sortedStudents.length; i++){
if(sortedStudents[i].getAverageExamScore() <=50){
gradeBook.put('F',sortedStudents[i]);
}else if(sortedStudents[i].getAverageExamScore() <=60) {
gradeBook.put('E', sortedStudents[i]);
}else if(sortedStudents[i].getAverageExamScore() <=65) {
gradeBook.put('D', sortedStudents[i]);
}else if(sortedStudents[i].getAverageExamScore() <=70) {
gradeBook.put('C', sortedStudents[i]);
}else if(sortedStudents[i].getAverageExamScore() <=90) {
gradeBook.put('B', sortedStudents[i]);
} else {
gradeBook.put('A', sortedStudents[i]);
}
}
return gradeBook;
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/zipcoder/ScoreComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.zipcoder;

import java.util.Comparator;


public class ScoreComparator implements Comparator<Student> {
public int compare(Student o1, Student o2) {
// if(o1.getAverageExamScore() < o2.getAverageExamScore()){
// return -1;
// }else if(o1.getAverageExamScore().equals(o2.getAverageExamScore())){
// return 0;
// }else{
// return 1;
// }
if(o1.getAverageExamScore() > o2.getAverageExamScore()){
return -1;
}else if(o1.getAverageExamScore().equals(o2.getAverageExamScore())){
return 0;
}else{
return 1;
}

}
}
79 changes: 78 additions & 1 deletion src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,81 @@
package io.zipcoder;

public class Student {
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Student{
private String firstName;
private String lastName;
List<Double> examScores;

public Student(){}


public Student (String firstName, String lastName, Double[] testScores ){
this.firstName = firstName;
this.lastName = lastName;
this.examScores = new ArrayList<>(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 Integer getNumberOfExamsTaken(){
return examScores.size();
}

public String getExamScores(){
StringBuilder sbScores = new StringBuilder();

System.out.println("Exam Scores : \n");
for(int i = 0; i < examScores.size();i++){

sbScores.append("Exam " + (i + 1) + " -> " + examScores.get(i) + "\n");
}
return sbScores.toString();
}

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

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

}

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

// public void takeExam(Double examScore){examScores.add(examScore);}

public String toString(){
String nameLine = "Student Name : " + getFirstName() + " " + getLastName() + "\n";
String avgLine = "Average Score : " + getAverageExamScore() + "\n";
String examLines = getExamScores() + "\n";

return nameLine + avgLine + examLines;
}
}
27 changes: 27 additions & 0 deletions src/main/java/io/zipcoder/StudentSorter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.zipcoder;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StudentSorter implements Comparator<Student> {
List<Student> studentList; //

public StudentSorter(Student[] students) {
this.studentList = Arrays.asList(students);//
}

public Student[] sort(Comparator<Student> comparator) {
Collections.sort(studentList, comparator);//
return (Student[]) studentList.toArray();//
}

public int compare(Student o1, Student o2) {
return 0;
}

public boolean equals(Student obj) {
return false;
}
}
72 changes: 72 additions & 0 deletions src/test/java/io/zipcoder/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
package io.zipcoder;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class ClassroomTest {

@Test
public void TestClassroom() {
Classroom classroom = new Classroom(20);
Integer expected = 20;
Integer actual = classroom.students.length;
Assert.assertEquals(expected, actual);
}

@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 TestgetStudentByScore(){
Double [] s1Scores = {2.0,99.,1.,};
Double [] s2Scores = {50.,49.,0.};
Double [] s3Scores = {100.,100.,100.};
Student s1 = new Student("Student","One",s1Scores);
Student s2 = new Student("Student", "Two",s2Scores);
Student s3 = new Student("Student", "Three", s3Scores);

Student []students = {s1,s2,s3};
Classroom classroom = new Classroom(students);

Student [] expected = {s3,s1,s2};
Student [] actual = classroom.getStudentByScore();

// Student [] studentsDescending = classroom.getStudentByScore();
// for(int i = 0; i < studentsDescending.length;i++){
// System.out.println(studentsDescending[i]);
// }
Assert.assertEquals(expected,actual);
}
//Test further
@Test
public void TestGetGradeBook(){
// Double [] s1Scores = {2.0,99.,1.,};
Double[] s1Scores = {0.};
Student s1 = new Student("Student","One",s1Scores);

Student []students = {s1};
Classroom classroom = new Classroom(students);

Map<Character, Student> gradeMap = classroom.getGradeBook();

for(int i =0; i < gradeMap.size();i++){
System.out.println(gradeMap.get('F'));
}
}

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

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

import java.sql.Array;
import java.sql.SQLOutput;
import java.util.*;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;


public class StudentTest {

@Test
public void TestStudentConstructor(){
String expectedName = "Bilbo";
String expectedLastName = "Baggins";
Double [] testScores = {10.,15.};

Student someStudent = new Student("Bilbo", "Baggins", testScores);

String actualName = someStudent.getFirstName();
String actualLastName = someStudent.getLastName();

Assert.assertEquals(expectedName,actualName);
Assert.assertEquals(expectedLastName,actualLastName);
}
@Test
public void TestStudentName(){
String expectedName = "Bilbo";
String expectedLastName = "Baggins";
Double [] testScores = {10.,15.};

Student someStudent = new Student(null, null, testScores);

someStudent.setFirstName("Bilbo");
someStudent.setLastName("Baggins");


String actualName = someStudent.getFirstName();
String actualLastName = someStudent.getLastName();

Assert.assertEquals(expectedName,actualName);
Assert.assertEquals(expectedLastName,actualLastName);
}
@Test
public void TestGetExamScore() {
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores ={1.3,1.2};
Student student = new Student(firstName, lastName, examScores);
String output = student.getExamScores();

System.out.println(output);
}

@Test
public void setExamScores() {
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = {1.3,99.};
Student student = new Student(firstName, lastName, examScores);

String output = student.getExamScores();

System.out.println(output);
}

@Test
public void testToString() {
String firstName = "Leon";
String lastName = "Hunter";
Double[] examScores = {1.3,99.};
Student student = new Student(firstName, lastName, examScores);

System.out.println(student.toString());
}
}
Binary file added target/classes/io/zipcoder/Classroom.class
Binary file not shown.
Binary file added target/classes/io/zipcoder/ScoreComparator.class
Binary file not shown.
Binary file added target/classes/io/zipcoder/Student.class
Binary file not shown.
Binary file added target/classes/io/zipcoder/StudentSorter.class
Binary file not shown.
Binary file not shown.
Binary file added target/test-classes/io/zipcoder/StudentTest.class
Binary file not shown.