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

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

public enum Classroom {
INSTANCE;
private Students students = Students.getInstance();
private Instructors instructors = Instructors.getInstance();

public void hostLecture(Teacher teacher, Double numberOfHours) {
teacher.lecture((Learner[])students.toArray(), numberOfHours);
}

public void hostLecture(long id, double numberOfHours) {
Teacher instructor = instructors.findById(id);
instructor.lecture(students.toArray(), numberOfHours);
}

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

public class Instructor extends Person implements Teacher {

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 numberOfHoursPerLearner = numberOfHours / learners.length;
for (int i =0; i < learners.length ; i++) {
Learner learner = learners[i];
learner.learn(numberOfHoursPerLearner);
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/github/curriculeon/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.curriculeon;

import java.util.List;

public class Instructors extends People<Instructor> {

private static final Instructors instance = new Instructors();

private Instructors() {
this.add(new Instructor(0L, "Leon Hunter"));
this.add(new Instructor(1L, "someone"));
}

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

public static Instructors getInstance() {
return instance;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/curriculeon/Learner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.curriculeon;

public interface Learner {
void learn(double numberOfHours);
double getTotalStudyTime();
}
60 changes: 60 additions & 0 deletions src/main/java/com/github/curriculeon/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.curriculeon;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

List<SomeLearnerType> personList;

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

public People(List<SomeLearnerType> personList) {
this.personList = personList;
}

public void add(SomeLearnerType personToBeAdded) {
this.personList.add(personToBeAdded);
}

public SomeLearnerType findById(long id) {
for (int i = 0; i < personList.size(); i++) {
SomeLearnerType person = personList.get(i);
if (person.getId() == id) { //if the id is correct,
return person; // return person
} else { // if its the wrong id,
continue; // keep lookin'!
}
} // finished loop; we've finished lookin'
return null; // we were not able to find the person with the id
}

public Boolean contains(SomeLearnerType personToBeFound) {
return personList.contains(personToBeFound);
}

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

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

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

public Person[] toArray() {
return personList.toArray(new Person[personList.size()]);
}

@Override
public Iterator<SomeLearnerType > iterator() {
return personList.iterator();
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/github/curriculeon/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package com.github.curriculeon;

public class Person {
private final long id;
private String name;

public Person() {
this.id = -1;
this.name = null;
}

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

public long getId() {
return this.id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/github/curriculeon/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.curriculeon;

public class Student extends Person implements Learner {
private double totalStudyTime;

public Student(long id, String name) {
super(0,"some Name");
}

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

@Override
public double getTotalStudyTime() {
return this.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 final Students INSTANCE = new Students();

private Students() {
super(); // optional invocation
this.add(new Student(0L,"OneMore"));
this.add(new Student(1L,"someone"));
this.add(new Student(2L,"AnotherOne"));
}

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

public static Students getInstance() {
return INSTANCE;
}
}
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;

import com.github.curriculeon.Learner;

public interface Teacher {
void teach(Learner learner, double numberOfHours );
void lecture(Learner[] learners, double numberOfHours);
}
22 changes: 22 additions & 0 deletions src/test/java/com/github/curriculeon/TestClassroom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.curriculeon;

import org.junit.Test;

import java.util.Map;

public class TestClassroom {

@Test
public void testHoursLecture() {
Classroom classroom = Classroom.INSTANCE;
Teacher lecturer = (Teacher) Instructors.getInstance().findById(1L);
Integer numberOfStudents = Students.getInstance().count();
Map<Student, Double> preStudyTimeMap = classroom.getStudyMap();
Double numberOfHoursToLecture = 300.0;
Double numberOfHoursStudidedPerStudent = numberOfHoursToLecture / numberOfStudents;

// when
classroom.hostLecture(lecturer, numberOfHoursToLecture);
Map<Student, Double> newStudyMap = classroom.getStudyMap();
}
}
68 changes: 68 additions & 0 deletions src/test/java/com/github/curriculeon/TestInstructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.curriculeon;

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

public class TestInstructor {

@Test
public void testImplementation() {

//given
Instructor instructor = new Instructor(-1,null);

//when
Boolean outcome = instructor instanceof Teacher;

//then
Assert.assertTrue(outcome);
}

@Test
public void testInheritance() {
Instructor instructor = new Instructor(-1, null);
Boolean outcome = instructor instanceof Person;
Assert.assertTrue(outcome);
}

@Test
public void testTeach() {
// given
Instructor instructor = new Instructor(-1, null);
Learner learner = new Student(-1, null);
Double numberOfHoursToTeach = 134.0;
Double preStudyTime = learner.getTotalStudyTime();
Double expected = preStudyTime + numberOfHoursToTeach;

// when
instructor.teach(learner,numberOfHoursToTeach);
Double actual = learner.getTotalStudyTime();

// then
Assert.assertEquals(expected, actual);
}

@Test
public void testLecture() {
//given
Teacher teacher = new Instructor(-1, null);
Learner[] learners = new Learner[] {
new Student(0L, "Leon"),
new Student(1L, "Christopher"),
new Student(2L, "Hunter")
};
Double numberOfHours = 128.0;
Double expected = numberOfHours / learners.length;

// when
teacher.lecture(learners,numberOfHours);

//then
for (int i =0 ; i < learners.length ; i++) {
Learner learner = learners[i];
Double actual = learner.getTotalStudyTime();
Assert.assertEquals(expected,actual);
}

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

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

import java.util.Arrays;
import java.util.List;

public class TestInstructors {
@Test
public void test() {
Instructors instructors = Instructors.getInstance();
List<String> expectedNames = Arrays.asList("Leon Hunter", "someone");
for(Person instructor : instructors.toArray()) {
Assert.assertTrue(expectedNames.contains(instructor));
}
}
}
Loading