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
30 changes: 30 additions & 0 deletions src/main/java/com/github/curriculeon/Classroom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.curriculeon;

import java.util.Map;
import java.util.HashMap;

public enum Classroom {

INSTANCE;
private Students students = Students.getINSTANCE();
private Instructors instructors = Instructors.getINSTANCE();

public void hostLecture(Teacher teacher, Double numberOfHours) {
teacher.lecture(students.getArray(), numberOfHours);
}

public void hostLecture(long id, Double numberOfHours) {
Person instructor = instructors.findById(id);
Person[] studentArray = new Student[students.count()];
students.personList.toArray(studentArray);
((Teacher)instructor).lecture((Learner[])studentArray, numberOfHours);
}

public Map<Student, Double> getStudyMap() {
Map<Student, Double> studentMap = new HashMap<>();
for(Student student: students.getArray()) {
studentMap.put(student, student.getTotalStudyTime());
}
return studentMap;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/github/curriculeon/Instructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.curriculeon;

public class Instructor extends Person implements Teacher {

public Instructor() {
}

public Instructor(long id, String name) {
super(id, name);
}

@Override
public void teach(Learner learner, Double numberOfHours) {
learner.learn(numberOfHours);
}

@Override
public void lecture(Learner[] learners, Double numberOfHours) {
Double numberOfHoursLearned = numberOfHours / learners.length;

for (int i = 0; i < learners.length; i++) {
this.teach(learners[i], numberOfHoursLearned);
}

}
}
27 changes: 27 additions & 0 deletions src/main/java/com/github/curriculeon/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.curriculeon;

import java.util.List;

public class Instructors extends People<Instructor> {

private static Instructors INSTANCE;

private Instructors() {}

public static Instructors getINSTANCE() {
if(INSTANCE == null) { //lazy loading, as opposed to eager loading
INSTANCE = new Instructors();
INSTANCE.add(new Instructor(444, "Lincoln Barnes"));
}
return INSTANCE;
}

public Instructor[] getArray() {
int sizeOfArray = count();
Instructor[] destinationArray = new Instructor[sizeOfArray];
List<Instructor> sourceList = personList;
return sourceList.toArray(destinationArray);
}

}

9 changes: 9 additions & 0 deletions src/main/java/com/github/curriculeon/Learner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.curriculeon;

public interface Learner {

public void learn(Double numberOfHours);

public Double getTotalStudyTime();

}
59 changes: 59 additions & 0 deletions src/main/java/com/github/curriculeon/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.curriculeon;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.lang.Iterable;

public abstract class People <E extends Person> implements Iterable<E> {

List<E> personList;

public People() {
this(new ArrayList<>());
}

public People(List<E> personList) {
this.personList = personList;
}
public void add(E person) {
this.personList.add(person);
}

public E findById(long id) {
for(E person: personList) {
if(person.getId() == id) {
return person;
}
}
return null;
}

public Boolean contains(E person) {
return personList.contains(person);
}

public void remove(E person) {
personList.remove(person);
}

public void removeAll() {
personList.clear();
}

public int count() {
return personList.size();
}

public abstract E[] getArray();

@Override
public Iterator<E> iterator() {
return personList.listIterator();
//return new PersonIterator<Person>(personList.iterator());
}



}
25 changes: 25 additions & 0 deletions src/main/java/com/github/curriculeon/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,29 @@

public class Person {

private final long id;
private String name;

public Person() {
this.id = Long.MIN_VALUE;
this.name = "";
}

public Person(Long id, String name) {
this.id = id;
this.name = name;
}

public long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

31 changes: 31 additions & 0 deletions src/main/java/com/github/curriculeon/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.curriculeon;

public class Student extends Person implements Learner {

private Double totalStudyTime;

public Student() {
super();
this.totalStudyTime = 0D;
}

public Student(long id, String name, Double totalStudyTime) {
super(id, name);
this.totalStudyTime = totalStudyTime;
}

public Student(Double totalStudyTime) {
super();
this.totalStudyTime = totalStudyTime;
}

@Override
public void learn(Double numberOfHours) {
this.totalStudyTime += numberOfHours;
}

@Override
public Double getTotalStudyTime() {
return totalStudyTime;
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/github/curriculeon/Students.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.curriculeon;

import java.util.List;

public class Students extends People<Student> {

private static Students INSTANCE = new Students();

private Students() {
super();
this.add(new Student(555, "Molly Fisher", 0D));
this.add(new Student(777, "Holly Becker", 0D));
this.add(new Student(999, "Brad Singer", 0D));
}

public static Students getINSTANCE() {
return INSTANCE;
}

public Student[] getArray() {
int sizeOfArray = count();
Student[] destinationArray = new Student[sizeOfArray];
List<Student> sourceList = personList;
return sourceList.toArray(destinationArray);
}

}
8 changes: 8 additions & 0 deletions src/main/java/com/github/curriculeon/Teacher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.curriculeon;

public interface Teacher {

public void teach(Learner learner, Double numberOfHours);
public void lecture(Learner[] learners, Double numberOfHourss);

}
36 changes: 36 additions & 0 deletions src/test/java/com/github/curriculeon/ClassroomTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.curriculeon;

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

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class ClassroomTest {

@Test
public void testHostLecture1() {
Classroom classroom = Classroom.INSTANCE;
Teacher teacher = Instructors.getINSTANCE().findById(444L);
Integer numberOfStudents = Students.getINSTANCE().count();
Double numberOfHoursToLecture = numberOfStudents.doubleValue();
Double expectedNumberOfHoursLearned = numberOfHoursToLecture / numberOfStudents;
Map<Student, Double> preStudyMap = classroom.getStudyMap();

// when
classroom.hostLecture(teacher, numberOfHoursToLecture);
Map<Student, Double> postStudyMap = classroom.getStudyMap();
Set<Student> keySet = postStudyMap.keySet();
for (Student student : keySet) {
Double preStudyTime = preStudyMap.get(student);
Double expectedStudyTime = preStudyTime + expectedNumberOfHoursLearned;
Double actualStudyTime = postStudyMap.get(student);

// then
Assert.assertEquals(expectedStudyTime, actualStudyTime);
}
}
}
71 changes: 71 additions & 0 deletions src/test/java/com/github/curriculeon/InstructorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.github.curriculeon;

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

public class InstructorTest {

@Test
public void testInstance() {
//Given
Person teacher;

//When
teacher = new Instructor();

//Then
Assert.assertTrue(teacher instanceof Person);
}

@Test
public void testImplementation() {
//Given
Teacher teacher;

//When
teacher = new Instructor();

//Then
Assert.assertTrue(teacher instanceof Teacher);
}

@Test
public void testLecture() {

//Given
Learner learner1 = new Student(5D);
Learner learner2 = new Student();
Learner learner3 = new Student(6D);
Learner[] learners = { learner1, learner2, learner3 };
Double[] expectedTotalStudyTimes = { 7D, 2D, 8D };

//When
Instructor instructor = new Instructor();
instructor.lecture(learners, 6D);
Double[] actualTotalStudyTimes = new Double[3];
for (int i = 0; i < learners.length; i++) {
actualTotalStudyTimes[i] = learners[i].getTotalStudyTime();
}

//Then
Assert.assertArrayEquals(expectedTotalStudyTimes, actualTotalStudyTimes);
}

@Test
public void testTeach() {
//Given
Learner learner = new Student(5D);
Teacher teacher = new Instructor();

//When
teacher.teach(learner, 6D);
Double actualTotalStudentStudyTime = learner.getTotalStudyTime();


//Then
Double expectedTotalStudentStudyTime = 5D + 6D;
Assert.assertEquals(expectedTotalStudentStudyTime, actualTotalStudentStudyTime);
}


}
27 changes: 27 additions & 0 deletions src/test/java/com/github/curriculeon/InstructorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.curriculeon;

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

public class InstructorsTest {

@Test
public void test() {
//given
// current instructors

//given
// current cohort

String expectedInstructorName1 = "Lincoln Barnes";

//when
Instructors instructors = Instructors.getINSTANCE();

String actualInstructorName1 = instructors.findById(444).getName();

//then
Assert.assertEquals(expectedInstructorName1, actualInstructorName1);
}

}
Loading