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

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

public enum Educator implements Teacher {
LEON,
HASEEB;

private Double hoursWorked;
private final Instructor instructor;

Educator() {
long id = this.ordinal();
String name = this.name();
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;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/github/curriculeon/Instructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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) {
for (Learner element : learners) {
teach(element, (numberOfHours / learners.length));
}
}

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

import java.lang.reflect.Constructor;

public class Instructors extends People<Instructor> {

private static final Instructors instance = new Instructors();

public Instructors() {
this.add(new Instructor(0L, "Leon Hunter"));
this.add(new Instructor(1L, "Haseeb Muhammad"));
}

@Override
public Instructor[] toArray() {
return personList.toArray(new Instructor[count()]);
}

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 {
public void learn(Double numberOfHours);
public Double getTotalStudyTime();
}
66 changes: 66 additions & 0 deletions src/main/java/com/github/curriculeon/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.curriculeon;

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

public abstract class People<SomePerson extends Person> implements Iterable<SomePerson> {
List<SomePerson> personList;

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

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

public void add(SomePerson person){
personList.add(person);
}

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

public boolean contains(SomePerson person){
return this.personList.contains(person);
}

public void remove(SomePerson person){
this.personList.remove(person);
}

public void remove(long id){
for (Person person: personList) {
if(person.getId().equals(id)){
this.personList.remove(person);
}
}
}

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

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


abstract public SomePerson[] toArray();

// public Person[] toArray(){ //return People or Person ??
// return this.personList.toArray(new Person[personList.size()]);
// }

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

public class Person {
private final Long id;
private String 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;
}
}
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 = 0.0;

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

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

@Override
public Double getTotalStudyTime() {
return this.totalStudyTime;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/github/curriculeon/Students.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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, "Chris"));
this.add(new Student(1L, "William"));
this.add(new Student(2L, "Adam"));
}

@Override
public Student[] toArray() {
return personList.toArray(new Student[count()]);
}

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;

public interface Teacher {

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

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

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

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

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);
}
}

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


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

public class TestInstructor {
@Test
public void testImplementation() {
//Given

Instructor instructor = new Instructor(0L, "some Name");

// When
boolean assertion = instructor instanceof Teacher;

// Then
Assert.assertTrue(assertion);

}

@Test
public void testInheritance() {
//Given

Instructor instructor = new Instructor(0L, "some Name");

// When
boolean assertion = instructor instanceof Person;

// Then
Assert.assertTrue(assertion);

}

@Test
public void testTeach(){
//Given

Instructor instructor = new Instructor(0L, "some Name");
Student student = new Student(0L, "some student");
Double numberOfHours = 5.5;
Double expected = student.getTotalStudyTime()+ numberOfHours;

// When
instructor.teach(student, numberOfHours);
Double actual = student.getTotalStudyTime();
// Then
Assert.assertEquals(expected, actual, 0);
}

@Test
public void testLecture (){
//Given

Instructor instructor = new Instructor(0L, "some Name");

Student[] students = { new Student(0L, "Chris"),
new Student(0L, "Adam"),
new Student(2L, "william")};

Double numberOfHours = 12.0;

// When
instructor.lecture(students, numberOfHours);

Double expected = numberOfHours/students.length;
for (int i = 0; i < students.length ; i++) {
Double actual = students[i].getTotalStudyTime();
// Then
Assert.assertEquals(expected, actual, 0);
}



}

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

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

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;

public class TestInstructors {
@Test
public void test() {

Instructors instructors = Instructors.getInstance();
String[] instructorsArray = {"Leon Hunter", "Haseeb Muhammad"};
List<String> studentNameList = Arrays.asList(instructorsArray);

Instructor[] InstructorArray = instructors.toArray();
for (Instructor instructor : InstructorArray) {
boolean hasPersonWithName = studentNameList.contains((instructor.getName()));
Assert.assertTrue(hasPersonWithName);
}
}
}
Loading