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..87c934b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,36 @@ +package com.github.curriculeon; + +import java.util.HashMap; +import java.util.Map; + +public class Classroom { + private final static Classroom instance = new Classroom(); + private Students students = Students.getINSTANCE(); + private Instructors instructors = Instructors.getINSTANCE(); + + public static Classroom getClassroom(){ + return instance; + } + + public void hostLecture(Teacher teacher, Double numbersOfHours){ + teacher.lecture(students.toArray(), numbersOfHours); + + } + + + public void hostLecture(Long id, Double numberOfHours){ + Person person = instructors.findById(id); + Instructor instructor = (Instructor)person; + 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(); + } + 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..3653bf6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,30 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher { + + LEON, + HASEEB; + + private Double timeWorked; + private final Instructor instructor; + + Educator(){ + long id = this.ordinal(); + String name = this.name();// get instructors name. + this.instructor = new Instructor(id, name); + Instructors.getINSTANCE().add(instructor); + } + + ; + @Override + public void teach(Learner learner, Double numberOfHours) { + instructor.teach(learner, numberOfHours); + timeWorked += numberOfHours; + } + + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + instructor.lecture(learners, numberOfHours); + timeWorked += 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..1ba5d27 --- /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..3fbdd14 --- /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(); + + + public Instructors(){ + this.add( new Instructor(0l, "Leon")); + this.add(new Instructor (1l,"Christel")); + + } + + @Override + public Instructor[] toArray() { + int arrCount = count(); + Instructor[] destinationArr = new Instructor[arrCount]; + List instructorList = personList; + return instructorList.toArray(destinationArr); + } + + + 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..ed44af1 --- /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..e5a47a6 --- /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.Arrays; +import java.util.Iterator; +import java.util.List; + +abstract class People implements Iterable{ + List personList; + public People(){ + new ArrayList<>(); + } + + public People(List personList){ + this.personList = personList; + } + public void add(somePeople personToAdd){ + personList.add(personToAdd); + } + + public somePeople findById(Long id) { + for (int i = 0; i < personList.size() ; i++) { + somePeople person = personList.get(i); + if (person.getId() == id){// check if the person exist. + return person; + }else { + continue; // keep looking until id is found + } + }// for + return null; + }// findById + + + public boolean contains(somePeople person){ + return personList.contains(person); //returns true if the object passed contains person. + } + + public void remove(somePeople person) { + personList.remove(person); + } + + public void remove(Long id) { + for (somePeople person : personList) { + if (person.getId() == id) { + personList.remove(id); + } + } + + } + + public void removeAll(){ + personList.clear(); + } + + + public Integer count(){ + return personList.size(); + } + + abstract public somePeople[] toArray(); + + @Override + public Iterator iterator(){ + return personList.iterator(); + } +}//class diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..ca08879 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,31 @@ package com.github.curriculeon; +/* + * Created By Emmanuel Orubele + * on 7/21/2020 + */ + public class Person { + private final Long id; + private String name; + +public Person() { + this.id = getId(); +} + 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..ed5ccec --- /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) { + 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..6ab5edc --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,29 @@ +package com.github.curriculeon; + +import java.util.List; + +public class Students extends People { + private static final Students INSTANCE = new Students(); + //private Map mates = new HashMap(); + // tried to use a hash map. + + private Students(){ + this.add(new Student(10l, "Christopher")); + this.add( new Student(15l, "David")); + + + } + + @Override + public Student[] toArray() { + int arraySize = count(); + Student[] destinationArr = new Student[arraySize]; + List sourceList = personList; + return sourceList.toArray(destinationArr); + + } + + 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..c1eba39 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,7 @@ +package com.github.curriculeon; + +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..a52a607 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -0,0 +1,41 @@ +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.getClassroom(); + Teacher teacher = Instructors.getINSTANCE().findById(2l); + Integer numberOfStudents = Students.getINSTANCE().count(); + Double numberOfHoursToLecture = numberOfStudents.doubleValue(); + Double expectedNumberOfHours = 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 + expectedNumberOfHours; + 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..6511903 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,79 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.function.DoubleUnaryOperator; + +public class TestInstructor { + + @Test + public void testImplementation(){ + //Given + Instructor instructor = new Instructor(null, null); + + //When + boolean expected = instructor instanceof Teacher; + + //Then + Assert.assertTrue(expected); + } + + @Test + public void testInheritance(){ + //Given + Instructor instructor = new Instructor(null, null); + + //When + boolean expected = instructor instanceof Person; + + //Then + Assert.assertTrue(expected); + + } + + @Test + public void testTeach(){ + //Given + Instructor instructor = new Instructor(null, null); + Learner learn = new Student(null, null); + Double numberOfTeachingHours = 90.0d; + Double hours = learn.getTotalStudyTime(); + Double expected = hours + numberOfTeachingHours ; + //When + instructor.teach(learn, numberOfTeachingHours); + Double actual = learn.getTotalStudyTime(); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testLecture(){ + //Given + Teacher instructor = new Instructor(null, null); + Double numberHours = 134d; + Learner[] learn = new Learner[] { + new Student(22l, "Emmanuel"), + new Student(23l, "Mike"), + new Student(24l, "Leon") + }; + + Double expected = numberHours/learn.length; + + + + //When + instructor.lecture(learn, numberHours); + + + //Then + for (int i = 0; i < learn.length; i++) { + Learner learner = learn[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..eab21be --- /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; + +public class TestInstructors { + + @Test + public void instructorsSingletonTest(){ + + //Given + Instructors instructor = Instructors.getINSTANCE(); + String expected = "Leon"; + + //When + String actual = instructor.findById(01l).getName(); + //Then + + Assert.assertEquals(expected, actual); + actual = instructor.findById(02l).getName(); + Assert.assertEquals("Christel", actual); + } + +} 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..b3ebc88 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,70 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + /**? + * Credits to Leon Christopher Hunter for all the help in building this project. + * Without Leon there would be no java project by me. + */ + + @Test + public void testAdd(){ + //Given + People people = Instructors.getINSTANCE(); + + Person people1 = new Person(); + Assert.assertFalse(people.contains(people1)); + + //When + people.add(people1); + //Then + + Assert.assertTrue(people.contains(people1)); + } + + @Test + public void testRemove(){ + //Given + People people = Instructors.getINSTANCE(); + Person people1 = new Person(); + Person people2 =new Person(); + + people.add(people1); + people.add(people2); + + Assert.assertTrue(people.contains(people1)); + Assert.assertTrue(people.contains(people2)); + + //When + people.remove(people1); + people.remove(people2); + + //Then + Assert.assertFalse(people.contains(people1)); + Assert.assertFalse(people.contains(people2)); + } + + @Test + public void testFindById() { + //Given + People people = Instructors.getINSTANCE(); + Person expected = new Person(22l, "Emmanuel"); + Person people2 =new Person(23l, "Johnson"); + + people.add(expected); + people.add(people2); + + Assert.assertTrue(people.contains(expected)); + Assert.assertTrue(people.contains(people2)); + + //When + Person actual = people.findById(22l); + + + //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..b1b9c76 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,46 @@ package com.github.curriculeon; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + public class TestPerson { + private void testConstructor(long expectedLong, String expectedString){ + // : Given + Person person = new Person(expectedLong, expectedString); + + + // : When + long actualLong = person.getId(); + String actualString = person.getName(); + + // : Then + assertEquals(expectedLong, actualLong); + assertEquals(expectedString, actualString); + + } + + @Test + public void test0(){ testConstructor(20, "abc"); } + +@Test + public void test1(){testConstructor(10, "Hello World");} + + +@Test + public void testSetName(){ + //Given + Person p = new Person(12l, "abced"); + + // When + String testName = "wwww"; + p.setName(testName); + + + //then + assertEquals(p.getName(), p.getName()); + + } + } 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..2c80d7c --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,39 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + + @Test + public void testImplementation(){ + //Given + Student student = new Student(null, null); + // when + boolean expected = student instanceof Learner; + //then + Assert.assertTrue(expected); + } + + @Test + public void testInheritance(){ + Assert.assertTrue(new Student(null, null) instanceof Person); + } + + + @Test + public void testLearn(){ + //Given + Student student = new Student(20l, "Emmanuel"); + Double studentStudyTime = 40.0; + Double preStudy = student.getTotalStudyTime(); + Double expected = studentStudyTime + preStudy; + //When + student.learn(studentStudyTime); + Double actual = student.getTotalStudyTime(); + + //then + + Assert.assertEquals(expected,actual); + } +} 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..3d5d39a --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,20 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudents { + + @Test + public void test(){ + //Given + Students students = Students.getINSTANCE(); + String expectedStudentName = "David"; + + + //Then + String actual = students.findById(15l).getName(); + //When + Assert.assertEquals(expectedStudentName, actual); + } +}