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..1385455 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -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 getStudyMap() { + Map result = new HashMap<>(); + for (Person studentAsPerson : students.toArray()) { + Student student = (Student)studentAsPerson; + Double studyTime = student.getTotalStudyTime(); + result.put(student, studyTime); + } + return result; + } +} 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..61e3d3e --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -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); + } + } +} 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..401e3aa --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,25 @@ +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, "Leon Hunter")); + this.add(new Instructor(1L, "someone")); + } + + @Override + public Person[] 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..472599a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,6 @@ +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..94fe50d --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -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 implements Iterable{ + + List personList; + + public People() { + this(new ArrayList<>()); + } + + public People(List 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 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..be35566 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -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; + } } 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..98dd9d6 --- /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; + + 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; + } +} \ No newline at end of file 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..ad1de2a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,27 @@ +package com.github.curriculeon; + +import java.util.List; + +public class Students extends People { + + 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 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..6d781dd --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -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); +} 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..46bb46e --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -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 preStudyTimeMap = classroom.getStudyMap(); + Double numberOfHoursToLecture = 300.0; + Double numberOfHoursStudidedPerStudent = numberOfHoursToLecture / numberOfStudents; + + // when + classroom.hostLecture(lecturer, numberOfHoursToLecture); + Map newStudyMap = classroom.getStudyMap(); + } +} 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..263d6f2 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -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); + } + + } +} 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..62689b2 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -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 expectedNames = Arrays.asList("Leon Hunter", "someone"); + for(Person instructor : instructors.toArray()) { + Assert.assertTrue(expectedNames.contains(instructor)); + } + } +} 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..f13769c --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,73 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + @Test + public void testAdd() { + //given + People people = new People(); + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + + //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(person2)); + + } + + @Test + public void testRemove() { + + // given + People people = new People(); + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + people.add(person1); + people.add(person2); + people.add(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(); + Person person1 = new Person(0L, null); + Person person2 = new Person(1L, null); + Person person3 = new Person(2l, null); + + 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 + Person actual = people.findById(person1.getId()); + + //then + Assert.assertEquals(person1, actual); + } +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..434bd56 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,36 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + public void testConstructor() { + // given + Long expectedId = 0L; + String expectedName = "Some name"; + + // when + Person person = new Person(expectedId, expectedName); + String actualName = person.getName(); + Long actualId = person.getId(); + + // then + Assert.assertEquals(expectedId,actualId); + Assert.assertEquals(expectedName,actualName); + } + + @Test + public void testSetName() { + // given + Person person = new Person(-1,null); + String expectedName = "Some name"; + Assert.assertNotEquals(expectedName, person.getName()); + + // when + person.setName(expectedName); + String actualName = person.getName(); + // then + Assert.assertEquals(expectedName,actualName); + } } 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..3502c05 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,48 @@ +package com.github.curriculeon; + +import org.junit.Test; +import org.junit.Assert; + +public class TestStudent { + + @Test + public void testImplementation() { + // given + Student student = new Student(-1, null); + + // when + Boolean outcome = student instanceof Learner; + + // then + Assert.assertTrue(outcome); + } + + @Test + public void testInheritance(){ + // given + Student student = new Student(-1, null); + + // when + Boolean outcome = student instanceof Person; + + // then + Assert.assertTrue(outcome); + } + + @Test + public void testLearn() { + + // given + final double DELTA = 1e-15; + double expectedTotalStudyTime = 12D; + double expectedNumberOfHours = 12D; + + // when + Student student = new Student(-1, null); + student.learn(expectedNumberOfHours); + double actualTotalStudyTime = student.getTotalStudyTime(); + + // then + Assert.assertEquals(expectedTotalStudyTime,actualTotalStudyTime,DELTA); + } +} 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..38659c1 --- /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.Arrays; +import java.util.List; + +public class TestStudents { + + @Test + public void test() { + // given + Students students = Students.getInstance(); + String[] studentNameArray = {"OneMore", "someone", "AnotherOne"}; + List studentNameList = Arrays.asList(studentNameArray); + Person[] studentArray = students.toArray(); + + for (int i = 0; i < studentArray.length ; i++) { + Person person1 = studentArray[i]; + String personName = person1.getName(); + boolean hasPeopleWithName = studentNameList.contains(personName); + Assert.assertTrue(hasPeopleWithName); + } + } +}