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

public class Classroom {
import java.util.*;
import java.util.function.ToLongFunction;

public class Classroom implements Comparator<Student> {
private Student[] students;
private Integer maxNumberOfStudents;

//constructor 1: int representative of the maxNumberOfStudents that this Classroom can hold.
public Classroom(Integer maxNumberOfStudents) {
this.students = new Student[maxNumberOfStudents];
}
//constructor 2 Student[] representative of the collection of Student objects this Classroom will store.
public Classroom(Student[] students) {
this.students = students;
}
//constructor 3: nullary. initializes the composite students object o
//be an empty array of 30 Student objects
public Classroom() {
this.students = new Student[30];
}
//getter return the composit students object

public String getStudents() {
StringBuilder builder = new StringBuilder();
for (Student s : students) {
builder.append("===================\n");
try {
builder.append(s.toString());
} catch (Exception e) {
builder.append("No Students Currently Enrolled\n");
}
builder.append("===================\n\n");
}
return builder.toString();
}
//getter: returns sum of all exams divided by the number of students
public Double getAverageExamScore() {
double sum = 0.0;
for (Student s : students) {
if (s != null) {
sum += s.getAverageExamScore();
}
}
return sum / students.length;
}
//addStudent student uses Student parameter to add a Student object to students list
public void addStudent(Student student) {
List<Student> studentsList = new ArrayList<>(Arrays.asList(students));
studentsList.add(student);
studentsList.remove(null);
students = studentsList.toArray(new Student[0]);
}

//removes student from students object. array is re-ordered after removed. null at end
public void removeStudent(String firstName, String lastName) {
List<Student> studentList = new ArrayList<>(Arrays.asList(students));
for (Student s : studentList) {
if (s.getFirstName().equals(firstName) && s.getLastName().equals(lastName)) {
studentList.remove(s);
break;
}
}
studentList.sort(this);
students = studentList.toArray(new Student[0]);
}

//can't mdify arrays.asList so wrapper ArrayList
public ArrayList<Student> getStudentByScore() {
ArrayList<Student> studentList = new ArrayList<>(Arrays.asList(students));
studentList.sort(this);
return studentList;
}

//getStudentByScore returns and array representation of Student object sorted in descending score order.
//if two students same score, order them lexigraphically
public String getGradeBook() {
Map<String, ArrayList<Student>> gradeBook = new TreeMap<>();
ArrayList<Student> sortedStudentList = getStudentByScore();
Integer binSize = sortedStudentList.size() / 5;
gradeBook.put("A", new ArrayList<>(sortedStudentList.subList(0, binSize)));
gradeBook.put("B", new ArrayList<>(sortedStudentList.subList(binSize, binSize * 2)));
gradeBook.put("C", new ArrayList<>(sortedStudentList.subList(binSize * 2, binSize * 3)));
gradeBook.put("D", new ArrayList<>(sortedStudentList.subList(binSize * 3, binSize * 4)));
gradeBook.put("F", new ArrayList<>(sortedStudentList.subList(binSize * 4, binSize * 5)));

StringBuilder builder = new StringBuilder();
for (Map.Entry entry : gradeBook.entrySet()) {
builder.append("" + entry.getKey() + " Students: \n\n\n" + entry.getValue().toString() + "\n");
}
return builder.toString();
}

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

@Override
public Comparator<Student> thenComparingLong(ToLongFunction<? super Student> keyExtractor) {
return null;
}
}



83 changes: 82 additions & 1 deletion src/main/java/io/zipcoder/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,85 @@
package io.zipcoder;
import java.util.Arrays;
import java.util.ArrayList;

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

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

public Student() {
this.firstName = "First";
this.lastName = "Last";
this.examScores = null;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public ArrayList<Double> getExamScores() {
return examScores;
}

//public Double getExamScoresAsString() {
// Student student = new Student();;
// return getScoresAsString(input);
// }

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getNumberOfExamsTaken() {
return this.getExamScores().size();
}

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

public void setExamScores(int examNumber, double newScore) {
examScores.remove(examNumber - 1);
examScores.add(examNumber - 1, newScore);
}

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

}

public String toString() {
String output = "Student Name: " + firstName + " " + lastName + "\n> Average Score: " + getAverageExamScore() + "\n> Exam Scores:\n";
return getScoresAsString(output);
}

private String getScoresAsString(String output) {
for (int i = 0; i < examScores.size(); i++) {
output += "\tExam " + (i + 1) + " -> " + Math.round(examScores.get(i)) + "\n";
}

return output;
}
}




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

import org.junit.Test;

public class ClassroomTest {

@Test
public void gtStudentsTest() {
//given
Double[] examScores = {};
Student student1 = new Student("Cameron", "Howard", examScores);
Student student2 = new Student("April", "Howard", examScores);
Student[] students = {student1, student2};
Classroom classroom = new Classroom(students);
//when
String output = classroom.getStudents();
//then
System.out.println(output);
}
}
133 changes: 132 additions & 1 deletion src/test/java/io/zipcoder/StudentTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,136 @@
package io.zipcoder;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.assertEquals;

public class StudentTest {
String expectedFirstName = "";
String expectedLastName = "";
Double[] examScores = {};
Student student = new Student();


@Before
public void setUp() throws Exception {
String expectedFirstName = "";
String expectedLastName = "";
Double[] examScores = {};
Student student = new Student();
}

// @After
//public void tearDown() throws Exception {
// return null;
// }

@Test
public void setFirstNameTest() {
//Given
Student student = new Student();
String expected = "April";
//When
student.setFirstName(expected);
String actual = student.getFirstName();
//Then
Assert.assertEquals(expected, actual);
System.out.println(expected);
System.out.println(actual);
}

@Test
public void getFirstName() {
//Given
String expected = "Jill";
//When
student.getFirstName();
String actual = student.getFirstName();
//Then
Assert.assertEquals(expected, actual);
System.out.println(expected);
System.out.println(actual);
}


@Test
public void getLastName() {
Student student = new Student();
String expected = "Jordan";

// When
student.getLastName();
String actual = student.getLastName();

// Then
assertEquals(expected, actual);
}


@Test
public void setLastName() {
//Given
Student student = new Student();
String expected = "April";
//When
student.setLastName(expected);
String actual = student.getLastName();
//Then
Assert.assertEquals(expected, actual);
System.out.println(expected);
System.out.println(actual);
}

@Test
public void getExamScores() {
//Given
Student student = new Student();
Double expected = 55.00;
//When
student.getExamScores();
ArrayList<Double> actual = student.getExamScores();
//Then
Assert.assertEquals(expected, actual);
System.out.println(expected);
System.out.println(actual);
}

@Test
public void addExamScore() {
//Give
String firstName = "Geg";
String lastName = "Geggy";
Double[] examScores = {100.};
Student student = new Student();

addExamScore();

ArrayList<Double> actual = student.getExamScores();
Assert.assertEquals("Exam: ", actual);
}

@Test
public void setExamScores() {
Student student = new Student();
student.setExamScores(1, 100.);
ArrayList<Double> actual = student.getExamScores();
Assert.assertEquals("Exam 1: 100.00", actual);
}

@Test
public void getAverageExamScore() {
Student student = new Student();
Double[] actual = {100., 75., 95., 100.};
double expected = 92.5;
Assert.assertEquals(expected, actual);

}
}




}
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 not shown.