diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java new file mode 100644 index 0000000..2d112c8 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -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 getStudyMap(){ + Map result = new HashMap<>(); + for (Student student: students.toArray()) { + Double studyTime = student.getTotalStudyTime(); + result.put(student,studyTime); + } + return result; + } + +} diff --git a/src/main/java/com/github/curriculeon/Educator.java b/src/main/java/com/github/curriculeon/Educator.java new file mode 100644 index 0000000..102b1ea --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -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; + } +} diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java new file mode 100644 index 0000000..e8b6307 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -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)); + } + } + +} diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java new file mode 100644 index 0000000..e601482 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; + +import java.lang.reflect.Constructor; + +public class Instructors extends People { + + 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; + } + +} diff --git a/src/main/java/com/github/curriculeon/Learner.java b/src/main/java/com/github/curriculeon/Learner.java new file mode 100644 index 0000000..c68a4c2 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Learner { + public void learn(Double numberOfHours); + public Double getTotalStudyTime(); +} diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java new file mode 100644 index 0000000..2d4ec37 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,66 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public abstract class People implements Iterable { + List personList; + + public People() { + this(new ArrayList<>()); + } + + public People(List 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 iterator() { + return personList.iterator(); + } +} diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..5fc7883 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -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; + } } diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java new file mode 100644 index 0000000..28b4c6e --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -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; + } +} diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java new file mode 100644 index 0000000..3fe0cfa --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; + +import java.util.List; + +public class Students extends People { + 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; + } +} diff --git a/src/main/java/com/github/curriculeon/Teacher.java b/src/main/java/com/github/curriculeon/Teacher.java new file mode 100644 index 0000000..0fdb3af --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,8 @@ +package com.github.curriculeon; + +public interface Teacher { + + void teach(Learner learner, Double numberOfHours); + void lecture(Learner[] learners, Double numberOfHours); + +} diff --git a/src/test/java/com/github/curriculeon/TestClassroom.java b/src/test/java/com/github/curriculeon/TestClassroom.java new file mode 100644 index 0000000..3e3a6ba --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -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 preStudyMap = classroom.getStudyMap(); + + //When + classroom.hostLecture(teacher, numberOfHoursToLecture); + Map postStudyMap = classroom.getStudyMap(); + Set 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); + } + } + +} diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java new file mode 100644 index 0000000..7d28b38 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -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); + } + + + + } + +} diff --git a/src/test/java/com/github/curriculeon/TestInstructors.java b/src/test/java/com/github/curriculeon/TestInstructors.java new file mode 100644 index 0000000..c7bc736 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -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 studentNameList = Arrays.asList(instructorsArray); + + Instructor[] InstructorArray = instructors.toArray(); + for (Instructor instructor : InstructorArray) { + boolean hasPersonWithName = studentNameList.contains((instructor.getName())); + Assert.assertTrue(hasPersonWithName); + } + } +} diff --git a/src/test/java/com/github/curriculeon/TestPeople.java b/src/test/java/com/github/curriculeon/TestPeople.java new file mode 100644 index 0000000..e9523c6 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,50 @@ +package com.github.curriculeon; + + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + @Test + public void testAdd() { + //Given + Person person = new Person(null, null); + People people = Instructors.getInstance(); + // When + Assert.assertFalse(people.contains(person)); + people.add(person); + + // Then + Assert.assertTrue(people.contains(person)); + + } + + @Test + public void testRemove() { + //Given + Person person1 = new Person(1L, null); + People people1 = Instructors.getInstance(); + + // When + people1.add(person1); + Assert.assertTrue(people1.contains(person1)); + people1.remove(person1); + // Then + Assert.assertFalse(people1.contains(person1)); + + } + + @Test + public void testFindById() { + //Given + Person person1 = new Person(1L, null); + People people1 = Instructors.getInstance(); + people1.removeAll(); + // When + people1.add(person1); + Person actualPerson = people1.findById(1L); + // Then + Assert.assertEquals(person1,actualPerson); + + } +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..38be440 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,37 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor() { + //Given + Long expectedId = 4212121L; + String expectedName = "SomeName"; + // When + Person person = new Person(expectedId, expectedName); + + // Then + String actualName = person.getName(); + Long actualId = person.getId(); + + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + + } + + @Test + public void testSetName() { + // Given + String expectedName = "SomeName"; + Person person = new Person(null,null); + + // When + person.setName(expectedName); + String actual = person.getName(); + // Then + Assert.assertEquals(expectedName, actual); + } } diff --git a/src/test/java/com/github/curriculeon/TestStudent.java b/src/test/java/com/github/curriculeon/TestStudent.java new file mode 100644 index 0000000..5d460dd --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,44 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + @Test + public void testImplementation() { + //Given + Student student = new Student(0L, "some Name"); + + // When + boolean assertion = student instanceof Learner; + + // Then + Assert.assertTrue(assertion); + } + + @Test + public void testInheritance() { + //Given + Student student = new Student(0L, "some Name"); + + // When + boolean assertion = student instanceof Person; + + // Then + Assert.assertTrue(assertion); + } + + @Test + public void testLearn() { + //Given + Student student = new Student(0L, "some Name"); + + // When + student.learn(2.0); + student.learn(2.0); + double actualHours = student.getTotalStudyTime(); + + // Then + Assert.assertEquals(4.0, actualHours, 0.001); + } +} diff --git a/src/test/java/com/github/curriculeon/TestStudents.java b/src/test/java/com/github/curriculeon/TestStudents.java new file mode 100644 index 0000000..f1744e5 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,30 @@ + +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class TestStudents { + + @Test + public void test() { + Students students = Students.getInstance(); + String[] studentNameArray = {"Chris", "William", "Adam"}; + List studentNameList = Arrays.asList(studentNameArray); + + Person[] studentArray = students.toArray(); + for (Person person: studentArray) { + boolean hasPersonWithName = studentNameList.contains((person.getName())); + Assert.assertTrue(hasPersonWithName); + } + + } + + + +}