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
32 changes: 32 additions & 0 deletions src/main/java/com/github/curriculeon/Classroom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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(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(Student student : students.toArray()) {
Double studyTime = student.getTotalStudyTime();
result.put(student, studyTime);
}
return result;
}

}



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

public enum Educator implements Teacher {
PROFESSORPEEP;

private Double hoursWorked;
private final Instructor instructor;

Educator() {
long id = this.ordinal(); // inherited from `Enum` implicit super class
String name = this.name(); // inherited from `Enum` implicit super class
this.instructor = new Instructor(id, name);
Instructors.getInstance().add(instructor);
}


@Override
public void teach(Learner learner, double numberOfHours) {
instructor.teach(learner, numberOfHours);
hoursWorked += numberOfHours;

}

@Override
public void lecture(Learner[] learners, double numberOfHours) {
instructor.lecture(learners, numberOfHours);
hoursWorked += numberOfHours;

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

}

28 changes: 28 additions & 0 deletions src/main/java/com/github/curriculeon/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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, "Professor Peep"));
this.add(new Instructor(0L, "Professor George"));
}

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

public static Instructors getInstance() {
return instance;
}


}

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

public interface Learner {
void learn(Double numberOfHours);

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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* Created by leon on 7/22/2020.
*/
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 person) {
this.personList.add(person);
}

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 SomeLearnerType findByIdExpanded(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 specifiedPerson) {
return personList.contains(specifiedPerson);
}

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

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

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

abstract public SomeLearnerType[] toArray();

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

package com.github.curriculeon;

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

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

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

public class Student extends Person implements Learner {

private double totalStudyTime;

public Student(Long id, String name) {
super(id, name);
}

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

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

public class Students extends People<Student> {


private static final Students INSTANCE = new Students();

private Students() {
super();
this.add(new Student(0L, "Steven"));
this.add(new Student(1L, "Greg"));
this.add(new Student(2L, "Joe"));

}

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

public interface Teacher {

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

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

import java.util.Map;
import java.util.Set;

/**
* Created by leon on 7/27/2020.
*/
public class TestClassroom {
@Test
public void testHostLecture() {
// given
Classroom classroom = Classroom.INSTANCE;
Teacher teacher = Instructors.getInstance().findById(0L);
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);
}

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

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

public class TestInstructor {
@Test
public void testImplementation() {
// given
Instructor instructor = new Instructor(null, null);

// when
Boolean result = instructor instanceof Teacher;

// then
Assert.assertTrue(result);
}
@Test
public void testInheritance() {
// given
Instructor instructor = new Instructor(null, null);

// when
boolean outcome = instructor instanceof Person;

// then
Assert.assertTrue(outcome);
}

@Test
public void testTeach() {
// given
Instructor instructor = new Instructor(null, null);
Learner learner = new Student(null, 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(null, 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);
}
}
}
Loading