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..164d86b --- /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 { + Students students; + Instructors instructors; + + public Classroom(){ + this(Students.getINSTANCE(),Instructors.getINSTANCE()); + } + + public Classroom(Students students, Instructors instructors) { + this.students = students; + this.instructors = instructors; + + } + + public void hostLecture(Teacher teacher, double numOfHours){ + teacher.lecture(students.toArray(),numOfHours); + } + + public void hostLecture(long id, double numberOfHours){ + hostLecture(instructors.findById(id), numberOfHours); + } + + public Map getStudyMap(){ + Map tempMapping = new HashMap(); + Student[] tempStudents = new Student[students.count()]; + tempStudents = students.toArray(); + for (Student student: tempStudents) + tempMapping.put(student,student.getTotalStudyTime()); + return tempMapping; + } +} 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..2327df4 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,39 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher{ + Leon(0.0), + Haseeb(0.0); + private final long id; + private final String name; + private final Instructor teacher; + private double timeWorked = 0; + + Educator(double timeWorked) { + this.id = this.ordinal(); + this.name = this.name(); + teacher = new Instructor(id,name); + } + + @Override + public void teach(Learner learner, double numberOfHours) { + this.teacher.teach(learner,numberOfHours); + timeWorked+=numberOfHours; + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + this.teacher.lecture(learners,numberOfHours); + } + + public double getTimeWorked() { + return timeWorked; + } + + public String getName() { + return name; + } + + public long getId() { + return id; + } +} 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..27ad599 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,20 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher { + 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 (Learner learner : learners){ + this.teach(learner,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..0f26338 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,33 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.function.Consumer; + +public final class Instructors extends People { + private final static Instructors INSTANCE = new Instructors(); + private People instructors; + + private Instructors(){ + final Instructor leon = new Instructor(Educator.Leon.getId(),Educator.Leon.getName()); + final Instructor haseeb = new Instructor(Educator.Haseeb.getId(),Educator.Haseeb.getName()); + this.add(leon); + this.add(haseeb); + } + + public static Instructors getINSTANCE() { + return INSTANCE; + } + + @Override + public Instructor[] toArray() { + Instructor[] tempArray = new Instructor[this.instructors.count()]; + int i = 0; + for (Instructor instructor : this) { + tempArray[i] = instructor; + i++; + } + return tempArray ; + } +} 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..27d95ca --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,10 @@ +package com.github.curriculeon; + +public interface Learner { + long getId(); + String getName(); + void learn(double numberOfHours); + Double getTotalStudyTime(); + void setName(String name); + +} \ No newline at end of file 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..1463da0 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,68 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public abstract class People implements Iterable { + private final List personList; + + public People(List personList){ + this.personList = personList; + } + + public People(){ + this(new ArrayList()); + } + + + public void add(PersonType person){ + personList.add(person); + } + + public PersonType findById(long id){ + for(PersonType person: this.personList){ + if(person.getId() == id) + return person; + } + return null; + } + public boolean contains(PersonType person){ + return personList.contains(person); + } + public void remove(PersonType person) { + personList.remove(person); + } + + public void remove(long id){ + for(PersonType person: personList){ + if(person.getId() == id) { + personList.remove(person); + return; + } + } + } + public void removeAll(){ + this.personList.clear(); + } + + public int count(){ + return personList.size(); + } + + public abstract PersonType[] 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..7307939 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,27 @@ package com.github.curriculeon; public class Person { + private final long id; + private String name; + 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..12783c3 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,22 @@ +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+=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..c685ffa --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,35 @@ +package com.github.curriculeon; + +import java.util.ArrayList; +import java.util.List; +import java.util.Spliterator; +import java.util.function.Consumer; + +public final class Students extends People { + + final static Students INSTANCE = new Students(); + private Students(){ + Student student1 = new Student(1, "Marcus"); + Student student2 = new Student(2, "Yuru"); + Student student3 = new Student(3,"David"); + this.add(student1); + this.add(student2); + this.add(student3); + } + + public static Students getINSTANCE() { + return INSTANCE; + } + + @Override + public Student[] toArray() { + Student[] tempArray = new Student[this.count()]; + int i = 0; + for (Student student : this) { + tempArray[i] = student; + i++; + } + return tempArray ; + } + +} 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..2d9a34b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,6 @@ +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..cd44226 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -0,0 +1,23 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestClassroom { + + @Test + public void testHostLecture(){ + Classroom classroom = new Classroom(); + Instructor leon = new Instructor(1, "Leon"); + classroom.hostLecture(leon,9); + Student student = classroom.students.findById(1); + double expected = 3; + Map tempMap = classroom.getStudyMap(); + double actual = tempMap.get(student); + Assert.assertEquals(actual,expected,0.0); + + } +} diff --git a/src/test/java/com/github/curriculeon/TestEducator.java b/src/test/java/com/github/curriculeon/TestEducator.java new file mode 100644 index 0000000..f4bc880 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestEducator.java @@ -0,0 +1,17 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestEducator { + + @Test + public void testConstructor(){ + Instructors instructors = Instructors.getINSTANCE(); + Educator educator = Educator.Haseeb; + double actual = educator.getTimeWorked(); + Assert.assertEquals(actual,0.0,0.0); + + + } +} 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..65c3e12 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,72 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructor { + + public void testTeach(Learner learner,Double expected, Double numOfHours){ + // When + Instructor instructor = new Instructor(1234, "John Doe"); + instructor.teach(learner,numOfHours); + + // Then + Double actual = learner.getTotalStudyTime(); + Assert.assertEquals(expected, actual); + } + + public void testLecture(Learner[] learners,Double expected, Double numOfHours){ + // When + Instructor instructor = new Instructor(1234, "John Doe"); + instructor.lecture(learners,numOfHours); + + // Then + Double actual = learners[0].getTotalStudyTime(); + Assert.assertEquals(expected, actual); + } + + + + @Test + public void testInheritance() { + // given + Object instructor = new Instructor(1234, ""); + + // when + Boolean outcome = instructor instanceof Person; + + // then + Assert.assertTrue(outcome); + } + @Test + public void testImplementation() { + //given + Object instructor = new Instructor(0,""); + //when + boolean outcome = instructor instanceof Teacher; + //then + Assert.assertTrue(outcome); + } + + @Test + public void test0(){ + Student student = new Student(1234, ""); + testTeach(student,5.0,5.0); + } + + @Test + public void test1(){ + Student student = new Student(1234, ""); + Student student2 = new Student(1234, ""); + Student student3 = new Student(1234, ""); + Learner[] students = new Learner[3]; + students[0] = student; + students[1] = student2; + students[2] = student3; + testLecture(students,3.0,9.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..aa75d5e --- /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; + +public class TestInstructors { + @Test + public void test(){ + Instructors instructors = Instructors.getINSTANCE(); + String actual = instructors.findById(0).getName(); + Assert.assertEquals("Leon",actual); + actual = instructors.findById(1).getName(); + Assert.assertEquals("Haseeb",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..edf27de --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,77 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; + +public class TestPeople extends People { + public void testAdd(Person person1, Person person2, Person person3){ + // When + ArrayList expected = new ArrayList(); + expected.add(person1); expected.add(person2); expected.add(person3); + //Then + this.add(person1); this.add(person2); this.add(person3); + + Person[] actual = this.toArray(); + Assert.assertEquals(expected.toArray(), actual); + this.removeAll(); + } + + @Test + public void test(){ + Person person = new Person(1, "Person1"); + Person person2 = new Person(2, "Person2"); + Person person3 = new Person(3, "Person3"); + testAdd(person,person2,person3); + } + @Test + public void testRemove() { + + Person person = new Person(1, "Person1"); + Person person2 = new Person(2, "Person2"); + Person person3 = new Person(3, "Person3"); + this.add(person); + this.add(person2); + this.add(person3); + + this.remove(person); + ArrayList expected = new ArrayList(); + expected.add(person2); expected.add(person3); + + Person[] actual = this.toArray(); + Assert.assertEquals(expected.toArray(),actual); + this.removeAll(); + + + } + + @Test + public void testFindById(){ + Person person = new Person(1, "Person1"); + Person person2 = new Person(2, "Person2"); + Person person3 = new Person(3, "Person3"); + this.add(person); + this.add(person2); + this.add(person3); + + Assert.assertEquals(person,this.findById(1)); + Assert.assertEquals(person2,this.findById(2)); + this.removeAll(); + + } + + @Override + public Person[] toArray() { + Person[] tempArray = new Person[this.count()]; + int i = 0; + for (Person person : this) { + tempArray[i] = person; + i++; + } + return tempArray ; + } +} + + + diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..2fe88e7 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,43 @@ package com.github.curriculeon; - +import org.junit.Assert; +import org.junit.Test; +import com.github.curriculeon.Person; public class TestPerson { + private void test(String expected) { + + // When + Person person = new Person(1234567,expected); + + // Then + String actual = person.getName(); + long actualId = person.getId(); + Assert.assertEquals(1234567, actualId); + Assert.assertEquals(expected, actual); + } + + + @Test + public void test0() { + test(null); + } + @Test + public void test1() { + test("John Charles Cutler"); + } + @Test + public void test2() { + test("John Africa Vincent Leaphart"); + } + + @Test + public void test3() { + test("Tuskegee & Guatemala"); + } + + @Test + public void test4() { + test("What is morality?"); + } + } 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..518d826 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,54 @@ +package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + + private void testLearn(Double expected) { + + // When + Student student = new Student(1234, "John Doe"); + student.learn(expected); + + // Then + Double actual = student.getTotalStudyTime(); + Assert.assertEquals(expected, actual); + + } + + + @Test + public void testInheritance() { + // given + Student student = new Student(1234, ""); + + // when + Boolean outcome = student instanceof Person; + + // then + Assert.assertTrue(outcome); + } + @Test + public void testImplementation() { + //given + Student student = new Student(0,""); + //when + boolean outcome = student instanceof Learner; + //then + Assert.assertTrue(outcome); + } + + @Test + public void test0(){ + testLearn((double) 5); + } + @Test + public void test1(){ + testLearn((double) 6); + } + @Test + public void test2(){ + testLearn((double) 0); + } + +} 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..3aec2c0 --- /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(){ + Students students = Students.getINSTANCE(); + String expected = "Marucs"; + String actual = students.findById(1).getName(); + Assert.assertEquals("Marcus",actual); + actual = students.findById(2).getName(); + Assert.assertEquals("Yuru",actual); + actual = students.findById(3).getName(); + Assert.assertEquals("David",actual); + + + } +}