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..e16fc0a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -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 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..c0bdea9 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -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; + + } +} 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..750b5b4 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -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); + } + } + +} + 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..2c5cd05 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; +import java.util.List; + +public class Instructors extends People { + + 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 sourceList = personList; + return sourceList.toArray(destinationArray); + } + + 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..72e3750 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,7 @@ +package com.github.curriculeon; + +public interface Learner { + void learn(Double numberOfHours); + + 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..7b39951 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -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 implements Iterable { + List personList; + + public People() { + this(new ArrayList<>()); + } + + public People(List 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 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..cf130eb 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -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; + } +} \ No newline at end of file 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..9a0fc50 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -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; + } +} 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..ba3e4f1 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,28 @@ +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, "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 sourceList = personList; + return sourceList.toArray(destinationArray); + } + + 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..ad8e21f --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,7 @@ +package com.github.curriculeon; + +public interface Teacher { + + void teach(Learner learner, double numberOfHours); + void lecture(Learner[] learner, 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..66f7e2e --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -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 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); + } + + } +} \ No newline at end of file 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..91b0ecb --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -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); + } + } +} 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..495b1ad --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,22 @@ + +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +/** + * Created by leon on 7/27/2020. + */ +public class TestInstructors { + @Test + public void test() { + Instructors instructors = Instructors.getInstance(); + List expectedNames = Arrays.asList("Professor Peep", "Professor George"); + for(Person instructor : instructors.toArray()) { + Assert.assertTrue(expectedNames.contains(instructor.getName())); + } + } +} \ No newline at end of file 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..04268e7 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,97 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + + @Test + public void testAdd() { + // given + People people = new People() { + @Override + public Person[] toArray() { + return new Person[0]; + } + }; + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + + Assert.assertFalse(people.contains(person1)); + Assert.assertFalse(people.contains(person2)); + Assert.assertFalse(people.contains(person3)); + + // when + people.add(person1); + people.add(person2); + people.add(person3); + + // then + Assert.assertTrue(people.contains(person1)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + } + + @Test + public void testRemove() { + // given + People people = new People() { + @Override + public Person[] toArray() { + return new Person[0]; + } + }; + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + + people.add(person1); + people.add(person2); + people.add(person3); + + Assert.assertTrue(people.contains(person1)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + + // when + people.remove(person1); + people.remove(person2); + people.remove(person3); + + // then + Assert.assertFalse(people.contains(person1)); + Assert.assertFalse(people.contains(person2)); + Assert.assertFalse(people.contains(person3)); + } + + @Test + public void testFindById() { + // given + People people = new People() { + @Override + public Person[] toArray() { + return new Person[0]; + } + }; + Person expected = new Person(0L, null); + Person person2 = new Person(1L, null); + Person person3 = new Person(2L, null); + + people.add(expected); + people.add(person2); + people.add(person3); + + Assert.assertTrue(people.contains(expected)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + + // when + Person actual = people.findById(expected.getId()); + + // then + Assert.assertEquals(expected, actual); + } + + +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..a6d58ab 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 = 0L; + String expectedName = "Some name"; + + // when + Person person = new Person(expectedId, expectedName); + Long actualId = person.getId(); + String actualName = person.getName(); + + // then + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void testSetName() { + // given + Person person = new Person(null, null); + String expectedName = "Some name"; + Assert.assertNotEquals(expectedName, person.getName()); + + // when + person.setName(expectedName); + String actualName = person.getName(); -} + // then + Assert.assertEquals(expectedName, actualName); + } +} \ No newline at end of file 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..98e28ec --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,49 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by leon on 7/21/2020. + */ +public class TestStudent { + @Test + public void testImplementation() { + // given + Student student = new Student(null, null); + + // when + Boolean result = student instanceof Learner; + + // then + Assert.assertTrue(result); + } + + @Test + public void testInheritance() { + // given + Student student = new Student(null, null); + + // when + Boolean result = student instanceof Person; + + // then + Assert.assertTrue(result); + } + + @Test + public void testLearn() { + // given + Student student = new Student(null, null); + Double numberOfHoursToLearn = 98.0; + Double preStudyTime = student.getTotalStudyTime(); + Double expected = preStudyTime + numberOfHoursToLearn; + + // when + student.learn(numberOfHoursToLearn); + Double actual = student.getTotalStudyTime(); + + // then + Assert.assertEquals(expected, actual, 0.01); + } +} \ No newline at end of file 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..34b3ba7 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,25 @@ +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 = {"Steven", "Greg", "Joe"}; + List studentNameList = Arrays.asList(studentNameArray); + + Person[] studentArray = students.toArray(); + for (int i = 0; i < studentArray.length; i++) { + Person person = studentArray[i]; + String personName = person.getName(); + boolean hasPersonWithName = studentNameList.contains(personName); + Assert.assertTrue(hasPersonWithName); + } + } +}